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.table.selection;
017:
018: import java.util.LinkedList;
019: import java.util.ListIterator;
020:
021: import javax.swing.ListSelectionModel;
022: import javax.swing.event.ListSelectionEvent;
023: import javax.swing.event.ListSelectionListener;
024: import javax.swing.tree.TreePath;
025:
026: import org.columba.api.command.ICommandReference;
027: import org.columba.api.selection.ISelectionListener;
028: import org.columba.api.selection.SelectionChangedEvent;
029: import org.columba.core.selection.SelectionHandler;
030: import org.columba.mail.command.MailFolderCommandReference;
031: import org.columba.mail.folder.IMailFolder;
032: import org.columba.mail.gui.table.IMessageNode;
033: import org.columba.mail.gui.table.ITableController;
034: import org.columba.mail.gui.table.model.MessageNode;
035: import org.columba.mail.gui.tree.selection.TreeSelectionChangedEvent;
036:
037: /**
038: * TableSelectionHandler adds another abstraction layer to the swing JTable
039: * selection model.
040: * <p>
041: * It is responsible for providing a mapping between swing table rows or tree
042: * nodes into message UIDs and back.
043: * <p>
044: * Additionally it is able to encapsulate a message object transparently for
045: * every action in Columba. This means actions don't need to care about if this
046: * message is actually from a folder-/table-selection, as it usually is
047: * (example: user selects a message in the table and does a move operation on
048: * it), or if it is just a temporary message ( example: pgp-decrypted message ).
049: * <p>
050: * For this reason it uses a temporary folder to save such a message and provide
051: * actions with the correctly mapped MailFolderCommandReference[] object.
052: *
053: *
054: *
055: * @author fdietz
056: */
057: public class TableSelectionHandler extends SelectionHandler implements
058: ListSelectionListener, ISelectionListener {
059: public static final String HANDLER_ID = "mail.table";
060:
061: private LinkedList<IMessageNode> messages;
062:
063: private IMailFolder folder;
064:
065: // if this is set to true, we use the local selection, instead
066: // of using the table selection
067: private boolean useLocalSelection;
068:
069: private MailFolderCommandReference local;
070:
071: private ITableController tableController;
072:
073: /**
074: * @param id
075: */
076: public TableSelectionHandler(ITableController tableController) {
077: super (TableSelectionHandler.HANDLER_ID);
078:
079: this .tableController = tableController;
080:
081: tableController.getListSelectionModel()
082: .addListSelectionListener(this );
083:
084: messages = new LinkedList<IMessageNode>();
085:
086: useLocalSelection = false;
087: }
088:
089: /**
090: *
091: * @see org.columba.core.gui.util.SelectionHandler#getSelection()
092: */
093: public ICommandReference getSelection() {
094: if (useLocalSelection == true) {
095: return local;
096: }
097:
098: MailFolderCommandReference reference = new MailFolderCommandReference(
099: folder, getUidArray());
100:
101: return reference;
102: }
103:
104: /**
105: *
106: * @see org.columba.core.gui.util.SelectionHandler#setSelection(ICommandReference)
107: */
108: public void setSelection(ICommandReference selection) {
109: MailFolderCommandReference ref = (MailFolderCommandReference) selection;
110:
111: folder = (IMailFolder) ref.getSourceFolder();
112:
113: useLocalSelection = false;
114:
115: /*
116: * if (ref.getUids() != null) { messages.clear(); for (int i = 0; i <
117: * ref.getUids().length; i++) { Object uid = ref.getUids()[i];
118: * messages.add(tableController.getMessageNode(uid)); } }
119: */
120:
121: }
122:
123: private Object[] getUidArray() {
124: Object[] result = new Object[messages.size()];
125: ListIterator it = messages.listIterator();
126:
127: int i = 0;
128:
129: while (it.hasNext()) {
130: result[i++] = ((MessageNode) it.next()).getUid();
131: }
132:
133: return result;
134: }
135:
136: /**
137: *
138: * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
139: */
140: public void valueChanged(ListSelectionEvent e) {
141:
142: useLocalSelection = false;
143:
144: // user is still manipulating the selection
145: if (e.getValueIsAdjusting() == true) {
146: return;
147: }
148:
149: messages = new LinkedList<IMessageNode>();
150:
151: ListSelectionModel lsm = (ListSelectionModel) e.getSource();
152:
153: if (lsm.isSelectionEmpty()) {
154: // no rows are selected
155:
156: } else {
157: int[] rows = tableController.getSelectedRows();
158:
159: for (int i = 0; i < rows.length; i++) {
160: TreePath path = tableController.getPathForRow(rows[i]);
161: if (path == null)
162: continue;
163:
164: IMessageNode node = (IMessageNode) path
165: .getLastPathComponent();
166: messages.add(node);
167: }
168: }
169:
170: fireSelectionChanged(new TableSelectionChangedEvent(folder,
171: getUidArray()));
172:
173: }
174:
175: public void setLocalReference(MailFolderCommandReference r) {
176: this .local = r;
177:
178: useLocalSelection = true;
179: }
180:
181: /**
182: *
183: * @see org.columba.core.gui.util.ISelectionListener#connectionChanged(org.columba.core.gui.util.SelectionChangedEvent)
184: */
185: public void selectionChanged(SelectionChangedEvent e) {
186: if (((TreeSelectionChangedEvent) e).getSelected().length > 0) {
187: folder = ((TreeSelectionChangedEvent) e).getSelected()[0];
188: } else {
189: folder = null;
190: }
191: }
192: }
|