KickJava   Java API By Example, From Geeks To Geeks.

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


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.ListSelectionEvent JavaDoc;
23 import javax.swing.event.TreeSelectionEvent JavaDoc;
24 import javax.swing.event.TreeSelectionListener JavaDoc;
25 import javax.swing.tree.TreePath JavaDoc;
26
27 import org.columba.addressbook.folder.AbstractFolder;
28 import org.columba.addressbook.folder.AddressbookTreeNode;
29 import org.columba.addressbook.folder.GroupFolder;
30 import org.columba.addressbook.folder.IContactStorage;
31 import org.columba.addressbook.folder.IGroupFolder;
32 import org.columba.addressbook.gui.dialog.contact.ContactEditorDialog;
33 import org.columba.addressbook.gui.dialog.group.EditGroupDialog;
34 import org.columba.addressbook.gui.focus.FocusManager;
35 import org.columba.addressbook.gui.focus.FocusOwner;
36 import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
37 import org.columba.addressbook.gui.table.TableController;
38 import org.columba.addressbook.gui.tree.TreeController;
39 import org.columba.addressbook.model.IContactModel;
40 import org.columba.addressbook.model.IGroupModel;
41 import org.columba.addressbook.util.AddressbookResourceLoader;
42 import org.columba.api.gui.frame.IFrameMediator;
43 import org.columba.core.gui.dialog.ErrorDialog;
44 import org.columba.core.logging.Logging;
45
46 /**
47  * Edit properties of selected contact or group.
48  *
49  * @author fdietz
50  */

51 public class EditPropertiesAction extends DefaultTableAction implements
52         TreeSelectionListener JavaDoc {
53     public EditPropertiesAction(IFrameMediator frameController) {
54         super(frameController, AddressbookResourceLoader.getString("menu",
55                 "mainframe", "menu_file_properties"));
56
57         // tooltip text
58
putValue(SHORT_DESCRIPTION, AddressbookResourceLoader.getString("menu",
59                 "mainframe", "menu_file_properties_tooltip")
60                 .replaceAll("&", ""));
61
62         putValue(TOOLBAR_NAME, AddressbookResourceLoader.getString("menu",
63                 "mainframe", "menu_file_properties_toolbar"));
64
65         
66
67         setEnabled(false);
68
69         // register interest on tree selection changes
70
((AddressbookFrameMediator) frameMediator)
71                 .addTreeSelectionListener(this);
72     }
73
74     /**
75      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
76      */

77     public void actionPerformed(ActionEvent JavaDoc evt) {
78         AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
79
80         FocusOwner focusOwner = FocusManager.getInstance().getCurrentOwner();
81
82         TableController table = ((AddressbookFrameMediator) frameMediator)
83                 .getTable();
84
85         boolean tableHasFocus = false;
86         if (table.equals(focusOwner))
87             tableHasFocus = true;
88
89         if (tableHasFocus) {
90
91             // get selected contact/group card
92
String JavaDoc[] uids = mediator.getTable().getUids();
93
94             // get selected folder
95
IContactStorage folder = (IContactStorage) mediator.getTree()
96                     .getSelectedFolder();
97
98             if (uids.length == 0) {
99                 return;
100             }
101
102             IContactModel card = null;
103             try {
104                 card = (IContactModel) folder.get(uids[0]);
105             } catch (Exception JavaDoc e) {
106
107                 if (Logging.DEBUG)
108                     e.printStackTrace();
109
110                 ErrorDialog.createDialog(e.getMessage(), e);
111             }
112
113             //
114
ContactEditorDialog dialog = new ContactEditorDialog(mediator.getView()
115                     .getFrame(), card);
116
117             if (dialog.getResult()) {
118
119                 try {
120                     // modify card properties in folder
121
folder.modify(uids[0], dialog.getDestModel());
122                 } catch (Exception JavaDoc e1) {
123                     if (Logging.DEBUG)
124                         e1.printStackTrace();
125
126                     ErrorDialog.createDialog(e1.getMessage(), e1);
127                 }
128
129                 if (folder instanceof GroupFolder)
130                     // re-select folder
131
mediator.getTree().setSelectedFolder(
132                             (AbstractFolder) folder);
133
134             }
135
136         } else {
137             // tree has focus
138

139             GroupFolder folder = (GroupFolder) mediator.getTree()
140                     .getSelectedFolder();
141
142             IGroupModel card = folder.getGroup();
143
144             EditGroupDialog dialog = new EditGroupDialog(mediator.getView()
145                     .getFrame(), card, (AbstractFolder) folder.getParent());
146
147             if (dialog.getResult()) {
148                 // re-select folder
149
mediator.getTree().setSelectedFolder(folder);
150
151             }
152
153         }
154
155     }
156
157     /**
158      * Enable or disable action on selection change.
159      *
160      * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
161      */

162     public void valueChanged(ListSelectionEvent JavaDoc event) {
163         // return if selection change is in flux
164
if (event.getValueIsAdjusting()) {
165             return;
166         }
167
168         FocusOwner focusOwner = FocusManager.getInstance().getCurrentOwner();
169
170         TableController table = ((AddressbookFrameMediator) frameMediator)
171                 .getTable();
172
173         if (table.equals(focusOwner)) {
174
175             // table has focus
176

177             Object JavaDoc[] uids = ((AddressbookFrameMediator) frameMediator)
178                     .getTable().getUids();
179
180             if (uids.length > 0) {
181                 setEnabled(true);
182                 return;
183             }
184         }
185
186         setEnabled(false);
187
188     }
189
190     public void valueChanged(TreeSelectionEvent JavaDoc e) {
191         TreePath JavaDoc path = e.getNewLeadSelectionPath();
192
193         FocusOwner focusOwner = FocusManager.getInstance().getCurrentOwner();
194
195         TreeController tree = ((AddressbookFrameMediator) frameMediator)
196                 .getTree();
197
198         if (tree.equals(focusOwner)) {
199             // tree has focus
200

201             AddressbookTreeNode treeNode = null;
202             // remember last selected folder treenode
203
if (path != null) {
204                 treeNode = (AddressbookTreeNode) path.getLastPathComponent();
205             }
206
207             // enable, if more than zero treenodes selected
208
if ((path != null) && (treeNode instanceof IGroupFolder)) {
209                 setEnabled(true);
210             } else
211                 setEnabled(false);
212
213         } else
214
215             setEnabled(false);
216     }
217 }
Popular Tags