Skip to main content
summaryrefslogtreecommitdiffstats
blob: a1179bc79a90f8e4dae64bb2344ade6b45eb01b6 (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 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.section;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jst.jsf.facesconfig.emf.ApplicationType;
import org.eclipse.jst.jsf.facesconfig.emf.FacesConfigPackage;
import org.eclipse.jst.jsf.facesconfig.emf.FacesConfigType;
import org.eclipse.jst.jsf.facesconfig.emf.FactoryType;
import org.eclipse.jst.jsf.facesconfig.emf.LifecycleType;
import org.eclipse.jst.jsf.facesconfig.ui.EditorMessages;
import org.eclipse.jst.jsf.facesconfig.ui.page.IFacesConfigPage;
import org.eclipse.jst.jsf.facesconfig.ui.page.OthersPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * @author sfshi
 * 
 */
public class OverviewOthersSection extends AbstractOverviewSection {

	private static final int COLUMN_WITH = 100;

	private OverviewOthersSectionAdapter overviewOthersSectionAdapter;

	/**
	 * 
	 * @param parent
	 * @param managedForm
	 * @param page
	 * @param toolkit
	 */
	public OverviewOthersSection(Composite parent, IManagedForm managedForm,
			IFacesConfigPage page, FormToolkit toolkit) {
		super(parent, managedForm, page, toolkit, OthersPage.PAGE_ID,
				EditorMessages.OverviewPage_OthersSection_name,
				EditorMessages.OverviewPage_OthersSection_description,
				null, null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.jsf.facesconfig.ui.section.AbstractOverviewSection#configTableViewer(org.eclipse.jface.viewers.TableViewer)
	 */
	protected void configTableViewer(TableViewer tableViewer1) {
		tableViewer1.setContentProvider(new IStructuredContentProvider() {

			public Object[] getElements(Object inputElement) {
				List othersNodesList = (List) inputElement;
				return othersNodesList.toArray();
			}

			public void dispose() {
                // do nothing
			}

			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
			    // do nothing
			}
		});

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.jsf.facesconfig.ui.section.AbstractOverviewSection#createTable(org.eclipse.swt.widgets.Composite)
	 */
	protected Table createTable(Composite container) {
		Table table = new Table(container, SWT.H_SCROLL | SWT.V_SCROLL
				| SWT.FULL_SELECTION | SWT.BORDER);

		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 1;
		gd.heightHint = 100;
		table.setLayoutData(gd);

		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		TableLayout tablelayout = new TableLayout();
		table.setLayout(tablelayout);

		TableColumn valuecol = new TableColumn(table, SWT.NONE);
		tablelayout.addColumnData(new ColumnWeightData(1, COLUMN_WITH, true));
		valuecol
				.setText(EditorMessages.OverviewPage_OthersSection_table_valuecol);
		valuecol.setResizable(true);

		TableColumn typecol = new TableColumn(table, SWT.NONE);
		tablelayout.addColumnData(new ColumnWeightData(1, COLUMN_WITH, true));
		typecol
				.setText(EditorMessages.OverviewPage_OthersSection_table_typecol);
		typecol.setResizable(true);

		return table;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.jsf.facesconfig.ui.section.AbstractOverviewSection#refreshAll()
	 */
	public void refreshAll() {
		if(getSection() == null || getSection().isDisposed()) {
			return;
		}
		List othersNodesList = new ArrayList();
		if (getInput() instanceof FacesConfigType) {
			Object[] applicationNodes = null;
			Object[] factoryNodes = null;
			Object[] lifecycleNodes = null;
			FacesConfigType facesConfig = (FacesConfigType) getInput();
			IStructuredContentProvider contentProvider = new AdapterFactoryContentProvider(
					getAdapterFactory());
			if (facesConfig.getApplication().size() > 0) {

				ApplicationType application = (ApplicationType) facesConfig
						.getApplication().get(0);
				applicationNodes = contentProvider.getElements(application);

			}

			if (facesConfig.getFactory().size() > 0) {
				FactoryType factory = (FactoryType) facesConfig.getFactory()
						.get(0);
				factoryNodes = contentProvider.getElements(factory);
			}

			if (facesConfig.getLifecycle().size() > 0) {
				LifecycleType lifecycle = (LifecycleType) facesConfig
						.getLifecycle().get(0);
				lifecycleNodes = contentProvider.getElements(lifecycle);
			}

			if (applicationNodes != null) {
				for (int i = 0, n = applicationNodes.length; i < n; i++) {
					othersNodesList.add(applicationNodes[i]);
				}
			}

			if (factoryNodes != null) {
				for (int i = 0, n = factoryNodes.length; i < n; i++) {
					othersNodesList.add(factoryNodes[i]);
				}
			}

			if (lifecycleNodes != null) {
				for (int i = 0, n = lifecycleNodes.length; i < n; i++) {
					othersNodesList.add(lifecycleNodes[i]);
				}
			}
		}

		tableViewer.setInput(othersNodesList);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.jsf.facesconfig.ui.section.AbstractFacesConfigSection#addAdaptersOntoInput(java.lang.Object)
	 */
	protected void addAdaptersOntoInput(Object newInput) {
		FacesConfigType facesConfig = (FacesConfigType) newInput;
		addOverviewOthersSectionAdapter(facesConfig);
		IStructuredContentProvider contentProvider = new AdapterFactoryContentProvider(
				getAdapterFactory());
		if (facesConfig.getApplication().size() > 0) {
			ApplicationType application = (ApplicationType) facesConfig
					.getApplication().get(0);
			addOverviewOthersSectionAdapter(application);

			Object[] applicationNodes = contentProvider
					.getElements(application);
			for (int i = 0, n = applicationNodes.length; i < n; i++) {
				addOverviewOthersSectionAdapter((EObject) applicationNodes[i]);
			}

		}

		if (facesConfig.getFactory().size() > 0) {
			FactoryType factory = (FactoryType) facesConfig.getFactory().get(0);
			addOverviewOthersSectionAdapter(factory);

			Object[] factoryNodes = contentProvider.getElements(factory);
			for (int i = 0, n = factoryNodes.length; i < n; i++) {
				addOverviewOthersSectionAdapter((EObject) factoryNodes[i]);
			}

		}

		if (facesConfig.getLifecycle().size() > 0) {
			LifecycleType lifecycle = (LifecycleType) facesConfig
					.getLifecycle().get(0);
			addOverviewOthersSectionAdapter(lifecycle);

			Object[] lifecycleNodes = contentProvider.getElements(lifecycle);
			for (int i = 0, n = lifecycleNodes.length; i < n; i++) {
				addOverviewOthersSectionAdapter((EObject) lifecycleNodes[i]);
			}

		}

	}

	private void addOverviewOthersSectionAdapter(EObject object) {
		if (EcoreUtil.getExistingAdapter(object, OverviewOthersSection.class) == null) {
			object.eAdapters().add(getOverviewOthersSectionAdapter());
		}

	}

	private void removeOverviewOthersSectionAdapter(EObject object) {
		if (EcoreUtil.getExistingAdapter(object, OverviewOthersSection.class) != null) {
			object.eAdapters().remove(getOverviewOthersSectionAdapter());
		}
	}

	private OverviewOthersSectionAdapter getOverviewOthersSectionAdapter() {
		if (overviewOthersSectionAdapter == null) {
			overviewOthersSectionAdapter = new OverviewOthersSectionAdapter();
		}

		return overviewOthersSectionAdapter;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.jsf.facesconfig.ui.section.AbstractFacesConfigSection#removeAdaptersFromInput(java.lang.Object)
	 */
	protected void removeAdaptersFromInput(Object oldInput) {

		FacesConfigType facesConfig = (FacesConfigType) oldInput;
		removeOverviewOthersSectionAdapter(facesConfig);
		IStructuredContentProvider contentProvider = new AdapterFactoryContentProvider(
				getAdapterFactory());
		if (facesConfig.getApplication().size() > 0) {
			ApplicationType application = (ApplicationType) facesConfig
					.getApplication().get(0);
			removeOverviewOthersSectionAdapter(application);

			Object[] applicationNodes = contentProvider
					.getElements(application);
			for (int i = 0, n = applicationNodes.length; i < n; i++) {
				removeOverviewOthersSectionAdapter((EObject) applicationNodes[i]);
			}

		}

		if (facesConfig.getFactory().size() > 0) {
			FactoryType factory = (FactoryType) facesConfig.getFactory().get(0);
			removeOverviewOthersSectionAdapter(factory);

			Object[] factoryNodes = contentProvider.getElements(factory);
			for (int i = 0, n = factoryNodes.length; i < n; i++) {
				removeOverviewOthersSectionAdapter((EObject) factoryNodes[i]);
			}

		}

		if (facesConfig.getLifecycle().size() > 0) {
			LifecycleType lifecycle = (LifecycleType) facesConfig
					.getLifecycle().get(0);
			removeOverviewOthersSectionAdapter(lifecycle);

			Object[] lifecycleNodes = contentProvider.getElements(lifecycle);
			for (int i = 0, n = lifecycleNodes.length; i < n; i++) {
				removeOverviewOthersSectionAdapter((EObject) lifecycleNodes[i]);
			}

		}

	}

	class OverviewOthersSectionAdapter extends AdapterImpl {

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.emf.common.notify.impl.AdapterImpl#isAdapterForType(java.lang.Object)
		 */
		public boolean isAdapterForType(Object type) {
			if (type == OverviewOthersSection.class)
				return true;
			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
		 */
		public void notifyChanged(Notification msg) {
			if (msg.getFeature() == FacesConfigPackage.eINSTANCE
					.getFacesConfigType_Application()
					|| msg.getFeature() == FacesConfigPackage.eINSTANCE
							.getFacesConfigType_Factory()
					|| msg.getFeature() == FacesConfigPackage.eINSTANCE
							.getFacesConfigType_Lifecycle()
					|| msg.getNotifier() instanceof ApplicationType
					|| msg.getNotifier() instanceof FactoryType
					|| msg.getNotifier() instanceof LifecycleType) {

				if (msg.getEventType() == Notification.ADD) {
					EObject newObject = (EObject) msg.getNewValue();
					if (newObject != null) {
						addOverviewOthersSectionAdapter(newObject);
					}
				}

				if (msg.getEventType() == Notification.ADD
						|| msg.getEventType() == Notification.REMOVE
						|| msg.getEventType() == Notification.SET) {
					
					if (Thread.currentThread() == PlatformUI.getWorkbench().getDisplay().getThread()) {
						refreshAll();
					} else {
						PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
							public void run() {
								refreshAll();
							}
						});
					}
				}
			}
		}
	}
}

Back to the top