Skip to main content
summaryrefslogtreecommitdiffstats
blob: c958545297958b0423b68e4086ad28ccc6d1845e (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
/*******************************************************************************
 * Copyright (c) 2007 committers of openArchitectureWare 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.xtend.shared.ui.core.properties;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.internal.ui.preferences.PropertyAndPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.eclipse.xtend.shared.ui.Activator;
import org.eclipse.xtend.shared.ui.core.IXtendXpandProject;
import org.eclipse.xtend.shared.ui.core.metamodel.Contributor;
import org.eclipse.xtend.shared.ui.core.metamodel.MetamodelContributorRegistry;
import org.eclipse.xtend.shared.ui.core.preferences.PreferenceConstants;
import org.eclipse.xtend.shared.ui.internal.XtendLog;

/**
 * This class displays a property page or a preference page and lets the user
 * select the meta model contributors that should be enabled for the specific
 * scope.
 * 
 * @author Peter Friese
 * @author Dennis Huebner
 */
public class MetamodelContributorsPropertyAndPreferencePage extends
		PropertyAndPreferencePage {

	private Button downButton;
	private Button upButton;

	/**
	 * Renders a human readable representation of the meta model contributors.
	 */
	class TableLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object element, int columnIndex) {
			if (element instanceof Contributor) {
				Contributor contributor = (Contributor) element;
				return contributor.getDisplayName();
			}
			return element.toString();
		}

		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
	}

	private CheckboxTableViewer checkboxTableViewer;

	private IPreferenceStore prefStore;

	private List<Contributor> availableMetamodelContributors;
	private Table table;

	private List<Contributor> getMetamodelContributors(boolean includeDisabled) {
		Map<String, Contributor> availableMetamodelContributors = MetamodelContributorRegistry
				.getRegisteredMetamodelContributors();

		// retrieve all configured metamodels
		prefStore = getPreferenceStore();
		fixMetamodelContributorPreferences(prefStore);
		String rawString = prefStore
				.getString(PreferenceConstants.METAMODELCONTRIBUTORS);

		List<Contributor> result = new ArrayList<Contributor>();

		// first, get all enabled contributors
		if (isNotEmpty(rawString)) {
			String[] selected = rawString.split(",");

			for (String metaModelContributorClassName : selected) {
				Contributor contributor = availableMetamodelContributors
						.get(metaModelContributorClassName);
				if (contributor != null)
					result.add(contributor);
			}
		}
		if (includeDisabled) {
			// now, get disabled contributors
			for (Contributor contributor : availableMetamodelContributors
					.values()) {
				if (!result.contains(contributor)) {
					result.add(contributor);
				}
			}
		}

		return result;
	}

	/**
	 * fix: package name from UML2MetamodelContributor was wrong. For
	 * compatibility reasons we fix this name here on the fly. Later we could
	 * offer a migration path.
	 */
	private static void fixMetamodelContributorPreferences(
			IPreferenceStore prefStore) {
		String metamodelContr = prefStore
				.getString(PreferenceConstants.METAMODELCONTRIBUTORS);
		if (metamodelContr.indexOf("openarchitecturware") > 0) {
			metamodelContr = metamodelContr.replace("openarchitecturware",
					"openarchitectureware");
			prefStore.setValue(PreferenceConstants.METAMODELCONTRIBUTORS,
					metamodelContr);
		}
	}

	private boolean isNotEmpty(String rawString) {
		return rawString != null && !rawString.equals("");
	}

	@Override
	protected IPreferenceStore doGetPreferenceStore() {
		IPreferenceStore store;
		if (isProjectPreferencePage()) {
			// get java project
			IProject project = getProject();
			store = new ScopedPreferenceStore(new ProjectScope(project),
					Activator.getId());
		} else {
			store = new ScopedPreferenceStore(new InstanceScope(), Activator
					.getId());
		}
		return store;
	}

	private void updateButtonStates() {
		int index = table.getSelectionIndex();
		if (index == 0) {
			upButton.setEnabled(false);
		} else {
			upButton.setEnabled(true);
		}
		if (index >= checkboxTableViewer.getTable().getItemCount() - 1) {
			downButton.setEnabled(false);
		} else {
			downButton.setEnabled(true);
		}
	}

	@Override
	protected Control createPreferenceContent(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		gridLayout.marginHeight = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginWidth = 0;
		container.setLayout(gridLayout);

		final Label activatedMetamodelContributorsLabel = new Label(container,
				SWT.NONE);
		activatedMetamodelContributorsLabel
				.setText("&Activated metamodel contributors:");
		new Label(container, SWT.NONE);

		checkboxTableViewer = CheckboxTableViewer.newCheckList(container,
				SWT.BORDER);
		checkboxTableViewer.setLabelProvider(new TableLabelProvider());
		checkboxTableViewer.setContentProvider(new ArrayContentProvider());
		table = checkboxTableViewer.getTable();
		table.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				updateButtonStates();
			}

		});
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final Composite composite = new Composite(container, SWT.NONE);
		composite
				.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false));
		final GridLayout gridLayout_1 = new GridLayout();
		composite.setLayout(gridLayout_1);

		upButton = new Button(composite, SWT.NONE);
		upButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ISelection selection = checkboxTableViewer.getSelection();
				if (selection instanceof IStructuredSelection) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
					Object firstElement = structuredSelection.getFirstElement();
					if (firstElement instanceof Contributor) {
						Contributor contributor = (Contributor) firstElement;
						int index = availableMetamodelContributors
								.indexOf(contributor);
						availableMetamodelContributors.remove(contributor);
						availableMetamodelContributors.add(index - 1,
								contributor);
						checkboxTableViewer.refresh();
					}
				}
				updateButtonStates();
			}
		});
		final GridData gridData_1 = new GridData(SWT.FILL, SWT.CENTER, false,
				false);
		gridData_1.widthHint = 75;
		upButton.setLayoutData(gridData_1);
		upButton.setText("&Up");

		downButton = new Button(composite, SWT.NONE);
		downButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ISelection selection = checkboxTableViewer.getSelection();
				if (selection instanceof IStructuredSelection) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
					Object firstElement = structuredSelection.getFirstElement();
					if (firstElement instanceof Contributor) {
						Contributor contributor = (Contributor) firstElement;
						int index = availableMetamodelContributors
								.indexOf(contributor);
						availableMetamodelContributors.remove(contributor);
						availableMetamodelContributors.add(index + 1,
								contributor);
						checkboxTableViewer.refresh();
					}
				}
				updateButtonStates();
			}
		});
		final GridData gridData = new GridData(SWT.FILL, SWT.CENTER, false,
				false);
		gridData.widthHint = 75;
		downButton.setLayoutData(gridData);
		downButton.setText("&Down");
		//
		setupData();
		return container;
	}

	/**
	 * Fills the table viewer with a list of available metamodel contributors
	 * and visually checks the ones that are enabled.
	 */
	private void setupData() {
		availableMetamodelContributors = getMetamodelContributors(true);
		checkboxTableViewer.setInput(availableMetamodelContributors);

		Collection<Contributor> enabledMetamodelContributors = getMetamodelContributors(false);
		checkboxTableViewer.setCheckedElements(enabledMetamodelContributors
				.toArray());
	}

	@Override
	protected String getPreferencePageID() {
		return "org.eclipse.xtend.shared.ui.properties.MetamodelContributorsPropertyPage";
	}

	@Override
	protected String getPropertyPageID() {
		return "org.eclipse.xtend.shared.ui.properties.MetamodelContributorsPropertyPage";
	}

	@Override
	protected boolean hasProjectSpecificOptions(IProject project) {
		IPreferenceStore preferenceStore = getPreferenceStore();
		return preferenceStore
				.getBoolean(PreferenceConstants.PROJECT_SPECIFIC_METAMODEL);
	}

	@Override
	public boolean performOk() {
		if (isProjectPreferencePage()) {
			prefStore.setValue(PreferenceConstants.PROJECT_SPECIFIC_METAMODEL,
					useProjectSettings());
		}
		prefStore.setValue(PreferenceConstants.METAMODELCONTRIBUTORS,
				createStoreString());
		try {
			((ScopedPreferenceStore) prefStore).save();
		} catch (IOException e1) {
			XtendLog.logError(e1);
		}

		if (isProjectPreferencePage()) {
			final IProject project = getProject();
			final Job job = new Job("Analyzing " + project.getName() + " ...") {
				@Override
				protected IStatus run(IProgressMonitor monitor) {
					try {
						new WorkspaceModifyOperation() {
							@Override
							protected void execute(IProgressMonitor monitor)
									throws CoreException,
									java.lang.reflect.InvocationTargetException,
									InterruptedException {
								IXtendXpandProject p = Activator
										.getExtXptModelManager().findProject(
												project);
								if (p != null) {
									monitor.beginTask("...", p
											.getRegisteredResources().length);
									p.analyze(monitor);
									monitor.done();
								}
							}
						}.run(monitor);
					} catch (InvocationTargetException e) {
						XtendLog.logError(e);
					} catch (InterruptedException e) {
						XtendLog.logError(e);
					}
					return Status.OK_STATUS;
				}
			};
			job.schedule();
		} else {
			final Job job = new Job("Analyzing all oAW projects...") {

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					Activator.getExtXptModelManager().analyze(monitor);
					return Status.OK_STATUS;
				}

			};
			job.schedule();
		}

		return super.performOk();
	}

	/**
	 * @return
	 */
	private String createStoreString() {
		Object[] checkedElements = checkboxTableViewer.getCheckedElements();
		String result = "";
		for (int i = 0; i < checkedElements.length; i++) {
			Object object = checkedElements[i];
			if (object instanceof Contributor) {
				Contributor contributor = (Contributor) object;
				String className = contributor.getClassName();
				result += className;
			}
			if (i < checkedElements.length - 1) {
				result += ",";
			}
		}
		return result;
	}
}

Back to the top