001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.gui.tree.selection;
017:
018: import java.util.LinkedList;
019: import java.util.logging.Logger;
020:
021: import javax.swing.event.TreeSelectionEvent;
022: import javax.swing.event.TreeSelectionListener;
023: import javax.swing.tree.TreePath;
024:
025: import org.columba.api.command.ICommandReference;
026: import org.columba.core.selection.SelectionHandler;
027: import org.columba.mail.command.MailFolderCommandReference;
028: import org.columba.mail.folder.IMailFolder;
029: import org.columba.mail.gui.tree.TreeView;
030:
031: /**
032: * Handles the tree selection.
033: * <p>
034: * Listens for swing tree selection events and translates TreePath selection to
035: * MailFolderCommandReference.
036: * <p>
037: * Actions creating Commands and passing MailFolderCommandReference directly ask
038: * {@link TreeSelectionManager}for the selection. They don't talk with the
039: * swing JTree.
040: *
041: * @author fdietz, tstich
042: */
043: public class TreeSelectionHandler extends SelectionHandler implements
044: TreeSelectionListener {
045:
046: public static final String HANDLER_ID = "mail.tree";
047:
048: /** JDK 1.4+ logging framework logger, used for logging. */
049: private static final Logger LOG = Logger
050: .getLogger("org.columba.mail.gui.tree.selection");
051:
052: private static final IMailFolder[] FOLDER_ARRAY = { null };
053:
054: private TreeView view;
055:
056: private LinkedList selectedFolders;
057:
058: private boolean setSelection;
059:
060: public TreeSelectionHandler(TreeView view) {
061: super (TreeSelectionHandler.HANDLER_ID);
062: this .view = view;
063: view.addTreeSelectionListener(this );
064: selectedFolders = new LinkedList();
065:
066: setSelection = false;
067: }
068:
069: /*
070: * (non-Javadoc)
071: *
072: * @see org.columba.core.gui.util.SelectionHandler#getSelection()
073: */
074: public ICommandReference getSelection() {
075: if (selectedFolders.size() == 0)
076: return null;
077:
078: MailFolderCommandReference reference = new MailFolderCommandReference(
079: (IMailFolder) selectedFolders.get(0));
080:
081: return reference;
082: }
083:
084: /**
085: *
086: * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
087: */
088: public void valueChanged(TreeSelectionEvent e) {
089: // BUGFIX but don't know why that bug occurs
090: if (e.getPath() == null) {
091: return;
092: }
093:
094: // If the tree is in a DND action then we dont need to update all
095: // listeners, since this only a temporary folder selection.
096: if (view.isInDndAction()) {
097: return;
098: }
099:
100: if (setSelection) {
101: selectedFolders.clear();
102: setSelection = false;
103: }
104:
105: for (int i = 0; i < e.getPaths().length; i++) {
106: if (e.getPaths()[i].getLastPathComponent() instanceof IMailFolder) {
107: IMailFolder folder = (IMailFolder) e.getPaths()[i]
108: .getLastPathComponent();
109:
110: if (e.isAddedPath(i)) {
111: LOG.info("Folder added to Selection= "
112: + folder.getName());
113: selectedFolders.add(folder);
114: } else {
115: LOG.info("Folder removed from Selection= "
116: + folder.getName());
117: selectedFolders.remove(folder);
118: }
119: }
120: }
121:
122: if (selectedFolders.size() == 0)
123: return;
124:
125: fireSelectionChanged(new TreeSelectionChangedEvent(
126: (IMailFolder[]) selectedFolders.toArray(FOLDER_ARRAY)));
127:
128: }
129:
130: public void setSelection(ICommandReference selection) {
131:
132: selectedFolders.clear();
133:
134: if (selection == null
135: || ((MailFolderCommandReference) selection)
136: .getSourceFolder() == null) {
137: view.clearSelection();
138: } else {
139:
140: TreePath path = ((IMailFolder) ((MailFolderCommandReference) selection)
141: .getSourceFolder()).getSelectionTreePath();
142: view.setSelectionPath(path);
143: view.expandPath(path);
144: selectedFolders
145: .add(((MailFolderCommandReference) selection)
146: .getSourceFolder());
147: }
148:
149: setSelection = true;
150:
151: fireSelectionChanged(new TreeSelectionChangedEvent(
152: (IMailFolder[]) selectedFolders.toArray(FOLDER_ARRAY)));
153:
154: }
155: }
|