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-2007 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.uml.ui.support.applicationmanager;
043:
044: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
045: import org.netbeans.modules.uml.common.ETException;
046: import org.netbeans.modules.uml.common.generics.ETPairT;
047: import org.netbeans.modules.uml.core.metamodel.core.foundation.FactoryRetriever;
048: import org.netbeans.modules.uml.core.metamodel.core.foundation.ICreationFactory;
049: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
050: import org.netbeans.modules.uml.core.support.umlutils.ETList;
051: import org.netbeans.modules.uml.ui.products.ad.drawengines.ETDrawEngineFactory;
052: import org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI;
053: import org.netbeans.modules.uml.ui.support.CreationFactoryHelper;
054: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchive;
055: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchiveDefinitions;
056: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchiveElement;
057: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ICompartment;
058: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine;
059: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IEventManager;
060: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ILabelManager;
061: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ISimpleListCompartment;
062:
063: public class DrawingFactory implements IDrawingFactory {
064: private static final String PACKAGE_NAME = "org.netbeans.modules.uml.ui.support.applicationmanager.";
065: protected static ETList<ETPairT<ICompartment, IProductArchiveElement>> m_CompartmentResourcePairs = new ETArrayList<ETPairT<ICompartment, IProductArchiveElement>>();
066:
067: public DrawingFactory() {
068:
069: }
070:
071: public IGraphPresentation createPresentation(IDrawEngine drawEngine) {
072: return createPresentationObj(drawEngine);
073: }
074:
075: public static IGraphPresentation createPresentationObj(
076: IDrawEngine drawEngine) {
077:
078: IGraphPresentation pe = null;
079: if (drawEngine != null && drawEngine.getUI() != null) {
080: String typeName = getPresentationClassName(drawEngine);
081:
082: if (typeName != null) {
083: // try{
084: // pe = (IGraphPresentation) Class.forName(PACKAGE_NAME + className).newInstance();
085: //
086: // if (pe != null) {
087: // pe.setUI(drawEngine.getUI());
088: // drawEngine.getUI().getModelElement().addPresentationElement(pe);
089: // }
090: // }
091: // catch(Exception e){
092: // e.printStackTrace();
093: // }
094:
095: ICreationFactory factory = FactoryRetriever.instance()
096: .getCreationFactory();
097: if (factory != null) {
098: Object presentationObj = factory.retrieveMetaType(
099: typeName, "");
100: if (presentationObj instanceof IGraphPresentation) {
101: pe = (IGraphPresentation) presentationObj;
102:
103: IETGraphObjectUI engine = drawEngine.getUI();
104: if (engine != null) {
105: engine.getModelElement()
106: .addPresentationElement(pe);
107: }
108: }
109: }
110: }
111: }
112: return pe;
113: }
114:
115: /**
116: * Retrieve the type that is our node presentation element
117: *
118: * @param pCreatedPE [out,retval] The created node presentation element
119: */
120: public static INodePresentation retrieveNodePresentationMetaType() {
121: INodePresentation retObj = null;
122: ICreationFactory factory = FactoryRetriever.instance()
123: .getCreationFactory();
124: if (factory != null) {
125: Object presentationObj = factory.retrieveMetaType(
126: "NodePresentation", null);
127: if (presentationObj instanceof INodePresentation) {
128: retObj = (INodePresentation) presentationObj;
129: }
130: }
131: return retObj;
132: }
133:
134: /**
135: * Retrieve the type that is our label presentation element
136: *
137: * @param pCreatedPE [out,retval] The created label presentation element
138: */
139: public static ILabelPresentation retrieveLabelPresentationMetaType() {
140: ILabelPresentation retObj = null;
141: ICreationFactory factory = FactoryRetriever.instance()
142: .getCreationFactory();
143: if (factory != null) {
144: Object presentationObj = factory.retrieveMetaType(
145: "LabelPresentation", null);
146: if (presentationObj instanceof ILabelPresentation) {
147: retObj = (ILabelPresentation) presentationObj;
148: }
149: }
150: return retObj;
151: }
152:
153: /**
154: * Retrieve the type that is our edge presentation element
155: *
156: * @param typeName [in] The type of edge presentation element we should create
157: * @param pCreatedPE [out,retval] The created node presentation element
158: */
159: public static IEdgePresentation retrieveEdgePresentationMetaType(
160: String typeName) {
161: IEdgePresentation retObj = null;
162: ICreationFactory factory = FactoryRetriever.instance()
163: .getCreationFactory();
164: if (factory != null) {
165: Object presentationObj = factory.retrieveMetaType(typeName,
166: null);
167: if (presentationObj instanceof IEdgePresentation) {
168: retObj = (IEdgePresentation) presentationObj;
169: }
170: }
171: return retObj;
172: }
173:
174: protected static String getPresentationClassName(
175: IDrawEngine drawEngine) {
176: return drawEngine != null ? drawEngine.getPresentationType()
177: : null;
178: }
179:
180: public static IDrawEngine createDrawEngine(IETGraphObjectUI ui,
181: IProductArchive prodArch, IProductArchiveElement archEle) {
182: IDrawEngine retObj = null;
183: try {
184: retObj = ETDrawEngineFactory.createDrawEngine(ui);
185: if (retObj != null) {
186: //Need to uncomment this once I figure out why nodes are not loaded
187: //in proper order when I do the C++ way.
188: retObj.readFromArchive(prodArch, archEle);
189: }
190: } catch (ETException e) {
191: e.printStackTrace();
192: }
193: return retObj;
194: }
195:
196: /**
197: * Creates a compartment based on reading in from the archive. The compartment name is used
198: * as a lookup into EssentialConfig file that will resolve to a progid and cocreate the correct
199: * compartment.
200: *
201: * @param pParentDrawEngine [in] The DrawEngine these new compartments are a part of.
202: * @param pProductArchive [in] The archive file (etlp) that contains the drawing information.
203: * @param pElement [in] The toplevel element for a compartment.
204: */
205: public static void createCompartments(
206: IDrawEngine pParentDrawEngine,
207: IProductArchive pProductArchive,
208: IProductArchiveElement pElement) {
209: if (pParentDrawEngine != null) {
210: if (pElement != null) {
211: IProductArchiveElement[] subElems = pElement
212: .getElements();
213: if (subElems != null) {
214: for (int i = 0; i < subElems.length; i++) {
215: IProductArchiveElement elem = subElems[i];
216: ETPairT<IProductArchiveElement, String> result = pProductArchive
217: .getTableEntry(
218: elem,
219: IProductArchiveDefinitions.COMPARTMENTNAMETABLEINDEXATTRIBUTE_STRING,
220: IProductArchiveDefinitions.COMPARTMENTNAMETABLE_STRING);
221: String foundElemId = null;
222: if (result != null) {
223: foundElemId = result.getParamTwo();
224: }
225: if (foundElemId != null
226: && foundElemId.length() > 0) {
227: // Create the compartment based on the name in the archive
228: ICompartment newComp = CreationFactoryHelper
229: .createCompartment(foundElemId);
230: if (newComp != null) {
231: // Add this compartment to the engine.
232: //newComp.setEngine(pParentDrawEngine);
233: //pParentDrawEngine.addCompartment(newComp, 0);
234: pParentDrawEngine.addCompartment(
235: newComp, -1);
236: newComp.readFromArchive(
237: pProductArchive, elem);
238: }
239: }
240: }
241: }
242: }
243: }
244: }
245:
246: /**
247: * Creates a group of compartments
248: */
249: public static void createCompartments(
250: ISimpleListCompartment pParentCompartment,
251: IProductArchive pProductArchive,
252: IProductArchiveElement[] pElements) {
253: if (pParentCompartment != null && pProductArchive != null
254: && pElements != null) {
255: int count = pElements.length;
256: for (int i = 0; i < count; i++) {
257: IProductArchiveElement pEle = pElements[i];
258: ETPairT<IProductArchiveElement, String> result = pProductArchive
259: .getTableEntry(
260: pEle,
261: IProductArchiveDefinitions.COMPARTMENTNAMETABLEINDEXATTRIBUTE_STRING,
262: IProductArchiveDefinitions.COMPARTMENTNAMETABLE_STRING);
263: if (result != null) {
264: String foundId = result.getParamTwo();
265:
266: if (foundId != null && foundId.length() > 0) {
267: // Create the compartment based on the name in the archive
268: ICompartment newComp = CreationFactoryHelper
269: .createCompartment(foundId);
270: if (newComp != null) {
271: // Add this compartment to the engine. Do before the ReadFromArchive
272: // because the ReadFromArchive requires the draw engine which is set in
273: // AddCompartment
274: pParentCompartment.addCompartment(newComp,
275: -1, false);
276: newComp.readFromArchive(pProductArchive,
277: pEle);
278: }
279: }
280: }
281: }
282: }
283: }
284:
285: /**
286: * Adds a compartment resource pair
287: */
288: public static void addCompartmentResourcePair(
289: ICompartment pCompartment, IProductArchiveElement pElement) {
290: if (pCompartment != null && pElement != null) {
291: m_CompartmentResourcePairs
292: .add(new ETPairT<ICompartment, IProductArchiveElement>(
293: pCompartment, pElement));
294: }
295: }
296:
297: /**
298: * Reads the resources for all comparments
299: */
300: public static void readCompartmentResourcesFromArchive(
301: IProductArchive pProductArchive) {
302: if (pProductArchive != null) {
303: int count = m_CompartmentResourcePairs.size();
304:
305: for (int i = 0; i < count; i++) {
306: ETPairT<ICompartment, IProductArchiveElement> val = m_CompartmentResourcePairs
307: .get(i);
308: ICompartment pCompartment = val.getParamOne();
309: IProductArchiveElement pProductArchiveElement = val
310: .getParamTwo();
311: if (pCompartment != null
312: && pProductArchiveElement != null) {
313: pCompartment.getCompartmentResourceUser()
314: .readResourcesFromArchive(pProductArchive,
315: pProductArchiveElement);
316: }
317: }
318: }
319: m_CompartmentResourcePairs.clear();
320: }
321:
322: /**
323: * Retrieve the type that is our element
324: *
325: * @param typeName [in] The type of element we should create
326: * @param pCreatedElement [out,retval] The created element
327: */
328: public static IElement retrieveModelElement(String typeName) {
329: IElement retObj = null;
330: ICreationFactory factory = FactoryRetriever.instance()
331: .getCreationFactory();
332: if (factory != null) {
333: Object obj = factory.retrieveMetaType(typeName, null);
334: if (obj instanceof IElement) {
335: retObj = (IElement) obj;
336: }
337: }
338: return retObj;
339: }
340:
341: /**
342: * Retrieve the type specified by the type name, e.g. Class. The type has been fully prepared and initialize
343: */
344: public static Object retrieveMetaType(String typeName) {
345: Object retObj = null;
346: ICreationFactory factory = FactoryRetriever.instance()
347: .getCreationFactory();
348: if (factory != null) {
349: retObj = factory.retrieveMetaType(typeName, null);
350: }
351: return retObj;
352: }
353:
354: /**
355: * @param eventManagerType
356: * @return
357: */
358: public static IEventManager retrieveEventManager(
359: String eventManagerType) {
360: IEventManager retObj = null;
361: ICreationFactory factory = FactoryRetriever.instance()
362: .getCreationFactory();
363: if (factory != null) {
364: Object obj = factory.retrieveEmptyMetaType("Managers",
365: eventManagerType, null);
366: if (obj instanceof IEventManager) {
367: retObj = (IEventManager) obj;
368: }
369: }
370: return retObj;
371: }
372:
373: public static ILabelManager retrieveLabelManager(
374: String labelManagerType) {
375: ILabelManager retObj = null;
376: ICreationFactory factory = FactoryRetriever.instance()
377: .getCreationFactory();
378: if (factory != null) {
379: Object obj = factory.retrieveEmptyMetaType("Managers",
380: labelManagerType, null);
381: if (obj instanceof ILabelManager) {
382: retObj = (ILabelManager) obj;
383: }
384: }
385: return retObj;
386: }
387:
388: /**
389: * Retrieve the type that is our compartment
390: *
391: * @param typeName [in] The type of element we should create
392: * @param pCreatedManager [out,retval] The created manager
393: */
394: public static ICompartment retrieveCompartment(String typeName) {
395: ICompartment retObj = null;
396: Object obj = CreationFactoryHelper.retrieveMetaType(
397: "Compartments", typeName);
398: if (obj instanceof ICompartment) {
399: retObj = (ICompartment) obj;
400: }
401: return retObj;
402: }
403: }
|