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.resources;
042:
043: import org.netbeans.modules.vmd.api.codegen.*;
044: import org.netbeans.modules.vmd.api.model.*;
045: import org.netbeans.modules.vmd.api.properties.DefaultPropertiesPresenter;
046: import org.netbeans.modules.vmd.midp.codegen.MidpCodeSupport;
047: import org.netbeans.modules.vmd.midp.components.MidpTypes;
048: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
049: import org.netbeans.modules.vmd.midp.components.MidpVersionable;
050: import org.netbeans.modules.vmd.midp.propertyeditors.MidpPropertiesCategories;
051: import org.netbeans.modules.vmd.midp.propertyeditors.PropertyEditorComboBox;
052:
053: import java.util.*;
054: import org.openide.util.NbBundle;
055:
056: /**
057: * @author David Kaspar
058: */
059:
060: public final class FontCD extends ComponentDescriptor {
061:
062: public static final TypeID TYPEID = new TypeID(
063: TypeID.Kind.COMPONENT, "javax.microedition.lcdui.Font"); // NOI18N
064:
065: public static final int VALUE_FACE_SYSTEM = 0;
066: public static final int VALUE_FACE_MONOSPACE = 32;
067: public static final int VALUE_FACE_PROPORTIONAL = 64;
068:
069: public static final int VALUE_FONT_STATIC_TEXT = 0;
070: public static final int VALUE_FONT_INPUT_TEXT = 1;
071:
072: public static final int VALUE_SIZE_MEDIUM = 0;
073: public static final int VALUE_SIZE_SMALL = 8;
074: public static final int VALUE_SIZE_LARGE = 16;
075:
076: public static final int VALUE_STYLE_PLAIN = 0;
077: public static final int VALUE_STYLE_BOLD = 1;
078: public static final int VALUE_STYLE_ITALIC = 2;
079: public static final int VALUE_STYLE_UNDERLINED = 4;
080:
081: public static final int VALUE_KIND_DEFAULT = 0;
082: public static final int VALUE_KIND_CUSTOM = 1;
083: public static final int VALUE_KIND_STATIC = 2;
084: public static final int VALUE_KIND_INPUT = 3;
085:
086: public static final String PROP_FACE = "face"; // NOI18N
087: public static final String PROP_STYLE = "style"; // NOI18N
088: public static final String PROP_SIZE = "bold"; // NOI18N
089: public static final String PROP_FONT_KIND = "fontKind"; // NOI18N
090:
091: public static final String LABEL_KIND_DEFAULT = "Default"; // NOI18N
092: public static final String LABEL_KIND_CUSTOM = "Custom"; // NOI18N
093: public static final String LABEL_KIND_STATIC = "Static"; // NOI18N
094: public static final String LABEL_KIND_INPUT = "Input"; // NOI18N
095:
096: public static final String LABEL_FACE_SYSTEM = "SYSTEM"; // NOI18N
097: public static final String LABEL_FACE_MONOSPACE = "MONOSPACE"; // NOI18N
098: public static final String LABEL_FACE_PROPORTIONAL = "PROPORTIONAL"; // NOI18N
099:
100: public static final String LABEL_SIZE_MEDIUM = "MEDIUM"; // NOI18N
101: public static final String LABEL_SIZE_LARGE = "LARGE"; // NOI18N
102: public static final String LABEL_SIZE_SMALL = "SMALL"; // NOI18N
103:
104: public static final String LABEL_STYLE_PLAIN = "PLAIN"; // NOI18N
105: public static final String LABEL_STYLE_BOLD = "BOLD"; // NOI18N
106: public static final String LABEL_STYLE_ITALIC = "ITALIC"; // NOI18N
107: public static final String LABEL_STYLE_UNDERLINED = "UNDERLINED"; // NOI18N
108:
109: private static Map<String, PropertyValue> kindTypes;
110: private static Map<String, PropertyValue> faceTypes;
111: private static Map<String, PropertyValue> sizeTypes;
112: private static Map<String, PropertyValue> styleTypes;
113:
114: public TypeDescriptor getTypeDescriptor() {
115: return new TypeDescriptor(ResourceCD.TYPEID, TYPEID, true, true);
116: }
117:
118: public VersionDescriptor getVersionDescriptor() {
119: return MidpVersionDescriptor.MIDP;
120: }
121:
122: public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
123: return Arrays.asList(new PropertyDescriptor(PROP_FONT_KIND,
124: MidpTypes.TYPEID_INT, MidpTypes
125: .createIntegerValue(VALUE_KIND_DEFAULT), false,
126: false, MidpVersionable.MIDP), new PropertyDescriptor(
127: PROP_FACE, MidpTypes.TYPEID_INT, MidpTypes
128: .createIntegerValue(VALUE_FACE_SYSTEM), false,
129: true, MidpVersionable.MIDP), new PropertyDescriptor(
130: PROP_SIZE, MidpTypes.TYPEID_INT, MidpTypes
131: .createIntegerValue(VALUE_SIZE_MEDIUM), false,
132: true, MidpVersionable.MIDP), new PropertyDescriptor(
133: PROP_STYLE, MidpTypes.TYPEID_INT, MidpTypes
134: .createIntegerValue(VALUE_STYLE_PLAIN), false,
135: true, MidpVersionable.MIDP));
136: }
137:
138: private static DefaultPropertiesPresenter createPropertiesPresenter() {
139: return new DefaultPropertiesPresenter().addPropertiesCategory(
140: MidpPropertiesCategories.CATEGORY_PROPERTIES)
141: .addProperty(
142: NbBundle.getMessage(FontCD.class,
143: "DISP_Font_Kind"), // NOI18N
144: PropertyEditorComboBox.createInstance(
145: getKindTypes(), TYPEID,
146: NbBundle.getMessage(FontCD.class,
147: "DISP_Font_Kind_RB_LABEL"), // NOI18N
148: NbBundle.getMessage(FontCD.class,
149: "DISP_Font_Kind_UCLABEL")),
150: PROP_FONT_KIND) // NOI18N
151: .addProperty(
152: NbBundle.getMessage(FontCD.class,
153: "DISP_Font_Face"), // NOI18N
154: PropertyEditorComboBox.createInstance(
155: getFaceTypes(), TYPEID, TYPEID,
156: NbBundle.getMessage(FontCD.class,
157: "DISP_Font_Face_RB_LABEL"), // NOI18N
158: NbBundle.getMessage(FontCD.class,
159: "DISP_Font_Face_UCLABEL")),
160: PROP_FACE) // NOI18N
161: .addProperty(
162: NbBundle.getMessage(FontCD.class,
163: "DISP_Font_Size"), // NOI18N
164: PropertyEditorComboBox.createInstance(
165: getSizeTypes(), TYPEID, TYPEID,
166: NbBundle.getMessage(FontCD.class,
167: "DISP_Font_Size_RB_LABEL"), // NOI18N
168: NbBundle.getMessage(FontCD.class,
169: "DISP_Font_Size_UCLABEL")),
170: PROP_SIZE) // NOI18N
171: .addProperty(
172: NbBundle.getMessage(FontCD.class,
173: "DISP_Font_Style"), // NOI18N
174: PropertyEditorComboBox.createInstance(
175: getStyleTypes(), TYPEID, TYPEID,
176: NbBundle.getMessage(FontCD.class,
177: "DISP_Font_Style_RB_LABEL"), // NOI18N
178: NbBundle.getMessage(FontCD.class,
179: "DISP_Font_Style_UCLABEL")),
180: PROP_STYLE); // NOI18N
181: }
182:
183: private static Presenter createSetterPresenter() {
184: return new CodeSetterPresenter().addSetters(new FontSetter());
185: }
186:
187: protected List<? extends Presenter> createPresenters() {
188: return Arrays.asList(
189: // properties
190: createPropertiesPresenter(),
191: // code
192: createSetterPresenter());
193: }
194:
195: public static Map<String, PropertyValue> getKindTypes() {
196: if (kindTypes == null) {
197: kindTypes = new TreeMap<String, PropertyValue>();
198: kindTypes.put(LABEL_KIND_DEFAULT, MidpTypes
199: .createIntegerValue(VALUE_KIND_DEFAULT)); // NOI18N
200: kindTypes.put(LABEL_KIND_CUSTOM, MidpTypes
201: .createIntegerValue(VALUE_KIND_CUSTOM)); // NOI18N
202: kindTypes.put(LABEL_KIND_STATIC, MidpTypes
203: .createIntegerValue(VALUE_KIND_STATIC)); // NOI18N
204: kindTypes.put(LABEL_KIND_INPUT, MidpTypes
205: .createIntegerValue(VALUE_KIND_INPUT)); // NOI18N
206: }
207: return kindTypes;
208: }
209:
210: public static Map<String, PropertyValue> getFaceTypes() {
211: if (faceTypes == null) {
212: faceTypes = new TreeMap<String, PropertyValue>();
213: faceTypes.put(LABEL_FACE_SYSTEM, MidpTypes
214: .createIntegerValue(VALUE_FACE_SYSTEM));
215: faceTypes.put(LABEL_FACE_MONOSPACE, MidpTypes
216: .createIntegerValue(VALUE_FACE_MONOSPACE));
217: faceTypes.put(LABEL_FACE_PROPORTIONAL, MidpTypes
218: .createIntegerValue(VALUE_FACE_PROPORTIONAL));
219: }
220: return faceTypes;
221: }
222:
223: public static Map<String, PropertyValue> getSizeTypes() {
224: if (sizeTypes == null) {
225: sizeTypes = new TreeMap<String, PropertyValue>();
226: sizeTypes.put(LABEL_SIZE_MEDIUM, MidpTypes
227: .createIntegerValue(VALUE_SIZE_MEDIUM));
228: sizeTypes.put(LABEL_SIZE_SMALL, MidpTypes
229: .createIntegerValue(VALUE_SIZE_SMALL));
230: sizeTypes.put(LABEL_SIZE_LARGE, MidpTypes
231: .createIntegerValue(VALUE_SIZE_LARGE));
232: }
233: return sizeTypes;
234: }
235:
236: public static Map<String, PropertyValue> getStyleTypes() {
237: if (styleTypes == null) {
238: styleTypes = new TreeMap<String, PropertyValue>();
239: styleTypes.put(LABEL_STYLE_PLAIN, MidpTypes
240: .createIntegerValue(VALUE_STYLE_PLAIN));
241: styleTypes.put(LABEL_STYLE_BOLD, MidpTypes
242: .createIntegerValue(VALUE_STYLE_BOLD));
243: styleTypes.put(LABEL_STYLE_ITALIC, MidpTypes
244: .createIntegerValue(VALUE_STYLE_ITALIC));
245: styleTypes.put(LABEL_STYLE_UNDERLINED, MidpTypes
246: .createIntegerValue(VALUE_STYLE_UNDERLINED));
247: }
248: return styleTypes;
249: }
250:
251: public static String getKindByCode(int code) {
252: switch (code) {
253: case VALUE_KIND_DEFAULT:
254: return LABEL_KIND_DEFAULT;
255: case VALUE_KIND_CUSTOM:
256: return LABEL_KIND_CUSTOM;
257: case VALUE_KIND_STATIC:
258: return LABEL_KIND_STATIC;
259: case VALUE_KIND_INPUT:
260: return LABEL_KIND_INPUT;
261: }
262: return LABEL_KIND_DEFAULT;
263: }
264:
265: public static String getFaceByCode(int code) {
266: switch (code) {
267: case VALUE_FACE_SYSTEM:
268: return LABEL_FACE_SYSTEM;
269: case VALUE_FACE_MONOSPACE:
270: return LABEL_FACE_MONOSPACE;
271: case VALUE_FACE_PROPORTIONAL:
272: return LABEL_FACE_PROPORTIONAL;
273: }
274: return LABEL_FACE_SYSTEM;
275: }
276:
277: public static String getStyleByCode(int code) {
278: switch (code) {
279: case VALUE_STYLE_PLAIN:
280: return LABEL_STYLE_PLAIN;
281: case VALUE_STYLE_BOLD:
282: return LABEL_STYLE_BOLD;
283: case VALUE_STYLE_ITALIC:
284: return LABEL_STYLE_ITALIC;
285: case VALUE_STYLE_UNDERLINED:
286: return LABEL_STYLE_UNDERLINED;
287: }
288: return LABEL_STYLE_PLAIN;
289: }
290:
291: public static String getSizeByCode(int code) {
292: switch (code) {
293: case VALUE_SIZE_SMALL:
294: return LABEL_SIZE_SMALL;
295: case VALUE_SIZE_MEDIUM:
296: return LABEL_SIZE_MEDIUM;
297: case VALUE_SIZE_LARGE:
298: return LABEL_SIZE_LARGE;
299: }
300: return LABEL_SIZE_MEDIUM;
301: }
302:
303: public static int getKindByValue(Object value) {
304: if (value == LABEL_KIND_DEFAULT) {
305: return VALUE_KIND_DEFAULT;
306: } else if (value == LABEL_KIND_CUSTOM) {
307: return VALUE_KIND_CUSTOM;
308: } else if (value == LABEL_KIND_STATIC) {
309: return VALUE_KIND_STATIC;
310: } else if (value == LABEL_KIND_INPUT) {
311: return VALUE_KIND_INPUT;
312: }
313: return VALUE_KIND_DEFAULT;
314: }
315:
316: public static int getFaceByValue(Object value) {
317: if (value == LABEL_FACE_SYSTEM) {
318: return VALUE_FACE_SYSTEM;
319: } else if (value == LABEL_FACE_MONOSPACE) {
320: return VALUE_FACE_MONOSPACE;
321: } else if (value == LABEL_FACE_PROPORTIONAL) {
322: return VALUE_FACE_PROPORTIONAL;
323: }
324: return VALUE_FACE_SYSTEM;
325: }
326:
327: public static int getStyleByValue(Object value) {
328: if (value == LABEL_STYLE_PLAIN) {
329: return VALUE_STYLE_PLAIN;
330: } else if (value == LABEL_STYLE_BOLD) {
331: return VALUE_STYLE_BOLD;
332: } else if (value == LABEL_STYLE_ITALIC) {
333: return VALUE_STYLE_ITALIC;
334: } else if (value == LABEL_STYLE_UNDERLINED) {
335: return VALUE_STYLE_UNDERLINED;
336: }
337: return VALUE_STYLE_PLAIN;
338: }
339:
340: public static int getSizeByValue(Object value) {
341: if (value == LABEL_SIZE_SMALL) {
342: return VALUE_SIZE_SMALL;
343: } else if (value == LABEL_SIZE_MEDIUM) {
344: return VALUE_SIZE_MEDIUM;
345: } else if (value == LABEL_SIZE_LARGE) {
346: return VALUE_SIZE_LARGE;
347: }
348: return VALUE_SIZE_MEDIUM;
349: }
350:
351: private static class FontSetter implements Setter {
352:
353: public boolean isConstructor() {
354: return true;
355: }
356:
357: public TypeID getConstructorRelatedTypeID() {
358: return TYPEID;
359: }
360:
361: public int getPriority() {
362: return 0;
363: }
364:
365: public String getSetterName() {
366: return "Font.getFont"; // NOI18N
367: }
368:
369: public Versionable getVersionable() {
370: return MidpVersionable.MIDP;
371: }
372:
373: public void generateSetterCode(MultiGuardedSection section,
374: DesignComponent component,
375: Map<String, Parameter> name2parameter) {
376: CodeWriter writer = section.getWriter();
377: writer.write(
378: CodeReferencePresenter
379: .generateDirectAccessCode(component))
380: .write(" = "); // NOI18N
381:
382: PropertyValue propertyValue = component
383: .readProperty(PROP_FONT_KIND);
384: switch (MidpTypes.getInteger(propertyValue)) {
385: case VALUE_KIND_DEFAULT:
386: writer.write("Font.getDefaultFont ()"); // NOI18N
387: break;
388: case VALUE_KIND_INPUT:
389: writer.write("Font.getFont (Font.FONT_INPUT_TEXT)"); // NOI18N
390: break;
391: case VALUE_KIND_STATIC:
392: writer.write("Font.getFont (Font.FONT_STATIC_TEXT)"); // NOI18N
393: break;
394: case VALUE_KIND_CUSTOM:
395:
396: writer.write("Font.getFont ("); // NOI18N
397:
398: propertyValue = component.readProperty(PROP_FACE);
399: if (propertyValue.getKind() == PropertyValue.Kind.VALUE) {
400: switch (MidpTypes.getInteger(propertyValue)) {
401: case VALUE_FACE_SYSTEM:
402: writer.write("Font.FACE_SYSTEM");
403: break; // NOI18N
404: case VALUE_FACE_MONOSPACE:
405: writer.write("Font.FACE_MONOSPACE");
406: break; // NOI18N
407: case VALUE_FACE_PROPORTIONAL:
408: writer.write("Font.FACE_PROPORTIONAL");
409: break; // NOI18N
410: default:
411: throw Debug.illegalState();
412: }
413: } else
414: MidpCodeSupport.generateCodeForPropertyValue(
415: writer, propertyValue);
416:
417: writer.write(", "); // NOI18N
418:
419: propertyValue = component.readProperty(PROP_STYLE);
420: if (propertyValue.getKind() == PropertyValue.Kind.VALUE) {
421: int code = MidpTypes.getInteger(propertyValue);
422: if (VALUE_STYLE_PLAIN == code) {
423: writer.write("Font.STYLE_PLAIN"); // NOI18N
424: } else {
425: boolean needOR = false;
426: if ((code & VALUE_STYLE_BOLD) != 0) {
427: writer.write("Font.STYLE_BOLD"); // NOI18N
428: needOR = true;
429: }
430: if ((code & VALUE_STYLE_ITALIC) != 0) {
431: if (needOR) {
432: writer.write(" | "); // NOI18N
433: }
434: writer.write("Font.STYLE_ITALIC"); // NOI18N
435: needOR = true;
436: }
437: if ((code & VALUE_STYLE_UNDERLINED) != 0) {
438: if (needOR) {
439: writer.write(" | "); // NOI18N
440: }
441: writer.write("Font.STYLE_UNDERLINED"); // NOI18N
442: }
443: }
444: } else
445: MidpCodeSupport.generateCodeForPropertyValue(
446: writer, propertyValue);
447:
448: writer.write(", "); // NOI18N
449:
450: propertyValue = component.readProperty(PROP_SIZE);
451: if (propertyValue.getKind() == PropertyValue.Kind.VALUE) {
452: switch (MidpTypes.getInteger(propertyValue)) {
453: case VALUE_SIZE_SMALL:
454: writer.write("Font.SIZE_SMALL");
455: break; // NOI18N
456: case VALUE_SIZE_MEDIUM:
457: writer.write("Font.SIZE_MEDIUM");
458: break; // NOI18N
459: case VALUE_SIZE_LARGE:
460: writer.write("Font.SIZE_LARGE");
461: break; // NOI18N
462: default:
463: throw Debug.illegalState();
464: }
465: } else
466: MidpCodeSupport.generateCodeForPropertyValue(
467: writer, propertyValue);
468:
469: writer.write(")"); // NOI18N
470: break;
471: default:
472: throw Debug.illegalState();
473: }
474:
475: writer.write(";\n"); // NOI18N
476: }
477:
478: public List<String> getParameters() {
479: return Collections.emptyList();
480: }
481:
482: }
483:
484: }
|