KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > tree > TreeController


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.tree;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 import javax.swing.JPopupMenu JavaDoc;
22 import javax.swing.event.EventListenerList JavaDoc;
23 import javax.swing.event.TreeExpansionEvent JavaDoc;
24 import javax.swing.event.TreeSelectionEvent JavaDoc;
25 import javax.swing.event.TreeSelectionListener JavaDoc;
26 import javax.swing.event.TreeWillExpandListener JavaDoc;
27 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
28 import javax.swing.tree.ExpandVetoException JavaDoc;
29 import javax.swing.tree.TreeModel JavaDoc;
30 import javax.swing.tree.TreePath JavaDoc;
31 import javax.swing.tree.TreeSelectionModel JavaDoc;
32
33 import org.columba.api.gui.frame.IFrameMediator;
34 import org.columba.core.gui.menu.ExtendablePopupMenu;
35 import org.columba.core.gui.menu.MenuXMLDecoder;
36 import org.columba.core.io.DiskIO;
37 import org.columba.core.xml.XmlElement;
38 import org.columba.mail.config.IFolderItem;
39 import org.columba.mail.folder.IMailFolder;
40 import org.columba.mail.gui.tree.comparator.FolderComparator;
41 import org.columba.mail.gui.tree.comparator.UnreadFolderComparator;
42 import org.columba.mail.gui.tree.util.FolderTreeCellRenderer;
43
44 /**
45  * this class shows the the folder hierarchy
46  */

47 public class TreeController implements TreeSelectionListener JavaDoc,
48         TreeWillExpandListener JavaDoc, ITreeController {
49
50     private IMailFolder selectedFolder;
51
52     private TreeView view;
53
54     private IFrameMediator frameController;
55
56     private ExtendablePopupMenu menu;
57
58     private FolderComparator folderComparator;
59
60     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
61
62     /**
63      * Constructor for tree controller.
64      *
65      * @param controller
66      * the parent controller.
67      * @param model
68      * the tree model to display.
69      */

70     public TreeController(IFrameMediator controller, FolderTreeModel model) {
71         frameController = controller;
72
73         view = new TreeView(model);
74         view.setSortingEnabled(false);
75
76         view.addTreeWillExpandListener(this);
77
78         FolderTreeCellRenderer renderer = new FolderTreeCellRenderer();
79         view.setCellRenderer(renderer);
80
81         getView().setTransferHandler(new TreeViewTransferHandler(controller));
82         getView().setDragEnabled(true);
83
84         getView().getSelectionModel().setSelectionMode(
85                 TreeSelectionModel.SINGLE_TREE_SELECTION);
86
87         getView().addTreeSelectionListener(this);
88
89     }
90
91     /**
92      * @see org.columba.mail.gui.tree.ITreeController#sortAscending(boolean)
93      */

94     public void sortAscending(boolean ascending) {
95         folderComparator.setAscending(ascending);
96         view.setSortingComparator(folderComparator);
97     }
98
99     /**
100      * Returns the tree view.
101      *
102      * @return the tree view.
103      */

104     public TreeView getView() {
105         return view;
106     }
107
108     /**
109      * Set the specified folder as seleceted.
110      *
111      * @param folder
112      * the new selected folder.
113      */

114     public void setSelected(IMailFolder folder) {
115         view.clearSelection();
116
117         TreePath JavaDoc path = folder.getSelectionTreePath();
118
119         // @author: fdietz never request focus
120
//view.requestFocus();
121
/*
122          * view.setLeadSelectionPath(path); view.setAnchorSelectionPath(path);
123          */

124         view.setSelectionPath(path);
125         view.expandPath(path);
126
127         this.selectedFolder = folder;
128
129     }
130
131     /**
132      * Creates a Popup menu.
133      */

134     public void createPopupMenu() {
135         try {
136             InputStream JavaDoc is = DiskIO
137                     .getResourceStream("org/columba/mail/action/tree_contextmenu.xml");
138
139             menu = new MenuXMLDecoder(frameController).createPopupMenu(is);
140         } catch (IOException JavaDoc e) {
141             e.printStackTrace();
142         }
143     }
144
145     /**
146      * Returns the pop up menu for the controller.
147      *
148      * @return the pop up menu.
149      */

150     public JPopupMenu JavaDoc getPopupMenu() {
151         return menu;
152     }
153
154     /**
155      * @see org.columba.mail.gui.tree.ITreeController#getSelected()
156      */

157     public IMailFolder getSelected() {
158         return selectedFolder;
159     }
160
161     /**
162      * Returns the mailFrameController.
163      *
164      * @return MailFrameController
165      */

166     public IFrameMediator getFrameController() {
167         return frameController;
168     }
169
170     /**
171      * ****************** TreeWillExpand Interface
172      * ******************************
173      */

174
175     /** {@inheritDoc} */
176     public void treeWillExpand(TreeExpansionEvent JavaDoc e) throws ExpandVetoException JavaDoc {
177         IMailFolder treeNode = (IMailFolder) e.getPath().getLastPathComponent();
178
179         if (treeNode == null) {
180             return;
181         }
182
183         // save expanded state
184
saveExpandedState(treeNode, e.getPath());
185     }
186
187     /** {@inheritDoc} */
188     public void treeWillCollapse(TreeExpansionEvent JavaDoc e) {
189         IMailFolder treeNode = (IMailFolder) e.getPath().getLastPathComponent();
190
191         if (treeNode == null) {
192             return;
193         }
194
195         // save expanded state
196
saveExpandedState(treeNode, e.getPath());
197     }
198
199     /**
200      * Saves the tree expanded state.
201      *
202      * @param folder
203      * the folder to get the configuration for.
204      * @param path
205      * the tree path in the tree view.
206      */

207     private void saveExpandedState(IMailFolder folder, TreePath JavaDoc path) {
208         IFolderItem item = folder.getConfiguration();
209
210         XmlElement property = item.getElement("property");
211
212         // Note: we negate the expanded state because this is
213
// a will-expand/collapse listener
214
if (!getView().isExpanded(path)) {
215             property.addAttribute("expanded", "true");
216         } else {
217             property.addAttribute("expanded", "false");
218         }
219     }
220
221     /**
222      * @see org.columba.mail.gui.tree.ITreeController#getModel()
223      */

224     public TreeModel JavaDoc getModel() {
225         return getView().getModel();
226     }
227
228     /**
229      * @see org.columba.mail.gui.tree.ITreeController#setSortingEnabled(boolean)
230      */

231     public void setSortingEnabled(boolean enabled) {
232         view.setSortingEnabled(enabled);
233     }
234
235     public void setSortingMode(SORTING_MODE_ENUM sortingMode, boolean ascending) {
236
237         if (sortingMode == SORTING_MODE_ENUM.ALPHABETICAL) {
238             setFolderComparator(new FolderComparator(ascending));
239         } else if (sortingMode == SORTING_MODE_ENUM.UNREAD_COUNT) {
240             setFolderComparator(new UnreadFolderComparator(ascending));
241         } else {
242             // no sorting
243
}
244     }
245
246     /**
247      * Set a new folder comparator for sorting the folders.
248      *
249      * @param comparator
250      * the folder comparator to use.
251      */

252     private void setFolderComparator(FolderComparator comparator) {
253         folderComparator = comparator;
254         view.setSortingComparator(folderComparator);
255     }
256
257     public void addFolderSelectionListener(IFolderSelectionListener l) {
258         listenerList.add(IFolderSelectionListener.class, l);
259     }
260
261     public void removeFolderSelectionListener(IFolderSelectionListener l) {
262         listenerList.remove(IFolderSelectionListener.class, l);
263     }
264
265     protected void fireFolderSelectionChangedEvent(IMailFolder folder) {
266
267         IFolderSelectionEvent e = new FolderSelectionEvent(this, folder);
268         // Guaranteed to return a non-null array
269
Object JavaDoc[] listeners = listenerList.getListenerList();
270
271         // Process the listeners last to first, notifying
272
// those that are interested in this event
273
for (int i = listeners.length - 2; i >= 0; i -= 2) {
274             if (listeners[i] == IFolderSelectionListener.class) {
275                 ((IFolderSelectionListener) listeners[i + 1])
276                         .selectionChanged(e);
277             }
278         }
279     }
280
281     public void valueChanged(TreeSelectionEvent JavaDoc e) {
282         DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) getView()
283                 .getLastSelectedPathComponent();
284
285         if (node == null)
286             return;
287
288         // safe to cast to IMailFolder here, because only those are visible to the user
289
fireFolderSelectionChangedEvent((IMailFolder)node);
290     }
291
292 }
Popular Tags