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.components;
041:
042: import org.netbeans.modules.vmd.api.io.DataObjectContext;
043: import org.netbeans.modules.vmd.api.io.ProjectUtils;
044: import org.netbeans.modules.vmd.api.io.serialization.ComponentElement;
045: import org.netbeans.modules.vmd.api.io.serialization.DocumentSerializationController;
046: import org.netbeans.modules.vmd.api.io.serialization.PropertyElement;
047: import org.netbeans.modules.vmd.api.model.DescriptorRegistry;
048: import org.netbeans.modules.vmd.api.model.DesignComponent;
049: import org.netbeans.modules.vmd.api.model.DesignDocument;
050: import org.netbeans.modules.vmd.api.model.TypeID;
051: import org.netbeans.modules.vmd.midp.components.categories.*;
052: import org.netbeans.modules.vmd.midp.components.general.RootCD;
053: import org.netbeans.modules.vmd.midp.components.items.ItemCD;
054: import org.netbeans.modules.vmd.midp.palette.wizard.ComponentInstaller;
055: import java.util.ArrayList;
056: import java.util.Collection;
057: import java.util.HashSet;
058: import java.util.Map;
059: import org.netbeans.modules.vmd.api.io.serialization.DocumentErrorHandler;
060:
061: /**
062: * @author David Kaspar
063: */
064: public class MidpDocumentSerializationController extends
065: DocumentSerializationController {
066:
067: public static final String VERSION_1 = "1"; // NOI18N
068: private Collection<String> unresolved = new HashSet<String>();
069:
070: public void approveComponents(DataObjectContext context,
071: DesignDocument loadingDocument, String documentVersion,
072: final Collection<ComponentElement> componentElements,
073: DocumentErrorHandler errorHandler) {
074: final DescriptorRegistry registry = loadingDocument
075: .getDescriptorRegistry();
076:
077: registry.readAccess(new Runnable() {
078:
079: public void run() {
080: for (ComponentElement element : componentElements) {
081: String string = element.getTypeID().getString();
082: if (MidpTypes.isValidFQNClassName(string)) {
083: if (registry.getComponentDescriptor(new TypeID(
084: TypeID.Kind.COMPONENT, string)) == null) {
085: unresolved.add(string);
086: }
087: }
088: }
089: }
090: });
091: if (!unresolved.isEmpty()) {
092: Map<String, ComponentInstaller.Item> found = ComponentInstaller
093: .search(ProjectUtils.getProject(context));
094: ArrayList<ComponentInstaller.Item> install = new ArrayList<ComponentInstaller.Item>();
095: for (String s : unresolved) {
096: ComponentInstaller.Item item = found.get(s);
097: if (item != null) {
098: install.add(item);
099: }
100: }
101: ComponentInstaller.install(found, install);
102: }
103: }
104:
105: public void approveProperties(DataObjectContext context,
106: DesignDocument loadingDocument, String documentVersion,
107: DesignComponent component,
108: Collection<PropertyElement> propertyElements,
109: DocumentErrorHandler errorHandler) {
110: if (!MidpDocumentSupport.PROJECT_TYPE_MIDP.equals(context
111: .getProjectType())
112: || !VERSION_1.equals(documentVersion)) {
113: return;
114: }
115: if (loadingDocument.getDescriptorRegistry().isInHierarchy(
116: ItemCD.TYPEID, component.getType())) {
117: ArrayList<PropertyElement> elementsToRemove = new ArrayList<PropertyElement>();
118: ArrayList<PropertyElement> elementsToAdd = new ArrayList<PropertyElement>();
119: for (PropertyElement propertyElement : propertyElements) {
120: if (ItemCD.PROP_OLD_ITEM_COMMAND_LISTENER
121: .equals(propertyElement.getPropertyName())) {
122: elementsToRemove.add(propertyElement);
123: elementsToAdd.add(PropertyElement.create(
124: ItemCD.PROP_ITEM_COMMAND_LISTENER,
125: propertyElement.getTypeID(),
126: propertyElement.getSerialized()));
127: break;
128: }
129: }
130: propertyElements.removeAll(elementsToRemove);
131: propertyElements.addAll(elementsToAdd);
132: for (PropertyElement pe : propertyElements) {
133: pe.getSerialized();
134: }
135: }
136: }
137:
138: public void postValidateDocument(DataObjectContext context,
139: DesignDocument loadingDocument, String documentVersion,
140: DocumentErrorHandler errorHandler) {
141: if (!MidpDocumentSupport.PROJECT_TYPE_MIDP.equals(context
142: .getProjectType())
143: || !VERSION_1.equals(documentVersion)) {
144: //checkInstanceNames(DocumentSupport.gatherAllComponentsOfTypeID(loadingDocument.getRootComponent(), ClassCD.TYPEID), errorHandler);
145: return;
146: }
147: DesignComponent rootComponent = loadingDocument
148: .getRootComponent();
149: if (rootComponent == null) {
150: rootComponent = loadingDocument
151: .createComponent(RootCD.TYPEID);
152: loadingDocument.setRootComponent(rootComponent);
153: }
154: MidpDocumentSupport.getCategoryComponent(loadingDocument,
155: CommandsCategoryCD.TYPEID);
156: MidpDocumentSupport.getCategoryComponent(loadingDocument,
157: ControllersCategoryCD.TYPEID);
158: MidpDocumentSupport.getCategoryComponent(loadingDocument,
159: DisplayablesCategoryCD.TYPEID);
160: MidpDocumentSupport.getCategoryComponent(loadingDocument,
161: PointsCategoryCD.TYPEID);
162: MidpDocumentSupport.getCategoryComponent(loadingDocument,
163: ResourcesCategoryCD.TYPEID);
164: //checkInstanceNames(rootComponent.getComponents(), errorHandler);
165: }
166:
167: /*
168: private void checkInstanceNames(Collection<DesignComponent> components, DocumentErrorHandler errorHandler) {
169: for (DesignComponent component : components) {
170: PropertyDescriptor descriptor = component.getComponentDescriptor().getPropertyDescriptor(ClassCD.PROP_INSTANCE_NAME);
171: if (descriptor == null) {
172: return;
173: }
174:
175: if (component.readProperty(ClassCD.PROP_INSTANCE_NAME).getPrimitiveValue() == null) {
176: PropertyValue value = InstanceNameResolver.createFromSuggested(component, ClassCode.getSuggestedMainName(component.getType()));
177: component.writeProperty(ClassCD.PROP_INSTANCE_NAME, value);
178: errorHandler.addWarning(NbBundle.getMessage(MidpDocumentSerializationController.class, "MSG_null_instance_name_1") //NOI18N
179: + " " + component + " " + NbBundle.getMessage(MidpDocumentSerializationController.class, "MSG_null_instance_name_2") //NOI18N
180: + " " + InfoPresenter.getDisplayName(component)); //NOI18N
181: }
182: }
183: }
184: */
185: }
|