001: /*
002: * $Id: Process.java 561 2005-10-24 16:27:03Z nebulosu $
003: * (c) Copyright 2004 con:cern development team.
004: *
005: * This file is part of con:cern (http://concern.sf.net).
006: *
007: * con:cern is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.concern.model;
015:
016: import javax.swing.event.EventListenerList;
017: import java.util.*;
018: import java.beans.PropertyChangeListener;
019: import java.beans.PropertyChangeEvent;
020:
021: /**
022: * @version $Revision: 561 $
023: */
024: public class Process extends Element implements PropertyChangeListener {
025: private String subject;
026: private Loader loader;
027: private SortedSet actors = new TreeSet();
028: private SortedSet activities = new TreeSet();
029: private SortedSet conditions = new TreeSet();
030: private SortedSet events = new TreeSet();
031: private SortedSet listeners = new TreeSet();
032: private SortedSet collaborators = new TreeSet();
033:
034: EventListenerList listenerList = new EventListenerList();
035:
036: public Process(String name) {
037: super (name);
038: }
039:
040: public String getSubject() {
041: return subject;
042: }
043:
044: public void setSubject(String subject) {
045: Object old = this .subject;
046: this .subject = subject;
047: changes.firePropertyChange("subject", old, subject);
048: }
049:
050: public Loader getLoader() {
051: return loader;
052: }
053:
054: public void setLoader(Loader loader) {
055: Object old = this .loader;
056: this .loader = loader;
057: changes.firePropertyChange("loader", old, loader);
058: }
059:
060: public void addActor(Actor actor) {
061: if (actors.add(actor)) {
062: actor.addPropertyChangeListener("name", this );
063: fireNodeAdded(actor);
064: }
065: }
066:
067: public void removeActor(Actor actor) {
068: if (actors.remove(actor)) {
069: fireNodeRemoved(actor);
070: actor.removePropertyChangeListener("name", this );
071: }
072: }
073:
074: public Actor getActor(String name) {
075: return (Actor) getElement(actors, name);
076: }
077:
078: public Collection getActors() {
079: return Collections.unmodifiableCollection(actors);
080: }
081:
082: public void addActivity(Activity activity) {
083: if (activities.add(activity)) {
084: activity.addPropertyChangeListener("name", this );
085: fireNodeAdded(activity);
086: }
087: }
088:
089: public void removeActivity(Activity activity) {
090: if (activities.remove(activity)) {
091: fireNodeRemoved(activity);
092: activity.removePropertyChangeListener("name", this );
093: }
094: }
095:
096: public Activity getActivity(String name) {
097: return (Activity) getElement(activities, name);
098: }
099:
100: public Collection getActivities() {
101: return Collections.unmodifiableCollection(activities);
102: }
103:
104: public void addCondition(Condition condition) {
105: if (conditions.add(condition)) {
106: condition.addPropertyChangeListener("name", this );
107: fireNodeAdded(condition);
108: }
109: }
110:
111: public void removeCondition(Condition condition) {
112: if (conditions.remove(condition)) {
113: fireNodeRemoved(condition);
114: condition.removePropertyChangeListener("name", this );
115: }
116: }
117:
118: public Condition getCondition(String name) {
119: return (Condition) getElement(conditions, name);
120: }
121:
122: public Collection getConditions() {
123: return Collections.unmodifiableCollection(conditions);
124: }
125:
126: public void addEvent(Event event) {
127: if (events.add(event)) {
128: event.addPropertyChangeListener("name", this );
129: fireNodeAdded(event);
130: }
131: }
132:
133: public void removeEvent(Event event) {
134: if (events.remove(event)) {
135: fireNodeRemoved(event);
136: event.removePropertyChangeListener("name", this );
137: }
138: }
139:
140: public Event getEvent(String name) {
141: return (Event) getElement(events, name);
142: }
143:
144: public Collection getEvents() {
145: return Collections.unmodifiableCollection(events);
146: }
147:
148: public void addListener(Listener listener) {
149: if (listeners.add(listener)) {
150: listener.addPropertyChangeListener("name", this );
151: fireNodeAdded(listener);
152: }
153: }
154:
155: public void removeListener(Listener listener) {
156: if (listeners.remove(listener)) {
157: fireNodeRemoved(listener);
158: listener.removePropertyChangeListener("name", this );
159: }
160: }
161:
162: public Listener getListener(String name) {
163: return (Listener) getElement(listeners, name);
164: }
165:
166: public Collection getListeners() {
167: return Collections.unmodifiableCollection(listeners);
168: }
169:
170: public void addCollaborator(Collaborator collaborator) {
171: if (collaborators.add(collaborator)) {
172: collaborator.addPropertyChangeListener("name", this );
173: fireNodeAdded(collaborator);
174: }
175: }
176:
177: public void removeCollaborator(Collaborator collaborator) {
178: if (collaborators.remove(collaborator)) {
179: fireNodeRemoved(collaborator);
180: collaborator.removePropertyChangeListener("name", this );
181: }
182: }
183:
184: public Collaborator getCollaborator(String name) {
185: return (Collaborator) getElement(collaborators, name);
186: }
187:
188: public Collection getCollaborators() {
189: return Collections.unmodifiableCollection(collaborators);
190: }
191:
192: public void propertyChange(PropertyChangeEvent evt) {
193: fireNodeChanged((Element) evt.getSource());
194: }
195:
196: protected void fireNodeAdded(Element element) {
197: Object[] listeners = listenerList.getListenerList();
198: ProcessChangeEvent e = new ProcessChangeEvent(this , element,
199: ProcessChangeEvent.ADDED);
200:
201: for (int i = listeners.length - 2; i >= 0; i -= 2) {
202: if (listeners[i] == ProcessChangeListener.class) {
203: ((ProcessChangeListener) listeners[i + 1]).nodeAdded(e);
204: }
205: }
206: }
207:
208: protected void fireNodeRemoved(Element element) {
209: Object[] listeners = listenerList.getListenerList();
210: ProcessChangeEvent e = new ProcessChangeEvent(this , element,
211: ProcessChangeEvent.REMOVED);
212:
213: for (int i = listeners.length - 2; i >= 0; i -= 2) {
214: if (listeners[i] == ProcessChangeListener.class) {
215: ((ProcessChangeListener) listeners[i + 1])
216: .nodeRemoved(e);
217: }
218: }
219: }
220:
221: protected void fireNodeChanged(Element element) {
222: Object[] listeners = listenerList.getListenerList();
223: ProcessChangeEvent e = new ProcessChangeEvent(this , element,
224: ProcessChangeEvent.CHANGED);
225:
226: for (int i = listeners.length - 2; i >= 0; i -= 2) {
227: if (listeners[i] == ProcessChangeListener.class) {
228: ((ProcessChangeListener) listeners[i + 1])
229: .nodeChanged(e);
230: }
231: }
232: }
233:
234: public void addProcessChangeListener(ProcessChangeListener listener) {
235: listenerList.add(ProcessChangeListener.class, listener);
236: }
237:
238: public void removeProcessChangeListener(
239: ProcessChangeListener listener) {
240: listenerList.remove(ProcessChangeListener.class, listener);
241: }
242:
243: public void addElement(Element element) {
244: if (element instanceof Collaborator) {
245: Collaborator collaborator = (Collaborator) element;
246: element.setName(unifyName(getCollaborators(), element
247: .getName()));
248: addCollaborator(collaborator);
249: } else if (element instanceof Actor) {
250: Actor actor = (Actor) element;
251: element.setName(unifyName(getActors(), element.getName()));
252: addActor(actor);
253: } else if (element instanceof Condition) {
254: Condition condition = (Condition) element;
255: element.setName(unifyName(getConditions(), element
256: .getName()));
257: addCondition(condition);
258: } else if (element instanceof Event) {
259: Event event = (Event) element;
260: element.setName(unifyName(getEvents(), element.getName()));
261: addEvent(event);
262: } else if (element instanceof Activity) {
263: Activity activity = (Activity) element;
264: element.setName(unifyName(getActivities(), element
265: .getName()));
266: addActivity(activity);
267: } else if (element instanceof Listener) {
268: Listener listener = (Listener) element;
269: element
270: .setName(unifyName(getListeners(), element
271: .getName()));
272: addListener(listener);
273: }
274: }
275:
276: private String unifyName(Collection elements, String name) {
277: if (getElement(elements, name) == null)
278: return name;
279:
280: int i = 2;
281: while (getElement(elements, name + " " + i) != null)
282: i++;
283:
284: return name + " " + i;
285: }
286:
287: private Element getElement(Collection elements, String name) {
288: for (Iterator iterator = elements.iterator(); name != null
289: && iterator.hasNext();) {
290: Element element = (Element) iterator.next();
291: if (name.equals(element.getName()))
292: return element;
293: }
294: return null;
295: }
296:
297: public void removeNode(Element element) {
298: boolean removed = false;
299: removed |= conditions.remove(element);
300: removed |= activities.remove(element);
301: removed |= events.remove(element);
302: removed |= listeners.remove(element);
303: removed |= actors.remove(element);
304: removed |= collaborators.remove(element);
305: if (removed)
306: fireNodeRemoved(element);
307: }
308: }
|