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.midp.propertyeditors.api.resource.element;
042:
043: import org.netbeans.modules.vmd.api.model.DesignComponent;
044: import org.netbeans.modules.vmd.api.model.PropertyValue;
045: import org.netbeans.modules.vmd.api.model.TypeID;
046: import org.netbeans.modules.vmd.midp.components.general.ClassCode;
047: import org.netbeans.modules.vmd.midp.components.resources.ResourceCD;
048: import javax.swing.*;
049: import java.util.*;
050: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorMessageAwareness;
051:
052: /**
053: *
054: * @author Anton Chechel
055: */
056: public abstract class PropertyEditorResourceElement extends JPanel {
057:
058: private List<PropertyEditorResourceElementListener> listeners = new ArrayList<PropertyEditorResourceElementListener>(
059: 1);
060:
061: // component to represent view
062: public abstract JComponent getJComponent();
063:
064: // component to represent model
065: // can be null
066: public abstract void setDesignComponentWrapper(
067: DesignComponentWrapper component);
068:
069: // TypeID
070: public abstract TypeID getTypeID();
071:
072: // List of Property Values
073: public abstract List<String> getPropertyValueNames();
074:
075: // support for post setValue() action
076: public boolean isPostSetValueSupported(DesignComponent component) {
077: return false;
078: }
079:
080: // support for post setValue() action
081: public void postSetValue(DesignComponent parentComponent,
082: DesignComponent childComponent) {
083: }
084:
085: // messageAwareness allows to show warning/error message in custom PropertyEditor
086: public void setPropertyEditorMessageAwareness(
087: PropertyEditorMessageAwareness messageAwareness) {
088: }
089:
090: // icon path
091: public String getIconPath() {
092: return ResourceCD.ICON_PATH;
093: }
094:
095: public String getResourceNameSuggestion() {
096: return ClassCode.getSuggestedMainName(getTypeID());
097: }
098:
099: public void addPropertyEditorResourceElementListener(
100: PropertyEditorResourceElementListener listener) {
101: listeners.add(listener);
102: }
103:
104: public void removePropertyEditorResourceElementListener(
105: PropertyEditorResourceElementListener listener) {
106: listeners.remove(listener);
107: }
108:
109: protected void fireElementChanged(long componentID,
110: String propertyName, PropertyValue propertyValue) {
111: for (PropertyEditorResourceElementListener listener : listeners) {
112: listener
113: .elementChanged(new PropertyEditorResourceElementEvent(
114: componentID, propertyName, propertyValue));
115: }
116: }
117:
118: public static boolean isPropertyValueAUserCodeType(
119: PropertyValue propertyValue) {
120: return propertyValue != null
121: && propertyValue.getKind() == PropertyValue.Kind.USERCODE;
122: }
123:
124: public static class DesignComponentWrapper {
125:
126: private DesignComponent component;
127: private boolean isDeleted;
128: private long componentID;
129: private TypeID typeID;
130: private Map<String, PropertyValue> changesMap;
131:
132: public DesignComponentWrapper(DesignComponent component) {
133: this (component.getComponentID(), component.getType());
134: this .component = component;
135: }
136:
137: public DesignComponentWrapper(long componentID, TypeID typeID) {
138: this .componentID = componentID;
139: this .typeID = typeID;
140: changesMap = new HashMap<String, PropertyValue>();
141: }
142:
143: public DesignComponent getComponent() {
144: return component;
145: }
146:
147: public void deleteComponent() {
148: component = null;
149: isDeleted = true;
150: }
151:
152: public long getComponentID() {
153: return componentID;
154: }
155:
156: public TypeID getTypeID() {
157: return typeID;
158: }
159:
160: public void setChangeRecord(String propertyName,
161: PropertyValue value) {
162: changesMap.put(propertyName, value);
163: }
164:
165: public Map<String, PropertyValue> getChanges() {
166: return Collections.unmodifiableMap(changesMap);
167: }
168:
169: public boolean hasChanges() {
170: return changesMap.size() > 0 || component == null;
171: }
172:
173: public boolean isDeleted() {
174: return isDeleted;
175: }
176: }
177:
178: public static class StringComparator implements Comparator {
179: public static final StringComparator instance = new StringComparator();
180:
181: private StringComparator() {
182: }
183:
184: @SuppressWarnings("unchecked")
185: // NOI18N
186: public int compare(Object o1, Object o2) {
187: if (o1 instanceof String && o2 instanceof String) {
188: return ((String) o1).toLowerCase().compareTo(
189: ((String) o2).toLowerCase());
190: }
191: return ((Comparable) o1).compareTo((Comparable) o2);
192: }
193: }
194: }
|