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: * EnumLiteralChangeFacility.java
044: *
045: * Created on April 8, 2005, 11:08 AM
046: */
047:
048: package org.netbeans.modules.uml.core.roundtripframework;
049:
050: import org.netbeans.modules.uml.core.metamodel.core.constructs.IEnumeration;
051: import org.netbeans.modules.uml.core.metamodel.core.constructs.IEnumerationLiteral;
052: import org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ILanguage;
053: import org.netbeans.modules.uml.core.support.umlsupport.ProductRetriever;
054: import org.netbeans.modules.uml.core.support.umlutils.ETList;
055:
056: /**
057: *
058: * @author Administrator
059: */
060: public abstract class EnumLiteralChangeFacility extends RequestFacility
061: implements IEnumLiteralChangeFacility {
062:
063: /** Creates a new instance of EnumLiteralChangeFacility */
064: public EnumLiteralChangeFacility() {
065: }
066:
067: public IEnumerationLiteral addLiteral(String sName,
068: IEnumeration pClassifier, boolean rtOffCreate,
069: boolean rtOffPostProcessing) {
070: if (pClassifier == null)
071: return null;
072:
073: int roundTripMode = getRoundTripMode();
074:
075: // We do not want to send round trip events if rtOffCreate is true
076: if (rtOffCreate) {
077: setRoundTripMode(RTMode.RTM_OFF);
078: } else {
079: setRoundTripMode(RTMode.RTM_LIVE);
080: }
081:
082: IEnumerationLiteral literal = null;
083:
084: try {
085:
086: literal = createLiteral(sName, pClassifier);
087:
088: // Only handle the post processing of the attribute if round
089: // trip was off during the creation of the attribute. If RT
090: // was on during the creation of the attribute RT has already
091: // done the post processing, so do not do it again.
092: if (literal != null && rtOffCreate) {
093: if (rtOffPostProcessing) {
094: setRoundTripMode(RTMode.RTM_OFF);
095: } else {
096: setRoundTripMode(RTMode.RTM_LIVE);
097: }
098:
099: added(literal);
100: }
101: } finally {
102: // Reset the state of RT.
103: setRoundTripMode(roundTripMode);
104: }
105:
106: return literal;
107: }
108:
109: public void changeName(IEnumerationLiteral literal,
110: String sNewName, boolean rtOffCreate,
111: boolean rtOffPostProcessing) {
112: if (literal == null)
113: return;
114:
115: int createFlag = RoundTripUtils
116: .getRTModeFromTurnOffFlag(rtOffCreate);
117: RoundTripModeRestorer restorer = new RoundTripModeRestorer(
118: createFlag);
119:
120: literal.setName(sNewName);
121:
122: int postFlag = RoundTripUtils
123: .getRTModeFromTurnOffFlag(rtOffPostProcessing);
124: restorer.setMode(postFlag);
125:
126: nameChanged(literal);
127: }
128:
129: public IEnumerationLiteral createLiteral(String sName,
130: IEnumeration pClassifier) {
131: if (pClassifier == null)
132: return null;
133:
134: ILanguage pLanguage = getLanguage();
135:
136: // Now if the attribute type is a data type (as specified by the
137: // language manager then we want to create a IAttribute inside the
138: // classifier. Otherwise we want to create an association.
139: if (pLanguage != null) {
140: RoundTripModeRestorer rest = new RoundTripModeRestorer(
141: RTMode.RTM_OFF);
142: try {
143: return pClassifier.createLiteral(sName);
144: } finally {
145: rest.restoreOriginalMode();
146: }
147: }
148:
149: return null;
150: }
151:
152: public void delete(IEnumerationLiteral literal,
153: boolean rtOffDelete, boolean rtOffPostDelete) {
154: if (literal == null)
155: return;
156:
157: IEnumeration pClassifier = literal.getEnumeration();
158:
159: if (pClassifier != null) {
160: int deleteFlag = RoundTripUtils
161: .getRTModeFromTurnOffFlag(rtOffDelete);
162: RoundTripModeRestorer restorer = new RoundTripModeRestorer(
163: deleteFlag);
164:
165: pClassifier.removeLiteral(literal);
166: literal.delete();
167:
168: int postFlag = RoundTripUtils
169: .getRTModeFromTurnOffFlag(rtOffPostDelete);
170: restorer.setMode(postFlag);
171:
172: deleted(literal, pClassifier);
173: }
174: }
175:
176: public void findAndChangeName(String sOldName, String sNewName,
177: IEnumeration pClassifier) {
178: ETList<IEnumerationLiteral> literals = pClassifier
179: .getLiterals();
180: for (IEnumerationLiteral literal : literals) {
181: String name = literal.getName();
182: if (sOldName.equals(name) == true) {
183: literal.setName(sNewName);
184: nameChanged(literal);
185: break;
186: }
187: }
188: }
189:
190: public void findAndDelete(String sName, IEnumeration pClassifier) {
191: ETList<IEnumerationLiteral> literals = pClassifier
192: .getLiterals();
193: for (IEnumerationLiteral literal : literals) {
194: String name = literal.getName();
195: if (sName.equals(name) == true) {
196: literal.delete();
197: deleted(literal, pClassifier);
198: break;
199: }
200: }
201: }
202:
203: ///////////////////////////////////////////////////////////////////////////
204: // Helper Methods
205:
206: protected int getRoundTripMode() {
207: return ProductRetriever.retrieveProduct()
208: .getRoundTripController().getMode();
209: }
210:
211: protected void setRoundTripMode(/*RTMode*/int value) {
212: // IZ 84855 conover - RT is always off and shouldn't be
213: // turned on for any reason
214: if (value != RTMode.RTM_LIVE)
215: ProductRetriever.retrieveProduct().getRoundTripController()
216: .setMode(value);
217: }
218:
219: protected String retrieveShortName(String fullNameStr) {
220: int pos = fullNameStr.lastIndexOf("::");
221: return pos != -1 ? fullNameStr.substring(pos + 2) : fullNameStr;
222: }
223: }
|