KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > docnavigation > AWTContainerNavigatorFactory


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.docnavigation;
35
36 import edu.rice.cs.plt.tuple.Pair;
37 import edu.rice.cs.util.swing.Utilities;
38
39 import java.util.List JavaDoc;
40 import java.util.*;
41 import java.awt.event.FocusListener JavaDoc;
42
43 public class AWTContainerNavigatorFactory<ItemT extends INavigatorItem> implements IDocumentNavigatorFactory<ItemT> {
44   
45   public AWTContainerNavigatorFactory() { }
46
47   /** Creates a new List Navigator
48    * @return a list navigator
49    */

50     public IDocumentNavigator<ItemT> makeListNavigator() { return new JListSortNavigator<ItemT>(); }
51
52   /** Returns a new tree Navigator with the specified root
53    * @param path the path name of the root node
54    * @return a tree navigator
55    */

56     public IDocumentNavigator<ItemT> makeTreeNavigator(String JavaDoc path) { return new JTreeSortNavigator<ItemT>(path); }
57     
58     /** Creates a list navigator and migrates the navigator items from parent to the new navigator. The migration
59       * is asynchronous but it completes before any subsequent computation in the event thread.
60       * @param parent the navigator to migrate from
61       * @return the new list navigator
62       */

63     public IDocumentNavigator<ItemT> makeListNavigator(final IDocumentNavigator<ItemT> parent) {
64       final IDocumentNavigator<ItemT> child = makeListNavigator();
65       Utilities.invokeLater(new Runnable JavaDoc() {
66         public void run() {
67 // synchronized (child.getModelLock()) { // dropped because of cost; each atomic action is still synchronized
68
migrateNavigatorItems(child, parent);
69             migrateListeners(child, parent);
70           }
71 // }
72
});
73       return child;
74     }
75   
76     /** Creates a tree navigator and migrates the navigator items from the parent to the new navigator. The migration
77       * is asynchronous but it completes before any subsequent computation in the event thread.
78       * @param name the name of the root node
79       * @param parent the navigator to migrate from
80       * @return the new tree navigator
81       */

82     public IDocumentNavigator<ItemT> makeTreeNavigator(String JavaDoc name, final IDocumentNavigator<ItemT> parent,
83                                                 final List JavaDoc<Pair<String JavaDoc, INavigatorItemFilter<ItemT>>> l) {
84       
85       final IDocumentNavigator<ItemT> child = makeTreeNavigator(name);
86       Utilities.invokeLater(new Runnable JavaDoc() {
87         public void run() {
88 // synchronized (child.getModelLock()) { // dropped because of cost; each atomic action is still synchronized
89
for(Pair<String JavaDoc, INavigatorItemFilter<ItemT>> p: l) { child.addTopLevelGroup(p.first(), p.second()); }
90             migrateNavigatorItems(child, parent);
91             migrateListeners(child, parent);
92           }
93 // }
94
});
95       return child;
96     }
97     
98     /** Migrates all the navigator items from parent to child
99      * @param child the navigator to migrate to
100      * @param parent the navigator to migrate from
101      */

102     // As a first step to weakening the restriction on parent's type, this allows parent to be based on an arbitrary item type, as
103
// long as it extends ItemT.
104
private void migrateNavigatorItems(IDocumentNavigator<ItemT> child, IDocumentNavigator<ItemT> parent) {
105       Enumeration<ItemT> enumerator = parent.getDocuments();
106       while (enumerator.hasMoreElements()) {
107         ItemT navitem = enumerator.nextElement();
108         child.addDocument(navitem);
109       }
110       parent.clear(); // Remove documents from old navigator (parent)
111
}
112     
113     /** Migrates all the listeners from parent to child
114      * @param child the navigator to migrate to
115      * @param parent the navigator to migrate from
116      */

117     // As a first step to weakening the restriction on parent's type, this allows parent to be based on an arbitrary item type, as
118
// long as it extends ItemT.
119
private void migrateListeners(IDocumentNavigator<ItemT> child, IDocumentNavigator<ItemT> parent) {
120       for (INavigationListener<? super ItemT> nl: parent.getNavigatorListeners()) child.addNavigationListener(nl);
121       for (FocusListener JavaDoc fl: parent.getFocusListeners()) child.addFocusListener(fl);
122     }
123 }
124
Popular Tags