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.midp.components.items;
043:
044: import org.netbeans.modules.vmd.api.codegen.CodeSetterPresenter;
045: import org.netbeans.modules.vmd.api.inspector.InspectorFolderComponentPresenter;
046: import org.netbeans.modules.vmd.api.inspector.InspectorOrderingController;
047: import org.netbeans.modules.vmd.api.inspector.InspectorPositionPresenter;
048: import org.netbeans.modules.vmd.api.inspector.common.ArrayPropertyOrderingController;
049: import org.netbeans.modules.vmd.api.model.*;
050: import org.netbeans.modules.vmd.api.model.common.AcceptPresenter;
051: import org.netbeans.modules.vmd.api.model.common.AcceptSuggestion;
052: import org.netbeans.modules.vmd.api.model.presenters.actions.ActionsPresenter;
053: import org.netbeans.modules.vmd.api.model.presenters.actions.DeleteDependencyPresenter;
054: import org.netbeans.modules.vmd.api.model.presenters.actions.DeletePresenter;
055: import org.netbeans.modules.vmd.api.model.support.ArraySupport;
056: import org.netbeans.modules.vmd.api.properties.DefaultPropertiesPresenter;
057: import org.netbeans.modules.vmd.api.screen.actions.EditDependencyPresenter;
058: import org.netbeans.modules.vmd.midp.actions.MidpActionsSupport;
059: import org.netbeans.modules.vmd.midp.codegen.MidpParameter;
060: import org.netbeans.modules.vmd.midp.codegen.MidpSetter;
061: import org.netbeans.modules.vmd.midp.components.MidpDocumentSupport;
062: import org.netbeans.modules.vmd.midp.components.MidpTypes;
063: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
064: import org.netbeans.modules.vmd.midp.components.MidpVersionable;
065: import org.netbeans.modules.vmd.midp.components.categories.CommandsCategoryCD;
066: import org.netbeans.modules.vmd.midp.components.commands.CommandCD;
067: import org.netbeans.modules.vmd.midp.components.displayables.FormCD;
068: import org.netbeans.modules.vmd.midp.components.general.ClassCD;
069: import org.netbeans.modules.vmd.midp.components.general.RootCode;
070: import org.netbeans.modules.vmd.midp.components.listeners.ItemCommandListenerCD;
071: import org.netbeans.modules.vmd.midp.components.sources.ItemCommandEventSourceCD;
072: import org.netbeans.modules.vmd.midp.screen.display.ScreenMoveArrayAcceptPresenter;
073: import org.netbeans.modules.vmd.midp.inspector.controllers.ComponentsCategoryPC;
074: import org.netbeans.modules.vmd.midp.inspector.folders.MidpInspectorSupport;
075: import org.netbeans.modules.vmd.midp.propertyeditors.*;
076: import org.netbeans.modules.vmd.midp.screen.DisplayableResourceCategoriesPresenter;
077: import org.netbeans.modules.vmd.midp.screen.display.ItemDisplayPresenter;
078:
079: import java.util.*;
080: import org.openide.util.NbBundle;
081:
082: /**
083: *
084: * @author Karol Harezlak
085: */
086:
087: public class ItemCD extends ComponentDescriptor {
088:
089: public static final TypeID TYPEID = new TypeID(
090: TypeID.Kind.COMPONENT, "javax.microedition.lcdui.Item"); // NOI18N
091:
092: public static final String ICON_PATH = "org/netbeans/modules/vmd/midp/resources/components/item_16.png"; // NOI18N
093: public static final String LARGE_ICON_PATH = "org/netbeans/modules/vmd/midp/resources/components/item_32.png"; // NOI18N
094:
095: public static final int VALUE_PLAIN = 0;
096: public static final int VALUE_HYPERLINK = 1;
097: public static final int VALUE_BUTTON = 2;
098:
099: public static final int VALUE_LAYOUT_DEFAULT = 0;
100: public static final int VALUE_LAYOUT_LEFT = 1;
101: public static final int VALUE_LAYOUT_RIGHT = 2;
102: public static final int VALUE_LAYOUT_CENTER = 3;
103: public static final int VALUE_LAYOUT_TOP = 0x10;
104: public static final int VALUE_LAYOUT_BOTTOM = 0x20;
105: public static final int VALUE_LAYOUT_VCENTER = 0x30;
106: public static final int VALUE_LAYOUT_NEWLINE_BEFORE = 0x100;
107: public static final int VALUE_LAYOUT_NEWLINE_AFTER = 0x200;
108: public static final int VALUE_LAYOUT_SHRINK = 0x400;
109: public static final int VALUE_LAYOUT_VSHRINK = 0x800;
110: public static final int VALUE_LAYOUT_EXPAND = 0x1000;
111: public static final int VALUE_LAYOUT_VEXPAND = 0x2000;
112: public static final int VALUE_LAYOUT_2 = 0x4000;
113:
114: public static final String PROP_LABEL = "label"; // NOI18N
115: public static final String PROP_LAYOUT = "layout"; // NOI18N
116: public static final String PROP_PREFERRED_HEIGHT = "preferredHeight"; // NOI18N
117: public static final String PROP_PREFERRED_WIDTH = "preferredWidth"; // NOI18N
118: public static final String PROP_COMMANDS = "commands"; // NOI18N
119: public static final String PROP_DEFAULT_COMMAND = "defaultCommand"; //NOI18N
120: public static final String PROP_ITEM_COMMAND_LISTENER = "itemCommandListener"; //NOI18N
121:
122: public static final String PROP_APPEARANCE_MODE = "appearanceMode"; // NOI18N
123:
124: public static final String PROP_OLD_ITEM_COMMAND_LISTENER = "itemCommandlistener"; //NOI18N
125:
126: public static final PropertyValue UNLOCKED_VALUE = MidpTypes
127: .createIntegerValue(-1);
128:
129: static {
130: MidpTypes.registerIconResource(TYPEID, ICON_PATH);
131: }
132:
133: public TypeDescriptor getTypeDescriptor() {
134: return new TypeDescriptor(ClassCD.TYPEID, TYPEID, true, true);
135: }
136:
137: public VersionDescriptor getVersionDescriptor() {
138: return MidpVersionDescriptor.MIDP;
139: }
140:
141: @Override
142: public void postInitialize(DesignComponent component) {
143: component.writeProperty(PROP_LABEL, component
144: .readProperty(ClassCD.PROP_INSTANCE_NAME));
145:
146: DesignComponent listener = MidpDocumentSupport
147: .getCommandListener(component.getDocument(),
148: ItemCommandListenerCD.TYPEID);
149: component.writeProperty(PROP_ITEM_COMMAND_LISTENER,
150: PropertyValue.createComponentReference(listener));
151: }
152:
153: public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
154: return Arrays
155: .asList(
156: new PropertyDescriptor(PROP_LABEL,
157: MidpTypes.TYPEID_JAVA_LANG_STRING,
158: PropertyValue.createNull(), true, true,
159: MidpVersionable.MIDP),
160: new PropertyDescriptor(
161: PROP_LAYOUT,
162: MidpTypes.TYPEID_INT,
163: MidpTypes
164: .createIntegerValue(VALUE_LAYOUT_DEFAULT),
165: false, true, MidpVersionable.MIDP),
166: new PropertyDescriptor(PROP_PREFERRED_HEIGHT,
167: MidpTypes.TYPEID_INT, MidpTypes
168: .createIntegerValue(-1), false,
169: true, MidpVersionable.MIDP_2),
170: new PropertyDescriptor(PROP_PREFERRED_WIDTH,
171: MidpTypes.TYPEID_INT, MidpTypes
172: .createIntegerValue(-1), false,
173: true, MidpVersionable.MIDP_2),
174: new PropertyDescriptor(
175: PROP_COMMANDS,
176: ItemCommandEventSourceCD.TYPEID
177: .getArrayType(),
178: PropertyValue
179: .createEmptyArray(ItemCommandEventSourceCD.TYPEID),
180: false, true, MidpVersionable.MIDP_2),
181: new PropertyDescriptor(PROP_DEFAULT_COMMAND,
182: ItemCommandEventSourceCD.TYPEID,
183: PropertyValue.createNull(), true, true,
184: MidpVersionable.MIDP_2),
185: new PropertyDescriptor(
186: PROP_ITEM_COMMAND_LISTENER,
187: ItemCommandListenerCD.TYPEID,
188: PropertyValue.createNull(), true, true,
189: MidpVersionable.MIDP_2));
190: }
191:
192: private static DefaultPropertiesPresenter createPropertiesPresenter() {
193: return new DefaultPropertiesPresenter().addPropertiesCategory(
194: MidpPropertiesCategories.CATEGORY_PROPERTIES)
195: .addProperty(
196: NbBundle.getMessage(ItemCD.class,
197: "DISP_Item_Label"), // NOI18N
198: PropertyEditorString.createInstance(NbBundle
199: .getMessage(ItemCD.class,
200: "LBL_Item_Label")), PROP_LABEL) // NOI18N
201: .addProperty(
202: NbBundle.getMessage(ItemCD.class,
203: "DISP_Item_Default_Command"), // NOI18N
204: PropertyEditorDefaultCommand.createInstance(),
205: PROP_DEFAULT_COMMAND).addProperty(
206: NbBundle.getMessage(ItemCD.class,
207: "DISP_Item_Layout"),
208: PropertyEditorLayout.createInstance(),
209: PROP_LAYOUT) // NOI18N
210: .addProperty(
211: NbBundle.getMessage(ItemCD.class,
212: "DISP_Item_Preferred_Width"), // NOI18N
213: PropertyEditorPreferredSize.createInstance(
214: NbBundle.getMessage(ItemCD.class,
215: "LBL_Item_Preferred_Width"), // NOI18N
216: NbBundle.getMessage(ItemCD.class,
217: "DISP_Item_Preferred_Width")),
218: ItemCD.PROP_PREFERRED_WIDTH) // NOI18N
219: .addProperty(
220: NbBundle.getMessage(ItemCD.class,
221: "DISP_Item_Preferred_Height"), // NOI18N
222: PropertyEditorPreferredSize.createInstance(
223: NbBundle.getMessage(ItemCD.class,
224: "LBL_Item_Preferred_Height"), // NOI18N
225: NbBundle.getMessage(ItemCD.class,
226: "DISP_Item_Preferred_Height")),
227: ItemCD.PROP_PREFERRED_HEIGHT); // NOI18N
228: }
229:
230: private static Presenter createSetterPresenter() {
231: return new CodeSetterPresenter()
232: .addParameters(
233: MidpParameter.create(PROP_LABEL,
234: PROP_PREFERRED_WIDTH,
235: PROP_PREFERRED_HEIGHT))
236: .addParameters(ItemCode.createCommandParameter())
237: .addParameters(
238: ItemCode.createItemCommandListenerParameter())
239: .addParameters(ItemCode.createItemLayoutParameter())
240: .addParameters(ItemCode.createDefaultCommandParameter())
241: .addSetters(
242: MidpSetter.createSetter("addCommand",
243: MidpVersionable.MIDP_2)
244: .setArrayParameter(
245: ItemCode.PARAM_COMMAND)
246: .addParameters(ItemCode.PARAM_COMMAND)) // NOI18N
247: .addSetters(
248: MidpSetter.createSetter(
249: "setItemCommandListener",
250: MidpVersionable.MIDP_2).addParameters(
251: ItemCode.PARAM_ITEM_COMMAND_LISTENER)) // NOI18N
252: .addSetters(
253: MidpSetter.createSetter("setDefaultCommand",
254: MidpVersionable.MIDP_2).addParameters(
255: ItemCode.PARAM_DEFAULT_COMMAND)) // NOI18N
256: .addSetters(
257: MidpSetter.createSetter("setLabel",
258: MidpVersionable.MIDP).addParameters(
259: PROP_LABEL)) // NOI18N
260: .addSetters(
261: MidpSetter.createSetter("setLayout",
262: MidpVersionable.MIDP_2).addParameters(
263: ItemCode.PARAM_LAYOUT)) // NOI18N
264: .addSetters(
265: MidpSetter.createSetter("setPreferredSize",
266: MidpVersionable.MIDP_2).addParameters(
267: PROP_PREFERRED_WIDTH,
268: PROP_PREFERRED_HEIGHT)); // NOI18N
269: }
270:
271: @Override
272: protected void gatherPresenters(ArrayList<Presenter> presenters) {
273: for (Presenter presenter : presenters
274: .toArray(new Presenter[presenters.size()])) {
275: if (presenter instanceof ActionsPresenter)
276: presenters.remove(presenter);
277: }
278: MidpActionsSupport.addCommonActionsPresentersParentEditAction(
279: presenters, true, true, true, true, true);
280: MidpActionsSupport.addNewActionPresenter(presenters,
281: CommandCD.TYPEID);
282: MidpActionsSupport
283: .addUnusedCommandsAddActionForItem(presenters);
284: MidpActionsSupport.addMoveActionPresenter(presenters,
285: FormCD.PROP_ITEMS);
286:
287: super .gatherPresenters(presenters);
288: }
289:
290: protected List<? extends Presenter> createPresenters() {
291: return Arrays.asList(
292: // properties
293: createPropertiesPresenter(),
294: // inspector
295: new InspectorFolderComponentPresenter(true),
296: InspectorPositionPresenter
297: .create(new ComponentsCategoryPC(
298: MidpInspectorSupport.TYPEID_ELEMENTS)),
299: MidpInspectorSupport.createComponentCommandsCategory(
300: createOrderingArrayController(),
301: CommandCD.TYPEID),
302: // accept
303: new AcceptItemCommandPresenter(),
304: new ScreenMoveArrayAcceptPresenter(FormCD.PROP_ITEMS,
305: ItemCD.TYPEID),
306: // action
307: EditDependencyPresenter.createEditablePresenter(),
308: // code
309: createSetterPresenter(),
310: new RootCode.CodeComponentDependencyPresenter() {
311: protected void collectRequiredComponents(
312: Collection<DesignComponent> requiredComponents) {
313: PropertyValue propertyValue = getComponent()
314: .readProperty(PROP_COMMANDS);
315: ArrayList<DesignComponent> itemCommandEventSources = new ArrayList<DesignComponent>();
316: Debug.collectAllComponentReferences(
317: propertyValue, itemCommandEventSources);
318: for (DesignComponent component : itemCommandEventSources)
319: RootCode.collectRequiredComponents(
320: component, requiredComponents);
321: }
322: },
323: // delete
324: DeleteDependencyPresenter
325: .createDependentOnParentComponentPresenter(),
326: DeleteDependencyPresenter
327: .createNullableComponentReferencePresenter(PROP_ITEM_COMMAND_LISTENER),
328: DeleteDependencyPresenter
329: .createNullableComponentReferencePresenter(PROP_DEFAULT_COMMAND),
330: new DeletePresenter() {
331: protected void delete() {
332: DesignComponent component = getComponent();
333: DesignComponent parent = component
334: .getParentComponent();
335: //TOOD this is should be check of Gauge attached to the ALert it is too generic
336: //This typeID check is becaouse of Gauge which could be also attached to the AlertCD
337: if (parent.getType() == FormCD.TYPEID)
338: ArraySupport.remove(parent,
339: FormCD.PROP_ITEMS, component);
340: }
341: },
342: // screen
343: new ItemDisplayPresenter(),
344: new DisplayableResourceCategoriesPresenter()
345:
346: );
347: }
348:
349: private List<InspectorOrderingController> createOrderingArrayController() {
350: return Collections
351: .<InspectorOrderingController> singletonList(new ArrayPropertyOrderingController(
352: PROP_COMMANDS, 0,
353: ItemCommandEventSourceCD.TYPEID));
354: }
355:
356: private static class AcceptItemCommandPresenter extends
357: AcceptPresenter {
358:
359: public AcceptItemCommandPresenter() {
360: super (Kind.COMPONENT_PRODUCER);
361: }
362:
363: @Override
364: public boolean isAcceptable(ComponentProducer producer,
365: AcceptSuggestion suggestion) {
366: DescriptorRegistry registry = getComponent().getDocument()
367: .getDescriptorRegistry();
368: return registry.isInHierarchy(CommandCD.TYPEID, producer
369: .getMainComponentTypeID());
370: }
371:
372: @Override
373: public final ComponentProducer.Result accept(
374: ComponentProducer producer, AcceptSuggestion suggestion) {
375: DesignComponent item = getComponent();
376: DesignDocument document = item.getDocument();
377:
378: DesignComponent command = producer
379: .createComponent(document).getMainComponent();
380: MidpDocumentSupport.getCategoryComponent(document,
381: CommandsCategoryCD.TYPEID).addComponent(command);
382:
383: DesignComponent source = document
384: .createComponent(ItemCommandEventSourceCD.TYPEID);
385: MidpDocumentSupport.addEventSource(item,
386: ItemCD.PROP_COMMANDS, source);
387:
388: source.writeProperty(ItemCommandEventSourceCD.PROP_ITEM,
389: PropertyValue.createComponentReference(item));
390: source.writeProperty(ItemCommandEventSourceCD.PROP_COMMAND,
391: PropertyValue.createComponentReference(command));
392:
393: return new ComponentProducer.Result(item);
394: }
395:
396: }
397:
398: }
|