1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2007</p>
22 * <p>Company: Ontotext Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.bpmo.ui.editor.editpart.tasks;
26
27 import java.beans.PropertyChangeEvent;
28 import java.beans.PropertyChangeListener;
29 import java.util.Iterator;
30 import java.util.List;
31
32 import org.eclipse.draw2d.*;
33 import org.eclipse.draw2d.geometry.*;
34 import org.eclipse.draw2d.text.FlowPage;
35 import org.eclipse.draw2d.text.TextFlow;
36 import org.eclipse.gef.*;
37 import org.eclipse.jface.dialogs.InputDialog;
38 import org.eclipse.jface.window.Window;
39 import org.eclipse.swt.widgets.Display;
40 import org.wsmostudio.bpmo.model.WorkflowEntityNode;
41 import org.wsmostudio.bpmo.model.tasks.AbstractTaskNode;
42 import org.wsmostudio.bpmo.ui.editor.Utils;
43 import org.wsmostudio.bpmo.ui.editor.editpart.*;
44 import org.wsmostudio.bpmo.ui.editor.policies.WorkflowConnectionEditPolicy;
45
46 public class ManualTaskEditpart extends WorkflowEditPart implements
47 PropertyChangeListener, NodeEditPart {
48
49 public ManualTaskEditpart(EditPart context) {
50 super(context);
51 }
52
53 protected DefaultConnectionAnchor anchor_center;
54
55 protected Label referenceLab;
56 protected TextFlow lab;
57
58 protected IFigure createFigure()
59 {
60 AbstractTaskNode model = (AbstractTaskNode)getModel();
61 RoundedRectangle rectangle = new RoundedRectangle();
62 if (model.getSize() == null) {
63 model.setSize(model.getPreferredSize());
64 }
65 rectangle.setSize(model.getSize().width, model.getSize().height);
66 Utils.updateBackground(rectangle, (WorkflowEntityNode)getModel());
67 rectangle.setLineWidth(1);
68
69 FlowPage fPage = new FlowPage();
70 lab = new TextFlow();
71 fPage.setHorizontalAligment(PositionConstants.CENTER);
72 fPage.add(lab);
73 lab.setText(model.getName());
74 lab.setSize(FigureUtilities.getTextExtents(lab.getText(),
75 Display.getCurrent().getSystemFont()));
76
77 referenceLab = new Label();
78 updateWSMOReference();
79
80 BorderLayout layout = new BorderLayout();
81 rectangle.setLayoutManager(layout);
82 rectangle.setBorder(new MarginBorder(6, 3, 3, 3));
83
84 rectangle.add(new Label(), BorderLayout.TOP);
85 rectangle.add(fPage, BorderLayout.CENTER);
86 rectangle.add(referenceLab, BorderLayout.BOTTOM);
87
88 anchor_center = new DefaultConnectionAnchor(rectangle);
89
90 createOrUpdateAnchorsLocations();
91
92 return rectangle;
93 }
94
95 public void performRequest(Request request){
96 if (request.getType() == RequestConstants.REQ_DIRECT_EDIT) {
97 performDirectEdit();
98 }
99 }
100
101 @SuppressWarnings("unchecked")
102 public void refresh() {
103 super.refresh();
104 for(Iterator it = getChildren().iterator(); it.hasNext(); ) {
105 ((EditPart)it.next()).refresh();
106 }
107 for(Iterator it = getSourceConnections().iterator(); it.hasNext(); ) {
108 ((EditPart)it.next()).refresh();
109 }
110 for(Iterator it = getTargetConnections().iterator(); it.hasNext(); ) {
111 ((EditPart)it.next()).refresh();
112 }
113 }
114
115 private void performDirectEdit(){
116 AbstractTaskNode model = (AbstractTaskNode)getModel();
117 InputDialog dialog = new InputDialog(
118 Display.getCurrent().getActiveShell(),
119 "Task Name",
120 "New Name",
121 model.getName(),
122 null);
123 if (dialog.open() != Window.OK) {
124 return;
125 }
126 model.setName(dialog.getValue());
127 lab.setText(model.getName());
128 fitModelSizeToLabel();
129 model.notifyContentChanged();
130 }
131
132 private void fitModelSizeToLabel() {
133 AbstractTaskNode model = (AbstractTaskNode)getModel();
134 lab.getUpdateManager().performUpdate();
135 Rectangle textSize = lab.getBounds();
136 double horizontalExpKoef = ((double)lab.getFragments().size()) / 3.0;
137
138 if (horizontalExpKoef > 1.0) {
139 textSize.width = (int)
140 Math.round((horizontalExpKoef * ((double)textSize.width)));
141 textSize.height = (int)
142 Math.round(((double)textSize.height) / horizontalExpKoef);
143 }
144 model.setSize(Math.max(textSize.width + 10, model.getSize().width),
145 Math.max(textSize.height + 45, model.getSize().height));
146 }
147
148 /***
149 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
150 **/
151 protected void createEditPolicies()
152 {
153
154 installEditPolicy(EditPolicy.COMPONENT_ROLE, new TaskComponentEditPolicy());
155 installEditPolicy(EditPolicy.LAYOUT_ROLE, new BpmoXYLayoutEditPolicy());
156 installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new WorkflowConnectionEditPolicy());
157 installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new LabelDirectEditPolicy());
158 }
159
160 /***
161 * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
162 **/
163 protected void refreshVisuals()
164 {
165 AbstractTaskNode model = (AbstractTaskNode)getModel();
166 String labText = (model.getName() != null) ? model.getName() : "";
167 if (false == labText.equals(lab.getText())) {
168 lab.setText(labText);
169 fitModelSizeToLabel();
170 }
171 updateWSMOReference();
172
173 Point loc = model.getLocation();
174 Dimension size = model.getSize();
175
176 Rectangle r = new Rectangle(loc ,size);
177 ((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(), r);
178 Utils.updateBackground(getFigure(), model);
179 }
180
181 /***
182 * @see java.beans.PropertyChangeListener#propertyChange(PropertyChangeEvent)
183 **/
184 public void propertyChange(PropertyChangeEvent evt)
185 {
186 String prop = evt.getPropertyName();
187 WorkflowEntityNode model = (WorkflowEntityNode)getModel();
188 if (prop.equals("size")) {
189 Utils.arrangeWorkflowEntities(model.getOwner());
190 createOrUpdateAnchorsLocations();
191 refreshVisuals();
192 getParent().refresh();
193 }
194 else if (prop.equals("location")) {
195 Utils.arrangeWorkflowEntities(model.getOwner());
196 refreshVisuals();
197 getParent().refresh();
198 }
199 else {
200 refreshVisuals();
201 }
202 refreshSourceConnections();
203 refreshTargetConnections();
204 }
205
206
207 /***
208 * @see org.eclipse.gef.EditPart#activate()
209 **/
210 public void activate() {
211 if (isActive()==false) {
212 super.activate();
213 ((AbstractTaskNode)getModel()).addPropertyChangeListener(this);
214 }
215 }
216
217
218 /***
219 * @see org.eclipse.gef.EditPart#deactivate()
220 **/
221 public void deactivate() {
222 if (isActive()) {
223 super.deactivate();
224 ((AbstractTaskNode)getModel()).removePropertyChangeListener(this);
225 }
226 }
227
228
229
230 /***
231 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelSourceConnections()
232 **/
233 @SuppressWarnings("unchecked")
234 protected List getModelSourceConnections() {
235 return ((AbstractTaskNode)getModel()).listOutArcs();
236 }
237
238
239 /***
240 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelTargetConnections()
241 **/
242 @SuppressWarnings("unchecked")
243 protected List getModelTargetConnections() {
244 return ((AbstractTaskNode)getModel()).listInArcs();
245 }
246
247
248
249
250
251 public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
252 return getConnectionAnchor();
253 }
254
255
256
257
258
259 public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) {
260 return getConnectionAnchor();
261 }
262
263 protected ConnectionAnchor getConnectionAnchor() {
264 if (getModel() instanceof AbstractTaskNode) {
265 return new ChopboxAnchor(getFigure());
266 }
267 return null;
268 }
269
270 /***
271 * Return the source connection anchor of the given connection.
272 **/
273 public ConnectionAnchor getSourceConnectionAnchor(GraphConnectorEditPart iConnection) {
274 return anchor_center;
275 }
276
277 /***
278 * Return the target connection anchor of the given connection.
279 **/
280 public ConnectionAnchor getTargetConnectionAnchor(GraphConnectorEditPart iConnection) {
281 return anchor_center;
282 }
283
284
285 /***
286 * Set the anchors location based on current figure's rectangle size.
287 **/
288 private void createOrUpdateAnchorsLocations()
289 {
290 Dimension size = ((AbstractTaskNode)getModel()).getSize();
291 anchor_center.offsetH = size.width / 2;
292 anchor_center.offsetV = size.height / 2;
293 }
294
295
296
297 /***
298 * Return the closest source connection anchor to the request's origin location.
299 **/
300 public ConnectionAnchor getSourceConnectionAnchor(Request iRequest) {
301 return anchor_center;
302 }
303
304 /***
305 * Return the closest source connection anchor to the request's origin location.
306 **/
307 public ConnectionAnchor getTargetConnectionAnchor(Request iRequest) {
308 return anchor_center;
309 }
310
311 protected void updateWSMOReference() {
312
313 }
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331