KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > gui > action > AddContactCardAction


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.addressbook.gui.action;
19
20 import java.awt.event.ActionEvent JavaDoc;
21
22 import javax.swing.event.TreeSelectionEvent JavaDoc;
23 import javax.swing.event.TreeSelectionListener JavaDoc;
24 import javax.swing.tree.TreePath JavaDoc;
25
26 import org.columba.addressbook.folder.AddressbookFolder;
27 import org.columba.addressbook.folder.AddressbookTreeNode;
28 import org.columba.addressbook.folder.IContactFolder;
29 import org.columba.addressbook.gui.dialog.contact.ContactEditorDialog;
30 import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
31 import org.columba.addressbook.resourceloader.ContactImageLoader;
32 import org.columba.addressbook.resourceloader.IconKeys;
33 import org.columba.addressbook.util.AddressbookResourceLoader;
34 import org.columba.api.gui.frame.IFrameMediator;
35 import org.columba.core.gui.action.AbstractColumbaAction;
36 import org.columba.core.gui.dialog.ErrorDialog;
37 import org.columba.core.logging.Logging;
38
39 /**
40  * Add new contact card to selected addressbook.
41  *
42  * @author fdietz
43  */

44 public class AddContactCardAction extends AbstractColumbaAction implements
45         TreeSelectionListener JavaDoc {
46
47     private AddressbookTreeNode treeNode;
48
49     public AddContactCardAction(IFrameMediator frameController) {
50         super(frameController, AddressbookResourceLoader.getString("menu",
51                 "mainframe", "menu_file_addcontact"));
52
53         // tooltip text
54
putValue(SHORT_DESCRIPTION, AddressbookResourceLoader.getString("menu",
55                 "mainframe", "menu_file_addcontact_tooltip")
56                 .replaceAll("&", ""));
57
58         putValue(TOOLBAR_NAME, AddressbookResourceLoader.getString("menu",
59                 "mainframe", "menu_file_addcontact_toolbar"));
60
61         // icons
62
putValue(SMALL_ICON, ContactImageLoader
63                 .getSmallIcon(IconKeys.NEW_CONTACT));
64         putValue(LARGE_ICON, ContactImageLoader.getIcon(IconKeys.NEW_CONTACT));
65
66         if (frameController instanceof AddressbookFrameMediator) {
67             // register interest on tree selection changes
68
((AddressbookFrameMediator) frameMediator)
69                     .addTreeSelectionListener(this);
70
71             setEnabled(false);
72         } else
73             setEnabled(false);
74     }
75
76     /**
77      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
78      */

79     public void actionPerformed(ActionEvent JavaDoc evt) {
80         if (frameMediator instanceof AddressbookFrameMediator) {
81
82             AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
83
84             // get selected folder
85
AddressbookFolder folder = (AddressbookFolder) mediator.getTree()
86                     .getSelectedFolder();
87
88             ContactEditorDialog dialog = new ContactEditorDialog(mediator
89                     .getView().getFrame());
90
91             if (dialog.getResult()) {
92                 try {
93                     // add contact to folder
94
folder.add(dialog.getDestModel());
95                 } catch (Exception JavaDoc e) {
96                     if (Logging.DEBUG)
97                         e.printStackTrace();
98
99                     ErrorDialog.createDialog(e.getMessage(), e);
100                 }
101
102             }
103         } else {
104             ContactEditorDialog dialog = new ContactEditorDialog(frameMediator
105                     .getView().getFrame());
106
107 // if (dialog.getResult()) {
108
// try {
109
// IContactFolder folder = dialog.getFolder();
110
//
111
// // add contact to folder
112
// folder.add(dialog.getDestModel());
113
// } catch (Exception e) {
114
// if (Logging.DEBUG)
115
// e.printStackTrace();
116
//
117
// ErrorDialog.createDialog(e.getMessage(), e);
118
// }
119
//
120
// }
121
}
122     }
123
124     /**
125      * Enable or disable action on tree selection changes.
126      * <p>
127      * Actions should overwrite this method if they need more fine-grained
128      * control.
129      *
130      */

131     public void valueChanged(TreeSelectionEvent JavaDoc e) {
132         TreePath JavaDoc path = e.getNewLeadSelectionPath();
133
134         // remember last selected folder treenode
135
if (path != null) {
136             treeNode = (AddressbookTreeNode) path.getLastPathComponent();
137         }
138
139         // enable, if more than zero treenodes selected
140
if ((path != null) && (treeNode instanceof AddressbookFolder)) {
141             setEnabled(true);
142         } else {
143             setEnabled(false);
144         }
145     }
146 }
Popular Tags