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:
042: package org.netbeans.modules.vmd.api.inspector.common;
043:
044: import java.util.ArrayList;
045: import java.util.Arrays;
046: import java.util.Collection;
047: import java.util.Collections;
048: import java.util.Comparator;
049: import java.util.List;
050: import org.netbeans.modules.vmd.api.inspector.InspectorFolder;
051: import org.netbeans.modules.vmd.api.inspector.InspectorOrderingController;
052: import org.netbeans.modules.vmd.api.model.DesignComponent;
053: import org.netbeans.modules.vmd.api.model.DesignDocument;
054: import org.netbeans.modules.vmd.api.model.TypeID;
055:
056: /**
057: *
058: * @author Karol Harezlak
059: */
060:
061: /**
062: * Implementation of InspectorOrderingController. This class sorts InspectorFolders in the Mobility Visual Designer Navigator
063: * according of their natural order. Parameter used to sort InspectorFolders is display name.
064: */
065: public class DefaultOrderingController implements
066: InspectorOrderingController {
067:
068: private static final Comparator<InspectorFolder> DEFAULT_STRING_COMPARATOR = new Comparator<InspectorFolder>() {
069:
070: public int compare(InspectorFolder fd1, InspectorFolder fd2) {
071: return fd1.getDisplayName().compareTo(fd2.getDisplayName());
072: }
073: };
074: private Integer order;
075: private List<TypeID> types;
076:
077: /**
078: * Creates DefaultOrderingController.
079: * @param order Insteger number of the ordering controller, higher number means higher position in the tree branch
080: * @param supportedTypeID array of TypeIDs accepted by this ordering controller
081: */
082: public DefaultOrderingController(Integer order, TypeID... types) {
083: if (types == null) {
084: throw new IllegalArgumentException(
085: "types argument cant be null"); //NOI18N
086: }
087: this .order = order;
088: this .types = Arrays.asList(types);
089: }
090:
091: /**
092: * Returns list of sorted InspectorFolders in the natural order based on the InspectorFolder display name.
093: * @param component current DesignComponent
094: * @parma folders InspectorFolders to sort
095: * @raturn ordered List of InspectorFolders
096: */
097: public List<InspectorFolder> getOrdered(DesignComponent component,
098: Collection<InspectorFolder> folders) {
099: List<InspectorFolder> sortedList = new ArrayList<InspectorFolder>(
100: folders);
101: Collections.sort(sortedList, DEFAULT_STRING_COMPARATOR);
102: return sortedList;
103: }
104:
105: /**
106: * Returns order Integer number for list of folders sorted by method getOrdered.
107: * Higher Integer number means that sorted list of folders has higher priority when
108: * it's necessary to order not only folders but also lists (List<InspectorFolder>) of sorted folders
109: * in the same tree branch. For example if in the same tree branch there is two or more InspectorOrderingController
110: * then ordered lists need to be ordered by Integer number returns by this method.
111: *
112: * @return Integer number, when null then sorted list has lowest possible priority
113: */
114: public Integer getOrder() {
115: return order;
116: }
117:
118: /**
119: * Checks if given TypeId is supported by this ordering controller.
120: * @param document current DesignDocument
121: * @param typeID typeID to check
122: * @return Boolen.TRUE when TypyID is supported, Boolean.FALSE when TypeID is
123: * not supported
124: */
125: public boolean isTypeIDSupported(DesignDocument document,
126: TypeID typeID) {
127: if (types.contains(typeID)) {
128: return true;
129: }
130: return false;
131: }
132: }
|