Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a3e32ff90a775af39b68bb0414bb7bb7c168c512 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

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

import org.eclipse.core.resources.IResource;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jst.jsf.facesconfig.ui.EditorPlugin;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.figure.PageflowNodeFigure;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.Pageflow;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowElement;
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.properties.PageflowElementPropertySource;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.synchronization.PFBatchAdapter;
import org.eclipse.jst.jsf.facesconfig.ui.util.WebrootUtil;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.views.properties.IPropertySource;

/**
 * 
 * 
 * <code>PageflowElementEditPart</code> is the base class for all the
 * EditParts in the application, whose model is a subclass of PageflowElement.
 * This class maintains the figure's size and location inresponse to changes in
 * the model, and registers as a listener on its model using the
 * <code>org.eclipse.emf.common.notify.Adapter</code> interface
 */
public abstract class PageflowElementEditPart extends AbstractGraphicalEditPart
		implements IAnnotationEditPart {
	/** image description for different edit part */
	public static final ImageDescriptor IMG_ACTION = EditorPlugin.getDefault()
			.getImageDescriptor("facesconfig/Pageflow_Action16.gif"); //$NON-NLS-1$

	protected Adapter adapter;

	public Adapter createEMFAdapter() {
		return new PFBatchAdapter() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see AbstractGraphicalEditPart#notifyChanged()
			 */
			public void doNotifyChanged(Notification notification) {
				int type = notification.getEventType();
				int featureId = notification
						.getFeatureID(PageflowPackage.class);
				if (type == Notification.SET) {
					switch (featureId) {
					case PageflowPackage.PAGEFLOW_ELEMENT__X:
					case PageflowPackage.PAGEFLOW_ELEMENT__Y:
					case PageflowPackage.PAGEFLOW_ELEMENT__WIDTH:
					case PageflowPackage.PAGEFLOW_ELEMENT__HEIGHT:
						refreshVisuals();
						break;
					}
				}
			}
		};
	}

	public static final ImageDescriptor IMG_PAGE = EditorPlugin.getDefault()
			.getImageDescriptor("facesconfig/Pageflow_Page.gif"); //$NON-NLS-1$

	public static final ImageDescriptor IMG_NODE = EditorPlugin.getDefault()
			.getImageDescriptor("facesconfig/FacesConfig_Pageflow16.gif"); //$NON-NLS-1$

	/** property source of the pageflow element */
	private IPropertySource propertySource = null;

	private Image image = null;

	protected PageflowElementEditPart(PageflowElement element) {
		setModel(element);
	}

	/**
	 * Returns the image for the pageflow element.
	 * 
	 * @param element -
	 *            pageflow element
	 * @return - the image for the pageflow element.
	 */
	public Image getImage(PageflowElement element) {
		if (image == null) {
			ImageDescriptor imageDescriptor = null;

			if (element instanceof PageflowPage) {
				imageDescriptor = IMG_PAGE;
			} else {
				imageDescriptor = IMG_NODE;
			}

			image = EditorPlugin.getDefault().getImageRegistry().get(
					imageDescriptor.toString());
			if (null == image) {
				EditorPlugin.getDefault().getImageRegistry().put(
						imageDescriptor.toString(), imageDescriptor);
				image = EditorPlugin.getDefault().getImageRegistry().get(
						imageDescriptor.toString());
			}
		}

		return image;
	}

	/**
	 * get the type name of the pageflow element
	 * 
	 * @param element -
	 *            pageflow element
	 * @return - the type name of the pageflow element
	 */
	public static String getTypeName(PageflowElement element) {
		if (element instanceof Pageflow) {
			return "Pageflow"; //$NON-NLS-1$
		} else if (element instanceof PageflowPage) {
			return "Page"; //$NON-NLS-1$
		}

		return "[unknown]"; //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractGraphicalEditPart#activate()
	 */
	public void activate() {
		if (isActive()) {
			return;
		}

		// start listening for changes in the model
		hookIntoPageflowElement(getPageflowElement());

		super.activate();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractGraphicalEditPart#deactivate()
	 */
	public void deactivate() {
		if (!isActive()) {
			return;
		}

		// stop listening for changes in the model
		unhookFromPageflowElement(getPageflowElement());

		super.deactivate();
	}

	/**
	 * Returns the model as pageflow element.
	 * 
	 * @return - pageflow element
	 */
	public PageflowElement getPageflowElement() {
		return (PageflowElement) getModel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractEditPart#refreshVisuals()
	 */
	protected void refreshVisuals() {
		if (getParent() != null) {
			Point loc = new Point(getPageflowElement().getX(),
					getPageflowElement().getY());
			Dimension size = new Dimension(getPageflowElement().getWidth(),
					getPageflowElement().getHeight());
			Rectangle r = new Rectangle(loc, size);

			((GraphicalEditPart) getParent()).setLayoutConstraint(this,
					getFigure(), r);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see IAdaptable#getAdapter(Class)
	 */
	public Object getAdapter(Class key) {
		/*
		 * override the default behavior defined in AbstractEditPart which would
		 * expect the model to be a property sourced. instead the editpart can
		 * provide a property source
		 */
		if (key == IPropertySource.class) {
			return getPropertySource();
		}
		if (key == Adapter.class) {
			if (adapter == null) {
				adapter = createEMFAdapter();
			}
			return adapter;
		}
		return super.getAdapter(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see PageflowElementEditPart#getPropertySource()
	 */
	protected IPropertySource getPropertySource() {
		if (propertySource == null) {
			propertySource = new PageflowElementPropertySource(
					getPageflowElement());
		}
		return propertySource;
	}

	/**
	 * Registers this edit part as a listener for change notifications to the
	 * specified pageflow element.
	 * 
	 * @param element
	 *            the pagelfow element that should be observed for change
	 *            notifications
	 */
	protected void hookIntoPageflowElement(PageflowElement element) {
		if (null != element) {
			if (!element.eAdapters().contains(getAdapter(Adapter.class))) {
				element.eAdapters().add((Adapter) getAdapter(Adapter.class));
			}
		}
	}

	/**
	 * Removes this edit part from the specified pageflow element. Thus, it will
	 * no longe receive change notifications.
	 * 
	 * @param element
	 *            the pagelfow element that should not be observed any more
	 */
	protected void unhookFromPageflowElement(PageflowElement element) {
		if (null != element) {
			element.eAdapters().remove(getAdapter(Adapter.class));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		IFigure figure_ = null;
		figure_ = new PageflowNodeFigure();
		PageflowElement elm = getPageflowElement();
		if (elm.getName() != null) {
			((PageflowNodeFigure) figure_).setImageText(getImage(elm), elm
					.getName());
		} else {
			((PageflowNodeFigure) figure_).setImageText(getImage(elm),
					getTypeName(elm));
		}
		((PageflowNodeFigure) figure_).update();
		return figure_;
	}

	/**
	 * perform corresponding request related with double click or direct edit
	 * click.
	 * 
	 * @param request :
	 *            request composed by mouse click.
	 */
	public void performRequest(Request request) {
		// the request is triggered by double clicked on a edit part
		if (request.getType() == RequestConstants.REQ_OPEN) {
			performOpen();
		} else if (request.getType() == RequestConstants.REQ_DIRECT_EDIT) {
			performDirectEdit();
		}
	}

	/**
	 * perfrom direct edit request
	 * 
	 */
	protected abstract void performDirectEdit();

	/**
	 * perform double click request
	 * 
	 */
	protected abstract void performOpen();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.editparts.IEditPartDecorator#decorateEditPart()
	 */
	public abstract void addAnnotation(Annotation annotation);
    
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.editparts.IEditPartDecorator#undecorateEditPart()
	 */
	public abstract void removeAnnotation();
    
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.marker.IEditPartMarker#getMarkerResource()
	 */
	public IResource getMarkerResource() {
		IResource resource = WebrootUtil.getResource((EObject) getModel());
		return resource;
	}
}

Back to the top