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.ListSelectionEvent;
023: import javax.swing.event.TreeSelectionEvent;
024: import javax.swing.event.TreeSelectionListener;
025: import javax.swing.tree.TreePath;
026:
027: import org.columba.addressbook.folder.AbstractFolder;
028: import org.columba.addressbook.folder.AddressbookTreeNode;
029: import org.columba.addressbook.folder.GroupFolder;
030: import org.columba.addressbook.folder.IContactStorage;
031: import org.columba.addressbook.folder.IGroupFolder;
032: import org.columba.addressbook.gui.dialog.contact.ContactEditorDialog;
033: import org.columba.addressbook.gui.dialog.group.EditGroupDialog;
034: import org.columba.addressbook.gui.focus.FocusManager;
035: import org.columba.addressbook.gui.focus.FocusOwner;
036: import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
037: import org.columba.addressbook.gui.table.TableController;
038: import org.columba.addressbook.gui.tree.TreeController;
039: import org.columba.addressbook.model.IContactModel;
040: import org.columba.addressbook.model.IGroupModel;
041: import org.columba.addressbook.util.AddressbookResourceLoader;
042: import org.columba.api.gui.frame.IFrameMediator;
043: import org.columba.core.gui.dialog.ErrorDialog;
044: import org.columba.core.logging.Logging;
045:
046: /**
047: * Edit properties of selected contact or group.
048: *
049: * @author fdietz
050: */
051: public class EditPropertiesAction extends DefaultTableAction implements
052: TreeSelectionListener {
053: public EditPropertiesAction(IFrameMediator frameController) {
054: super (frameController, AddressbookResourceLoader.getString(
055: "menu", "mainframe", "menu_file_properties"));
056:
057: // tooltip text
058: putValue(SHORT_DESCRIPTION, AddressbookResourceLoader
059: .getString("menu", "mainframe",
060: "menu_file_properties_tooltip").replaceAll("&",
061: ""));
062:
063: putValue(TOOLBAR_NAME, AddressbookResourceLoader.getString(
064: "menu", "mainframe", "menu_file_properties_toolbar"));
065:
066: setEnabled(false);
067:
068: // register interest on tree selection changes
069: ((AddressbookFrameMediator) frameMediator)
070: .addTreeSelectionListener(this );
071: }
072:
073: /**
074: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
075: */
076: public void actionPerformed(ActionEvent evt) {
077: AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
078:
079: FocusOwner focusOwner = FocusManager.getInstance()
080: .getCurrentOwner();
081:
082: TableController table = ((AddressbookFrameMediator) frameMediator)
083: .getTable();
084:
085: boolean tableHasFocus = false;
086: if (table.equals(focusOwner))
087: tableHasFocus = true;
088:
089: if (tableHasFocus) {
090:
091: // get selected contact/group card
092: String[] uids = mediator.getTable().getUids();
093:
094: // get selected folder
095: IContactStorage folder = (IContactStorage) mediator
096: .getTree().getSelectedFolder();
097:
098: if (uids.length == 0) {
099: return;
100: }
101:
102: IContactModel card = null;
103: try {
104: card = (IContactModel) folder.get(uids[0]);
105: } catch (Exception e) {
106:
107: if (Logging.DEBUG)
108: e.printStackTrace();
109:
110: ErrorDialog.createDialog(e.getMessage(), e);
111: }
112:
113: //
114: ContactEditorDialog dialog = new ContactEditorDialog(
115: mediator.getView().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 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
145: .getView().getFrame(), card,
146: (AbstractFolder) folder.getParent());
147:
148: if (dialog.getResult()) {
149: // re-select folder
150: mediator.getTree().setSelectedFolder(folder);
151:
152: }
153:
154: }
155:
156: }
157:
158: /**
159: * Enable or disable action on selection change.
160: *
161: * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
162: */
163: public void valueChanged(ListSelectionEvent event) {
164: // return if selection change is in flux
165: if (event.getValueIsAdjusting()) {
166: return;
167: }
168:
169: FocusOwner focusOwner = FocusManager.getInstance()
170: .getCurrentOwner();
171:
172: TableController table = ((AddressbookFrameMediator) frameMediator)
173: .getTable();
174:
175: if (table.equals(focusOwner)) {
176:
177: // table has focus
178:
179: Object[] uids = ((AddressbookFrameMediator) frameMediator)
180: .getTable().getUids();
181:
182: if (uids.length > 0) {
183: setEnabled(true);
184: return;
185: }
186: }
187:
188: setEnabled(false);
189:
190: }
191:
192: public void valueChanged(TreeSelectionEvent e) {
193: TreePath path = e.getNewLeadSelectionPath();
194:
195: FocusOwner focusOwner = FocusManager.getInstance()
196: .getCurrentOwner();
197:
198: TreeController tree = ((AddressbookFrameMediator) frameMediator)
199: .getTree();
200:
201: if (tree.equals(focusOwner)) {
202: // tree has focus
203:
204: AddressbookTreeNode treeNode = null;
205: // remember last selected folder treenode
206: if (path != null) {
207: treeNode = (AddressbookTreeNode) path
208: .getLastPathComponent();
209: }
210:
211: // enable, if more than zero treenodes selected
212: if ((path != null) && (treeNode instanceof IGroupFolder)) {
213: setEnabled(true);
214: } else
215: setEnabled(false);
216:
217: } else
218:
219: setEnabled(false);
220: }
221: }
|