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.screen.resource;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.Comparator;
048: import java.util.List;
049: import org.netbeans.modules.vmd.api.model.DesignComponent;
050: import org.netbeans.modules.vmd.api.model.PropertyValue;
051: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
052:
053: /**
054: *
055: * @author Karol Harezlak
056: */
057: public abstract class ScreenResourceOrderingController {
058:
059: private static ArrayOrderingController arrayOrdering;
060: private static DefaultOrderingController defaultOrdering;
061:
062: public static ScreenResourceOrderingController getArrayOrdering(
063: final String propertyName) {
064: if (arrayOrdering == null)
065: arrayOrdering = new ArrayOrderingController(propertyName);
066:
067: return arrayOrdering;
068: }
069:
070: public static ScreenResourceOrderingController getDefaultOrdering() {
071: if (defaultOrdering == null)
072: defaultOrdering = new DefaultOrderingController();
073:
074: return defaultOrdering;
075: }
076:
077: public abstract List<ScreenResourceItemPresenter> getOrdered(
078: DesignComponent component,
079: Collection<ScreenResourceItemPresenter> items);
080:
081: private static class ArrayOrderingController extends
082: ScreenResourceOrderingController {
083:
084: private String propertyName;
085:
086: private ArrayOrderingController(String propertyName) {
087: this .propertyName = propertyName;
088: }
089:
090: public List<ScreenResourceItemPresenter> getOrdered(
091: DesignComponent component,
092: Collection<ScreenResourceItemPresenter> items) {
093: List<ScreenResourceItemPresenter> list = new ArrayList<ScreenResourceItemPresenter>(
094: items.size());
095:
096: List<PropertyValue> array = new ArrayList<PropertyValue>(
097: component.readProperty(propertyName).getArray());
098: for (PropertyValue value : array) {
099: DesignComponent commandEventSource = value
100: .getComponent();
101: for (ScreenResourceItemPresenter descriptor : items)
102: if (descriptor.getRelatedComponent() == commandEventSource) {
103: list.add(descriptor);
104: break;
105: }
106: }
107:
108: return list;
109: }
110: }
111:
112: private static class DefaultOrderingController extends
113: ScreenResourceOrderingController {
114: public List<ScreenResourceItemPresenter> getOrdered(
115: DesignComponent component,
116: Collection<ScreenResourceItemPresenter> items) {
117: List<ScreenResourceItemPresenter> orderedList = new ArrayList<ScreenResourceItemPresenter>(
118: items);
119: Collections.sort(orderedList,
120: new Comparator<ScreenResourceItemPresenter>() {
121: public int compare(
122: ScreenResourceItemPresenter item1,
123: ScreenResourceItemPresenter item2) {
124: String name1 = InfoPresenter
125: .getDisplayName(item1
126: .getRelatedComponent());
127: String name2 = InfoPresenter
128: .getDisplayName(item2
129: .getRelatedComponent());
130: if (name1 == null || name2 == null)
131: throw new NullPointerException();
132: return name1.compareTo(name2);
133: }
134: });
135:
136: return orderedList;
137: }
138: }
139: }
|