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: * File : ElementInfo.java
044: * Version : 1.4
045: * Description : Base class for the source info classes.
046: * Author : Trey Spiva
047: */
048: package org.netbeans.modules.uml.integration.ide.events;
049:
050: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamedElement;
051: import org.netbeans.modules.uml.core.metamodel.core.foundation.ITaggedValue;
052: import org.netbeans.modules.uml.core.metamodel.structure.IProject;
053:
054: /**
055: * ElementInfo is the base class of all Source Element information class.
056: * ElementInfo provides the functionality to store the common data needed
057: * by all source code elements.
058: *
059: * Revision History
060: * No. Date Who What
061: * --- ---- --- ----
062: * 1 2002-05-06 Darshan Added constructor to take an INamedElement
063: * for model-source work.
064: */
065: public abstract class ElementInfo implements Cloneable {
066: /** Specifes that the element is a to be created. */
067: public final static int CREATE = 0;
068:
069: /** Specifies that the element is modifing an existing element. */
070: public final static int MODIFY = 1;
071:
072: /** Specifies that the element is deleting an existing element. */
073: public final static int DELETE = 2;
074:
075: private String mName = null;
076: private String mNewName = null;
077: protected Integer mModifiers = null;
078: private int mChangeType = CREATE;
079: private String mComment = null;
080:
081: private boolean isCommentSet = false;
082:
083: /**
084: * Contructs a new ElementInfo and specifies the type of change that is to
085: * occur.
086: * @param type The change that that occured (CREATE, MODIFY, DELETE).
087: */
088: public ElementInfo(int type) {
089: setChangeType(type);
090: }
091:
092: public IProject getProject() {
093: return null;
094: }
095:
096: public String getFilename() {
097: return null;
098: }
099:
100: private INamedElement element;
101:
102: public ElementInfo(INamedElement el) {
103: setChangeType(MODIFY);
104: if (el != null)
105: setComment(el.getDocumentation());
106: element = el;
107: }
108:
109: /**
110: * Updates Describe of the model change. Each decendent will have to implement
111: * <b>update</b> to send the correct messages to Describe. The currently active
112: * Describe system will be used. <b>Use the EventManager to send messages to
113: * Describe.</b>
114: * @see GDProSupport#getCurrentSystem()
115: */
116: abstract public void update();
117:
118: /**
119: * Returns the Describe IProject that owns the element described by this
120: * ElementInfo. The IProject is only returned if this ElementInfo has an
121: * associated Describe IElement.
122: *
123: * @return The owning IProject, or null if this information is unavailable.
124: */
125: public IProject getOwningProject() {
126: return null;
127: }
128:
129: /**
130: * Sets the name of the model element. This is the original name of the element.
131: * When used with setNewName before and after information is provided.
132: * @param value The name of the element.
133: * @see #setNewName(String value)
134: */
135: public void setName(String value) {
136: mName = value;
137: }
138:
139: /**
140: * Retrieves the name of the model element. This is the original name of the element.
141: * When used with getNewName before and after information is provided.
142: * @return The name of the element.
143: * @see #getNewName()
144: */
145: public String getName() {
146: return mName;
147: }
148:
149: /**
150: * Sets the new name of the model element. This is the name of the element after
151: * a name changed has occured. When used with setName before and after
152: * information is provided.
153: * @param value The name of the element.
154: * @see #setName ( String value )
155: */
156: public void setNewName(String value) {
157: mNewName = value;
158: }
159:
160: /**
161: * Retrieves the new name of the model element. This is the name of the element after
162: * a name changed has occured. When used with getName before and after
163: * information is provided.
164: * @return The name of the element.
165: * @see #getName()
166: */
167: public String getNewName() {
168: return mNewName;
169: }
170:
171: /**
172: * Sets the elements modifiers.
173: * @param mod - disjunction of constants from
174: * <A HREF="http://java.sun.com/products/jdk/1.2/docs/api/java/lang/reflect/Modifier.html"><CODE>Modifier</CODE></A>.
175: */
176: public void setModifiers(Integer mod) {
177: mModifiers = mod;
178: }
179:
180: /**
181: * Gets the elements modifiers.
182: * @return disjunction of constants from
183: * <A HREF="http://java.sun.com/products/jdk/1.2/docs/api/java/lang/reflect/Modifier.html"><CODE>Modifier</CODE></A>.
184: */
185: public Integer getModifiers() {
186: return mModifiers;
187: }
188:
189: /**
190: * Sets the type of change that has occured.
191: * @param value CREATE, MODIFY, or DELETE
192: * @sse #CREATE
193: * @see #MODIFY
194: * @see #DELETE
195: */
196: public void setChangeType(int value) {
197: mChangeType = value;
198: }
199:
200: /**
201: * Retrieves the type of change that has occured.
202: * @see #CREATE
203: * @see #MODIFY
204: * @see #DELETE
205: *
206: * @return <code>CREATE, MODIFY,</code> or <code>DELETE</code>
207: */
208: public int getChangeType() {
209: return mChangeType;
210: }
211:
212: /**
213: * Retrieve a textual representation of the type of change that occured.
214: * <B>Use for Debuging.</B>
215: * @return The name of the change.
216: */
217: public String getChangeName() {
218: String retVal = "";
219:
220: switch (getChangeType()) {
221: case CREATE:
222: retVal = "Create";
223: break;
224: case MODIFY:
225: retVal = "Modify";
226: break;
227: case DELETE:
228: retVal = "Delete";
229: break;
230: }
231:
232: return retVal;
233: }
234:
235: /**
236: * Sets the Comment that appears before the element.
237: * @param value The comment.
238: */
239: public void setComment(String value) {
240:
241: // I am having to trim the comment to remove white space from the
242: // beginning and the end of the string. I am having to do this because
243: // When calling getJavadocText() on a Java model element is returing
244: // a couple of additional newlines. I think the java models javadoc
245: // logic just removes the '*' characters, but keeps the '\n' characters.
246: // which is fine if the there are actual comments on the line, but if
247: // it is white space the it is not what you want.
248: if (value == null)
249: return;
250:
251: mComment = value.trim();
252: isCommentSet = true;
253: }
254:
255: /**
256: * Returns <code>true</code> if a valid comment is set on this object.
257: * @return <code>true</code> if a valid comment is set.
258: */
259: public boolean isCommentSet() {
260: return /*mComment != null*/isCommentSet;
261: }
262:
263: /**
264: * produces marker id
265: */
266: public String getMarkerID() {
267: String markerID = null;
268: ITaggedValue tag = element.getTaggedValueByName("MarkerID");
269: if (tag != null) {
270: markerID = tag.getDataValue();
271: }
272: if (markerID == null || markerID.trim().equals("")) {
273: markerID = element.getXMIID();
274: }
275: return markerID;
276: }
277:
278: /**
279: * Retrieves the Comment that appears before the element.
280: * @return The comment.
281: */
282: public String getComment() {
283:
284: if (mComment != null && mComment.trim().length() == 0)
285: mComment = null;
286: return mComment;
287: }
288:
289: /**
290: * Determines if the given name is a valid Java identifier.
291: *
292: * @param name The name to be tested for identifier-hood :-)
293: * @return <code>true</code> if the name is an identifier.
294: */
295: public static boolean isIdentifier(String name) {
296: if (name == null || (name = name.trim()).length() == 0)
297: return false;
298:
299: if (!Character.isJavaIdentifierStart(name.charAt(0)))
300: return false;
301: for (int i = 1; i < name.length(); ++i) {
302: if (!Character.isJavaIdentifierPart(name.charAt(i)))
303: return false;
304: }
305: return true;
306: }
307:
308: protected void assertIsIdentifier(String name)
309: throws InvalidIdentifierException {
310: if (!isIdentifier(name)) {
311: throw new InvalidIdentifierException("'" + name
312: + "' is not a valid Java identifier");
313: }
314: }
315:
316: public Object clone() {
317: try {
318: return super .clone();
319: } catch (CloneNotSupportedException ignored) {
320: }
321: return null;
322: }
323:
324: /**
325: * Returns a code unique to each subclass of ElementInfo.
326: *
327: * @return A short <code>String</code> unique among the subclasses of
328: * <code>ElementInfo</code>.
329: */
330: abstract public String getCode();
331:
332: /**
333: * Transfers the contents of relevant 'newXyz' fields to the corresponding
334: * 'xyz' fields, and nulls out the newXyz fields. Only those fields that are
335: * needed for the element to be located in the Describe model need to be
336: * synced; although subclasses may choose to sync additional fields, this is
337: * not mandatory.
338: */
339: public void syncFields() {
340: if (getNewName() != null) {
341: setName(getNewName());
342: setNewName(null);
343: }
344: }
345:
346: /**
347: * Determines if the element supplied matches selected non-null fields in
348: * this element. The fields examined should concern only this element and
349: * qualifying parent elements, not any children. A minimal implementation will
350: * check just element names and call match() for the parent elements. This
351: * method will be used to filter out events from model-source and source-model
352: * and should be as specific as necessary.
353: *
354: * @param el An <code>ElementInfo</code> of the same class as this.
355: * @return <code>true</code> If el is of the same class as this and all non-
356: * null fields of this element are equal to the corresponding fields
357: * of <code>el</code>.
358: */
359: public boolean matches(ElementInfo el) {
360: return (this == el);
361: }
362: }
|