001: package org.drools.eclipse.flow.common.editor.core;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.io.ObjectInputStream;
021: import java.io.Serializable;
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.drools.ruleflow.common.core.Process;
030: import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
031: import org.eclipse.ui.views.properties.IPropertyDescriptor;
032: import org.eclipse.ui.views.properties.IPropertySource;
033: import org.eclipse.ui.views.properties.TextPropertyDescriptor;
034:
035: /**
036: * A wrapper for process element.
037: *
038: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
039: */
040: public abstract class ProcessWrapper implements IPropertySource,
041: Serializable {
042:
043: public static final int CHANGE_ELEMENTS = 1;
044: public static final int CHANGE_ROUTER_LAYOUT = 2;
045:
046: public static final Integer ROUTER_LAYOUT_MANUAL = new Integer(0);
047: public static final Integer ROUTER_LAYOUT_MANHATTAN = new Integer(1);
048: public static final Integer ROUTER_LAYOUT_SHORTEST_PATH = new Integer(
049: 2);
050:
051: protected static IPropertyDescriptor[] descriptors;
052:
053: public static final String NAME = "name";
054: public static final String VERSION = "version";
055: public static final String ID = "id";
056: public static final String ROUTER_LAYOUT = "routerLayout";
057: static {
058: descriptors = new IPropertyDescriptor[] {
059: new TextPropertyDescriptor(NAME, "Name"),
060: new TextPropertyDescriptor(VERSION, "Version"),
061: new TextPropertyDescriptor(ID, "Id"),
062: new ComboBoxPropertyDescriptor(ROUTER_LAYOUT,
063: "Router Layout", new String[] { "Manual",
064: "Manhatten", "Shortest Path" }), };
065: }
066:
067: private Process process;
068: private Map elements = new HashMap();
069: private Integer routerLayout;
070: private transient List listeners = new ArrayList();
071:
072: public ProcessWrapper() {
073: process = createProcess();
074: }
075:
076: protected abstract Process createProcess();
077:
078: public Process getProcess() {
079: return process;
080: }
081:
082: public String getName() {
083: return process.getName() == null ? "" : process.getName();
084: }
085:
086: public void setName(String name) {
087: process.setName(name);
088: }
089:
090: public String getVersion() {
091: return process.getVersion() == null ? "" : process.getVersion();
092: }
093:
094: public void setVersion(String version) {
095: process.setVersion(version);
096: }
097:
098: public String getId() {
099: return process.getId();
100: }
101:
102: public void setId(String id) {
103: process.setId(id);
104: }
105:
106: public Integer getRouterLayout() {
107: if (routerLayout == null) {
108: routerLayout = ROUTER_LAYOUT_SHORTEST_PATH;
109: }
110: return routerLayout;
111: }
112:
113: public void setRouterLayout(Integer routerLayout) {
114: this .routerLayout = routerLayout;
115: notifyListeners(CHANGE_ROUTER_LAYOUT);
116: }
117:
118: public List getElements() {
119: return Collections.unmodifiableList(new ArrayList(elements
120: .values()));
121: }
122:
123: public ElementWrapper getElement(String id) {
124: return (ElementWrapper) elements.get(id);
125: }
126:
127: public void addElement(ElementWrapper element) {
128: internalAddElement(element);
129: //id is set in methode above
130: elements.put(element.getId(), element);
131: notifyListeners(CHANGE_ELEMENTS);
132: }
133:
134: protected abstract void internalAddElement(ElementWrapper element);
135:
136: public void removeElement(ElementWrapper element) {
137: elements.remove(element.getId());
138: notifyListeners(CHANGE_ELEMENTS);
139: internalRemoveElement(element);
140: }
141:
142: protected abstract void internalRemoveElement(ElementWrapper element);
143:
144: public void addListener(ModelListener listener) {
145: listeners.add(listener);
146: }
147:
148: public void removeListener(ModelListener listener) {
149: listeners.remove(listener);
150: }
151:
152: public void notifyListeners(int change) {
153: ModelEvent event = new ModelEvent(change);
154: for (Iterator it = listeners.iterator(); it.hasNext();) {
155: ModelListener listener = (ModelListener) it.next();
156: listener.modelChanged(event);
157: }
158: }
159:
160: private void readObject(ObjectInputStream aInputStream)
161: throws ClassNotFoundException, IOException {
162: aInputStream.defaultReadObject();
163: listeners = new ArrayList();
164: }
165:
166: public Object getEditableValue() {
167: return this ;
168: }
169:
170: public IPropertyDescriptor[] getPropertyDescriptors() {
171: return descriptors;
172: }
173:
174: public Object getPropertyValue(Object id) {
175: if (NAME.equals(id)) {
176: return getName();
177: }
178: if (VERSION.equals(id)) {
179: return getVersion();
180: }
181: if (ID.equals(id)) {
182: return getId() + "";
183: }
184: if (ROUTER_LAYOUT.equals(id)) {
185: return routerLayout;
186: }
187: return null;
188: }
189:
190: public boolean isPropertySet(Object id) {
191: return true;
192: }
193:
194: public void resetPropertyValue(Object id) {
195: if (NAME.equals(id)) {
196: setName("");
197: }
198: if (VERSION.equals(id)) {
199: setVersion("");
200: }
201: if (ID.equals(id)) {
202: setId("");
203: }
204: if (ID.equals(id)) {
205: setRouterLayout(null);
206: }
207: }
208:
209: public void setPropertyValue(Object id, Object value) {
210: if (NAME.equals(id)) {
211: setName((String) value);
212: } else if (VERSION.equals(id)) {
213: setVersion((String) value);
214: } else if (ID.equals(id)) {
215: setId((String) value);
216: } else if (ROUTER_LAYOUT.equals(id)) {
217: setRouterLayout((Integer) value);
218: }
219: }
220: }
|