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;
043:
044: import org.netbeans.modules.uml.core.metamodel.core.constructs.IEnumerationLiteral;
045: import java.util.Vector;
046:
047: import org.netbeans.modules.uml.integration.ide.events.ClassInfo;
048: import org.netbeans.modules.uml.integration.ide.events.MemberInfo;
049: import org.netbeans.modules.uml.integration.ide.listeners.IAttributeChangeListener;
050: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
051: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IAttribute;
052: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IClassifier;
053: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.INavigableEnd;
054: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IStructuralFeature;
055: import org.netbeans.modules.uml.core.roundtripframework.IAssociationEndTransformChangeRequest;
056: import org.netbeans.modules.uml.core.roundtripframework.IChangeRequest;
057: import org.netbeans.modules.uml.core.roundtripframework.IRoundTripEnumLiteralEventsSink;
058: import org.netbeans.modules.uml.core.support.umlsupport.IResultCell;
059: import org.netbeans.modules.uml.core.support.umlsupport.Log;
060: import org.netbeans.modules.uml.integration.ide.events.LiteralInfo;
061: import org.netbeans.modules.uml.integration.ide.listeners.IEnumLiteralChangeListener;
062:
063: /**
064: * @author Daniel Prusa
065: * @version 1.0
066: */
067: public class RoundTripEnumLiteralEventsSink extends RoundTripSource
068: implements IRoundTripEnumLiteralEventsSink {
069:
070: public void onPreEnumLiteralChangeRequest(IChangeRequest newVal,
071: IResultCell cell) {
072: }
073:
074: public void onEnumLiteralChangeRequest(IChangeRequest newVal,
075: IResultCell cell) {
076: try {
077: //ChangeUtils.say(newVal);
078: fireLiteralChangeEvent(newVal, false);
079: } catch (Exception e) {
080: Log.stackTrace(e);
081: }
082: }
083:
084: protected void fireLiteralChangeEvent(IChangeRequest newVal,
085: boolean beforeChange) {
086: // Get change type
087: int changeType = newVal.getState();
088:
089: IEnumerationLiteral before = null, after = null;
090:
091: try {
092: before = (IEnumerationLiteral) newVal.getBefore();
093: after = (IEnumerationLiteral) newVal.getAfter();
094: } catch (ClassCastException e) {
095: Log.stackTrace(e);
096: }
097:
098: switch (changeType) {
099: case ChangeUtils.CT_CREATE: {
100: if (isValidEvent(after)) {
101: LiteralInfo lit = new LiteralInfo(after);
102: fireEnumLiteralAddedEvent(lit, beforeChange);
103: scheduleForNavigation(after);
104: }
105: break;
106: }
107: case ChangeUtils.CT_DELETE: {
108: if (isValidEvent(before)) {
109: LiteralInfo lit = new LiteralInfo(before);
110: fireEnumLiteralDeletedEvent(lit, beforeChange);
111: }
112: break;
113: }
114: case ChangeUtils.CT_MODIFY: {
115: if (isValidEvent(before) && isValidEvent(after)) {
116: LiteralInfo oldC = new LiteralInfo(before), newC = new LiteralInfo(
117: after);
118: if (oldC.getContainingClass() == null)
119: oldC.setContainingClass(newC.getContainingClass());
120: fireEnumLiteralChangedEvent(oldC, newC, beforeChange);
121: }
122: break;
123: }
124: }
125: }
126:
127: protected void fireEnumLiteralAddedEvent(final LiteralInfo lit,
128: final boolean before) {
129: if (!isValidEvent(lit, lit.getContainingClass()))
130: return;
131:
132: for (int i = 0; i < changeListeners.size(); ++i) {
133: final IEnumLiteralChangeListener listener = (IEnumLiteralChangeListener) changeListeners
134: .elementAt(i);
135: RoundtripThread r = new RoundtripThread() {
136: public void work() {
137: setDefaultProject(lit);
138: listener.enumLiteralAdded(lit, before);
139: }
140: };
141: queue(r);
142: }
143: }
144:
145: protected void fireEnumLiteralDeletedEvent(final LiteralInfo lit,
146: final boolean before) {
147: if (!isValidEvent(lit, lit.getContainingClass()))
148: return;
149:
150: for (int i = 0; i < changeListeners.size(); ++i) {
151: final IEnumLiteralChangeListener listener = (IEnumLiteralChangeListener) changeListeners
152: .elementAt(i);
153: RoundtripThread r = new RoundtripThread() {
154: public void work() {
155: setDefaultProject(lit);
156: listener.enumLiteralDeleted(lit, before);
157: }
158: };
159: queue(r);
160: }
161: }
162:
163: protected void fireEnumLiteralChangedEvent(final LiteralInfo oldF,
164: final LiteralInfo newF, final boolean before) {
165: if (!isValidEvent(oldF, oldF.getContainingClass()))
166: return;
167:
168: for (int i = 0; i < changeListeners.size(); ++i) {
169: final IEnumLiteralChangeListener listener = (IEnumLiteralChangeListener) changeListeners
170: .elementAt(i);
171: RoundtripThread r = new RoundtripThread() {
172: public void work() {
173: setDefaultProject(newF);
174: listener.enumLiteralChanged(oldF, newF, before);
175: }
176: };
177: queue(r);
178: }
179: }
180:
181: public static void addEnumLiteralChangeListener(
182: IEnumLiteralChangeListener listener) {
183: if (listener != null && !changeListeners.contains(listener))
184: changeListeners.add(listener);
185: }
186:
187: public static void removeEnumLiteralChangeListener(
188: IEnumLiteralChangeListener listener) {
189: if (listener != null)
190: changeListeners.remove(listener);
191: }
192:
193: private static Vector changeListeners = new Vector();
194: }
|