Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9336e11e992b6b256693b164ba4b95c631c9116d (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
/*******************************************************************************
 * 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.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.editparts.AbstractTreeEditPart;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jst.jsf.facesconfig.ui.EditorPlugin;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowElement;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowPage;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.properties.PageflowElementPropertySource;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.IPropertySource;

/**
 * This is the abstract base <code>TreeEditPart</code> for pageflow elements.
 */
public class PageflowElementTreeEditPart extends AbstractTreeEditPart implements
		Adapter {
	/** Image descriptor of tree item for the different edit parts */
	private static final ImageDescriptor IMG_PAGE = EditorPlugin.getDefault()
			.getImageDescriptor("facesconfig/Pageflow_Page16.gif"); //$NON-NLS-1$

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

	/** Property resource for shared property view */
	private IPropertySource propertySource = null;

	/** notifer to pageflow element */
	private Notifier target = null;

	/**
	 * Returns the image for the pageflow element.
	 * 
	 * @param element -
	 *            pageflow element, such as Begin, End, Page, and Action.
	 * @return - the image for the pageflow element
	 */
	public static Image getImage(PageflowElement element) {
		ImageDescriptor imageDescriptor = null;

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

		if (null == imageDescriptor) {
			return null;
		}

		Image 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;
	}

	/**
	 * Creates a new PageflowElementTreeEditPart instance.
	 * 
	 * @param pageflowElement -
	 *            create a new edit part according to the pageflow model
	 */
	public PageflowElementTreeEditPart(PageflowElement pageflowElement) {
		super(pageflowElement);
	}

	/*
	 * (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();
		}
		return super.getAdapter(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractTreeEditPart#getImage()
	 */
	protected Image getImage() {
		return getImage(getPageflowElement());
	}

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see Adapter#getTarget()
	 */
	public Notifier getTarget() {
		return target;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see AbstractTreeEditPart#getText()
	 */
	protected String getText() {
		return (null != getPageflowElement().getName() ? getPageflowElement()
				.getName() : "[unnamed]"); //$NON-NLS-1$
	}

	/**
	 * Returns the model as <code>PageflowElement</code>.
	 * 
	 * @return - the model as <code>PageflowElement</code>
	 */
	public PageflowElement getPageflowElement() {
		return (PageflowElement) getModel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Adapter#isAdapterForType(Object)
	 */
	public boolean isAdapterForType(Object type) {
		return type.equals(getModel().getClass());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Adapter#notifyChanged(Notification)
	 */
	public void notifyChanged(Notification notification) {
		int type = notification.getEventType();

		switch (type) {
		case Notification.ADD:
		case Notification.ADD_MANY:
		case Notification.REMOVE:
		case Notification.REMOVE_MANY:
		case Notification.SET:
			if (Thread.currentThread() == PlatformUI.getWorkbench().getDisplay().getThread()) {
				refreshVisuals();
			} else {
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable(){
					public void run() {
						refreshVisuals();
					}
				});
			}
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Adapter#setTarget(Notifier)
	 */
	public void setTarget(Notifier newTarget) {
		target = newTarget;
	}

	/*
	 * (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();
	}

	/**
	 * 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) {
			element.eAdapters().add(this);
		}
	}

	/**
	 * 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
	 */
	protected void unhookFromPageflowElement(PageflowElement element) {
		if (null != element) {
			element.eAdapters().remove(this);
		}
	}

}

Back to the top