/machinelearning/5.0.x/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/flow/common/editor/editpart/ElementEditPart.java

https://github.com/etirelli/droolsjbpm-contributed-experiments · Java · 156 lines · 113 code · 22 blank · 21 comment · 24 complexity · 23405a7bd90432fa1d0b653fa184170c MD5 · raw file

  1. package org.drools.eclipse.flow.common.editor.editpart;
  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.util.List;
  18. import org.drools.eclipse.flow.common.editor.core.ElementConnection;
  19. import org.drools.eclipse.flow.common.editor.core.ElementWrapper;
  20. import org.drools.eclipse.flow.common.editor.core.ModelEvent;
  21. import org.drools.eclipse.flow.common.editor.core.ModelListener;
  22. import org.drools.eclipse.flow.common.editor.editpart.figure.ElementFigure;
  23. import org.drools.eclipse.flow.common.editor.policy.ElementDirectEditManager;
  24. import org.drools.eclipse.flow.common.editor.policy.ElementDirectEditPolicy;
  25. import org.drools.eclipse.flow.common.editor.policy.ElementEditPolicy;
  26. import org.drools.eclipse.flow.common.editor.policy.ElementNodeEditPolicy;
  27. import org.eclipse.draw2d.ChopboxAnchor;
  28. import org.eclipse.draw2d.ConnectionAnchor;
  29. import org.eclipse.draw2d.Label;
  30. import org.eclipse.gef.ConnectionEditPart;
  31. import org.eclipse.gef.EditPolicy;
  32. import org.eclipse.gef.GraphicalEditPart;
  33. import org.eclipse.gef.NodeEditPart;
  34. import org.eclipse.gef.Request;
  35. import org.eclipse.gef.RequestConstants;
  36. import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
  37. import org.eclipse.gef.tools.DirectEditManager;
  38. import org.eclipse.jdt.core.IJavaProject;
  39. import org.eclipse.jface.viewers.TextCellEditor;
  40. /**
  41. * Default implementation of an element EditPart.
  42. *
  43. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  44. */
  45. public abstract class ElementEditPart extends AbstractGraphicalEditPart implements NodeEditPart, ModelListener {
  46. private DirectEditManager manager;
  47. private IJavaProject project;
  48. protected void createEditPolicies() {
  49. installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new ElementNodeEditPolicy());
  50. installEditPolicy(EditPolicy.COMPONENT_ROLE, new ElementEditPolicy());
  51. installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new ElementDirectEditPolicy());
  52. }
  53. public ElementWrapper getElementWrapper() {
  54. return (ElementWrapper) getModel();
  55. }
  56. protected List<ElementConnection> getModelSourceConnections() {
  57. return getElementWrapper().getOutgoingConnections();
  58. }
  59. protected List<ElementConnection> getModelTargetConnections() {
  60. return getElementWrapper().getIncomingConnections();
  61. }
  62. public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
  63. return new ChopboxAnchor(getFigure());
  64. }
  65. public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) {
  66. return new ChopboxAnchor(getFigure());
  67. }
  68. public ConnectionAnchor getSourceConnectionAnchor(Request request) {
  69. return new ChopboxAnchor(getFigure());
  70. }
  71. public ConnectionAnchor getTargetConnectionAnchor(Request request) {
  72. return new ChopboxAnchor(getFigure());
  73. }
  74. protected void refreshVisuals() {
  75. ElementWrapper element = getElementWrapper();
  76. ElementFigure figure = (ElementFigure) getFigure();
  77. figure.setText(element.getName());
  78. if (element.getConstraint().width == -1) {
  79. element.getConstraint().width = figure.getBounds().width;
  80. }
  81. if (element.getConstraint().height == -1) {
  82. element.getConstraint().height = figure.getBounds().height;
  83. }
  84. ((GraphicalEditPart) getParent()).setLayoutConstraint(this, figure, element.getConstraint());
  85. }
  86. public void modelChanged(ModelEvent event) {
  87. if (event.getChange() == ElementWrapper.CHANGE_INCOMING_CONNECTIONS) {
  88. refreshTargetConnections();
  89. } else if (event.getChange() == ElementWrapper.CHANGE_OUTGOING_CONNECTIONS) {
  90. refreshSourceConnections();
  91. } else if (event.getChange() == ElementWrapper.CHANGE_NAME) {
  92. refreshVisuals();
  93. } else if (event.getChange() == ElementWrapper.CHANGE_CONSTRAINT) {
  94. refreshVisuals();
  95. }
  96. }
  97. public void activate() {
  98. super.activate();
  99. ((ElementWrapper) getModel()).addListener(this);
  100. }
  101. public void deactivate() {
  102. ((ElementWrapper) getModel()).removeListener(this);
  103. super.deactivate();
  104. }
  105. public void performRequest(Request request) {
  106. if (request.getType() == RequestConstants.REQ_DIRECT_EDIT) {
  107. performDirectEdit();
  108. } if (request.getType() == RequestConstants.REQ_OPEN) {
  109. doubleClicked();
  110. } else {
  111. super.performRequest(request);
  112. }
  113. }
  114. protected void doubleClicked() {
  115. // do nothing
  116. }
  117. private void performDirectEdit() {
  118. Label label = ((ElementFigure) getFigure()).getLabel();
  119. if (label == null) {
  120. return;
  121. }
  122. if (manager == null) {
  123. manager = new ElementDirectEditManager(this, TextCellEditor.class,
  124. new ElementCellEditorLocator(label));
  125. }
  126. manager.show();
  127. }
  128. public void setProject(IJavaProject project) {
  129. this.project = project;
  130. }
  131. public IJavaProject getProject() {
  132. return this.project;
  133. }
  134. }