001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.gui.composer;
017:
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Vector;
021:
022: import javax.swing.JOptionPane;
023:
024: import org.columba.addressbook.facade.IContactFacade;
025: import org.columba.addressbook.facade.IFolder;
026: import org.columba.addressbook.facade.IFolderFacade;
027: import org.columba.addressbook.facade.IHeaderItem;
028: import org.columba.addressbook.facade.IModelFacade;
029: import org.columba.api.exception.ServiceNotFoundException;
030: import org.columba.api.exception.StoreException;
031: import org.columba.mail.connector.ServiceConnector;
032: import org.columba.mail.gui.composer.util.AddressCollector;
033: import org.columba.mail.parser.ListBuilder;
034: import org.columba.mail.parser.ListParser;
035: import org.columba.mail.util.MailResourceLoader;
036:
037: /**
038: * Recipients editor component.
039: *
040: * @author fdietz
041: */
042: public class HeaderController {
043:
044: private ComposerController controller;
045:
046: private HeaderView view;
047:
048: private AddressCollector addressCollector;
049:
050: public HeaderController(ComposerController controller) {
051: this .controller = controller;
052:
053: view = new HeaderView(this );
054:
055: addressCollector = AddressCollector.getInstance();
056:
057: if (addressCollector != null) {
058: // clear autocomplete hashmap
059: addressCollector.clear();
060:
061: try {
062: IContactFacade facade = ServiceConnector
063: .getContactFacade();
064: IFolderFacade folderFacade = ServiceConnector
065: .getFolderFacade();
066:
067: // fill hashmap with all available contacts and groups
068: try {
069: List<IFolder> list = folderFacade.getAllFolders();
070: Iterator<IFolder> it = list.iterator();
071: while (it.hasNext()) {
072: IFolder f = it.next();
073:
074: List<IHeaderItem> l = facade.getAllHeaderItems(
075: f.getId(), false);
076: addressCollector.addAllContacts(l, true);
077: }
078:
079: } catch (StoreException e) {
080: e.printStackTrace();
081: }
082:
083: } catch (ServiceNotFoundException e) {
084: }
085:
086: }
087:
088: view.initAutocompletion();
089: }
090:
091: public ComposerController getComposerController() {
092: return controller;
093: }
094:
095: public HeaderView getView() {
096: return view;
097: }
098:
099: public boolean checkState() {
100:
101: Iterator it = getHeaderItemList(0).iterator();
102:
103: while (it.hasNext()) {
104: IHeaderItem item = (IHeaderItem) it.next();
105: if (isValid(item))
106: return true;
107: }
108:
109: JOptionPane.showMessageDialog(controller.getComponent(),
110: MailResourceLoader.getString("menu", "mainframe",
111: "composer_no_recipients_found"));
112:
113: return false;
114: }
115:
116: protected boolean isValid(IHeaderItem headerItem) {
117: if (headerItem.isContact()) {
118: /*
119: * String address = (String) headerItem.get("email;internet");
120: *
121: * if (AddressParser.isValid(address)) { return true; }
122: *
123: * address = (String) headerItem.get("displayname");
124: *
125: * if (AddressParser.isValid(address)) { return true; }
126: */
127: return true;
128: } else {
129: return true;
130: }
131:
132: }
133:
134: public void installListener() {
135: // view.table.getModel().addTableModelListener(this);
136: }
137:
138: public void updateComponents(boolean b) {
139: if (b) {
140:
141: String s = ListParser.createStringFromList(controller
142: .getModel().getToList(), ",");
143: getView().getToComboBox().setText(s);
144:
145: s = ListParser.createStringFromList(controller.getModel()
146: .getCcList(), ",");
147: getView().getCcComboBox().setText(s);
148:
149: s = ListParser.createStringFromList(controller.getModel()
150: .getBccList(), ",");
151: getView().getBccComboBox().setText(s);
152:
153: } else {
154:
155: String s = getView().getToComboBox().getText();
156: List list = ListParser.createListFromString(s);
157: controller.getModel().setToList(list);
158:
159: s = getView().getCcComboBox().getText();
160: list = ListParser.createListFromString(s);
161: controller.getModel().setCcList(list);
162:
163: s = getView().getBccComboBox().getText();
164: list = ListParser.createListFromString(s);
165: controller.getModel().setBccList(list);
166:
167: }
168: }
169:
170: private List<IHeaderItem> getHeaderItemList(int recipient) {
171:
172: List<IHeaderItem> list = new Vector<IHeaderItem>();
173:
174: String str = null;
175: switch (recipient) {
176: case 0:
177: str = getView().getToComboBox().getText();
178:
179: break;
180: case 1:
181: str = getView().getCcComboBox().getText();
182:
183: break;
184: case 2:
185: str = getView().getBccComboBox().getText();
186:
187: break;
188:
189: }
190:
191: List l = ListParser.createListFromString(str);
192: if (l == null)
193: return list;
194:
195: Iterator it = l.iterator();
196:
197: while (it.hasNext()) {
198: String s = (String) it.next();
199: // skip empty strings
200: if (s.length() == 0)
201: continue;
202:
203: IHeaderItem item = null;
204: if (addressCollector != null)
205: item = addressCollector.getHeaderItem(s);
206: if (item == null) {
207:
208: try {
209: IModelFacade c = ServiceConnector.getModelFacade();
210: item = c.createContactItem();
211: item.setName(s);
212: } catch (ServiceNotFoundException e) {
213:
214: e.printStackTrace();
215: }
216:
217: }
218:
219: list.add(item);
220: }
221:
222: return list;
223: }
224:
225: public List<IHeaderItem> getToHeaderItemList() {
226: return getHeaderItemList(0);
227: }
228:
229: public List<IHeaderItem> getCcHeaderItemList() {
230: return getHeaderItemList(1);
231: }
232:
233: public List<IHeaderItem> getBccHeaderItemList() {
234: return getHeaderItemList(2);
235: }
236:
237: public void setToHeaderItemList(List<IHeaderItem> list) {
238: List<String> stringList = ListBuilder
239: .createStringListFromItemList(list);
240:
241: ((ComposerModel) controller.getModel()).setToList(stringList);
242: }
243:
244: public void setCcHeaderItemList(List<IHeaderItem> list) {
245: ((ComposerModel) controller.getModel()).setCcList(ListBuilder
246: .createStringListFromItemList(list));
247: }
248:
249: public void setBccHeaderItemList(List<IHeaderItem> list) {
250: ((ComposerModel) controller.getModel()).setBccList(ListBuilder
251: .createStringListFromItemList(list));
252: }
253:
254: // public void setHeaderItemLists(List<IHeaderItem>[] lists) {
255: // ((ComposerModel) controller.getModel()).setToList(ListBuilder
256: // .createStringListFromItemList(lists[0]));
257: //
258: // ((ComposerModel) controller.getModel()).setCcList(ListBuilder
259: // .createStringListFromItemList(lists[1]));
260: //
261: // ((ComposerModel) controller.getModel()).setBccList(ListBuilder
262: // .createStringListFromItemList(lists[2]));
263: //
264: // updateComponents(true);
265: // }
266:
267: }
|