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.integration.ide.events;
043:
044: import org.netbeans.modules.uml.core.metamodel.core.constructs.IEnumeration;
045: import org.netbeans.modules.uml.core.metamodel.core.constructs.IEnumerationLiteral;
046: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IClassifier;
047: import org.netbeans.modules.uml.integration.ide.ChangeUtils;
048: import org.netbeans.modules.uml.integration.ide.JavaClassUtils;
049: import org.netbeans.modules.uml.integration.ide.UMLSupport;
050: import org.netbeans.modules.uml.core.metamodel.structure.IProject;
051: import org.netbeans.modules.uml.core.support.umlsupport.Log;
052: import java.util.Iterator;
053:
054: public class LiteralTransaction {
055:
056: private IEnumerationLiteral mLiteral = null;
057:
058: /** The system that is to be updated. */
059: private IProject mProj = null;
060:
061: /** Flag to indicate if this is an implementation attribute. */
062: private boolean mIsImplLiteral = false;
063:
064: private IClassifier mSym = null;
065:
066: public LiteralTransaction(IProject proj) {
067: setLiteral(null);
068: setProject(proj);
069: }
070:
071: /**
072: * Create a new MemberTransaction and specify the symbol to search and
073: * data member to find. If the member will be created if one is needed.
074: * @param trans The symbol transaction used when searching for the memeber.
075: * @param member The information required to locate the data member.
076: */
077: public LiteralTransaction(SymbolTransaction trans,
078: final LiteralInfo lit) {
079: this (UMLSupport.getCurrentProject());
080: if (lit != null) {
081: if (trans.getSymbol() != null) {
082: setSymbol(trans.getSymbol());
083: }
084: setLiteral(trans, lit);
085: }
086: }
087:
088: protected void setProject(IProject proj) {
089: mProj = proj;
090: }
091:
092: public IProject getProject() {
093: return mProj;
094: }
095:
096: public IEnumerationLiteral getLiteral() {
097: return mLiteral;
098: }
099:
100: public void setLiteral(IEnumerationLiteral lit) {
101: mLiteral = lit;
102: }
103:
104: public boolean isImplLiteral() {
105: return mIsImplLiteral;
106: }
107:
108: public void setIsImplLiteral(boolean value) {
109: mIsImplLiteral = value;
110: }
111:
112: public void setLiteral(SymbolTransaction trans,
113: final LiteralInfo lit) {
114: // First set the symbol to null to allow the current symbol to be GC
115: setLiteral(null);
116: IEnumeration sym = (IEnumeration) trans.getSymbol();
117:
118: if (sym != null) {
119: mLiteral = findLiteral(sym, lit.getName());
120:
121: if (mLiteral == null
122: && lit.getChangeType() == ElementInfo.CREATE)
123: mLiteral = createLiteral(sym, lit.getName());
124: }
125: }
126:
127: public void setSymbol(IClassifier clazz) {
128: mSym = clazz;
129: }
130:
131: public IClassifier getSymbol() {
132: return mSym;
133: }
134:
135: protected IEnumerationLiteral createLiteral(IEnumeration sym,
136: String name) {
137: IEnumerationLiteral retVal = null;
138: EventManager.getEventManager().getEventFilter().blockEventType(
139: ChangeUtils.RDT_DEPENDENCY_ADDED);
140: try {
141: if (findLiteral(sym, name) == null) {
142: sym.addLiteral(sym.createLiteral(name));
143: }
144: } catch (Exception e) {
145: Log.stackTrace(e);
146: } finally {
147: EventManager.getEventManager().getEventFilter()
148: .unblockEventType(ChangeUtils.RDT_DEPENDENCY_ADDED);
149: }
150: return retVal;
151: }
152:
153: private IEnumerationLiteral findLiteral(IEnumeration sym,
154: String name) {
155: for (Iterator iter = sym.getLiterals().iterator(); iter
156: .hasNext();) {
157: IEnumerationLiteral lit = (IEnumerationLiteral) iter.next();
158: if (name.equals(lit.getName())) {
159: return lit;
160: }
161: }
162: return null;
163: }
164:
165: }
|