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.api.model;
042:
043: import java.util.Arrays;
044: import java.util.List;
045:
046: /**
047: * Represents a factory for components. The DescriptorRegistry contains a set of component producers. These producers are
048: * usually visualized in the palette and represents a component, a group of components or component with special post-initialization.
049: * ComponentProducer is automatically created for all ComponentDescriptors which return non-null value from getPaletteDescriptor method.
050: *
051: * Usually you have to implement postInitialize method to initialize main component and/or create secondary components.
052: * Then you have to implement checkValidity method.
053: *
054: * @author David Kaspar
055: */
056: public abstract class ComponentProducer {
057:
058: private String producerID;
059: private TypeID typeID;
060: private PaletteDescriptor paletteDescriptor;
061:
062: /**
063: * Creates a component producer.
064: * @param producerID the unique producer id
065: * @param typeID the type id of the main component created by the producer.
066: * @param paletteDescriptor the palette descriptor used for visualization of the producer.
067: */
068: protected ComponentProducer(String producerID, TypeID typeID,
069: PaletteDescriptor paletteDescriptor) {
070: assert producerID != null && typeID != null
071: && paletteDescriptor != null;
072: this .producerID = producerID;
073: this .typeID = typeID;
074: this .paletteDescriptor = paletteDescriptor;
075: }
076:
077: /**
078: * Returns producer id.
079: * @return the producer id
080: */
081: public final String getProducerID() {
082: return producerID;
083: }
084:
085: /**
086: * Returns a type id of the main component created by the producer.
087: * @return the type id of the main component
088: */
089: public final TypeID getMainComponentTypeID() {
090: return typeID;
091: }
092:
093: /**
094: * Returns palette descriptor of the producer.
095: * @return the palette descriptor
096: */
097: public final PaletteDescriptor getPaletteDescriptor() {
098: return paletteDescriptor;
099: }
100:
101: /**
102: * Creates a component.
103: * @param document the document
104: * @return the result of creation
105: */
106: public final Result createComponent(DesignDocument document) {
107: DesignComponent mainComponent = createMainComponent(document);
108: assert mainComponent != null;
109: Result result = postInitialize(document, mainComponent);
110: assert result != null;
111: assert result.getMainComponent() == mainComponent;
112: return result;
113: }
114:
115: /**
116: * Creates or finds a main component of the producer for a document.
117: * @param document the docuemnt
118: * @return created or found non-null main component
119: */
120: protected DesignComponent createMainComponent(
121: DesignDocument document) {
122: return document.createComponent(getMainComponentTypeID());
123: }
124:
125: /**
126: * Post-initialize main component. You can also create secondary components and initialize them too.
127: * Default implementation returns a result with unchanged main component only.
128: * @param document the document
129: * @param mainComponent the main component usually created from getMainComponentTypeID method
130: * @return the result of creation
131: */
132: public Result postInitialize(DesignDocument document,
133: DesignComponent mainComponent) {
134: return new Result(mainComponent);
135: }
136:
137: /**
138: * Called for checking validity or availability of the producer for a specified document.
139: * Usually it check whether the main component is in registry and the class in target language is
140: * available on the class of a project where the document belongs.
141: *
142: * @param document the document where the producer could be used (and therefore checked against)
143: * @param useCachedValue use value from cache
144: * @return the result checking; true if the producer is valid, false is not valid and null if unresolved yet
145: */
146: public abstract Boolean checkValidity(DesignDocument document,
147: boolean useCachedValue);
148:
149: /**
150: * Represents the result of creation by the producer. Should be created by implementation of ComponentProducer.createComponent method.
151: */
152: public static final class Result {
153:
154: private DesignComponent mainComponent;
155: private List<DesignComponent> components;
156:
157: /**
158: * Creates a result with an array of components that are created by a producer.
159: * @param components the array of components; the first component is taken as the main component
160: */
161: public Result(DesignComponent... components) {
162: this .mainComponent = components.length > 0 ? components[0]
163: : null;
164: this .components = Arrays.asList(components);
165: assert !this .components.contains(null);
166: }
167:
168: /**
169: * Creates a result with a list of components that are created by a producer.
170: * @param mainComponent the main component
171: * @param components the list of non-main components
172: */
173: public Result(DesignComponent mainComponent,
174: List<DesignComponent> components) {
175: this .mainComponent = mainComponent;
176: this .components = components;
177: }
178:
179: /**
180: * Returns a main component.
181: * @return the main component
182: */
183: public DesignComponent getMainComponent() {
184: return mainComponent;
185: }
186:
187: /**
188: * Returns all components created by a producer.
189: * @return the list of all components
190: */
191: public List<DesignComponent> getComponents() {
192: return components;
193: }
194:
195: }
196:
197: static ComponentProducer createDefault(
198: ComponentDescriptor descriptor) {
199: PaletteDescriptor paletteDescriptor = descriptor
200: .getPaletteDescriptor();
201: TypeID typeid = descriptor.getTypeDescriptor().getThisType();
202: if (paletteDescriptor == null)
203: return null;
204:
205: return new ComponentProducer(typeid.toString(), typeid,
206: paletteDescriptor) {
207: public Boolean checkValidity(DesignDocument document,
208: boolean useCachedValue) {
209: return true;
210: }
211: };
212: }
213:
214: }
|