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.core.support.umlutils;
043:
044: import java.lang.reflect.InvocationTargetException;
045: import java.lang.reflect.Method;
046: import java.util.Collection;
047: import java.util.Enumeration;
048: import java.util.HashMap;
049: import java.util.Hashtable;
050: import java.util.Iterator;
051: import java.util.StringTokenizer;
052: import java.util.Vector;
053: import org.netbeans.modules.uml.core.configstringframework.ConfigStringHelper;
054: import org.netbeans.modules.uml.core.configstringframework.ConfigStringTranslator;
055: import org.netbeans.modules.uml.core.configstringframework.IConfigStringTranslator;
056: import org.netbeans.modules.uml.core.coreapplication.ICoreProduct;
057: import org.netbeans.modules.uml.core.coreapplication.IPreferenceManager2;
058: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
059: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamedElement;
060: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamespace;
061: import org.netbeans.modules.uml.core.metamodel.structure.IProject;
062: import org.netbeans.modules.uml.core.support.umlsupport.IStrings;
063: import org.netbeans.modules.uml.core.support.umlsupport.ProductRetriever;
064: import org.netbeans.modules.uml.core.support.umlsupport.Strings;
065: import org.netbeans.modules.uml.core.typemanagement.IPickListManager;
066: import org.netbeans.modules.uml.core.typemanagement.ITypeManager;
067: import org.openide.ErrorManager;
068: import org.openide.util.Exceptions;
069:
070: //import org.netbeans.modules.uml.core.metamodel.structure.IProject;
071: //import org.netbeans.modules.uml.core.support.umlsupport.IStrings;
072: /**
073: * <p>Title: </p>
074: * <p>Description: </p>
075: * <p>Copyright: Copyright (c) 2003</p>
076: * <p>Company: </p>
077: * @author not attributable
078: * @version 1.0
079: */
080:
081: public class PropertyDefinition implements IPropertyDefinition {
082:
083: private String m_ID = null;
084: private String m_name = null;
085: private String m_displayName = null;
086: private String m_PropertyEditorShowName = null;
087: private String m_controlType = null;
088: private String m_helpDescription = null;
089: private String m_validValues = null;
090: private String m_validValues2 = null;
091: private String m_defaultValue = null;
092: private String m_getMethod = null;
093: private String m_setMethod = null;
094: private String m_insertMethod = null;
095: private String m_deleteMethod = null;
096: private String m_validateMethod = null;
097: private String m_createMethod = null;
098: private String m_image = null;
099: private String m_progID = null;
100:
101: private boolean m_required = false;
102: private boolean m_defaultExist = false;
103: private boolean m_onDemand = true;
104: private boolean m_modified = false;
105:
106: private long m_multiplicity = 1;
107:
108: private boolean m_ForceRefresh = false;
109:
110: private Hashtable<String, String> m_map = new Hashtable<String, String>();
111:
112: //vector for IPropertyDefinition[]
113: private HashMap<String, IPropertyDefinition> m_subDefinitions = new HashMap<String, IPropertyDefinition>();
114: private Vector<IPropertyDefinition> m_VecSubDefs = new Vector<IPropertyDefinition>();
115:
116: private IPropertyDefinition m_parent = null;
117:
118: public PropertyDefinition() {
119: }
120:
121: public String getName() {
122: return m_name;
123: }
124:
125: public void setName(String name) {
126: m_name = name;
127: }
128:
129: public boolean isRequired() {
130: return m_required;
131: }
132:
133: public void setRequired(boolean val) {
134: m_required = val;
135: }
136:
137: public void setForceRefersh(boolean val) {
138: m_ForceRefresh = val;
139: }
140:
141: public boolean isForceRefresh() {
142: return m_ForceRefresh;
143: }
144:
145: public long getMultiplicity() {
146: return m_multiplicity;
147: }
148:
149: public void setMultiplicity(long val) {
150: m_multiplicity = val;
151: }
152:
153: public String getHelpDescription() {
154: // due to localization issues, we have now stored some help text as a resource strings
155: // because of this, we will need to translate from the "special" string to the text
156: // if the string is not "specially" marked, it will not be translated, just returned
157: IConfigStringTranslator translator = ConfigStringHelper
158: .instance().getTranslator();
159: String retHelp = m_helpDescription;
160: if (translator != null) {
161: retHelp = translator.translate(this , m_helpDescription);
162: }
163: return retHelp;
164: }
165:
166: public void setHelpDescription(String val) {
167: m_helpDescription = val;
168: }
169:
170: public Vector<IPropertyDefinition> getSubDefinitions() {
171: return m_VecSubDefs;
172: //return (new Vector<IPropertyDefinition>(m_subDefinitions.values()));
173: //return m_subDefinitions;
174: }
175:
176: public HashMap getHashedSubDefinitions() {
177: return m_subDefinitions;
178: //return m_subDefinitions;
179: }
180:
181: public void setSubDefinitions(Vector val) {
182: m_subDefinitions.clear();
183: m_VecSubDefs.clear();
184: m_VecSubDefs = val;
185: if (val != null) {
186: int count = val.size();
187: for (int i = 0; i < count; i++) {
188: IPropertyDefinition def = (IPropertyDefinition) val
189: .elementAt(i);
190: m_subDefinitions.put(def.getName(), def);
191: }
192: }
193: //m_subDefinitions = val;
194: }
195:
196: public void addSubDefinition(IPropertyDefinition def) {
197: if (def != null) {
198: //m_subDefinitions.addElement(def);
199: def.setParent(this );
200: m_subDefinitions.put(def.getName(), def);
201: m_VecSubDefs.add(def);
202: }
203:
204: }
205:
206: public IPropertyDefinition getSubDefinition(int index) {
207: IPropertyDefinition retDef = null;
208: if (index >= 0) {
209: Collection col = m_subDefinitions.values();
210: if (!col.isEmpty()) {
211: Iterator iter = col.iterator();
212: Object obj = iter.next();
213: int count = 0;
214: while (obj != null) {
215: count++;
216: if (count > index) {
217: break;
218: }
219: obj = iter.next();
220: }
221: if (obj != null && obj instanceof IPropertyDefinition) {
222: retDef = (IPropertyDefinition) obj;
223: }
224: }
225: }
226: return retDef;
227: }
228:
229: public IPropertyDefinition getSubDefinition(String name) {
230: if (name != null) {
231: IPropertyDefinition def = m_subDefinitions.get(name);
232: return def;
233: }
234: return null;
235: }
236:
237: public String getDisplayName() {
238: String retVal = "";
239:
240: ConfigStringHelper helper = ConfigStringHelper.instance();
241: IConfigStringTranslator translator = helper.getTranslator();
242:
243: if (translator != null) {
244: retVal = translator.translate(this , m_displayName);
245: }
246: return retVal;
247: }
248:
249: public void setDisplayName(String val) {
250: m_displayName = val;
251: }
252:
253: public void setPropertyEditorShowName(String val) {
254: m_PropertyEditorShowName = val;
255: }
256:
257: public String getPropertyEditorShowName() {
258: if (m_PropertyEditorShowName != null
259: && m_PropertyEditorShowName.length() > 0) {
260: return m_PropertyEditorShowName;
261: } else {
262: return getDisplayName();
263: }
264: }
265:
266: public IPropertyDefinition getParent() {
267: return m_parent;
268: }
269:
270: public void setParent(IPropertyDefinition def) {
271: m_parent = def;
272: }
273:
274: public String getControlType() {
275: return m_controlType;
276: }
277:
278: public void setControlType(String str) {
279: m_controlType = str;
280: }
281:
282: public String getGetMethod() {
283: return m_getMethod;
284: }
285:
286: public void setGetMethod(String val) {
287: m_getMethod = val;
288: }
289:
290: public String getSetMethod() {
291: return m_setMethod;
292: }
293:
294: public void setSetMethod(String val) {
295: m_setMethod = val;
296: }
297:
298: public String getID() {
299: return m_ID;
300: }
301:
302: public void setID(String val) {
303: m_ID = val;
304: }
305:
306: public String getInsertMethod() {
307: return m_insertMethod;
308: }
309:
310: public void setInsertMethod(String val) {
311: m_insertMethod = val;
312: }
313:
314: public String getDeleteMethod() {
315: return m_deleteMethod;
316: }
317:
318: public void setDeleteMethod(String val) {
319: m_deleteMethod = val;
320: }
321:
322: public String getValidValues() {
323: return m_validValues;
324: }
325:
326: public void setValidValues(String val) {
327: m_validValues = val;
328: }
329:
330: public String getValidValues2() {
331: return m_validValues2;
332: }
333:
334: public void setValidValues2(String val) {
335: m_validValues2 = val;
336: }
337:
338: private String m_EnumValues = null;
339:
340: /**
341: * Some list are actually enumeration list. The enum values are used to
342: * determine the correct value that should be set and retreived.
343: */
344: public void setEnumValues(String values) {
345: m_EnumValues = values;
346: }
347:
348: /**
349: * Some list are actually enumeration list. The enum values are used to
350: * determine the correct value that should be set and retreived.
351: *
352: * @return The string will be the list of enumeration values seperated
353: * by "|" characters.
354: */
355: public String getEnumValues() {
356: return m_EnumValues;
357: }
358:
359: /**
360: * Some list are actually enumeration list. The enum values are used to
361: * determine the correct value that should be set and retreived.
362: *
363: * @return The list of enumeration values.
364: */
365: public String[] getEnumValueList() {
366: String[] retVal = null;
367:
368: if (m_EnumValues != null) {
369: StringTokenizer tokenizer = new StringTokenizer(
370: m_EnumValues, "|");
371: int tokens = tokenizer.countTokens();
372: retVal = new String[tokens];
373: for (int index = 0; index < tokens; index++) {
374: retVal[index] = tokenizer.nextToken();
375: }
376: }
377: return retVal;
378: }
379:
380: public void addToAttrMap(String name, String value) {
381: if ((name != null) && (value != null))
382: m_map.put(name, value);
383: }
384:
385: public String getFromAttrMap(String name) {
386: String value = null;
387: if (name != null) {
388: value = m_map.get(name);
389: }
390: return value;
391: }
392:
393: public boolean isOnDemand() {
394: return m_onDemand;
395: }
396:
397: public void setOnDemand(boolean val) {
398: m_onDemand = val;
399: }
400:
401: public String getCreateMethod() {
402: return m_createMethod;
403: }
404:
405: public void setCreateMethod(String val) {
406: m_createMethod = val;
407: }
408:
409: public String getImage() {
410: return m_image;
411: }
412:
413: public void setImage(String val) {
414: m_image = val;
415: }
416:
417: public String getProgID() {
418: return m_progID;
419: }
420:
421: public void setProgID(String val) {
422: m_progID = val;
423: }
424:
425: public void save() {
426: }
427:
428: public boolean isModified() {
429: return m_modified;
430: }
431:
432: public void setModified(boolean val) {
433: m_modified = val;
434: }
435:
436: public void remove() {
437: }
438:
439: /**
440: * Builds a "|" delimited string of representing the path to this definition. Gets each
441: * of its parent definition names and appends to the string
442: *
443: * @param pVal[out] The path
444: *
445: * @return HRESULT
446: */
447: public String getPath() {
448: String returnStr = getName();
449: IPropertyDefinition parent = getParent();
450: if (parent != null) {
451: do {
452: String name = parent.getName();
453: name = name + "|" + returnStr;
454: returnStr = name;
455: parent = parent.getParent();
456: } while (parent != null);
457: }
458: return returnStr;
459: }
460:
461: public String getDefaultValue() {
462: return m_defaultValue;
463: }
464:
465: public void setDefaultValue(String val) {
466: m_defaultValue = val;
467: }
468:
469: public boolean isDefaultExisting() {
470: return m_defaultExist;
471: }
472:
473: public void setDefaultExists(boolean val) {
474: m_defaultExist = val;
475: }
476:
477: public String getValidateMethod() {
478: return m_validateMethod;
479: }
480:
481: public void setValidateMethod(String val) {
482: m_validateMethod = val;
483: }
484:
485: public long getAttrMapCount() {
486: return m_map.size();
487: }
488:
489: /**
490: * Retrieves a particular xml attribute and value from the
491: * already built map based on its position
492: *
493: * @param pos - the position of the attribute
494: * @param name - the name in the position
495: * @param value - the value in the position
496: */
497: public void getFromAttrMap(long pos, String name, String value) {
498: if ((name != null) && (pos >= 0)) {
499: Enumeration enumVal = m_map.keys();
500: int i = 0;
501: while (enumVal.hasMoreElements()) {
502: String str = (String) enumVal.nextElement();
503: if (str.equalsIgnoreCase(name) && (i == pos)) {
504: value = (String) m_map.get(name);
505: }
506: i++;
507: }
508: }
509: }
510:
511: /**
512: * Some of the picklists used by the property editor are common and are built
513: * from xpath queries. This routine executes the xpath query and then gets the
514: * name from each element and stores it in a list buffer.
515: *
516: *
517: * @param pValues[out, retval]
518: *
519: * @return HRESULT
520: */
521: /**
522: * Gets a string that represents the data to be inserted into the
523: * control for this definition.
524: *
525: * @param pVal[out]
526: */
527: public IStrings getValidValue(IPropertyElement elem) {
528: IStrings retVal = null;
529: String str = getValidValues();
530: if (str != null && str.indexOf("|") >= 0) {
531: StringTokenizer tokenizer = new StringTokenizer(str, "|");
532: while (tokenizer.hasMoreTokens()) {
533: String token = tokenizer.nextToken();
534:
535: //should translate this value if needed
536: IConfigStringTranslator trans = ConfigStringHelper
537: .instance().getTranslator();
538: token = trans.translate(elem.getPropertyDefinition(),
539: token);
540: if (retVal == null) {
541: retVal = new Strings();
542: }
543: retVal.add(token);
544: }
545: // String[] strs = str.split("|");
546: // if (strs != null && strs.length > 0) {
547: // for (int i = 0; i < strs.length; i++) {
548: // String addStr = strs[i];
549: // retVal.add(addStr);
550: // }
551: // }
552: } else if (str != null && str.indexOf("#DataTypeList") >= 0) {
553: IProject proj = getProject(elem);
554: if (proj != null) {
555: //try to get the picklist
556: ITypeManager pTypeMgr = proj.getTypeManager();
557: if (pTypeMgr != null) {
558: IPickListManager pPickMgr = pTypeMgr
559: .getPickListManager();
560: if (pPickMgr != null) {
561: retVal = new Strings();
562: String filter = parseDataTypeList();
563: StringTokenizer tokenizer = new StringTokenizer(
564: filter, " ");
565: while (tokenizer.hasMoreTokens()) {
566: IStrings list;
567: String token = tokenizer.nextToken().trim();
568: if (token.equals("ParameterableElement")) {
569: INamespace space = null;
570: Object mobj = getModelElement(elem);
571: if (mobj instanceof INamespace) {
572: space = (INamespace) mobj;
573: } else if (mobj instanceof INamedElement) {
574: space = ((INamedElement) mobj)
575: .getNamespace();
576: }
577: list = (pPickMgr
578: .getTypeNamesWithStringFilterNamespaceVisible(
579: token, false, space));
580: } else {
581: if (isFullyQualified()) {
582: list = pPickMgr
583: .getFullyQualifiedTypeNamesWithStringFilter(token);
584: } else {
585: list = pPickMgr
586: .getTypeNamesWithStringFilter(token);
587: }
588: }
589: retVal.append(list);
590: }
591: }
592: }
593: }
594: } else if (str != null && str.startsWith("#Call") == true) {
595: try {
596: int end = str.lastIndexOf(')');
597: String call = str.substring(6, end);
598:
599: // When in a property editor that is creating a new model element
600: // element my be null. So, in this situation the call has to be
601: // made on the parent.
602: Object element = getModelElement(elem);
603:
604: Method m = element.getClass().getMethod(call);
605: if (m != null) {
606: Object returnValue = m.invoke(element);
607: if (returnValue instanceof String) {
608: String strValue = (String) returnValue;
609: StringTokenizer tokenizer = new StringTokenizer(
610: strValue, "|");
611: while (tokenizer.hasMoreTokens()) {
612: String token = tokenizer.nextToken();
613:
614: //should translate this value if needed
615: IConfigStringTranslator trans = ConfigStringHelper
616: .instance().getTranslator();
617: token = trans.translate(elem
618: .getPropertyDefinition(), token);
619: if (retVal == null) {
620: retVal = new Strings();
621: }
622: retVal.add(token);
623: }
624: } else if (returnValue instanceof Collection) {
625: for (Object curValue : (Collection) returnValue) {
626: if (retVal == null) {
627: retVal = new Strings();
628: }
629:
630: retVal.add(curValue.toString());
631: }
632: }
633: }
634: } catch (IllegalAccessException ex) {
635: ErrorManager.getDefault().notify(ex);
636: } catch (IllegalArgumentException ex) {
637: ErrorManager.getDefault().notify(ex);
638: } catch (InvocationTargetException ex) {
639: ErrorManager.getDefault().notify(ex);
640: } catch (NoSuchMethodException ex) {
641: ErrorManager.getDefault().notify(ex);
642: } catch (SecurityException ex) {
643: ErrorManager.getDefault().notify(ex);
644: }
645:
646: }
647:
648: return retVal;
649: }
650:
651: /**
652: * Method to navigate up the property element chain to retrieve the first model element in the chain
653: *
654: * @param[in] pEle The property element in which to get the model element
655: * @param[out] pModEle The model element
656: *
657: * @return HRESULT
658: *
659: */
660: private Object getModelElement(IPropertyElement pEle) {
661: Object obj = pEle.getElement();
662: if (obj == null) {
663: IPropertyElement elem = pEle.getParent();
664: if (elem != null) {
665: obj = getModelElement(elem);
666: }
667: }
668: return obj;
669: }
670:
671: /**
672: *
673: * Determines whether or not pick lists should be shown using fully qualified names
674: *
675: * @return HRESULT
676: *
677: */
678: private boolean isFullyQualified() {
679: //kris richards - "DisplayTypeFSN" pref removed. Set to true
680: return true;
681: }
682:
683: /**
684: * @return
685: */
686: private String parseDataTypeList() {
687: String retStr = "";
688:
689: String values = getValidValues();
690: if (values != null && values.length() > 0) {
691: // has it been tagged as an xpath query
692: if (values.indexOf("#DataTypeList") >= 0) {
693: int pos = values.indexOf("(");
694: if (pos >= 0) {
695: // get the string between the ( )'s, this is what needs to be passed back
696: retStr = values.substring(pos + 1,
697: values.length() - 1);
698: } else {
699: retStr = "DataType Class Interface Enumeration ParameterableElement";
700: }
701: }
702: }
703: return retStr;
704: }
705:
706: protected IProject getProject(IPropertyElement elem) {
707: IProject proj = null;
708: IPropertyElement elem1 = elem.getParent();
709: if (elem1 != null) {
710: Object obj = elem.getElement();
711: if (obj == null) {
712: obj = elem1.getElement();
713: }
714:
715: if (obj == null) {
716: proj = getProject(elem1);
717: } else {
718: if (obj instanceof IElement) {
719: IElement pElement = (IElement) obj;
720: Object proj1 = pElement.getProject();
721: if (proj1 == null)
722: proj = getProject(elem1);
723: else if (proj1 instanceof IProject) {
724: proj = (IProject) proj1;
725: }
726: // else if (Dispatch.isType(proj1, IProject.GUID))
727: // {
728: // proj = (IProject)(new IProjectProxy((Dispatch)proj1));
729: // }
730: } else {
731: proj = getProject(elem1);
732: }
733: }
734: }
735: return proj;
736: }
737:
738: }
|