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

https://github.com/etirelli/droolsjbpm-contributed-experiments · Java · 205 lines · 149 code · 36 blank · 20 comment · 10 complexity · d0ca88a3ebdf14d825f5ccfa22fa2d0c 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.Iterator;
  23. import java.util.List;
  24. import org.eclipse.draw2d.geometry.Rectangle;
  25. import org.eclipse.ui.views.properties.IPropertyDescriptor;
  26. import org.eclipse.ui.views.properties.IPropertySource;
  27. import org.eclipse.ui.views.properties.TextPropertyDescriptor;
  28. /**
  29. * Default wrapper of a model element.
  30. *
  31. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  32. */
  33. public abstract class DefaultElementWrapper implements IPropertySource, ElementWrapper, Serializable {
  34. protected static IPropertyDescriptor[] descriptors;
  35. public static final String NAME = "Name";
  36. static {
  37. descriptors = new IPropertyDescriptor[] {
  38. new TextPropertyDescriptor(NAME, "Name"),
  39. };
  40. }
  41. private Object element;
  42. private Rectangle constraint;
  43. private ProcessWrapper parent;
  44. private List incomingConnections = new ArrayList();
  45. private List outgoingConnections = new ArrayList();
  46. private transient List listeners = new ArrayList();
  47. protected void setElement(Object element) {
  48. this.element = element;
  49. }
  50. public Object getElement() {
  51. return element;
  52. }
  53. public void setConstraint(Rectangle constraint) {
  54. this.constraint = constraint;
  55. notifyListeners(CHANGE_CONSTRAINT);
  56. }
  57. public Rectangle getConstraint() {
  58. return constraint;
  59. }
  60. public void setParent(ProcessWrapper parent) {
  61. this.parent = parent;
  62. }
  63. protected ProcessWrapper getParent() {
  64. return parent;
  65. }
  66. public List getOutgoingConnections() {
  67. return Collections.unmodifiableList(outgoingConnections);
  68. }
  69. public List getIncomingConnections() {
  70. return Collections.unmodifiableList(incomingConnections);
  71. }
  72. public void addIncomingConnection(ElementConnection connection) {
  73. incomingConnections.add(connection);
  74. internalAddIncomingConnection(connection);
  75. notifyListeners(CHANGE_INCOMING_CONNECTIONS);
  76. }
  77. protected void internalAddIncomingConnection(ElementConnection connection) {
  78. }
  79. public void removeIncomingConnection(ElementConnection connection) {
  80. incomingConnections.remove(connection);
  81. internalRemoveIncomingConnection(connection);
  82. notifyListeners(CHANGE_INCOMING_CONNECTIONS);
  83. }
  84. protected void internalRemoveIncomingConnection(ElementConnection connection) {
  85. }
  86. public void addOutgoingConnection(ElementConnection connection) {
  87. outgoingConnections.add(connection);
  88. internalAddOutgoingConnection(connection);
  89. notifyListeners(CHANGE_OUTGOING_CONNECTIONS);
  90. }
  91. protected void internalAddOutgoingConnection(ElementConnection connection) {
  92. }
  93. public void removeOutgoingConnection(ElementConnection connection) {
  94. outgoingConnections.remove(connection);
  95. internalRemoveOutgoingConnection(connection);
  96. notifyListeners(CHANGE_OUTGOING_CONNECTIONS);
  97. }
  98. protected void internalRemoveOutgoingConnection(ElementConnection connection) {
  99. }
  100. public void setName(String name) {
  101. internalSetName(name);
  102. notifyListeners(CHANGE_NAME);
  103. }
  104. protected void internalSetName(String name) {
  105. }
  106. public List getOutgoingConnections(int type) {
  107. List result = new ArrayList();
  108. for (Iterator it = outgoingConnections.iterator(); it.hasNext();) {
  109. ElementConnection connection = (ElementConnection) it.next();
  110. if (connection.getType() == type) {
  111. result.add(connection);
  112. }
  113. }
  114. return Collections.unmodifiableList(result);
  115. }
  116. public List getIncomingConnections(int type) {
  117. List result = new ArrayList();
  118. for (Iterator it = incomingConnections.iterator(); it.hasNext();) {
  119. ElementConnection connection = (ElementConnection) it.next();
  120. if (connection.getType() == type) {
  121. result.add(connection);
  122. }
  123. }
  124. return Collections.unmodifiableList(result);
  125. }
  126. public void addListener(ModelListener listener) {
  127. listeners.add(listener);
  128. }
  129. public void removeListener(ModelListener listener) {
  130. listeners.remove(listener);
  131. }
  132. protected void notifyListeners(int change) {
  133. ModelEvent event = new ModelEvent(change);
  134. for (Iterator it = listeners.iterator(); it.hasNext();) {
  135. ModelListener listener = (ModelListener) it.next();
  136. listener.modelChanged(event);
  137. }
  138. }
  139. private void readObject(ObjectInputStream aInputStream)
  140. throws ClassNotFoundException, IOException {
  141. aInputStream.defaultReadObject();
  142. listeners = new ArrayList();
  143. }
  144. public IPropertyDescriptor[] getPropertyDescriptors() {
  145. return descriptors;
  146. }
  147. public Object getEditableValue() {
  148. return this;
  149. }
  150. public boolean isPropertySet(Object id) {
  151. return true;
  152. }
  153. public Object getPropertyValue(Object id) {
  154. if (NAME.equals(id)) {
  155. return getName();
  156. }
  157. return null;
  158. }
  159. public void resetPropertyValue(Object id) {
  160. if (NAME.equals(id)) {
  161. setName("");
  162. }
  163. }
  164. public void setPropertyValue(Object id, Object value) {
  165. if (NAME.equals(id)) {
  166. setName((String) value);
  167. }
  168. }
  169. }