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.addressbook.gui.action;
019:
020: import java.awt.event.ActionEvent;
021:
022: import javax.swing.event.TreeSelectionEvent;
023: import javax.swing.event.TreeSelectionListener;
024: import javax.swing.tree.TreePath;
025:
026: import org.columba.addressbook.folder.AddressbookFolder;
027: import org.columba.addressbook.folder.AddressbookTreeNode;
028: import org.columba.addressbook.folder.IContactFolder;
029: import org.columba.addressbook.gui.dialog.contact.ContactEditorDialog;
030: import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
031: import org.columba.addressbook.resourceloader.ContactImageLoader;
032: import org.columba.addressbook.resourceloader.IconKeys;
033: import org.columba.addressbook.util.AddressbookResourceLoader;
034: import org.columba.api.gui.frame.IFrameMediator;
035: import org.columba.core.gui.action.AbstractColumbaAction;
036: import org.columba.core.gui.dialog.ErrorDialog;
037: import org.columba.core.logging.Logging;
038:
039: /**
040: * Add new contact card to selected addressbook.
041: *
042: * @author fdietz
043: */
044: public class AddContactCardAction extends AbstractColumbaAction
045: implements TreeSelectionListener {
046:
047: private AddressbookTreeNode treeNode;
048:
049: public AddContactCardAction(IFrameMediator frameController) {
050: super (frameController, AddressbookResourceLoader.getString(
051: "menu", "mainframe", "menu_file_addcontact"));
052:
053: // tooltip text
054: putValue(SHORT_DESCRIPTION, AddressbookResourceLoader
055: .getString("menu", "mainframe",
056: "menu_file_addcontact_tooltip").replaceAll("&",
057: ""));
058:
059: putValue(TOOLBAR_NAME, AddressbookResourceLoader.getString(
060: "menu", "mainframe", "menu_file_addcontact_toolbar"));
061:
062: // icons
063: putValue(SMALL_ICON, ContactImageLoader
064: .getSmallIcon(IconKeys.NEW_CONTACT));
065: putValue(LARGE_ICON, ContactImageLoader
066: .getIcon(IconKeys.NEW_CONTACT));
067:
068: if (frameController instanceof AddressbookFrameMediator) {
069: // register interest on tree selection changes
070: ((AddressbookFrameMediator) frameMediator)
071: .addTreeSelectionListener(this );
072:
073: setEnabled(false);
074: } else
075: setEnabled(false);
076: }
077:
078: /**
079: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
080: */
081: public void actionPerformed(ActionEvent evt) {
082: if (frameMediator instanceof AddressbookFrameMediator) {
083:
084: AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
085:
086: // get selected folder
087: AddressbookFolder folder = (AddressbookFolder) mediator
088: .getTree().getSelectedFolder();
089:
090: ContactEditorDialog dialog = new ContactEditorDialog(
091: mediator.getView().getFrame());
092:
093: if (dialog.getResult()) {
094: try {
095: // add contact to folder
096: folder.add(dialog.getDestModel());
097: } catch (Exception e) {
098: if (Logging.DEBUG)
099: e.printStackTrace();
100:
101: ErrorDialog.createDialog(e.getMessage(), e);
102: }
103:
104: }
105: } else {
106: ContactEditorDialog dialog = new ContactEditorDialog(
107: frameMediator.getView().getFrame());
108:
109: // if (dialog.getResult()) {
110: // try {
111: // IContactFolder folder = dialog.getFolder();
112: //
113: // // add contact to folder
114: // folder.add(dialog.getDestModel());
115: // } catch (Exception e) {
116: // if (Logging.DEBUG)
117: // e.printStackTrace();
118: //
119: // ErrorDialog.createDialog(e.getMessage(), e);
120: // }
121: //
122: // }
123: }
124: }
125:
126: /**
127: * Enable or disable action on tree selection changes.
128: * <p>
129: * Actions should overwrite this method if they need more fine-grained
130: * control.
131: *
132: */
133: public void valueChanged(TreeSelectionEvent e) {
134: TreePath path = e.getNewLeadSelectionPath();
135:
136: // remember last selected folder treenode
137: if (path != null) {
138: treeNode = (AddressbookTreeNode) path
139: .getLastPathComponent();
140: }
141:
142: // enable, if more than zero treenodes selected
143: if ((path != null) && (treeNode instanceof AddressbookFolder)) {
144: setEnabled(true);
145: } else {
146: setEnabled(false);
147: }
148: }
149: }
|