Skip to main content
summaryrefslogtreecommitdiffstats
blob: a734e463ec66152210767047d457ee14e66ae4c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*******************************************************************************
 * Copyright (c) 2004, 2008 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.jsf.facesconfig.ui.pageflow.editpart;

import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.DropRequest;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jdt.ui.JavaElementImageDescriptor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.command.OpenEditorCommand;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.editpolicy.PageflowElementEditPolicy;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.editpolicy.PageflowNodeDirectEditPolicy;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.editpolicy.PageflowNodeEditPolicy;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.figure.ILabelDecorator;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.figure.PageflowNodeFigure;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowElement;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowLink;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowNode;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowPackage;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowPage;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.synchronization.PFBatchAdapter;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.util.PageflowAnnotationUtil;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;


/**
 * The base class for the applications EditParts that represent
 * PageflowNode-derived objects in the model. This class implements the
 * NodeEditPart interface which supports:
 * <ul>
 * <li>feedback for Connections when they are being
 * <li>
 * <li>initially connected and when they are disonnected/reconnected</li>
 * </ul>
 * 
 * 
 */
public class PageflowNodeEditPart extends PageflowElementEditPart implements
		NodeEditPart, INodePreference, PFValidator {

	/** property source of pageflow node */
	// private IPropertySource propertySource = null;
	protected DirectEditManager editManager;

	private class ImageDecorator implements ILabelDecorator {
		private Image decrateImage = null;

		/*
		 * (non-Javadoc)
		 * 
		 * @see com.sybase.stf.jmt.editors.pageflow.figures.ILabelDecorator#decorateImage(org.eclipse.swt.graphics.Image,
		 *      java.lang.Object)
		 */
		public Image decorateImage(Image image, Object element) {
			dispose();
			int adornmentFlags = computeAdornmentFlags(element);
			if (adornmentFlags != 0) {
				ImageDescriptor baseImage = ImageDescriptor.createFromImage(image);
				org.eclipse.swt.graphics.Rectangle bounds = image.getBounds();
				decrateImage = (new JavaElementImageDescriptor(baseImage,
						adornmentFlags, new org.eclipse.swt.graphics.Point(
								bounds.width, bounds.height))).createImage();
				return decrateImage;
			}
			return image;
		}

		/**
		 * Computes adornment flags for specified object.
		 * 
		 * Note: This method is for internal use only. Clients should not call
		 * this method.
		 * 
		 * @param obj Object to complute flags for.
		 * @return Adornment flags.
		 */
		protected int computeAdornmentFlags(Object obj) {
			return JavaElementImageDescriptor.WARNING;
		}

		/**
		 * Disposes this instance.
		 */
		public void dispose() {
			if (decrateImage != null) {
				decrateImage.dispose();
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see com.sybase.stf.jmt.editors.pageflow.figures.ILabelDecorator#decorateText(java.lang.String,
		 *      java.lang.Object)
		 */
		public String decorateText(String text, Object element) {
			// TODO Auto-generated method stub
			return null;
		}

	}

	/**
	 * Disposes this instance.
	 */
	public void dispose() {
		if (imageDecorator != null) {
			imageDecorator.dispose();
		}
	}

	private ImageDecorator imageDecorator = null;

	/**
	 * Creates a new PageflowNodeEditPart instance.
	 * 
	 * @param element -
	 *            pageflow node
	 */
	public PageflowNodeEditPart(PageflowNode element) {
		super(element);
	}

	/**
	 * get the pageflow node
	 * 
	 * @return - pageflow node
	 */
	public PageflowNode getPageflowNode() {
		return (PageflowNode) getModel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractGraphicalEditPart#getModelSourceConnections()
	 */
	protected List getModelSourceConnections() {
		return getPageflowNode().getOutlinks();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractGraphicalEditPart#getModelTargetConnections()
	 */
	protected List getModelTargetConnections() {
		return getPageflowNode().getInlinks();
	}

	/**
	 * Returns the Figure of this, as a node type figure.
	 * 
	 * @return - Figure as a NodeFigure.
	 */
	protected PageflowNodeFigure getPageflowNodeFigure() {
		return (PageflowNodeFigure) getFigure();
	}

	public Adapter createEMFAdapter() {
		return new PFBatchAdapter() {
			/**
			 * when ports are added to a PageflowNode, add this EditPart as a
			 * listener on the port so that it gets notified of PFLinks being
			 * added or removed. When links are added or removed from a port
			 * owned by the PageflowNode of this EditPart, refresh the
			 * connections.
			 */
			public void doNotifyChanged(Notification notification) {
				int type = notification.getEventType();
				// FC2PFTransformer.getInstance().NotifyChanged(notification,
				// (PageflowElement) getModel());
				switch (type) {
				case Notification.ADD:
				case Notification.ADD_MANY:
				case Notification.REMOVE:
				case Notification.REMOVE_MANY:
					if (notification.getNewValue() instanceof PageflowLink) {
						if (Thread.currentThread() == PlatformUI.getWorkbench().getDisplay().getThread()) {
							refreshTargetConnections();
							refreshSourceConnections();
							validate();
						} else {
							PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable(){
								public void run() {
									refreshTargetConnections();
									refreshSourceConnections();
									validate();
								}
							});
						}
					}
					break;

				case Notification.SET:
					int featureId = notification
							.getFeatureID(PageflowPackage.class);
					if (needValidation(featureId)) {
						validate();
					}
					if (Thread.currentThread() == PlatformUI.getWorkbench().getDisplay().getThread()) {
						refreshVisuals();
					} else {
						PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable(){
							public void run() {
								refreshVisuals();
							}
						});
					}
					break;
				}
			}
		};
	}

	private boolean needValidation(int featureId) {
		if (getModel() instanceof PageflowPage) {
			if (featureId == PageflowPackage.PF_PAGE__PATH) {
				return true;
			}
		}
		return false;
	}

	public void validate() {
		if (getModel() instanceof PageflowPage) {
			PageflowAnnotationUtil.validatePage(this);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		// install the edit policy to handle connection creation
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
				new PageflowNodeEditPolicy());

		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new PageflowElementEditPolicy());

		// install the direct policy
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,
				new PageflowNodeDirectEditPolicy());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see NodeEditPart#getSourceConnectionAnchor(ConnectionEditPart)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		PageflowLink link = (PageflowLink) connection.getModel();
		return getPageflowNodeFigure().getSourceConnectionAnchorAt(
				new Point(link.getSource().getX(), link.getSource().getY()));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see NodeEditPart#getSourceConnectionAnchor(Request)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		Point pt = new Point(((DropRequest) request).getLocation());
		return getPageflowNodeFigure().getSourceConnectionAnchorAt(pt);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see NodeEditPart#getTargetConnectionAnchor(ConnectionEditPart)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		PageflowLink link = (PageflowLink) connection.getModel();
		return getPageflowNodeFigure().getTargetConnectionAnchorAt(
				new Point(link.getTarget().getX(), link.getTarget().getY()));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see NodeEditPart#getTargetConnectionAnchor(Request)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		Point pt = new Point(((DropRequest) request).getLocation());
		return getPageflowNodeFigure().getTargetConnectionAnchorAt(pt);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractEditPart#refreshVisuals()
	 */
	protected void refreshVisuals() {
		super.refreshVisuals();

		getPageflowNodeFigure().setText(getPageflowNode().getName());
	}

	/**
	 * make this a listener on its ports
	 */
	public void activate() {
		super.activate();

		Iterator it;

		it = getPageflowNode().getInlinks().iterator();
		while (it.hasNext()) {
			PageflowLink link = (PageflowLink) it.next();

			hookIntoPageflowElement(link);
		}

		it = getPageflowNode().getOutlinks().iterator();
		while (it.hasNext()) {
			PageflowLink link = (PageflowLink) it.next();

			hookIntoPageflowElement(link);
		}
	}

	/**
	 * remove this as a listener on its ports
	 */
	public void deactivate() {
		super.deactivate();
		dispose();
		Iterator it;

		it = getPageflowNode().getInlinks().iterator();
		while (it.hasNext()) {
			PageflowLink link = (PageflowLink) it.next();

			unhookFromPageflowElement(link);
		}

		it = getPageflowNode().getOutlinks().iterator();
		while (it.hasNext()) {
			PageflowLink link = (PageflowLink) it.next();

			unhookFromPageflowElement(link);
		}
	}

	/**
	 * perfrom direct edit request
	 * 
	 */
	protected void performDirectEdit() {
		if (editManager == null) {
			Label l = ((PageflowNodeFigure) getFigure()).getLabel();
			editManager = new PageflowDirectEditManager(this,
					TextCellEditor.class, new DirectEditCellEditorLocator(l), l);
		}
		editManager.show();
	}

	/**
	 * perform double click request
	 * 
	 */
	protected void performOpen() {
		// only Page and Action support the double-click commands
		if (getModel() instanceof PageflowPage) {
			// CommandStack stack =
			// getViewer().getEditDomain().getCommandStack();
			Command command = new OpenEditorCommand(this);
			if (command != null && command.canExecute()) {
				// stack.execute(command);
				command.execute();
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.editparts.IFigurePreference#setForegroundColor(org.eclipse.swt.graphics.Color)
	 */
	public void setForegroundColor(Color c) {
		getPageflowNodeFigure().setForegroundColor(c);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.editparts.IFigurePreference#setBackgroundColor(org.eclipse.swt.graphics.Color)
	 */
	public void setBackgroundColor(Color c) {
		getPageflowNodeFigure().setBackgroundColor(c);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.editparts.IFigurePreference#setFont(org.eclipse.swt.graphics.Font)
	 */
	public void setFont(Font f) {
		getPageflowNodeFigure().setFont(f);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.editparts.INodePreference#setTextPlacement(int)
	 */
	public void setTextPlacement(int where) {
		getPageflowNodeFigure().setTextPlacement(where);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.editparts.IEditPartDecorator#decorateEditPart()
	 */
	public void addAnnotation(final Annotation annotation) {
		getViewer().getControl().getDisplay().asyncExec(new Runnable() {
			/* (non-Javadoc)
			 * @see java.lang.Runnable#run()
			 */
			public void run() {
				getPageflowNodeFigure().setImage(
						getDecoratedImage((PageflowElement) getModel()));
				getPageflowNodeFigure().setToolTipText(annotation.getText());
			}

		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.editparts.IEditPartDecorator#undecorateEditPart()
	 */
	public void removeAnnotation() {
		getViewer().getControl().getDisplay().asyncExec(new Runnable() {
			public void run() {
				getPageflowNodeFigure().setImage(
						getImage((PageflowElement) getModel()));
				getPageflowNodeFigure().setToolTipText(null);
			}

		});
	}

	/**
	 * Returns the image for the pageflow element.
	 * 
	 * @param element -
	 *            pageflow element
	 * @return - the image for the pageflow element.
	 */
	private Image getDecoratedImage(PageflowElement element) {
		Image decoratedImage = getImage(element);

		decoratedImage = getImageDecorator().decorateImage(getImage(element),
				null);

		return decoratedImage;
	}

	private ImageDecorator getImageDecorator() {
		if (imageDecorator == null) {
			imageDecorator = new ImageDecorator();
		}
		return imageDecorator;
	}

}

Back to the top