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.handlers;
042:
043: import org.netbeans.modules.vmd.api.codegen.CodeMultiGuardedLevelPresenter;
044: import org.netbeans.modules.vmd.api.codegen.CodeReferencePresenter;
045: import org.netbeans.modules.vmd.api.codegen.MultiGuardedSection;
046: import org.netbeans.modules.vmd.api.model.*;
047: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
048: import org.netbeans.modules.vmd.api.model.presenters.actions.DeleteDependencyPresenter;
049: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
050: import org.netbeans.modules.vmd.midp.components.MidpVersionable;
051: import org.netbeans.modules.vmd.midp.components.displayables.AlertCD;
052: import org.netbeans.modules.vmd.midp.components.displayables.DisplayableCD;
053: import org.netbeans.modules.vmd.midp.flow.FlowSwitchDisplayableEventHandlerEdgePresenter;
054: import org.netbeans.modules.vmd.midp.flow.FlowSwitchDisplayableEventHandlerForwardEdgePresenter;
055: import org.netbeans.modules.vmd.midp.flow.FlowSwitchDisplayableEventHandlerPinPresenter;
056: import org.netbeans.modules.vmd.midp.general.AbstractEventHandlerCreatorPresenter;
057: import org.netbeans.modules.vmd.midp.codegen.SwitchDisplayableParameterPresenter;
058:
059: import java.util.Arrays;
060: import java.util.List;
061:
062: /**
063: * @author David Kaspar
064: */
065:
066: public final class SwitchDisplayableEventHandlerCD extends
067: ComponentDescriptor {
068:
069: public static final TypeID TYPEID = new TypeID(
070: TypeID.Kind.COMPONENT, "#SwitchDisplayableEventHandler"); // NOI18N
071:
072: public static final String PROP_DISPLAYABLE = "displayable"; // NOI18N
073: public static final String PROP_ALERT = "alert"; // NOI18N
074:
075: public TypeDescriptor getTypeDescriptor() {
076: return new TypeDescriptor(EventHandlerCD.TYPEID, TYPEID, true,
077: false);
078: }
079:
080: public VersionDescriptor getVersionDescriptor() {
081: return MidpVersionDescriptor.MIDP;
082: }
083:
084: public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
085: return Arrays.asList(new PropertyDescriptor(PROP_DISPLAYABLE,
086: DisplayableCD.TYPEID, PropertyValue.createNull(),
087: false, false, MidpVersionable.MIDP),
088: new PropertyDescriptor(PROP_ALERT, AlertCD.TYPEID,
089: PropertyValue.createNull(), true, false,
090: MidpVersionable.MIDP));
091: }
092:
093: protected List<? extends Presenter> createPresenters() {
094: return Arrays
095: .asList(
096: // info
097: InfoPresenter
098: .create(EventHandlerSupport
099: .getSwitchDisplayableEventHandlerInfoResolver()),
100: // flow
101: new FlowSwitchDisplayableEventHandlerPinPresenter(),
102: new FlowSwitchDisplayableEventHandlerEdgePresenter(),
103: new FlowSwitchDisplayableEventHandlerForwardEdgePresenter(),
104: // code
105: new CodeMultiGuardedLevelPresenter() {
106: protected void generateMultiGuardedSectionCode(
107: MultiGuardedSection section) {
108: SwitchDisplayableParameterPresenter presenter;
109:
110: DesignComponent alertComponent = getComponent()
111: .readProperty(PROP_ALERT)
112: .getComponent();
113: presenter = alertComponent != null ? alertComponent
114: .getPresenter(SwitchDisplayableParameterPresenter.class)
115: : null;
116: String alert = presenter != null ? presenter
117: .generateSwitchDisplayableParameterCode()
118: : CodeReferencePresenter
119: .generateAccessCode(alertComponent);
120:
121: DesignComponent displayableComponent = getComponent()
122: .readProperty(PROP_DISPLAYABLE)
123: .getComponent();
124: presenter = displayableComponent != null ? displayableComponent
125: .getPresenter(SwitchDisplayableParameterPresenter.class)
126: : null;
127: String displayable = presenter != null ? presenter
128: .generateSwitchDisplayableParameterCode()
129: : CodeReferencePresenter
130: .generateAccessCode(displayableComponent);
131:
132: section.getWriter().write(
133: "switchDisplayable (" + alert
134: + ", " + displayable
135: + ");\n"); // NOI18N
136: }
137: },
138: // delete
139: DeleteDependencyPresenter
140: .createDependentOnPropertyPresenter(PROP_DISPLAYABLE),
141: DeleteDependencyPresenter
142: .createNullableComponentReferencePresenter(PROP_ALERT));
143: }
144:
145: public static AbstractEventHandlerCreatorPresenter createSwitchDisplayableEventHandlerCreatorPresenter() {
146: return new AbstractEventHandlerCreatorPresenter() {
147: public DesignComponent createReuseEventHandler(
148: DesignComponent eventSource,
149: DesignComponent currentEventHandler,
150: DesignComponent targetComponent) {
151: if (currentEventHandler == null
152: || !getComponent()
153: .getDocument()
154: .getDescriptorRegistry()
155: .isInHierarchy(
156: SwitchDisplayableEventHandlerCD.TYPEID,
157: currentEventHandler.getType()))
158: currentEventHandler = getComponent()
159: .getDocument()
160: .createComponent(
161: SwitchDisplayableEventHandlerCD.TYPEID);
162: currentEventHandler.writeProperty(PROP_ALERT,
163: PropertyValue.createNull());
164: currentEventHandler
165: .writeProperty(
166: PROP_DISPLAYABLE,
167: PropertyValue
168: .createComponentReference(targetComponent));
169: return currentEventHandler;
170: }
171: };
172: }
173:
174: public static AbstractEventHandlerCreatorPresenter createSwitchAlertEventHandlerCreatorPresenter() {
175: return new AbstractEventHandlerCreatorPresenter() {
176: public DesignComponent createReuseEventHandler(
177: DesignComponent eventSource,
178: DesignComponent currentEventHandler,
179: DesignComponent targetComponent) {
180: DesignDocument document = getComponent().getDocument();
181: DescriptorRegistry descriptorRegistry = document
182: .getDescriptorRegistry();
183: if (currentEventHandler == null
184: || !descriptorRegistry.isInHierarchy(
185: SwitchDisplayableEventHandlerCD.TYPEID,
186: currentEventHandler.getType()))
187: currentEventHandler = document
188: .createComponent(SwitchDisplayableEventHandlerCD.TYPEID);
189:
190: DesignComponent possibleSourceDisplayable = eventSource
191: .getParentComponent();
192: if (possibleSourceDisplayable != null
193: && descriptorRegistry.isInHierarchy(
194: DisplayableCD.TYPEID,
195: possibleSourceDisplayable.getType())) {
196: currentEventHandler
197: .writeProperty(
198: PROP_ALERT,
199: PropertyValue
200: .createComponentReference(targetComponent));
201: if (currentEventHandler.readProperty(
202: PROP_DISPLAYABLE).getComponent() == null)
203: currentEventHandler
204: .writeProperty(
205: PROP_DISPLAYABLE,
206: PropertyValue
207: .createComponentReference(possibleSourceDisplayable));
208: } else {
209: currentEventHandler.writeProperty(PROP_ALERT,
210: PropertyValue.createNull());
211: currentEventHandler
212: .writeProperty(
213: PROP_DISPLAYABLE,
214: PropertyValue
215: .createComponentReference(targetComponent));
216: }
217:
218: return currentEventHandler;
219: }
220: };
221: }
222:
223: }
|