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.IGroupFolder;
030: import org.columba.addressbook.gui.focus.FocusManager;
031: import org.columba.addressbook.gui.focus.FocusOwner;
032: import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
033: import org.columba.addressbook.gui.table.TableController;
034: import org.columba.addressbook.gui.tree.AddressbookTreeModel;
035: import org.columba.addressbook.gui.tree.TreeController;
036: import org.columba.addressbook.util.AddressbookResourceLoader;
037: import org.columba.api.gui.frame.IFrameMediator;
038: import org.columba.core.gui.dialog.ErrorDialog;
039: import org.columba.core.logging.Logging;
040: import org.columba.core.resourceloader.IconKeys;
041: import org.columba.core.resourceloader.ImageLoader;
042:
043: /**
044: * Delete selected contact or group item.
045: *
046: * @author fdietz
047: */
048: public class RemoveCardAction extends DefaultTableAction implements
049: TreeSelectionListener {
050: public RemoveCardAction(IFrameMediator frameController) {
051: super (frameController, AddressbookResourceLoader.getString(
052: "menu", "mainframe", "menu_file_remove"));
053:
054: // tooltip text
055: putValue(SHORT_DESCRIPTION, AddressbookResourceLoader
056: .getString("menu", "mainframe",
057: "menu_file_remove_tooltip").replaceAll("&", ""));
058:
059: putValue(TOOLBAR_NAME, AddressbookResourceLoader.getString(
060: "menu", "mainframe", "menu_file_remove_toolbar"));
061:
062: // icons
063: putValue(SMALL_ICON, ImageLoader
064: .getSmallIcon(IconKeys.EDIT_DELETE));
065: putValue(LARGE_ICON, ImageLoader.getIcon(IconKeys.EDIT_DELETE));
066:
067: setEnabled(false);
068:
069: // register interest on tree selection changes
070: ((AddressbookFrameMediator) frameMediator)
071: .addTreeSelectionListener(this );
072: }
073:
074: /**
075: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
076: */
077: public void actionPerformed(ActionEvent evt) {
078: AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
079:
080: FocusOwner focusOwner = FocusManager.getInstance()
081: .getCurrentOwner();
082:
083: TableController table = ((AddressbookFrameMediator) frameMediator)
084: .getTable();
085:
086: boolean tableHasFocus = false;
087: if (table.equals(focusOwner))
088: tableHasFocus = true;
089:
090: if (tableHasFocus) {
091:
092: // get selected contact/group card
093: String[] uids = mediator.getTable().getUids();
094:
095: // get selected folder
096: AbstractFolder folder = (AbstractFolder) mediator.getTree()
097: .getSelectedFolder();
098:
099: // remove contacts/group cards from folder
100: for (int i = 0; i < uids.length; i++) {
101: try {
102: folder.remove(uids[i]);
103: } catch (Exception e) {
104: if (Logging.DEBUG)
105: e.printStackTrace();
106:
107: ErrorDialog.createDialog(e.getMessage(), e);
108: }
109: }
110:
111: if (folder instanceof IGroupFolder)
112: // re-select folder
113: mediator.getTree().setSelectedFolder(folder);
114: } else {
115: // tree has focus
116:
117: // get selected folder
118: AbstractFolder folder = (AbstractFolder) mediator.getTree()
119: .getSelectedFolder();
120:
121: // get parent
122: AddressbookTreeNode parent = (AddressbookTreeNode) folder
123: .getParent();
124:
125: mediator.getTree().setSelectedFolder(
126: (AbstractFolder) parent);
127:
128: // remove folder from parent
129: folder.removeFromParent();
130:
131: // notify model
132: AddressbookTreeModel.getInstance().nodeStructureChanged(
133: parent);
134:
135: }
136: }
137:
138: /**
139: * Enable or disable action on selection change.
140: *
141: * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
142: */
143: public void valueChanged(ListSelectionEvent event) {
144: // return if selection change is in flux
145: if (event.getValueIsAdjusting()) {
146: return;
147: }
148:
149: FocusOwner focusOwner = FocusManager.getInstance()
150: .getCurrentOwner();
151:
152: TableController table = ((AddressbookFrameMediator) frameMediator)
153: .getTable();
154:
155: if (table.equals(focusOwner)) {
156:
157: // table has focus
158:
159: Object[] uids = ((AddressbookFrameMediator) frameMediator)
160: .getTable().getUids();
161:
162: if (uids.length > 0) {
163: setEnabled(true);
164: return;
165: }
166: }
167:
168: setEnabled(false);
169:
170: }
171:
172: public void valueChanged(TreeSelectionEvent e) {
173: TreePath path = e.getNewLeadSelectionPath();
174:
175: FocusOwner focusOwner = FocusManager.getInstance()
176: .getCurrentOwner();
177:
178: TreeController tree = ((AddressbookFrameMediator) frameMediator)
179: .getTree();
180:
181: if (tree.equals(focusOwner)) {
182: // tree has focus
183:
184: AddressbookTreeNode treeNode = null;
185: // remember last selected folder treenode
186: if (path != null) {
187: treeNode = (AddressbookTreeNode) path
188: .getLastPathComponent();
189: }
190:
191: // enable, if more than zero treenodes selected
192: if ((path != null) && (treeNode instanceof IGroupFolder)) {
193: setEnabled(true);
194: } else
195: setEnabled(false);
196:
197: } else
198:
199: setEnabled(false);
200: }
201: }
|