001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.api.model.common;
042:
043: import org.netbeans.modules.vmd.api.model.*;
044: import org.netbeans.modules.vmd.api.model.presenters.IdentifiablePresenter;
045: import org.netbeans.modules.vmd.api.model.presenters.OrderablePresenter;
046:
047: import java.util.*;
048:
049: /**
050: * @author David Kaspar
051: */
052: public class DocumentSupport {
053:
054: /**
055: * Gathers all components in main tree of components in a specific document.
056: * It returns a list of components that are or inherits a specific type-id using DescriptorRegistry.isInHierarchy method.
057: * @param document the document
058: * @param typeID the required typeid of components
059: * @return the filtered list of components
060: */
061: public static List<DesignComponent> gatherAllComponentsOfTypeID(
062: DesignDocument document, TypeID typeID) {
063: ArrayList<DesignComponent> list = new ArrayList<DesignComponent>();
064: gatherAllComponentsOfTypeID(list, typeID, document
065: .getRootComponent());
066: return list;
067: }
068:
069: /**
070: * Gathers all components in a document that are containing presenter of presenterClass.
071: * @param document the document
072: * @param presenterClass the presenterClass
073: * @return the list of components
074: */
075: public static <T extends Presenter> List<DesignComponent> gatherAllComponentsContainingPresenterClass(
076: DesignDocument document, Class<T> presenterClass) {
077: ArrayList<DesignComponent> list = new ArrayList<DesignComponent>();
078: DesignComponent rootComponent = document.getRootComponent();
079: if (rootComponent != null)
080: gatherAllComponentsContainingPresenterClass(list,
081: rootComponent, presenterClass);
082: return list;
083: }
084:
085: private static <T extends Presenter> void gatherAllComponentsContainingPresenterClass(
086: ArrayList<DesignComponent> list, DesignComponent component,
087: Class<T> presenterClass) {
088: if (component.getPresenter(presenterClass) != null)
089: list.add(component);
090: for (DesignComponent child : component.getComponents())
091: gatherAllComponentsContainingPresenterClass(list, child,
092: presenterClass);
093: }
094:
095: /**
096: * Gathers all components under specified component.
097: * It returns a list of components that are or inherits a specific type-id using DescriptorRegistry.isInHierarchy method.
098: * @param component the component underneath which it searches
099: * @param typeID the required typeid of components
100: * @return the filtered list of components
101: */
102: public static List<DesignComponent> gatherAllComponentsOfTypeID(
103: DesignComponent component, TypeID typeID) {
104: ArrayList<DesignComponent> list = new ArrayList<DesignComponent>();
105: gatherAllComponentsOfTypeID(list, typeID, component);
106: return list;
107: }
108:
109: private static void gatherAllComponentsOfTypeID(
110: Collection<? super DesignComponent> list, TypeID typeID,
111: DesignComponent component) {
112: if (component.getDocument().getDescriptorRegistry()
113: .isInHierarchy(typeID, component.getType()))
114: list.add(component);
115: for (DesignComponent child : component.getComponents())
116: gatherAllComponentsOfTypeID(list, typeID, child);
117: }
118:
119: /**
120: * Takes a specified list of presenters and removed those presenters which are compatible with specified presenter class.
121: * @param presenters the list of presenters
122: * @param presenterClass the presenter class
123: */
124: public static void removePresentersOfClass(
125: ArrayList<? super Presenter> presenters,
126: Class presenterClass) {
127: for (Iterator<? super Presenter> iterator = presenters
128: .iterator(); iterator.hasNext();) {
129: Object object = iterator.next();
130: if (presenterClass.isInstance(object))
131: iterator.remove();
132: }
133: }
134:
135: /**
136: * Takes a specified list of presenters and removed those presenters which are compatible with specified presenter id.
137: * @param presenters the list of presenters
138: * @param presenterID the presenter id
139: */
140: public static void removePresentersOfPresenterID(
141: ArrayList<Presenter> presenters, String presenterID) {
142: if (presenterID == null)
143: return;
144: for (Iterator<Presenter> iterator = presenters.iterator(); iterator
145: .hasNext();) {
146: Presenter presenter = iterator.next();
147: if (presenter instanceof IdentifiablePresenter)
148: if (presenterID
149: .equals(((IdentifiablePresenter) presenter)
150: .getPresenterID()))
151: iterator.remove();
152: }
153: }
154:
155: /**
156: * Sorts a list of presenters by their order defined by OrderablePresenter.
157: * @param presenters the list of presenters
158: */
159: public static void sortPresentersByOrder(
160: ArrayList<? extends OrderablePresenter> presenters) {
161: Collections.sort(presenters,
162: new Comparator<OrderablePresenter>() {
163: public int compare(OrderablePresenter o1,
164: OrderablePresenter o2) {
165: return o1.getOrder() - o2.getOrder();
166: }
167: });
168: }
169:
170: /**
171: * Returns a list of children components for a specified component. The children components will be only of a specified typeID.
172: * @param component the component which children are used
173: * @param typeID the type id of found children
174: * @return the list of found children components
175: */
176: public static List<DesignComponent> gatherSubComponentsOfType(
177: DesignComponent component, TypeID typeID) {
178: ArrayList<DesignComponent> list = new ArrayList<DesignComponent>();
179: for (DesignComponent child : component.getComponents()) {
180: if (typeID.equals(child.getType()))
181: list.add(child);
182: }
183: return list;
184: }
185:
186: /**
187: * Gathers all presenters of a specified class that are currently attached to a component in the main tree of components in a specific document.
188: * @param document the document
189: * @param clazz the presenter class that is searched for
190: * @return a collection of found presenters
191: */
192: public static <T extends Presenter> Collection<T> gatherAllPresentersOfClass(
193: DesignDocument document, Class<T> clazz) {
194: ArrayList<T> list = new ArrayList<T>();
195: gatherAllPresentersOfClass(list, document.getRootComponent(),
196: clazz);
197: return list;
198: }
199:
200: private static <T extends Presenter> void gatherAllPresentersOfClass(
201: ArrayList<T> list, DesignComponent component, Class<T> clazz) {
202: list.addAll(component.getPresenters(clazz));
203: for (DesignComponent child : component.getComponents())
204: gatherAllPresentersOfClass(list, child, clazz);
205: }
206:
207: /**
208: * Returns a collection of all presenters of a specified class that are in a specified collection.
209: * @param presenters the collec
210: * @param clazz the class of presenters to be found
211: * @return the collection of found presenters
212: */
213: public static <T extends Presenter> Collection<T> filterPresentersForClass(
214: Collection<? extends Presenter> presenters, Class<T> clazz) {
215: ArrayList<T> list = new ArrayList<T>();
216: for (Presenter presenter : presenters)
217: if (clazz.isInstance(presenter))
218: list.add((T) presenter);
219: return list;
220: }
221:
222: /**
223: * Returns component producers for given type.
224: * @param document the document
225: * @param typeID type of searched producers
226: * @return the producers
227: */
228: public static Collection<ComponentProducer> getComponentProducers(
229: DesignDocument document, TypeID typeID) {
230: Collection<ComponentProducer> producers = new HashSet<ComponentProducer>();
231: DescriptorRegistry registry = document.getDescriptorRegistry();
232: for (ComponentProducer producer : registry
233: .getComponentProducers()) {
234: if (registry.isInHierarchy(typeID, producer
235: .getMainComponentTypeID()))
236: producers.add(producer);
237: }
238: return producers;
239: }
240:
241: /**
242: * Returns a design component producer for given producer id.
243: * @param document the document
244: * @param producerID producer id of searched producer
245: * @return the producer
246: */
247: public static ComponentProducer getComponentProducer(
248: DesignDocument document, String producerID) {
249: for (ComponentProducer producer : document
250: .getDescriptorRegistry().getComponentProducers()) {
251: if (producer.getProducerID().equals(producerID))
252: return producer;
253: }
254: return null;
255: }
256:
257: }
|