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.util.HashMap;
045: import java.util.StringTokenizer;
046: import java.util.Vector;
047:
048: import org.netbeans.modules.uml.core.configstringframework.ConfigStringHelper;
049: import org.netbeans.modules.uml.core.configstringframework.IConfigStringTranslator;
050: import org.netbeans.modules.uml.core.metamodel.structure.IProject;
051:
052: /**
053: * <p>Title: </p>
054: * <p>Description: </p>
055: * <p>Copyright: Copyright (c) 2003</p>
056: * <p>Company: </p>
057: * @author not attributable
058: * @version 1.0
059: */
060:
061: public class PropertyElement implements IPropertyElement {
062:
063: private String m_name = null;
064: private String m_value = null;
065: private String m_origValue = null;
066:
067: private IPropertyElement m_parent = null;
068: private IPropertyDefinition m_definition = null;
069: private IPropertyElementManager m_elementManager = null;
070: private Object m_Element = null;
071:
072: private boolean m_modified = false;
073: private boolean m_onDemand = false;
074:
075: //< IPropertyElement[] >
076: private Vector<IPropertyElement> m_subElements = new Vector<IPropertyElement>();
077: private HashMap<String, IPropertyElement> m_subElementsHashed = new HashMap<String, IPropertyElement>();
078:
079: public PropertyElement() {
080: }
081:
082: public String getName() {
083: return m_name;
084: }
085:
086: public void setName(String value) {
087: m_name = value;
088: }
089:
090: public String getValue() {
091: return m_value;
092: // String retVal = m_value;
093: //
094: // IPropertyDefinition def = getPropertyDefinition();
095: // String enumValues = def.getEnumValues();
096: // if(enumValues != null)
097: // {
098: // StringTokenizer tokenizer = new StringTokenizer(enumValues, "|");
099: // int tokens = tokenizer.countTokens();
100: // for(int index = 0; index < tokens; index++)
101: // {
102: // String curToken = tokenizer.nextToken();
103: // if(curToken.equals(m_value) == true)
104: // {
105: // retVal = Integer.toString(index);
106: // break;
107: // }
108: // }
109: // }
110: // return retVal;
111: }
112:
113: public void setValue(String value) {
114: // // Now make sure that the value is not an enumeration value.
115: // IPropertyDefinition def = getPropertyDefinition();
116: // String[] enumValues = def.getEnumValueList();
117: // if(enumValues != null)
118: // {
119: // // Make sure that we actually have a valid value.
120: // if((value != null) && (value.length() > 0))
121: // {
122: // try
123: // {
124: // int enumValueIndex = Integer.parseInt(value);
125: // if(enumValueIndex < enumValues.length)
126: // {
127: // m_value = enumValues[enumValueIndex];
128: // }
129: // }
130: // catch(NumberFormatException e)
131: // {
132: // // Just use the value that was passed into the method.
133: // }
134: // }
135: // }
136: // else
137: // {
138: m_value = value;
139: // }
140: }
141:
142: public Vector<IPropertyElement> getSubElements() {
143: return m_subElements;
144: }
145:
146: public HashMap<String, IPropertyElement> getHashedSubElements() {
147: return m_subElementsHashed;
148: }
149:
150: public void setSubElements(Vector<IPropertyElement> value) {
151: m_subElements.clear();
152: m_subElementsHashed.clear();
153: m_subElements = value;
154: if (value != null) {
155: int count = value.size();
156: for (int i = 0; i < count; i++) {
157: IPropertyElement ele = value.get(i);
158: m_subElementsHashed.put(ele.getName(), ele);
159: }
160: }
161: }
162:
163: public void addSubElement(IPropertyElement element) {
164: if (element != null) {
165: element.setParent(this );
166: m_subElements.addElement(element);
167: m_subElementsHashed.put(element.getName(), element);
168: }
169: }
170:
171: public IPropertyElement getSubElement(int index,
172: IPropertyElement element) {
173: int pos = m_subElements.indexOf(element, index);
174: if (pos >= 0)
175: return element;
176: return null;
177: }
178:
179: public IPropertyElement getSubElement(String name,
180: IPropertyElement element) {
181: IPropertyElement retEle = null;
182: if (name != null) {
183: retEle = m_subElementsHashed.get(name);
184: }
185: return retEle;
186: }
187:
188: public IPropertyDefinition getPropertyDefinition() {
189: return m_definition;
190: }
191:
192: public void setPropertyDefinition(IPropertyDefinition value) {
193: m_definition = value;
194: }
195:
196: public Object getElement() {
197: if (m_Element != null) {
198: return m_Element;
199: }
200:
201: return null;
202: }
203:
204: public void setElement(Object value) {
205: m_Element = value;
206: }
207:
208: public IPropertyElement getParent() {
209: return m_parent;
210: }
211:
212: public void setParent(IPropertyElement value) {
213: m_parent = value;
214: }
215:
216: public boolean getModified() {
217: return m_modified;
218: }
219:
220: public void setModified(boolean value) {
221: m_modified = value;
222: }
223:
224: public boolean getOnDemand() {
225: return m_onDemand;
226: }
227:
228: public void setOnDemand(boolean value) {
229: m_onDemand = value;
230: }
231:
232: public boolean save() {
233: boolean saved = false;
234: if (m_elementManager != null) {
235: Object modelElem = this .getElement();
236: if (modelElem == null) {
237: modelElem = m_elementManager.createData(modelElem,
238: m_definition, this );
239: m_elementManager.insertData(m_parent.getElement(),
240: m_definition, this );
241: }
242: m_elementManager.setData(modelElem, m_definition, this );
243: saved = true;
244: }
245: return saved;
246: }
247:
248: public String getOrigValue() {
249: return m_origValue;
250: }
251:
252: public void setOrigValue(String value) {
253: m_origValue = value;
254: }
255:
256: public void remove() {
257: if (m_elementManager != null) {
258: IPropertyElement parent = getParent();
259: if (parent != null)
260: m_elementManager.deleteData(getParent(), this );
261: }
262: }
263:
264: /**
265: * Builds a "|" delimited string of representing the path to this element. Gets each
266: * of its parent element names and appends to the string
267: *
268: * @param pVal[out] The path
269: *
270: * @return HRESULT
271: */
272: public String getPath() {
273: String returnStr = getName();
274: IPropertyElement parent = getParent();
275: if (parent != null) {
276: do {
277: String name = parent.getName();
278: name = name + "|" + returnStr;
279: returnStr = name;
280: parent = parent.getParent();
281: } while (parent != null);
282: }
283: return returnStr;
284: }
285:
286: public String getTranslatedValue() {
287: String transStr = null;
288: transStr = m_value;
289: //use ConfigStringHelper to translate.
290: IConfigStringTranslator translator = ConfigStringHelper
291: .instance().getTranslator();
292: if (translator != null) {
293: transStr = translator.translate(m_definition, m_value);
294: }
295: /* CComPtr < IConfigStringTranslator > pTranslator;
296: _VH(CConfigStringHelper::Instance()->GetTranslator(&pTranslator));
297: if (pTranslator)
298: {
299: CComBSTR outVal;
300: pTranslator->Translate(m_Definition, m_Value, &outVal);
301: outVal.CopyTo(pVal);
302: }
303: */
304: return transStr;
305: }
306:
307: public IPropertyElementManager getPropertyElementManager() {
308: return m_elementManager;
309: }
310:
311: public void setPropertyElementManager(IPropertyElementManager value) {
312: m_elementManager = value;
313: }
314:
315: public IProject getProject() {
316: IProject proj = null;
317: return proj;
318: }
319:
320: public String toString() {
321: if (m_definition != null) {
322: return m_definition.getPropertyEditorShowName();
323: }
324: return getName();
325: }
326: }
|