/machinelearning/4.0.x/drools-eclipse3.3/drools-eclipse-plugin/src/main/java/org/drools/eclipse/flow/common/editor/core/ProcessWrapper.java

https://github.com/droolsjbpm/droolsjbpm-contributed-experiments · Java · 234 lines · 175 code · 38 blank · 21 comment · 25 complexity · c557a5c1aaf2d2bb298e59080ed20b40 MD5 · raw file

  1. package org.drools.eclipse.flow.common.editor.core;
  2. /*
  3. * Copyright 2005 JBoss Inc
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import org.drools.ruleflow.common.core.Process;
  27. import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
  28. import org.eclipse.ui.views.properties.IPropertyDescriptor;
  29. import org.eclipse.ui.views.properties.IPropertySource;
  30. import org.eclipse.ui.views.properties.TextPropertyDescriptor;
  31. /**
  32. * A wrapper for process element.
  33. *
  34. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  35. */
  36. public abstract class ProcessWrapper implements IPropertySource, Serializable {
  37. public static final int CHANGE_ELEMENTS = 1;
  38. public static final int CHANGE_ROUTER_LAYOUT = 2;
  39. public static final Integer ROUTER_LAYOUT_MANUAL = new Integer(0);
  40. public static final Integer ROUTER_LAYOUT_MANHATTAN = new Integer(1);
  41. public static final Integer ROUTER_LAYOUT_SHORTEST_PATH = new Integer(2);
  42. protected static IPropertyDescriptor[] descriptors;
  43. public static final String NAME = "name";
  44. public static final String VERSION = "version";
  45. public static final String ID = "id";
  46. public static final String PACKAGE_NAME = "packageName";
  47. public static final String ROUTER_LAYOUT = "routerLayout";
  48. static {
  49. descriptors = new IPropertyDescriptor[] {
  50. new TextPropertyDescriptor(NAME, "Name"),
  51. new TextPropertyDescriptor(VERSION, "Version"),
  52. new TextPropertyDescriptor(ID, "Id"),
  53. new TextPropertyDescriptor(PACKAGE_NAME, "Package"),
  54. new ComboBoxPropertyDescriptor(ROUTER_LAYOUT, "Connection Layout",
  55. new String[] { "Manual", "Manhatten", "Shortest Path" }),
  56. };
  57. }
  58. private Process process;
  59. private Map elements = new HashMap();
  60. private Integer routerLayout;
  61. private transient List listeners = new ArrayList();
  62. public ProcessWrapper() {
  63. process = createProcess();
  64. }
  65. protected abstract Process createProcess();
  66. public Process getProcess() {
  67. return process;
  68. }
  69. public String getName() {
  70. return process.getName() == null ? "" : process.getName();
  71. }
  72. public void setName(String name) {
  73. process.setName(name);
  74. }
  75. public String getVersion() {
  76. return process.getVersion() == null ? "" : process.getVersion();
  77. }
  78. public void setVersion(String version) {
  79. process.setVersion(version);
  80. }
  81. public String getId() {
  82. return process.getId();
  83. }
  84. public void setId(String id) {
  85. process.setId(id);
  86. }
  87. public String getPackageName() {
  88. return process.getPackageName() == null ? "" : process.getPackageName();
  89. }
  90. public void setPackageName(String packageName) {
  91. process.setPackageName(packageName);
  92. }
  93. public Integer getRouterLayout() {
  94. if (routerLayout == null) {
  95. routerLayout = ROUTER_LAYOUT_SHORTEST_PATH;
  96. }
  97. return routerLayout;
  98. }
  99. public void setRouterLayout(Integer routerLayout) {
  100. this.routerLayout = routerLayout;
  101. notifyListeners(CHANGE_ROUTER_LAYOUT);
  102. }
  103. public List getElements() {
  104. return Collections.unmodifiableList(new ArrayList(elements.values()));
  105. }
  106. public ElementWrapper getElement(String id) {
  107. return (ElementWrapper) elements.get(id);
  108. }
  109. public void addElement(ElementWrapper element) {
  110. internalAddElement(element);
  111. //id is set in methode above
  112. elements.put(element.getId(), element);
  113. notifyListeners(CHANGE_ELEMENTS);
  114. }
  115. protected abstract void internalAddElement(ElementWrapper element);
  116. public void removeElement(ElementWrapper element) {
  117. elements.remove(element.getId());
  118. notifyListeners(CHANGE_ELEMENTS);
  119. internalRemoveElement(element);
  120. }
  121. protected abstract void internalRemoveElement(ElementWrapper element);
  122. public void addListener(ModelListener listener) {
  123. listeners.add(listener);
  124. }
  125. public void removeListener(ModelListener listener) {
  126. listeners.remove(listener);
  127. }
  128. public void notifyListeners(int change) {
  129. ModelEvent event = new ModelEvent(change);
  130. for (Iterator it = listeners.iterator(); it.hasNext(); ) {
  131. ModelListener listener = (ModelListener) it.next();
  132. listener.modelChanged(event);
  133. }
  134. }
  135. private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException, IOException {
  136. aInputStream.defaultReadObject();
  137. listeners = new ArrayList();
  138. }
  139. public Object getEditableValue() {
  140. return this;
  141. }
  142. public IPropertyDescriptor[] getPropertyDescriptors() {
  143. return descriptors;
  144. }
  145. public Object getPropertyValue(Object id) {
  146. if (NAME.equals(id)) {
  147. return getName();
  148. }
  149. if (VERSION.equals(id)) {
  150. return getVersion();
  151. }
  152. if (ID.equals(id)) {
  153. return getId() + "";
  154. }
  155. if (PACKAGE_NAME.equals(id)) {
  156. return getPackageName();
  157. }
  158. if (ROUTER_LAYOUT.equals(id)) {
  159. return routerLayout;
  160. }
  161. return null;
  162. }
  163. public boolean isPropertySet(Object id) {
  164. return true;
  165. }
  166. public void resetPropertyValue(Object id) {
  167. if (NAME.equals(id)) {
  168. setName("");
  169. }
  170. if (VERSION.equals(id)) {
  171. setVersion("");
  172. }
  173. if (ID.equals(id)) {
  174. setId("");
  175. }
  176. if (PACKAGE_NAME.equals(id)) {
  177. setPackageName("");
  178. }
  179. if (ROUTER_LAYOUT.equals(id)) {
  180. setRouterLayout(null);
  181. }
  182. }
  183. public void setPropertyValue(Object id, Object value) {
  184. if (NAME.equals(id)) {
  185. setName((String) value);
  186. } else if (VERSION.equals(id)) {
  187. setVersion((String) value);
  188. } else if (ID.equals(id)) {
  189. setId((String) value);
  190. } else if (PACKAGE_NAME.equals(id)) {
  191. setPackageName((String) value);
  192. } else if (ROUTER_LAYOUT.equals(id)) {
  193. setRouterLayout((Integer) value);
  194. }
  195. }
  196. }