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: */package org.netbeans.modules.vmd.midp.converter.wizard;
041:
042: import org.netbeans.modules.vmd.api.io.ProjectUtils;
043: import org.netbeans.modules.vmd.api.model.*;
044: import org.netbeans.modules.vmd.api.model.common.DocumentSupport;
045: import org.netbeans.modules.vmd.midp.components.MidpTypes;
046: import org.netbeans.modules.vmd.midp.components.commands.CommandCD;
047: import org.netbeans.modules.vmd.midp.components.displayables.*;
048: import org.netbeans.modules.vmd.midp.components.items.*;
049: import org.netbeans.modules.vmd.midp.components.resources.TickerCD;
050: import org.netbeans.modules.vmd.midp.palette.wizard.ComponentInstaller;
051: import org.netbeans.modules.vmd.midpnb.components.displayables.AbstractInfoScreenCD;
052: import org.netbeans.modules.vmd.midpnb.components.displayables.SplashScreenCD;
053: import org.netbeans.modules.vmd.midpnb.components.displayables.WaitScreenCD;
054: import org.netbeans.modules.vmd.midpnb.components.items.TableItemCD;
055: import org.netbeans.modules.vmd.midpnb.components.resources.SimpleCancellableTaskCD;
056: import org.netbeans.modules.vmd.midpnb.components.resources.SimpleTableModelCD;
057: import org.netbeans.modules.vmd.midpnb.components.svg.*;
058: import org.openide.util.Utilities;
059:
060: import java.util.*;
061:
062: /**
063: * @author David Kaspar
064: */
065: public class ConverterCustom {
066:
067: static void loadItemsToRegistry(final List<ConverterItem> items,
068: DesignDocument document) {
069: final DescriptorRegistry registry = document
070: .getDescriptorRegistry();
071: final Collection<String> unresolved = new HashSet<String>();
072: registry.readAccess(new Runnable() {
073: public void run() {
074: for (ConverterItem item : items) {
075: if (!isClassComponent(item))
076: continue;
077: String string = item.getTypeID();
078: if (registry.getComponentDescriptor(new TypeID(
079: TypeID.Kind.COMPONENT, string)) == null)
080: unresolved.add(string);
081: }
082: }
083: });
084: if (!unresolved.isEmpty()) {
085: Map<String, ComponentInstaller.Item> found = ComponentInstaller
086: .search(ProjectUtils.getProject(document));
087: ArrayList<ComponentInstaller.Item> install = new ArrayList<ComponentInstaller.Item>();
088: for (String s : unresolved) {
089: ComponentInstaller.Item item = found.get(s);
090: if (item != null)
091: install.add(item);
092: }
093: ComponentInstaller.install(found, install);
094: }
095: }
096:
097: static boolean isClassComponent(ConverterItem item) {
098: return Utilities.isJavaIdentifier(item.getID())
099: && MidpTypes.isValidFQNClassName(item.getTypeID());
100: }
101:
102: // Created: YES, Adds: NO
103: static void convertCustom(HashMap<String, ConverterItem> id2item,
104: ConverterItem item, DesignDocument document) {
105: TypeID typeID = new TypeID(TypeID.Kind.COMPONENT, item
106: .getTypeID());
107: ComponentProducer producer = DocumentSupport
108: .getComponentProducer(document, typeID.toString());
109: if (producer == null)
110: return;
111:
112: DesignComponent component = document.createComponent(producer
113: .getMainComponentTypeID());
114: producer.postInitialize(document, component);
115: convertCustomProperties(id2item, item, component, component
116: .getComponentDescriptor());
117: }
118:
119: // Created: NO, Adds: NO
120: private static void convertCustomProperties(
121: HashMap<String, ConverterItem> id2item, ConverterItem item,
122: DesignComponent component, ComponentDescriptor descriptor) {
123: if (descriptor == null)
124: return;
125: TypeID typeID = descriptor.getTypeDescriptor().getThisType();
126: if (DisplayableCD.TYPEID.equals(typeID))
127: ConverterDisplayables.convertDisplayable(id2item, item,
128: component);
129: else if (CanvasCD.TYPEID.equals(typeID))
130: ConverterDisplayables.convertCanvas(id2item, item,
131: component);
132: else if (ScreenCD.TYPEID.equals(typeID))
133: ConverterDisplayables.convertScreen(id2item, item,
134: component);
135: else if (AlertCD.TYPEID.equals(typeID))
136: ConverterDisplayables.convertAlertCore(id2item, item,
137: component);
138: else if (FormCD.TYPEID.equals(typeID))
139: ConverterDisplayables.convertFormCore(id2item, item,
140: component);
141: else if (ListCD.TYPEID.equals(typeID))
142: ConverterDisplayables.convertListCore(id2item, item,
143: component);
144: else if (TextBoxCD.TYPEID.equals(typeID))
145: ConverterDisplayables.convertTextBoxCore(id2item, item,
146: component);
147:
148: else if (ItemCD.TYPEID.equals(typeID))
149: ConverterItems.convertItem(id2item, item, component);
150: else if (CustomItemCD.TYPEID.equals(typeID))
151: ConverterItems.convertCustomItem(id2item, item, component);
152: else if (DateFieldCD.TYPEID.equals(typeID))
153: ConverterItems.convertDateFieldCore(id2item, item,
154: component);
155: else if (ChoiceGroupCD.TYPEID.equals(typeID))
156: ConverterItems.convertChoiceGroupCore(id2item, item,
157: component);
158: else if (GaugeCD.TYPEID.equals(typeID))
159: ConverterItems.convertGaugeCore(id2item, item, component);
160: else if (ImageItemCD.TYPEID.equals(typeID))
161: ConverterItems.convertImageItemCore(id2item, item,
162: component);
163: else if (SpacerCD.TYPEID.equals(typeID))
164: ConverterItems.convertSpacerCore(id2item, item, component);
165: else if (StringItemCD.TYPEID.equals(typeID))
166: ConverterItems.convertStringItemCore(id2item, item,
167: component);
168: else if (TextFieldCD.TYPEID.equals(typeID))
169: ConverterItems.convertTextFieldCore(id2item, item,
170: component);
171:
172: else if (AbstractInfoScreenCD.TYPEID.equals(typeID))
173: ConverterBuilt.convertAbstractInfoScreen(id2item, item,
174: component);
175: else if (SimpleCancellableTaskCD.TYPEID.equals(typeID))
176: ConverterBuilt.convertSimpleCancellableTask(id2item, item,
177: component.getDocument());
178: else if (SimpleTableModelCD.TYPEID.equals(typeID))
179: ConverterBuilt.convertSimpleTableModel(id2item, item,
180: component.getDocument());
181: else if (SplashScreenCD.TYPEID.equals(typeID))
182: ConverterBuilt.convertSplashScreen(id2item, item, component
183: .getDocument());
184: else if (TableItemCD.TYPEID.equals(typeID))
185: ConverterBuilt.convertTableItem(id2item, item, component
186: .getDocument());
187: else if (WaitScreenCD.TYPEID.equals(typeID))
188: ConverterBuilt.convertWaitScreen(id2item, item, component
189: .getDocument());
190:
191: else if (CommandCD.TYPEID.equals(typeID))
192: ConverterResources.convertCommand(item, component
193: .getDocument());
194: else if (TickerCD.TYPEID.equals(typeID))
195: ConverterResources.convertTicker(item, component
196: .getDocument());
197:
198: else if (SVGImageCD.TYPEID.equals(typeID))
199: ConverterSVG.convertImage(id2item, item, component
200: .getDocument());
201: else if (SVGAnimatorWrapperCD.TYPEID.equals(typeID))
202: ConverterSVG.convertPlayer(id2item, item, component
203: .getDocument());
204: else if (SVGMenuCD.TYPEID.equals(typeID))
205: ConverterSVG.convertMenu(id2item, item, component
206: .getDocument());
207: else if (SVGSplashScreenCD.TYPEID.equals(typeID))
208: ConverterSVG.convertSplashScreen(id2item, item, component
209: .getDocument());
210: else if (SVGWaitScreenCD.TYPEID.equals(typeID))
211: ConverterSVG.convertWaitScreen(id2item, item, component
212: .getDocument());
213:
214: else {
215: convertCustomProperties(id2item, item, component,
216: descriptor.getSuperDescriptor());
217:
218: String fqn = MidpTypes.getFQNClassName(typeID);
219: for (PropertyDescriptor property : descriptor
220: .getPropertyDescriptors()) {
221: String prefix = fqn + "#"; // NOI18N
222: String name = property.getName();
223: if (!name.startsWith(prefix))
224: continue;
225: name = name.substring(prefix.length());
226: int index = name.indexOf('#'); // NOI18N
227: if (index >= 0) { // NOI18N
228: prefix = "%%" + name.substring(0, index) + "_"
229: + name.substring(index + 1) + "_"; // NOI18N
230: String found = null;
231: for (String s : item.getPropertyNames()) {
232: if (s.startsWith(prefix)) {
233: found = s;
234: break;
235: }
236: }
237: if (found != null)
238: ConverterUtil.convertToPropertyValue(component,
239: property.getName(), property.getType(),
240: item.getPropertyValue(found));
241: } else {
242: if (!name.startsWith("set")) // NOI18N
243: continue;
244: name = name.substring("set".length()); // NOI18N
245: if (item.isPropertyValueSet("%" + name)) // NOI18N
246: ConverterUtil.convertToPropertyValue(component,
247: property.getName(), property.getType(),
248: item.getPropertyValue("%" + name)); // NOI18N
249: else if (name.length() > 0) {
250: name = Character.toLowerCase(name.charAt(0))
251: + name.substring(1);
252: if (item.isPropertyValueSet("%" + name)) // NOI18N
253: ConverterUtil.convertToPropertyValue(
254: component, property.getName(),
255: property.getType(), item
256: .getPropertyValue("%"
257: + name)); // NOI18N
258: }
259: }
260: }
261: }
262: }
263:
264: }
|