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.components.elements;
042:
043: import org.netbeans.modules.vmd.api.model.Debug;
044: import org.netbeans.modules.vmd.api.model.DesignComponent;
045: import org.netbeans.modules.vmd.api.model.DesignEventFilter;
046: import org.netbeans.modules.vmd.api.model.PropertyValue;
047: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
048: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter.IconType;
049: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter.NameType;
050: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter.Resolver;
051: import org.netbeans.modules.vmd.midp.components.MidpTypes;
052: import org.netbeans.modules.vmd.midp.components.MidpValueSupport;
053: import org.netbeans.modules.vmd.midp.components.sources.ListElementEventSourceCD;
054: import org.openide.util.NbBundle;
055: import org.openide.util.Utilities;
056:
057: import java.awt.*;
058:
059: /**
060: * @author David Kaspar
061: */
062: public final class ElementSupport {
063:
064: private static final Resolver LIST_ELEMENT_RESOLVER = new ElementResolver(
065: ListElementEventSourceCD.PROP_STRING,
066: ChoiceElementCD.ICON_PATH);
067: private static final Resolver CHOICE_ELEMENT_RESOLVER = new ElementResolver(
068: ChoiceElementCD.PROP_STRING, ChoiceElementCD.ICON_PATH);
069:
070: public static Resolver createListElementInfoResolver() {
071: return LIST_ELEMENT_RESOLVER;
072: }
073:
074: static Resolver createChoiceElementInfoResolver() {
075: return CHOICE_ELEMENT_RESOLVER;
076: }
077:
078: private static class ElementResolver implements
079: InfoPresenter.Resolver {
080:
081: private String propertyName;
082: private Image icon;
083:
084: private ElementResolver(String propertyName, String iconResource) {
085: this .propertyName = propertyName;
086: this .icon = Utilities.loadImage(iconResource);
087: }
088:
089: public DesignEventFilter getEventFilter(
090: DesignComponent component) {
091: return new DesignEventFilter().addComponentFilter(
092: component, false);
093: }
094:
095: public String getDisplayName(DesignComponent component,
096: NameType nameType) {
097: switch (nameType) {
098: case PRIMARY:
099: return resolveName(component);
100: case SECONDARY:
101: return NbBundle.getMessage(ElementSupport.class,
102: "TYPE_Element"); // NOI18N
103: case TERTIARY:
104: return null;
105: default:
106: throw Debug.illegalState();
107: }
108: }
109:
110: private String resolveName(DesignComponent component) {
111: return MidpValueSupport.getHumanReadableString(component
112: .readProperty(propertyName));
113: }
114:
115: public boolean isEditable(DesignComponent component) {
116: return true;
117: }
118:
119: public String getEditableName(DesignComponent component) {
120: PropertyValue value = component.readProperty(propertyName);
121: if (value.getKind() == PropertyValue.Kind.VALUE)
122: return MidpTypes.getString(value);
123: else
124: return ""; // NOI18N
125: }
126:
127: public void setEditableName(DesignComponent component,
128: String enteredName) {
129: assert enteredName != null;
130: component.writeProperty(propertyName, MidpTypes
131: .createStringValue(enteredName));
132: }
133:
134: public Image getIcon(DesignComponent component,
135: IconType iconType) {
136: return iconType == IconType.COLOR_16x16 ? icon : null;
137: }
138:
139: }
140:
141: }
|