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.codegen.MultiGuardedSection;
046: import org.netbeans.modules.vmd.api.model.*;
047: import org.netbeans.modules.vmd.api.model.common.DocumentSupport;
048: import org.netbeans.modules.vmd.api.properties.DefaultPropertiesPresenter;
049: import org.netbeans.modules.vmd.api.screen.display.ScreenDisplayPresenter;
050: import org.netbeans.modules.vmd.midp.codegen.MidpCodeSupport;
051: import org.netbeans.modules.vmd.midp.codegen.MidpParameter;
052: import org.netbeans.modules.vmd.midp.codegen.MidpSetter;
053: import org.netbeans.modules.vmd.midp.components.MidpTypes;
054: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
055: import org.netbeans.modules.vmd.midp.components.MidpVersionable;
056: import org.netbeans.modules.vmd.midp.propertyeditors.MidpPropertiesCategories;
057: import org.netbeans.modules.vmd.midp.propertyeditors.PropertyEditorComboBox;
058: import org.netbeans.modules.vmd.midp.propertyeditors.date.PropertyEditorDate;
059: import org.netbeans.modules.vmd.midp.propertyeditors.timezone.PropertyEditorTimeZone;
060: import org.netbeans.modules.vmd.midp.screen.display.DateFieldDisplayPresenter;
061: import org.openide.util.NbBundle;
062:
063: import java.util.*;
064:
065: /**
066: *
067: * @author Karol Harezlak
068: */
069:
070: public class DateFieldCD extends ComponentDescriptor {
071:
072: private static Map<String, PropertyValue> inputModeValues;
073:
074: public static final TypeID TYPEID = new TypeID(
075: TypeID.Kind.COMPONENT, "javax.microedition.lcdui.DateField"); // NOI18N
076:
077: public static final int VALUE_DATE = 1;
078: public static final int VALUE_TIME = 2;
079: public static final int VALUE_DATE_TIME = 3;
080:
081: public static final String PROP_INPUT_MODE = "inputMode"; // NOI18N
082: public static final String PROP_DATE = "date"; // NOI18N
083: public static final String PROP_TIME_ZONE = "timeZone"; // NOI18N
084:
085: public TypeDescriptor getTypeDescriptor() {
086: return new TypeDescriptor(ItemCD.TYPEID, TYPEID, true, true);
087: }
088:
089: public VersionDescriptor getVersionDescriptor() {
090: return MidpVersionDescriptor.MIDP;
091: }
092:
093: @Override
094: public void postInitialize(DesignComponent component) {
095: component
096: .writeProperty(
097: PROP_DATE,
098: PropertyValue
099: .createUserCode("new java.util.Date (System.currentTimeMillis ())")); // NOI18N
100: component.writeProperty(PROP_INPUT_MODE, MidpTypes
101: .createIntegerValue(VALUE_DATE_TIME));
102: }
103:
104: public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
105: return Arrays.asList(new PropertyDescriptor(PROP_DATE,
106: MidpTypes.TYPEID_LONG, PropertyValue.createNull(),
107: true, true, MidpVersionable.MIDP),
108: new PropertyDescriptor(PROP_TIME_ZONE,
109: MidpTypes.TYPEID_JAVA_LANG_STRING,
110: PropertyValue.createNull(), true, true,
111: MidpVersionable.MIDP), new PropertyDescriptor(
112: PROP_INPUT_MODE, MidpTypes.TYPEID_INT,
113: PropertyValue.createNull(), false, true,
114: MidpVersionable.MIDP));
115: }
116:
117: @Override
118: protected void gatherPresenters(ArrayList<Presenter> presenters) {
119: DocumentSupport.removePresentersOfClass(presenters,
120: ScreenDisplayPresenter.class);
121: super .gatherPresenters(presenters);
122: }
123:
124: private static DefaultPropertiesPresenter createPropertiesPresenter() {
125: return new DefaultPropertiesPresenter()
126: .addPropertiesCategory(
127: MidpPropertiesCategories.CATEGORY_PROPERTIES)
128: .addProperty(
129: NbBundle.getMessage(DateFieldCD.class,
130: "DISP_DateField_Date"),
131: "dd.mm.yyyy hh:mm:ss",
132: PropertyEditorDate.createInstance(), PROP_DATE)
133: // NOI18N
134: .addProperty(
135: NbBundle.getMessage(DateFieldCD.class,
136: "DISP_DateField_Time_Zone"),
137: PropertyEditorTimeZone.createInstance(),
138: PROP_TIME_ZONE)
139: // NOI18N
140: .addProperty(
141: NbBundle.getMessage(DateFieldCD.class,
142: "DISP_DateField_Input_Mode"), // NOI18N
143: PropertyEditorComboBox
144: .createInstance(
145: getInputModeValues(),
146: TYPEID,
147: NbBundle
148: .getMessage(
149: DateFieldCD.class,
150: "DISP_DateField_Input_Mode_RB_LABEL"), // NOI18N
151: NbBundle
152: .getMessage(
153: DateFieldCD.class,
154: "DISP_DateField_Input_Mode_UCLABEL")),
155: PROP_INPUT_MODE); // NOI18N
156: }
157:
158: private static Presenter createSetterPresenter() {
159: return new CodeSetterPresenter().addParameters(
160: new DateParameter()).addParameters(
161: new TimeZoneParameter()).addParameters(
162: new InputModeParameter()).addSetters(
163: MidpSetter.createConstructor(TYPEID,
164: MidpVersionable.MIDP).addParameters(
165: ItemCD.PROP_LABEL,
166: InputModeParameter.PARAM_INPUT_MODE))
167: .addSetters(
168: MidpSetter.createConstructor(TYPEID,
169: MidpVersionable.MIDP).addParameters(
170: ItemCD.PROP_LABEL,
171: InputModeParameter.PARAM_INPUT_MODE,
172: TimeZoneParameter.PARAM_TIME_ZONE))
173: .addSetters(
174: MidpSetter.createSetter("setDate",
175: MidpVersionable.MIDP).addParameters(
176: PROP_DATE)) // NOI18N
177: .addSetters(
178: MidpSetter.createSetter("setInputMode",
179: MidpVersionable.MIDP).addParameters(
180: PROP_INPUT_MODE)); // NOI18N
181: }
182:
183: protected List<? extends Presenter> createPresenters() {
184: return Arrays.asList(
185: // properties
186: createPropertiesPresenter(),
187: // code
188: createSetterPresenter(),
189: // screen
190: new DateFieldDisplayPresenter());
191: }
192:
193: public static Map<String, PropertyValue> getInputModeValues() {
194: if (inputModeValues == null) {
195: inputModeValues = new TreeMap<String, PropertyValue>();
196: inputModeValues.put("DATE", MidpTypes
197: .createIntegerValue(DateFieldCD.VALUE_DATE)); // NOI18N
198: inputModeValues.put("DATE_TIME", MidpTypes
199: .createIntegerValue(DateFieldCD.VALUE_DATE_TIME)); // NOI18N
200: inputModeValues.put("TIME", MidpTypes
201: .createIntegerValue(DateFieldCD.VALUE_TIME)); // NOI18N
202: }
203:
204: return inputModeValues;
205: }
206:
207: private static class DateParameter extends MidpParameter {
208:
209: public static final String PARAM_DATE = "date"; // NOI18N
210:
211: protected DateParameter() {
212: super (PARAM_DATE);
213: }
214:
215: @Override
216: public void generateParameterCode(DesignComponent component,
217: MultiGuardedSection section, int index) {
218: PropertyValue propertyValue = component
219: .readProperty(PROP_DATE);
220: if (propertyValue.getKind() == PropertyValue.Kind.VALUE) {
221: long date = MidpTypes.getLong(propertyValue);
222: section.getWriter().write(
223: "new java.util.Date (" + date + "l)"); // NOI18N
224: } else
225: super .generateParameterCode(component, section, index);
226: }
227: }
228:
229: private static class InputModeParameter extends MidpParameter {
230:
231: public static final String PARAM_INPUT_MODE = "inputMode"; // NOI18N
232:
233: public InputModeParameter() {
234: super (PARAM_INPUT_MODE);
235: }
236:
237: @Override
238: public void generateParameterCode(DesignComponent component,
239: MultiGuardedSection section, int index) {
240: PropertyValue propertyValue = component
241: .readProperty(PROP_INPUT_MODE);
242: if (propertyValue.getKind() == PropertyValue.Kind.VALUE) {
243: int value = MidpTypes.getInteger(propertyValue);
244: switch (value) {
245: case VALUE_DATE:
246: section.getWriter().write("DateField.DATE"); // NOI18N
247: return;
248: case VALUE_DATE_TIME:
249: section.getWriter().write("DateField.DATE_TIME"); // NOI18N
250: return;
251: case VALUE_TIME:
252: section.getWriter().write("DateField.TIME"); // NOI18N
253: return;
254: default:
255: throw Debug.illegalState();
256: }
257: }
258: super .generateParameterCode(component, section, index);
259: }
260:
261: }
262:
263: private static class TimeZoneParameter extends MidpParameter {
264:
265: public static final String PARAM_TIME_ZONE = "timeZone"; // NOI18N
266:
267: public TimeZoneParameter() {
268: super (PARAM_TIME_ZONE);
269: }
270:
271: @Override
272: public void generateParameterCode(DesignComponent component,
273: MultiGuardedSection section, int index) {
274: PropertyValue propertyValue = component
275: .readProperty(PROP_TIME_ZONE);
276: if (propertyValue.getKind() == PropertyValue.Kind.VALUE) {
277: section.getWriter().write(
278: "java.util.TimeZone.getTimeZone ("); // NOI18N
279: MidpCodeSupport.generateCodeForPropertyValue(section
280: .getWriter(), propertyValue);
281: section.getWriter().write(")"); // NOI18N
282: } else
283: super.generateParameterCode(component, section, index);
284: }
285:
286: }
287:
288: }
|