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.sources;
042:
043: import java.awt.Image;
044: import java.util.List;
045:
046: import org.netbeans.modules.vmd.api.model.Debug;
047: import org.netbeans.modules.vmd.api.model.DesignComponent;
048: import org.netbeans.modules.vmd.api.model.DesignEventFilter;
049: import org.netbeans.modules.vmd.api.model.Presenter;
050: import org.netbeans.modules.vmd.api.model.PropertyValue;
051: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
052: import org.netbeans.modules.vmd.api.model.presenters.actions.ActionsPresenter;
053: import org.netbeans.modules.vmd.api.model.presenters.actions.ActionsSupport;
054: import org.netbeans.modules.vmd.midp.actions.MidpActionsSupport;
055: import org.netbeans.modules.vmd.midp.codegen.InstanceNameResolver;
056: import org.netbeans.modules.vmd.midp.components.MidpTypes;
057: import org.netbeans.modules.vmd.midp.components.commands.CommandCD;
058: import org.netbeans.modules.vmd.midp.components.displayables.DisplayableCD;
059: import org.netbeans.modules.vmd.midp.components.general.ClassCD;
060: import org.openide.actions.PropertiesAction;
061: import org.openide.util.NbBundle;
062: import org.openide.util.Utilities;
063:
064: /**
065: * @author David Kaspar
066: */
067: public final class EventSourceSupport {
068:
069: private static final InfoPresenter.Resolver COMMAND_EVENT_SOURCE_RESOLVER = new EventSourceResolver(
070: CommandEventSourceCD.PROP_COMMAND, true);
071: private static final InfoPresenter.Resolver ITEM_COMMAND_EVENT_SOURCE_RESOLVER = new EventSourceResolver(
072: ItemCommandEventSourceCD.PROP_COMMAND, true);
073: private static final String PROP_COMMAND = "command"; //NOI18N
074:
075: static InfoPresenter.Resolver createCommandEventSourceInfoResolver() {
076: return COMMAND_EVENT_SOURCE_RESOLVER;
077: }
078:
079: static InfoPresenter.Resolver createItemCommandEventSourceInfoResolver() {
080: return ITEM_COMMAND_EVENT_SOURCE_RESOLVER;
081: }
082:
083: static void addActionsPresentres(List<Presenter> presenters) {
084: for (Presenter presenter : presenters
085: .toArray(new Presenter[presenters.size()])) {
086: if (presenter instanceof ActionsPresenter)
087: presenters.remove(presenter);
088: if (presenter instanceof ActionsPresenter)
089: presenters.remove(presenter);
090: }
091: MidpActionsSupport.addCommonActionsPresenters(presenters, true,
092: true, true, true, false);
093: MidpActionsSupport.addMoveActionPresenter(presenters,
094: DisplayableCD.PROP_COMMANDS);
095: presenters.addAll(ActionsSupport.createByReference(
096: PROP_COMMAND, PropertiesAction.class)); //NOI18N
097: }
098:
099: private static class EventSourceResolver implements
100: InfoPresenter.Resolver {
101:
102: private String propertyName;
103: private boolean editable;
104:
105: private EventSourceResolver(String propertyName,
106: boolean editable) {
107: this .propertyName = propertyName;
108: this .editable = editable;
109: }
110:
111: public DesignEventFilter getEventFilter(
112: DesignComponent component) {
113: return new DesignEventFilter().addDescentFilter(component,
114: propertyName);
115: }
116:
117: public String getDisplayName(DesignComponent component,
118: InfoPresenter.NameType nameType) {
119: switch (nameType) {
120: case PRIMARY:
121: return resolveName(component);
122: case SECONDARY:
123: return NbBundle.getMessage(EventSourceSupport.class,
124: "TYPE_Command"); // NOI18N
125: case TERTIARY:
126: StringBuffer nameWithParent = new StringBuffer();
127: nameWithParent.append("<HTML>"); //NOI18N
128: nameWithParent.append(resolveName(component));
129: nameWithParent.append(" <font color=\"#808080\">["); // NOI18N
130: nameWithParent.append(component.getParentComponent()
131: .getPresenter(InfoPresenter.class)
132: .getEditableName());
133: nameWithParent.append("]"); // NOI18N
134: return nameWithParent.toString();
135: default:
136: throw Debug.illegalState();
137: }
138: }
139:
140: private String resolveName(DesignComponent component) {
141: component = component.readProperty(propertyName)
142: .getComponent();
143: if (component == null)
144: return null;
145:
146: PropertyValue value = component
147: .readProperty(ClassCD.PROP_INSTANCE_NAME);
148: if (value.getKind() == PropertyValue.Kind.VALUE
149: && MidpTypes.TYPEID_JAVA_LANG_STRING.equals(value
150: .getType()))
151: return (String) value.getPrimitiveValue();
152: throw Debug.error("Invalid instance name", value,
153: "for component", component); // NOI18N
154: }
155:
156: public boolean isEditable(DesignComponent component) {
157: return editable;
158: }
159:
160: public String getEditableName(DesignComponent component) {
161: DesignComponent refComponent = component.readProperty(
162: PROP_COMMAND).getComponent();
163: String name = (String) refComponent.readProperty(
164: ClassCD.PROP_INSTANCE_NAME).getPrimitiveValue();
165:
166: return name;
167: }
168:
169: public void setEditableName(DesignComponent component,
170: String enteredName) {
171: DesignComponent refComponent = component.readProperty(
172: PROP_COMMAND).getComponent();
173: PropertyValue newName = InstanceNameResolver
174: .createFromSuggested(refComponent, enteredName);
175: refComponent.writeProperty(ClassCD.PROP_INSTANCE_NAME,
176: newName);
177: }
178:
179: public Image getIcon(DesignComponent component,
180: InfoPresenter.IconType iconType) {
181: return Utilities.loadImage(CommandCD.ICON_PATH);
182: }
183:
184: }
185:
186: }
|