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: /*
043: * Created on Mar 29, 2005
044: *
045: * TODO To change the template for this generated file go to
046: * Window - Preferences - Java - Code Style - Code Templates
047: */
048: package org.netbeans.modules.uml.integration.ide;
049:
050: import java.util.ResourceBundle;
051: import java.util.StringTokenizer;
052:
053: //import org.netbeans.jmi.javamodel.Resource;
054: //import org.netbeans.modules.javacore.api.JavaModel;
055: import org.netbeans.modules.uml.common.Util;
056:
057: import org.netbeans.modules.uml.core.IApplication;
058: import org.netbeans.modules.uml.core.coreapplication.CoreProductManager;
059: import org.netbeans.modules.uml.core.coreapplication.ICoreProductManager;
060: import org.netbeans.modules.uml.core.coreapplication.IProductDescriptor;
061: import org.netbeans.modules.uml.core.metamodel.structure.IProject;
062: import org.netbeans.modules.uml.core.support.umlsupport.Log;
063: import org.netbeans.modules.uml.core.support.umlutils.ETList;
064: import org.netbeans.modules.uml.core.workspacemanagement.IWorkspace;
065: import org.netbeans.modules.uml.ui.products.ad.applicationcore.ADProduct;
066: import org.netbeans.modules.uml.ui.products.ad.applicationcore.IADProduct;
067: import org.netbeans.modules.uml.ui.support.ProductHelper;
068: import org.netbeans.modules.uml.ui.support.applicationmanager.IProduct;
069: import org.netbeans.modules.uml.integration.ide.events.EventHandler;
070: import org.netbeans.modules.uml.integration.ide.listeners.IAttributeChangeListener;
071: import org.netbeans.modules.uml.integration.ide.listeners.IClassChangeListener;
072: import org.netbeans.modules.uml.integration.ide.listeners.IEnumLiteralChangeListener;
073: import org.netbeans.modules.uml.integration.ide.listeners.IOperationChangeListener;
074: import org.netbeans.modules.uml.util.DummyCorePreference;
075: import org.openide.util.NbPreferences;
076:
077: /**
078: * @author alagud
079: *
080: * TODO To change the template for this generated type comment go to Window -
081: * Preferences - Java - Code Style - Code Templates
082: */
083: public class UMLSupport {
084:
085: private static UMLSupport umlsupport;
086: private static ProductProjectManager mProdManager;
087: private static IProject defaultProject = null;
088: // private static String projectSourceDirectory;
089: // private static String defaultProjectXMI = null;
090: // private static String defaultProjectLocation = null;
091: private static final String IMPLEMENTATION_MODE = "PSK_IMPLEMENTATION"; // NOI18N
092: private static boolean roundtripEnabled = true;
093: private IProduct product;
094: private EventHandler rtQueue = new EventHandler("RoundtripQueue"); // NOI18N
095: static ResourceBundle messages = ResourceBundle
096: .getBundle("org.netbeans.modules.uml.integration.ide.Bundle"); // NOI18N
097: protected IClassChangeListener classChangeListener;
098: protected IOperationChangeListener operationChangeListener;
099: protected IAttributeChangeListener attributeChangeListener;
100: protected IEnumLiteralChangeListener enumLiteralChangeListener;
101:
102: /** navigate to source on selecting class on diagram */
103: public static boolean navigateToSource = true;
104: private IIDEManager ideManager;
105: private DefaultSinkManager mSinkManager;
106: public static int SU_IDE_NONE;
107: public static final String PROJ_EXT = ".etd"; // NOI18N
108:
109: /**
110: * Singleton insatance of UMLSupport
111: */
112: public static UMLSupport getUMLSupport() {
113: if (umlsupport == null)
114: umlsupport = new UMLSupport();
115: return umlsupport;
116:
117: }
118:
119: // IZ 78480: conover
120: // I made this method static. Not sure why it wasn't before
121: // because mProdManager is a static member
122: public static ProductProjectManager getProjectManager() {
123: if (mProdManager == null)
124: mProdManager = new ProductProjectManager();
125:
126: return mProdManager;
127: }
128:
129: public static IProject getDefaultProject() {
130: return defaultProject;
131: }
132:
133: /**
134: * Determines whether the given Describe project is in the Implementation
135: * mode.
136: *
137: * @param proj The Describe project to be examined, possibly null.
138: * @return <code>true</code> if proj is non-null and in Implementation mode.
139: */
140: public static boolean isImplementationProject(IProject proj) {
141: return proj != null
142: && IMPLEMENTATION_MODE.equals(proj.getMode());
143: }
144:
145: /**
146: * Sets whether the given project is the 'default' project. The
147: * default project is usually the target for source-model roundtrip.
148: *
149: * @param proj The IProject that's the new default, possibly null.
150: */
151: public static void setDefaultProject(IProject proj) {
152: Log.out("In setDefaultProject() : " + proj); // NOI18N
153:
154: defaultProject = proj;
155:
156: // Remember the XMI ID. If the IProject reference gets clobbered, we'll
157: // need the XMI ID to identify the default project from the available
158: // projects.
159: if (defaultProject != null) {
160: getUMLSupport().getProjectManager().setCurrentProject(proj);
161: // defaultProjectXMI = defaultProject.getXMIID();
162: // defaultProjectLocation = defaultProject.getFileName();
163: // projectSourceDirectory = proj.getSourceDir();
164:
165: // The default project may be in Analysis mode, which is not what
166: // we want, so we force it to Implementation mode.
167: String existingMode = defaultProject.getMode();
168: Log.out("Project " + defaultProject.getName()
169: + " is in mode '" // NOI18N
170: + existingMode + "'"); // NOI18N
171: if (!isImplementationProject(defaultProject)) {
172: try {
173: Log.out("Forcing project "
174: + defaultProject.getName() // NOI18N
175: + " to " + IMPLEMENTATION_MODE + " mode"); // NOI18N
176: defaultProject.setMode(IMPLEMENTATION_MODE);
177: } catch (Exception ex) {
178: Log.stackTrace(ex);
179: }
180: }
181: if (!DefaultSinkManager.RT_LANGUAGE.equals(defaultProject
182: .getDefaultLanguage()))
183: defaultProject
184: .setDefaultLanguage(DefaultSinkManager.RT_LANGUAGE);
185: } else {
186: // defaultProjectXMI = null;
187: // defaultProjectLocation = null;
188: // projectSourceDirectory = null;
189: }
190:
191: // The default project can't be a library, since it's the target for
192: // source-to-model roundtrip activity.
193: if (defaultProject != null && defaultProject.getLibraryState())
194: defaultProject.setLibraryState(false);
195: }
196:
197: public static IProject getCurrentProject() {
198: // IZ 78480: conover
199: // I added the static getter because the static member was
200: // was null everytime.
201: return getProjectManager().getCurrentProject();
202: }
203:
204: /**
205: * Sets whether roundtrip events will be sent to the registered event
206: * sinks for model changes.
207: *
208: * @param enabled <code>true</code> to enable the roundtrip.
209: */
210: public static void setRoundTripEnabled(boolean enabled) {
211: if (enabled != roundtripEnabled) {
212: IProduct p = ProductHelper.getProduct();
213: p.getRoundTripController().setMode(enabled ? 1 : 0);
214:
215: roundtripEnabled = enabled;
216: }
217: }
218:
219: /**
220: * @return
221: */
222: public static boolean isRoundTripEnabled() {
223: return roundtripEnabled;
224: }
225:
226: /**
227: *
228: */
229: public IProduct getProduct1() {
230: if (product == null)
231: product = ProductHelper.getProduct();
232: return product;
233: }
234:
235: /**
236: * Returns the locate specific value for the String message.
237: * @param message
238: */
239: public static String getString(String message) {
240: try {
241: return messages.getString(message);
242: } catch (java.util.MissingResourceException mr) {
243: Log.out("Resource for " + message + " not found"); // NOI18N
244: return "!!" + message + "!!";
245: }
246: }
247:
248: public EventHandler getRoundtripQueue() {
249: return rtQueue;
250: }
251:
252: public String getCollectionOverride() {
253: //kris richards - made change to nbpreferences
254: return NbPreferences.forModule(DummyCorePreference.class).get(
255: "UML_COLLECTION_OVERRIDE_DEFAULT",
256: "java.util.ArrayList"); // NOI18N
257: }
258:
259: public IApplication getApplication() {
260: return ProductHelper.getApplication();
261:
262: }
263:
264: public static IADProduct getProduct() {
265: IADProduct retVal = null;
266:
267: ICoreProductManager productManager = CoreProductManager
268: .instance();
269: //ETList<IProductDescriptor> pDesc = productManager.getProducts();
270: ETList pDesc = productManager.getProducts();
271:
272: // Make sure that another project has not already created a product
273: if ((pDesc == null) || (pDesc.size() == 0)) {
274: // Create a new ADProduct
275: retVal = new ADProduct();
276: productManager.setCoreProduct(retVal);
277: } else {
278: IProductDescriptor descriptor = (IProductDescriptor) pDesc
279: .get(0);
280: if (descriptor.getCoreProduct() instanceof IADProduct) {
281: retVal = (IADProduct) descriptor.getCoreProduct();
282: }
283: }
284:
285: return retVal;
286: }
287:
288: /* NB60TBD
289: public static IProject getProjectForPath(String filename)
290: {
291: IProject retVal = null;
292: if(filename==null)
293: return retVal;
294: filename = filename.replace("%20"," ");
295: filename = NBFileUtils.normalizeFile(filename);
296: File test = new File(filename);
297: if(!test.exists())
298: {
299: while(test.getParentFile() !=null&& test.getParentFile().exists() )
300: { test = test.getParentFile();
301: FileObject fobj = FileUtil.toFileObject(test);
302: Log.out("Project Path is Null::::getProjectForPath"); // NOI18N
303: Project currentJavaProj =
304: FileOwnerQuery.getOwner(fobj);
305: Project uml = UMLJavaAssociationUtil.getAssociatedUMLProject(currentJavaProj);
306: UMLProjectHelper mHelper = null;
307: mHelper = (UMLProjectHelper) uml.getLookup().lookup(UMLProjectHelper.class);
308: retVal = mHelper.getProject();
309: return retVal;
310: }
311: }
312:
313: FileObject fobj = FileUtil.toFileObject(new File(filename));
314: if(fobj==null)
315: return null;
316: DataObject dObj = null;
317: try
318: {
319: dObj = DataObject.find(fobj);
320: }
321: catch (DataObjectNotFoundException e)
322: {
323: Log.stackTrace(e);
324: }
325: Project umlProject = UMLJavaAssociationUtil.getAssociatedUMLProject(dObj);
326: if( umlProject == null )
327: return null;
328: UMLProjectHelper mHelper = null;
329: mHelper = (UMLProjectHelper) umlProject.getLookup().lookup(UMLProjectHelper.class);
330: retVal = mHelper.getProject();
331:
332: return retVal;
333:
334: }
335: */
336: /* NB60TBD
337: public IProject getProjectForClassElement(ClassElement el)
338: {
339: String retVal = null;
340: IProject iproj = null;
341: DataObject dobj = (DataObject)el.getCookie(DataObject.class);
342: if(dobj!=null)
343: {
344: iproj = getProjectForPath(FileUtil.toFile(dobj.getPrimaryFile()).getAbsolutePath());
345: }
346: return iproj;
347: }
348: */
349: /* NB60TBD
350: public String getProjectName(ClassElement el)
351: {
352: String retVal = null;
353: // IProject iproj = null;
354: DataObject dobj = (DataObject)el.getCookie(DataObject.class);
355: if(dobj!=null)
356: {
357: // iproj = getProjectForPath(FileUtil.toFile(dobj.getPrimaryFile()).getAbsolutePath());
358: //
359: // if( iproj!=null )
360: // retVal = iproj.getName();
361: // else
362: // {
363: Project currentProj = FileOwnerQuery.getOwner(dobj.getPrimaryFile());
364:
365: if( currentProj!=null )
366: retVal = currentProj.getProjectDirectory().getName();
367: // }
368: }
369: return retVal;
370: }
371: */
372: /* NB60TBD
373: public static String getFileName(JavaClass clazz)
374: {
375: String retVal = null;
376: Resource resource = clazz.getResource();
377:
378: FileObject fObj = JavaModel.getFileObject(resource);
379: if (fObj != null)
380: {
381: retVal = fObj.getPath();
382: if (retVal != null && retVal.length() > 0)
383: retVal = NBFileUtils.normalizeFile(retVal);
384: }
385: return retVal;
386: }
387: */
388: /* NB60TBD
389: public static String getFileName(Resource resource)
390: {
391: String retVal = null;
392: FileObject fObj = JavaModel.getFileObject(resource);
393: if (fObj != null)
394: {
395: retVal = fObj.getPath();
396: if (retVal != null && retVal.length() > 0)
397: retVal = NBFileUtils.normalizeFile(retVal);
398: }
399: return retVal;
400: }
401: */
402: public static IWorkspace getCurrentWorkspace() {
403: return null;
404: }
405:
406: public static void setProjectForPath(String fileName,
407: String projName) {
408: }
409:
410: public static void reviveDescribe() {
411: // TODO Auto-generated method stub
412:
413: }
414:
415: /**
416: * Returns the IIDEManager for the current IDE. Note that this reference
417: * should not be cached, since the IDE might change it. Acquire the IDE
418: * manager from GDProSupport before each use and do NOT cache it.<br>
419: * If the IDE has not implemented this interface, a reference to the stub
420: * implementation IDEManagerAdapter is returned.
421: *
422: * @return IIDEManager The IDE's IIDEManager.
423: */
424: synchronized public IIDEManager getIDEManager() {
425: if (ideManager == null)
426: ideManager = new IDEManagerAdapter();
427: return ideManager;
428: }
429:
430: public void initializeSinkManager(DefaultSinkManager sm) {
431: mSinkManager = sm;
432: Log.out("About to boostrap Roundtrip"); // NOI18N
433: UMLSupport.getUMLSupport().getSinkManager()
434: .registerForProcessorEvents();
435: Log.out("registered for Processor events"); // NOI18N
436: startRoundtripThread();
437: }
438:
439: public DefaultSinkManager getSinkManager() {
440: return mSinkManager;
441: }
442:
443: public void closeProject(IProject proj) {
444: // TODO Auto-generated method stub
445:
446: }
447:
448: public static boolean isClobbered() {
449: // TODO Auto-generated method stub
450: return false;
451: }
452:
453: public boolean isConnected() {
454: // TODO Auto-generated method stub
455: return true;
456: }
457:
458: public void setAttributeChangeListener(
459: IAttributeChangeListener listener) {
460: RoundTripAttributeEventsSink
461: .removeAttributeChangeListener(attributeChangeListener);
462: attributeChangeListener = listener;
463: RoundTripAttributeEventsSink
464: .addAttributeChangeListener(listener);
465: }
466:
467: public void setClassChangeListener(IClassChangeListener listener) {
468: RoundTripClassEventsSink
469: .removeClassChangeListener(classChangeListener);
470: classChangeListener = listener;
471: RoundTripClassEventsSink.addClassChangeListener(listener);
472: }
473:
474: public void setEnumLiteralChangeListener(
475: IEnumLiteralChangeListener listener) {
476: RoundTripEnumLiteralEventsSink
477: .removeEnumLiteralChangeListener(enumLiteralChangeListener);
478: enumLiteralChangeListener = listener;
479: RoundTripEnumLiteralEventsSink
480: .addEnumLiteralChangeListener(listener);
481: }
482:
483: public void setOperationChangeListener(
484: IOperationChangeListener listener) {
485: RoundTripOperationEventsSink
486: .removeOperationChangeListener(operationChangeListener);
487: operationChangeListener = listener;
488: RoundTripOperationEventsSink
489: .addOperationChangeListener(listener);
490: }
491:
492: private void startRoundtripThread() {
493: // rtQueue.setProgressFactory(new IProgressIndicatorFactory() {
494: // public IProgressIndicator getProgressIndicator() {
495: // IIDEManager man = getIDEManager();
496: // return man != null? man.getProgressIndicator() : null;
497: // }
498: // });
499: rtQueue.startWorker();
500: }
501:
502: public static boolean getUseGenericsDefault() {
503: //kris richards - made change to nbpreferences
504: return NbPreferences.forModule(DummyCorePreference.class)
505: .getBoolean("UML_USE_GENERICS_DEFAULT", false); // NOI18N
506: }
507:
508: private static String extractNonCollectionType(String name) {
509: String retVal = name;
510: int genericStart = name.lastIndexOf('<');
511:
512: // see if this type has a generic type specified
513: if (genericStart < 0) {
514: // no '<' found so there is no generic type
515: // now is it a Collection type?
516: if (Util.isValidCollectionDataType(name)) {
517: // we have a Collection type with no Generic...
518: // so return Object as the type
519: return "java.lang.Object"; // NOI18N
520: }
521: }
522:
523: // so we must have a generic type specified
524: else {
525: retVal = name
526: .substring(genericStart + 1, name.indexOf('>'));
527: }
528:
529: return retVal;
530: }
531:
532: private static String convertGenericsToBrackets(String type) {
533: int genericStart = type.lastIndexOf('<');
534:
535: // see if this type has a generic type specified
536: if (genericStart < 0) {
537: // no '<' found so there is no generic type
538: // now is it a Collection type?
539: if (Util.isValidCollectionDataType(type)) {
540: // we have a Collection type with no Generic...
541: // so return only one set of array brackets
542: return "[]"; // NOI18N
543: }
544:
545: else
546: return ""; // NOI18N
547: }
548:
549: else {
550: StringTokenizer tokenizer = new StringTokenizer(type, "<"); // NOI18N
551: int dimensions = tokenizer.countTokens() - 1;
552: String brackets = ""; // NOI18N
553:
554: for (int i = 0; i < dimensions; i++) {
555: brackets += "[]"; // NOI18N
556: }
557:
558: return brackets;
559: }
560: }
561:
562: private static boolean isArray(String type) {
563: int arrayStart = type.lastIndexOf('[');
564: return arrayStart > -1 ? true : false;
565: }
566:
567: private static String getArrayType(String name) {
568: String retVal = name;
569: int index = name.indexOf('[');
570:
571: if (index > 0)
572: retVal = name.substring(0, index);
573:
574: return retVal;
575: }
576:
577: }
|