001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.gui.composer.contact;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Dimension;
022: import java.awt.GridLayout;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.awt.event.KeyEvent;
026: import java.awt.event.MouseEvent;
027: import java.util.List;
028:
029: import javax.swing.BorderFactory;
030: import javax.swing.JButton;
031: import javax.swing.JComboBox;
032: import javax.swing.JComponent;
033: import javax.swing.JDialog;
034: import javax.swing.JFrame;
035: import javax.swing.JLabel;
036: import javax.swing.JPanel;
037: import javax.swing.JScrollPane;
038: import javax.swing.KeyStroke;
039: import javax.swing.ListModel;
040: import javax.swing.SwingConstants;
041:
042: import org.columba.addressbook.facade.IContactFacade;
043: import org.columba.addressbook.facade.IFolder;
044: import org.columba.addressbook.facade.IFolderFacade;
045: import org.columba.addressbook.facade.IHeaderItem;
046: import org.columba.api.exception.ServiceNotFoundException;
047: import org.columba.core.gui.base.ButtonWithMnemonic;
048: import org.columba.core.gui.base.DoubleClickListener;
049: import org.columba.core.gui.base.SingleSideEtchedBorder;
050: import org.columba.mail.connector.ServiceConnector;
051: import org.columba.mail.gui.contact.list.ContactDNDListView;
052: import org.columba.mail.gui.contact.list.ContactListModel;
053: import org.columba.mail.util.MailResourceLoader;
054:
055: import com.jgoodies.forms.builder.PanelBuilder;
056: import com.jgoodies.forms.layout.CellConstraints;
057: import com.jgoodies.forms.layout.FormLayout;
058:
059: public class SelectAddressDialog extends JDialog implements
060: ActionListener {
061:
062: private static final java.util.logging.Logger LOG = java.util.logging.Logger
063: .getLogger("org.columba.addressbook.gui"); //$NON-NLS-1$
064:
065: // recipient lists
066: private ContactDNDListView toList;
067:
068: private ContactDNDListView ccList;
069:
070: private ContactDNDListView bccList;
071:
072: // addressbook list
073: private ContactDNDListView addressbookList;
074:
075: // action buttons for moving contacts
076: private JButton toButton;
077:
078: private JButton ccButton;
079:
080: private JButton bccButton;
081:
082: private JButton toRemoveButton;
083:
084: private JButton ccRemoveButton;
085:
086: private JButton bccRemoveButton;
087:
088: // labels
089: private JLabel toLabel;
090:
091: private JLabel ccLabel;
092:
093: private JLabel bccLabel;
094:
095: // choose addressbook
096: private FolderComboBox chooseComboBox;
097:
098: private JLabel chooseLabel;
099:
100: // models for addressbook/recipients lists
101: private ContactListModel[] dialogAddressbookListModel;
102:
103: private List<IHeaderItem> toListModel;
104:
105: private List<IHeaderItem> ccListModel;
106:
107: private List<IHeaderItem> bccListModel;
108:
109: private ButtonWithMnemonic cancelButton;
110:
111: private ButtonWithMnemonic okButton;
112:
113: private boolean success;
114:
115: public SelectAddressDialog(JFrame frame,
116: List<IHeaderItem> toListModel,
117: List<IHeaderItem> ccListModel,
118: List<IHeaderItem> bccListModel) {
119: super (frame, true);
120:
121: setTitle(MailResourceLoader.getString("dialog",
122: "selectaddressdialog", "title"));
123:
124: this .toListModel = toListModel;
125: this .ccListModel = ccListModel;
126: this .bccListModel = bccListModel;
127:
128: dialogAddressbookListModel = new ContactListModel[3];
129:
130: initComponents();
131:
132: layoutComponents();
133:
134: // try to fill the contact list
135: try {
136: IContactFacade contactFacade = ServiceConnector
137: .getContactFacade();
138: IFolderFacade folderFacade = ServiceConnector
139: .getFolderFacade();
140: IFolder folder = folderFacade.getLocalAddressbook();
141: List<IHeaderItem> list = contactFacade.getAllHeaderItems(
142: folder.getId(), false);
143:
144: addressbookList.setHeaderItemList(list);
145: } catch (ServiceNotFoundException e) {
146: e.printStackTrace();
147: }
148:
149: pack();
150: setLocationRelativeTo(null);
151:
152: setVisible(true);
153: }
154:
155: public List<IHeaderItem> getToList() {
156: return toListModel;
157: }
158:
159: public List<IHeaderItem> getCcList() {
160: return ccListModel;
161: }
162:
163: public List<IHeaderItem> getBccList() {
164: return bccListModel;
165: }
166:
167: /*
168: * public IHeaderItemList[] getHeaderItemLists() { return headerItemList; }
169: */
170: private JPanel createButtonPanel() {
171: JPanel panel = new JPanel();
172: FormLayout layout = new FormLayout(
173: "fill:default:grow, 6px, fill:default:grow, 6px, fill:default:grow", //$NON-NLS-1$
174: "default"); //$NON-NLS-1$
175:
176: PanelBuilder builder = new PanelBuilder(layout, panel);
177: CellConstraints cc = new CellConstraints();
178:
179: builder.add(toButton, cc.xy(1, 1));
180: builder.add(ccButton, cc.xy(3, 1));
181: builder.add(bccButton, cc.xy(5, 1));
182:
183: return panel;
184: }
185:
186: private JPanel createRemoveButtonPanel() {
187: JPanel panel = new JPanel();
188: FormLayout layout = new FormLayout(
189: "fill:default:grow, 6px, fill:default:grow, 6px, fill:default:grow", //$NON-NLS-1$
190: "default"); //$NON-NLS-1$
191:
192: PanelBuilder builder = new PanelBuilder(layout, panel);
193: CellConstraints cc = new CellConstraints();
194:
195: builder.add(toRemoveButton, cc.xy(1, 1));
196: builder.add(ccRemoveButton, cc.xy(3, 1));
197: builder.add(bccRemoveButton, cc.xy(5, 1));
198:
199: return panel;
200: }
201:
202: private JPanel createAddressbookPanel() {
203: JPanel panel = new JPanel();
204: FormLayout layout = new FormLayout(
205: "default, 6px, fill:default:grow", //$NON-NLS-1$
206: "default, 12px, fill:default:grow, 6px, default, 6px, default"); //$NON-NLS-1$
207:
208: PanelBuilder builder = new PanelBuilder(layout, panel);
209: CellConstraints cc = new CellConstraints();
210:
211: builder.add(chooseLabel, cc.xy(1, 1));
212: builder.add(chooseComboBox, cc.xy(3, 1));
213: builder.add(new JScrollPane(addressbookList), cc.xywh(1, 3, 3,
214: 1));
215: builder.add(createButtonPanel(), cc.xywh(1, 5, 3, 1));
216: builder.add(createRemoveButtonPanel(), cc.xywh(1, 7, 3, 1));
217:
218: return panel;
219: }
220:
221: private JPanel createRecipientsPanel() {
222: JPanel panel = new JPanel();
223: FormLayout layout = new FormLayout(
224: "fill:default:grow", //$NON-NLS-1$
225: "default, 6px, fill:default:grow, 12px, default, 6px, fill:default:grow, 12px, default, 6px, fill:default:grow"); //$NON-NLS-1$
226:
227: PanelBuilder builder = new PanelBuilder(layout, panel);
228: CellConstraints cc = new CellConstraints();
229:
230: int y = 1;
231: builder.add(toLabel, cc.xy(1, y));
232: y += 2;
233: builder.add(new JScrollPane(toList), cc.xy(1, y));
234: y += 2;
235: builder.add(ccLabel, cc.xy(1, y));
236: y += 2;
237: builder.add(new JScrollPane(ccList), cc.xy(1, y));
238: y += 2;
239: builder.add(bccLabel, cc.xy(1, y));
240: y += 2;
241: builder.add(new JScrollPane(bccList), cc.xy(1, y));
242:
243: return panel;
244: }
245:
246: private void layoutComponents() {
247: getContentPane().setLayout(new BorderLayout());
248:
249: JPanel mainPanel = new JPanel(new BorderLayout());
250: mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
251: 12));
252:
253: FormLayout layout = new FormLayout(
254: "fill:default:grow, 12px, fill:default:grow", //$NON-NLS-1$
255: "fill:default:grow"); //$NON-NLS-1$
256:
257: CellConstraints cc = new CellConstraints();
258: mainPanel.setLayout(layout);
259:
260: mainPanel.add(createAddressbookPanel(), cc.xy(1, 1));
261: mainPanel.add(createRecipientsPanel(), cc.xy(3, 1));
262:
263: getContentPane().add(mainPanel, BorderLayout.CENTER);
264:
265: JPanel bottomPanel = new JPanel(new BorderLayout());
266: bottomPanel.setBorder(new SingleSideEtchedBorder(
267: SwingConstants.TOP));
268:
269: JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 6, 0));
270: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
271: 12, 12));
272: buttonPanel.add(okButton);
273:
274: buttonPanel.add(cancelButton);
275: bottomPanel.add(buttonPanel, BorderLayout.EAST);
276: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
277: }
278:
279: private void initComponents() {
280: toLabel = new JLabel(MailResourceLoader.getString("dialog",
281: "selectaddressdialog", "to")); //$NON-NLS-1$
282:
283: dialogAddressbookListModel[0] = new ContactListModel();
284: dialogAddressbookListModel[0].setHeaderItemList(toListModel);
285: toList = new ContactDNDListView(dialogAddressbookListModel[0]);
286: toList.setMinimumSize(new Dimension(150, 150));
287: toList.addMouseListener(new DoubleClickListener() {
288:
289: public void doubleClick(MouseEvent ev) {
290: actionPerformed(new ActionEvent(toList, 0, "TO_REMOVE"));
291: }
292:
293: });
294:
295: ccLabel = new JLabel(MailResourceLoader.getString("dialog",
296: "selectaddressdialog", "cc")); //$NON-NLS-1$
297:
298: dialogAddressbookListModel[1] = new ContactListModel();
299: dialogAddressbookListModel[1].setHeaderItemList(ccListModel);
300: ccList = new ContactDNDListView(dialogAddressbookListModel[1]);
301: ccList.setMinimumSize(new Dimension(150, 150));
302: ccList.addMouseListener(new DoubleClickListener() {
303:
304: public void doubleClick(MouseEvent ev) {
305: actionPerformed(new ActionEvent(ccList, 0, "CC_REMOVE"));
306: }
307:
308: });
309:
310: bccLabel = new JLabel(MailResourceLoader.getString("dialog",
311: "selectaddressdialog", "bcc")); //$NON-NLS-1$
312:
313: dialogAddressbookListModel[2] = new ContactListModel();
314: dialogAddressbookListModel[2].setHeaderItemList(bccListModel);
315: bccList = new ContactDNDListView(dialogAddressbookListModel[2]);
316: bccList.setMinimumSize(new Dimension(150, 150));
317: bccList.addMouseListener(new DoubleClickListener() {
318:
319: public void doubleClick(MouseEvent ev) {
320: actionPerformed(new ActionEvent(bccList, 0,
321: "BCC_REMOVE"));
322: }
323:
324: });
325:
326: toButton = new JButton(MailResourceLoader.getString("dialog",
327: "selectaddressdialog", "right_arrow_to")); //$NON-NLS-1$
328: toButton.addActionListener(this );
329: toButton.setActionCommand("TO"); //$NON-NLS-1$
330:
331: toRemoveButton = new JButton(MailResourceLoader.getString(
332: "dialog", "selectaddressdialog", "left_arrow_to")); //$NON-NLS-1$
333: toRemoveButton.addActionListener(this );
334: toRemoveButton.setActionCommand("TO_REMOVE"); //$NON-NLS-1$
335:
336: ccButton = new JButton(MailResourceLoader.getString("dialog",
337: "selectaddressdialog", "right_arrow_cc")); //$NON-NLS-1$
338: ccButton.addActionListener(this );
339: ccButton.setActionCommand("CC"); //$NON-NLS-1$
340:
341: ccRemoveButton = new JButton(MailResourceLoader.getString(
342: "dialog", "selectaddressdialog", "left_arrow_cc")); //$NON-NLS-1$
343: ccRemoveButton.addActionListener(this );
344: ccRemoveButton.setActionCommand("CC_REMOVE"); //$NON-NLS-1$
345:
346: bccButton = new JButton(MailResourceLoader.getString("dialog",
347: "selectaddressdialog", "right_arrow_bcc")); //$NON-NLS-1$
348: bccButton.addActionListener(this );
349: bccButton.setActionCommand("BCC"); //$NON-NLS-1$
350:
351: bccRemoveButton = new JButton(MailResourceLoader.getString(
352: "dialog", "selectaddressdialog", "left_arrow_bcc")); //$NON-NLS-1$
353: bccRemoveButton.addActionListener(this );
354: bccRemoveButton.setActionCommand("BCC_REMOVE"); //$NON-NLS-1$
355:
356: chooseLabel = new JLabel(MailResourceLoader.getString("dialog",
357: "selectaddressdialog", "addressbook")); //$NON-NLS-1$
358:
359: chooseComboBox = new FolderComboBox(false);
360:
361: chooseComboBox.setActionCommand("CHOOSE"); //$NON-NLS-1$
362: chooseComboBox.addActionListener(this );
363:
364: addressbookList = new ContactDNDListView();
365: addressbookList.setMinimumSize(new Dimension(450, 200));
366: addressbookList.setAcceptDrop(false);
367: addressbookList.addMouseListener(new DoubleClickListener() {
368:
369: public void doubleClick(MouseEvent ev) {
370: actionPerformed(new ActionEvent(addressbookList, 0,
371: "TO"));
372: }
373:
374: });
375:
376: okButton = new ButtonWithMnemonic(MailResourceLoader.getString(
377: "global", "ok")); //$NON-NLS-1$ //$NON-NLS-2$
378: okButton.setActionCommand("OK"); //$NON-NLS-1$
379: okButton.addActionListener(this );
380:
381: cancelButton = new ButtonWithMnemonic(MailResourceLoader
382: .getString("global", "cancel")); //$NON-NLS-1$ //$NON-NLS-2$
383: cancelButton.setActionCommand("CANCEL"); //$NON-NLS-1$
384: cancelButton.addActionListener(this );
385:
386: getRootPane().setDefaultButton(okButton);
387: getRootPane().registerKeyboardAction(this ,
388: "CANCEL", //$NON-NLS-1$
389: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
390: JComponent.WHEN_IN_FOCUSED_WINDOW);
391: }
392:
393: /**
394: * @return Returns the success.
395: */
396: public boolean isSuccess() {
397: return success;
398: }
399:
400: public void actionPerformed(ActionEvent e) {
401: String command = e.getActionCommand();
402:
403: if (command.equals("CANCEL")) { //$NON-NLS-1$
404: setVisible(false);
405: } else if (command.equals("OK")) { //$NON-NLS-1$
406: success = true;
407:
408: setVisible(false);
409:
410: // toListModel.clear();
411: // ccListModel.clear();
412: // bccListModel.clear();
413:
414: // for (int i = 0; i < 3; i++) {
415: // Object[] array = dialogAddressbookListModel[i].toArray();
416: //
417: // for (int j = 0; j < array.length; j++) {
418: // IHeaderItem item = (IHeaderItem) array[j];
419: //
420: // if (i == 0) {
421: // ccListModel.add(item.clone());
422: // } else if (i == 1) {
423: // ccListModel.add(item.clone());
424: // } else if (i == 2) {
425: // bccListModel.add(item.clone());
426: // }
427: //
428: // }
429: // }
430: } else if (command.equals("TO")) { //$NON-NLS-1$
431:
432: int[] array = addressbookList.getSelectedIndices();
433: ListModel model = addressbookList.getModel();
434: IHeaderItem item;
435:
436: for (int j = 0; j < array.length; j++) {
437: item = (IHeaderItem) model.getElementAt(array[j]);
438: dialogAddressbookListModel[0].addElement(item);
439: }
440: } else if (command.equals("CC")) { //$NON-NLS-1$
441:
442: int[] array = addressbookList.getSelectedIndices();
443: ListModel model = addressbookList.getModel();
444: IHeaderItem item;
445:
446: for (int j = 0; j < array.length; j++) {
447: item = (IHeaderItem) model.getElementAt(array[j]);
448: dialogAddressbookListModel[1].addElement(item);
449: }
450: } else if (command.equals("BCC")) { //$NON-NLS-1$
451:
452: int[] array = addressbookList.getSelectedIndices();
453: ListModel model = addressbookList.getModel();
454: IHeaderItem item;
455:
456: for (int j = 0; j < array.length; j++) {
457: item = (IHeaderItem) model.getElementAt(array[j]);
458: dialogAddressbookListModel[2].addElement(item);
459: }
460: } else if (command.equals("TO_REMOVE")) { //$NON-NLS-1$
461:
462: Object[] array = toList.getSelectedValues();
463:
464: for (int j = 0; j < array.length; j++) {
465: dialogAddressbookListModel[0]
466: .removeElement((IHeaderItem) array[j]);
467: }
468: } else if (command.equals("CC_REMOVE")) { //$NON-NLS-1$
469:
470: Object[] array = ccList.getSelectedValues();
471:
472: for (int j = 0; j < array.length; j++) {
473: dialogAddressbookListModel[1]
474: .removeElement((IHeaderItem) array[j]);
475: }
476: } else if (command.equals("BCC_REMOVE")) { //$NON-NLS-1$
477:
478: Object[] array = bccList.getSelectedValues();
479:
480: for (int j = 0; j < array.length; j++) {
481: dialogAddressbookListModel[2]
482: .removeElement((IHeaderItem) array[j]);
483: }
484: } else if (command.equals("CHOOSE")) { //$NON-NLS-1$
485:
486: // folder selection changed
487: JComboBox cb = (JComboBox) e.getSource();
488:
489: IFolder contactFolder = (IFolder) cb.getSelectedItem();
490:
491: if (contactFolder != null) {
492:
493: try {
494: IContactFacade contactFacade = ServiceConnector
495: .getContactFacade();
496: List<IHeaderItem> list = contactFacade
497: .getAllHeaderItems(contactFolder.getId(),
498: false);
499:
500: addressbookList.setHeaderItemList(list);
501: } catch (ServiceNotFoundException e1) {
502: e1.printStackTrace();
503: }
504:
505: }
506: }
507: }
508: }
|