KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > table > selection > TableSelectionHandler


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.gui.table.selection;
17
18 import java.util.LinkedList JavaDoc;
19 import java.util.ListIterator JavaDoc;
20
21 import javax.swing.ListSelectionModel JavaDoc;
22 import javax.swing.event.ListSelectionEvent JavaDoc;
23 import javax.swing.event.ListSelectionListener JavaDoc;
24 import javax.swing.tree.TreePath JavaDoc;
25
26 import org.columba.api.command.ICommandReference;
27 import org.columba.api.selection.ISelectionListener;
28 import org.columba.api.selection.SelectionChangedEvent;
29 import org.columba.core.selection.SelectionHandler;
30 import org.columba.mail.command.MailFolderCommandReference;
31 import org.columba.mail.folder.IMailFolder;
32 import org.columba.mail.gui.table.IMessageNode;
33 import org.columba.mail.gui.table.ITableController;
34 import org.columba.mail.gui.table.model.MessageNode;
35 import org.columba.mail.gui.tree.selection.TreeSelectionChangedEvent;
36
37 /**
38  * TableSelectionHandler adds another abstraction layer to the swing JTable
39  * selection model.
40  * <p>
41  * It is responsible for providing a mapping between swing table rows or tree
42  * nodes into message UIDs and back.
43  * <p>
44  * Additionally it is able to encapsulate a message object transparently for
45  * every action in Columba. This means actions don't need to care about if this
46  * message is actually from a folder-/table-selection, as it usually is
47  * (example: user selects a message in the table and does a move operation on
48  * it), or if it is just a temporary message ( example: pgp-decrypted message ).
49  * <p>
50  * For this reason it uses a temporary folder to save such a message and provide
51  * actions with the correctly mapped MailFolderCommandReference[] object.
52  *
53  *
54  *
55  * @author fdietz
56  */

57 public class TableSelectionHandler extends SelectionHandler implements
58         ListSelectionListener JavaDoc, ISelectionListener {
59     public static final String JavaDoc HANDLER_ID = "mail.table";
60
61
62     private LinkedList JavaDoc<IMessageNode> messages;
63
64     private IMailFolder folder;
65
66     // if this is set to true, we use the local selection, instead
67
// of using the table selection
68
private boolean useLocalSelection;
69
70     private MailFolderCommandReference local;
71
72     private ITableController tableController;
73     /**
74      * @param id
75      */

76     public TableSelectionHandler(ITableController tableController) {
77         super(TableSelectionHandler.HANDLER_ID);
78
79         this.tableController = tableController;
80
81         tableController.getListSelectionModel().addListSelectionListener(this);
82
83         messages = new LinkedList JavaDoc<IMessageNode>();
84
85         useLocalSelection = false;
86     }
87
88     /**
89      *
90      * @see org.columba.core.gui.util.SelectionHandler#getSelection()
91      */

92     public ICommandReference getSelection() {
93         if (useLocalSelection == true) {
94             return local;
95         }
96
97         MailFolderCommandReference reference = new MailFolderCommandReference(
98                 folder, getUidArray());
99
100         return reference;
101     }
102
103     /**
104      *
105      * @see org.columba.core.gui.util.SelectionHandler#setSelection(ICommandReference)
106      */

107     public void setSelection(ICommandReference selection) {
108         MailFolderCommandReference ref = (MailFolderCommandReference) selection;
109
110         folder = (IMailFolder) ref.getSourceFolder();
111
112         useLocalSelection = false;
113
114         /*
115          * if (ref.getUids() != null) { messages.clear(); for (int i = 0; i <
116          * ref.getUids().length; i++) { Object uid = ref.getUids()[i];
117          * messages.add(tableController.getMessageNode(uid)); } }
118          */

119
120     }
121
122     private Object JavaDoc[] getUidArray() {
123         Object JavaDoc[] result = new Object JavaDoc[messages.size()];
124         ListIterator JavaDoc it = messages.listIterator();
125
126         int i = 0;
127
128         while (it.hasNext()) {
129             result[i++] = ((MessageNode) it.next()).getUid();
130         }
131
132         return result;
133     }
134
135     /**
136      *
137      * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
138      */

139     public void valueChanged(ListSelectionEvent JavaDoc e) {
140
141         useLocalSelection = false;
142
143         // user is still manipulating the selection
144
if (e.getValueIsAdjusting() == true) {
145             return;
146         }
147
148         messages = new LinkedList JavaDoc<IMessageNode>();
149
150         ListSelectionModel JavaDoc lsm = (ListSelectionModel JavaDoc) e.getSource();
151
152         if (lsm.isSelectionEmpty()) {
153             // no rows are selected
154

155         } else {
156             int[] rows = tableController.getSelectedRows();
157
158             for (int i = 0; i < rows.length; i++) {
159                 TreePath JavaDoc path = tableController.getPathForRow(rows[i]);
160                 if ( path == null ) continue;
161                 
162                 IMessageNode node = (IMessageNode) path.getLastPathComponent();
163                 messages.add(node);
164             }
165         }
166
167         fireSelectionChanged(new TableSelectionChangedEvent(folder,
168                 getUidArray()));
169
170     }
171
172     public void setLocalReference(MailFolderCommandReference r) {
173         this.local = r;
174
175         useLocalSelection = true;
176     }
177
178     /**
179      *
180      * @see org.columba.core.gui.util.ISelectionListener#connectionChanged(org.columba.core.gui.util.SelectionChangedEvent)
181      */

182     public void selectionChanged(SelectionChangedEvent e) {
183         if (((TreeSelectionChangedEvent) e).getSelected().length > 0) {
184             folder = ((TreeSelectionChangedEvent) e).getSelected()[0];
185         } else {
186             folder = null;
187         }
188     }
189 }
Popular Tags