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 : EventFilter.java
044: * Version : 1.0
045: * Description : Filters model-source roundtrip events to eliminate events
046: * caused by the source-model roundtrip.
047: * Author : Darshan
048: */
049: package org.netbeans.modules.uml.integration.ide.events;
050:
051: import java.util.HashSet;
052: import java.util.Hashtable;
053:
054: import org.netbeans.modules.uml.integration.ide.ChangeUtils;
055: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
056: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamedElement;
057: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IAttribute;
058: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IOperation;
059: import org.netbeans.modules.uml.core.support.umlsupport.Log;
060:
061: /**
062: * Filters model-source roundtrip events to eliminate events caused by the
063: * source-model roundtrip.
064: *
065: * Revision History
066: * No. Date Who What
067: * --- ---- --- ----
068: * 1 2002-07-15 Darshan Fixed filter ids using $ as a name separator
069: * while the check uses . as the separator.
070: */
071: public class EventFilter {
072: private Hashtable blockedEvents = new Hashtable();
073: private HashSet blockedEventTypes = new HashSet();
074:
075: public boolean isValid(ElementInfo element, ClassInfo parent) {
076: String eventId = getEventId(element, parent);
077: return (eventId != null && !isBlocked(eventId));
078: }
079:
080: public boolean isValid(INamedElement named) {
081: String eventId = getEventId(named);
082: return (eventId != null && !isBlocked(eventId));
083: }
084:
085: public boolean isValidEventType(int type) {
086: return !blockedEventTypes.contains(new Integer(type))
087: && type != ChangeUtils.RDT_SOURCE_DIR_CHANGED;
088: }
089:
090: protected String getEventId(ElementInfo element, ClassInfo parent) {
091: if (element == null)
092: return null;
093:
094: try {
095: StringBuffer buf = new StringBuffer();
096: buf = buf.append(element.getName());
097: if (parent != null)
098: buf.append("%").append(parent.getId());
099: return buf.toString();
100: } catch (NullPointerException e) {
101: Log.stackTrace(e);
102: }
103: return null;
104: }
105:
106: public String getEventId(INamedElement element) {
107: if (element == null)
108: return null;
109:
110: IElement parent = element.getOwner();
111: try {
112: String id = element.getName() + "%";
113: if (parent != null && parent instanceof INamedElement)
114: id += parent.getXMIID();
115: return id;
116: } catch (Exception ex) {
117: Log.stackTrace(ex);
118: }
119: return null;
120: }
121:
122: public void blockEvents(ElementInfo element, ClassInfo parent) {
123: String eventId = getEventId(element, parent);
124: if (eventId != null) {
125: Log.out("Blocking events for id " + eventId);
126: blockedEvents.put(eventId, eventId);
127:
128: // If the element name has been changed, also block events for the
129: // new name.
130: String elementName = element.getName(), newName = element
131: .getNewName();
132:
133: if (newName != null && !elementName.equals(newName)) {
134: element.setName(newName);
135: eventId = getEventId(element, parent);
136: if (eventId != null)
137: blockedEvents.put(eventId, eventId);
138: element.setName(elementName);
139: }
140: }
141: }
142:
143: public void blockEventType(int eventType) {
144: blockedEventTypes.add(new Integer(eventType));
145: }
146:
147: public void unblockEventType(int eventType) {
148: blockedEventTypes.remove(new Integer(eventType));
149: }
150:
151: public void unblockEvents(ElementInfo element, ClassInfo parent) {
152: String eventId = getEventId(element, parent);
153: if (eventId != null) {
154: Log.out("Unblocking events for id " + eventId);
155: blockedEvents.remove(eventId);
156:
157: // If the element name has been changed, also block events for the
158: // new name.
159: String elementName = element.getName(), newName = element
160: .getNewName();
161:
162: if (newName != null && !elementName.equals(newName)) {
163: element.setName(newName);
164: eventId = getEventId(element, parent);
165: if (eventId != null)
166: blockedEvents.remove(eventId);
167: element.setName(elementName);
168: }
169: }
170: }
171:
172: public void unblockAll() {
173: blockedEvents.clear();
174: }
175:
176: protected String getElementType(ElementInfo element) {
177: return element.getCode();
178: }
179:
180: protected String getElementType(INamedElement nel) {
181: if (nel instanceof IAttribute)
182: return "F";
183: else if (nel instanceof IOperation)
184: return "M";
185: else
186: return "C";
187: }
188:
189: protected boolean isBlocked(String eventId) {
190: boolean blocked = (eventId == null ? true : blockedEvents
191: .containsKey(eventId));
192: //Log.out("Event (" + eventId + ") is " + (blocked? "blocked" : "valid"));
193: return blocked;
194: }
195: }
|