001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.docnavigation;
038:
039: import edu.rice.cs.plt.tuple.Pair;
040: import edu.rice.cs.util.swing.Utilities;
041:
042: import java.util.List;
043: import java.util.*;
044: import java.awt.event.FocusListener;
045:
046: public class AWTContainerNavigatorFactory<ItemT extends INavigatorItem>
047: implements IDocumentNavigatorFactory<ItemT> {
048:
049: public AWTContainerNavigatorFactory() {
050: }
051:
052: /** Creates a new List Navigator
053: * @return a list navigator
054: */
055: public IDocumentNavigator<ItemT> makeListNavigator() {
056: return new JListSortNavigator<ItemT>();
057: }
058:
059: /** Returns a new tree Navigator with the specified root
060: * @param path the path name of the root node
061: * @return a tree navigator
062: */
063: public IDocumentNavigator<ItemT> makeTreeNavigator(String path) {
064: return new JTreeSortNavigator<ItemT>(path);
065: }
066:
067: /** Creates a list navigator and migrates the navigator items from parent to the new navigator. The migration
068: * is asynchronous but it completes before any subsequent computation in the event thread.
069: * @param parent the navigator to migrate from
070: * @return the new list navigator
071: */
072: public IDocumentNavigator<ItemT> makeListNavigator(
073: final IDocumentNavigator<ItemT> parent) {
074: final IDocumentNavigator<ItemT> child = makeListNavigator();
075: Utilities.invokeLater(new Runnable() {
076: public void run() {
077: // synchronized (child.getModelLock()) { // dropped because of cost; each atomic action is still synchronized
078: migrateNavigatorItems(child, parent);
079: migrateListeners(child, parent);
080: }
081: // }
082: });
083: return child;
084: }
085:
086: /** Creates a tree navigator and migrates the navigator items from the parent to the new navigator. The migration
087: * is asynchronous but it completes before any subsequent computation in the event thread.
088: * @param name the name of the root node
089: * @param parent the navigator to migrate from
090: * @return the new tree navigator
091: */
092: public IDocumentNavigator<ItemT> makeTreeNavigator(String name,
093: final IDocumentNavigator<ItemT> parent,
094: final List<Pair<String, INavigatorItemFilter<ItemT>>> l) {
095:
096: final IDocumentNavigator<ItemT> child = makeTreeNavigator(name);
097: Utilities.invokeLater(new Runnable() {
098: public void run() {
099: // synchronized (child.getModelLock()) { // dropped because of cost; each atomic action is still synchronized
100: for (Pair<String, INavigatorItemFilter<ItemT>> p : l) {
101: child.addTopLevelGroup(p.first(), p.second());
102: }
103: migrateNavigatorItems(child, parent);
104: migrateListeners(child, parent);
105: }
106: // }
107: });
108: return child;
109: }
110:
111: /** Migrates all the navigator items from parent to child
112: * @param child the navigator to migrate to
113: * @param parent the navigator to migrate from
114: */
115: // As a first step to weakening the restriction on parent's type, this allows parent to be based on an arbitrary item type, as
116: // long as it extends ItemT.
117: private void migrateNavigatorItems(IDocumentNavigator<ItemT> child,
118: IDocumentNavigator<ItemT> parent) {
119: Enumeration<ItemT> enumerator = parent.getDocuments();
120: while (enumerator.hasMoreElements()) {
121: ItemT navitem = enumerator.nextElement();
122: child.addDocument(navitem);
123: }
124: parent.clear(); // Remove documents from old navigator (parent)
125: }
126:
127: /** Migrates all the listeners from parent to child
128: * @param child the navigator to migrate to
129: * @param parent the navigator to migrate from
130: */
131: // As a first step to weakening the restriction on parent's type, this allows parent to be based on an arbitrary item type, as
132: // long as it extends ItemT.
133: private void migrateListeners(IDocumentNavigator<ItemT> child,
134: IDocumentNavigator<ItemT> parent) {
135: for (INavigationListener<? super ItemT> nl : parent
136: .getNavigatorListeners())
137: child.addNavigationListener(nl);
138: for (FocusListener fl : parent.getFocusListeners())
139: child.addFocusListener(fl);
140: }
141: }
|