Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5433dbb99123ec5843432313ac6896a6d734c5fe (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
/*******************************************************************************
 * Copyright (c) 2009, 2011, 2012 Mia-Software.
 * 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:
 *    Nicolas Bros (Mia-Software) - initial API and implementation
 *    Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
 *    Grégoire Dupé (Mia-Software) - Bug 387470 - [EFacet][Custom] Editors
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.util.emf.ui.internal.exported.util.wizard.page;

import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.papyrus.emf.facet.util.core.DebugUtils;
import org.eclipse.papyrus.emf.facet.util.emf.ui.internal.Activator;
import org.eclipse.papyrus.emf.facet.util.emf.ui.internal.Messages;
import org.eclipse.papyrus.emf.facet.util.emf.ui.internal.exported.util.composite.EClassifierSelectionControl;
import org.eclipse.papyrus.emf.facet.util.emf.ui.internal.exported.util.wizard.page.exception.SelectedEClassifierRuntimeException;
import org.eclipse.papyrus.emf.facet.util.emf.ui.internal.exported.wizard.page.ISelectEClassifierWizardPage;
import org.eclipse.papyrus.emf.facet.util.emf.ui.internal.exported.wizard.page.ISelectEPackageWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.dialogs.FilteredList;

/**
 * @since 0.3
 */
public class SelectEClassifierWizardPage<T extends EClassifier> extends
		WizardPage implements ISelectEClassifierWizardPage<T> {

	private static final boolean DEBUG = DebugUtils.getDebugStatus(Activator
			.getDefault());
	private EClassifierSelectionControl<T> eClassSelection;
	private final Class<? extends T> eTypeOption;
	private String metamodelNsUri;

	public SelectEClassifierWizardPage(final Class<? extends T> eTypeOption,
			final EPackage ePackage) {
		super("Whatever"); //$NON-NLS-1$
		if (eTypeOption == EClass.class) {
			setTitle(Messages.Select_EClass);
		} else if (eTypeOption == EDataType.class) {
			setTitle(Messages.Select_EDataType);
		} else if (eTypeOption == EClassifier.class) {
			setTitle(Messages.Select_EClassifier);
		} else if (eTypeOption == ETypedElement.class) {
			setTitle(Messages.Select_ETypedElement);
		}
		if (ePackage != null) {
			this.metamodelNsUri = ePackage.getNsURI();
		}
		this.eTypeOption = eTypeOption;
	}

	@Override
	public boolean isPageComplete() {
		return this.eClassSelection.getSelectedEClassifier() != null;
	}

	@Override
	public void setVisible(final boolean visible) {
		super.setVisible(visible);
		if (getPreviousPage() instanceof SelectEPackageWizardPage) {
			final ISelectEPackageWizardPage selectEPackage = (ISelectEPackageWizardPage) getPreviousPage();
			this.metamodelNsUri = selectEPackage.getSelectedEPackage()
					.getNsURI();
			if (this.metamodelNsUri != null) {
				setDescription(selectEPackage.getSelectedEPackage()
						.getNsURI());
			}
		}
		this.eClassSelection.updateList(this.metamodelNsUri);
	}

	public void createControl(final Composite parent) {
		this.eClassSelection = new EClassifierSelectionControl<T>(parent,
				this.metamodelNsUri, this.eTypeOption);
		final FilteredList filteredList = this.eClassSelection
				.getFilteredList();

		filteredList.addSelectionListener(new SelectionListener() {
			public void widgetSelected(final SelectionEvent event) {
				if (event.item != null) {
					updateButton();
					// setPageComplete(filteredList.getSelection().length == 1);
				}
			}

			public void widgetDefaultSelected(final SelectionEvent event) {
				if (getWizard().canFinish()) {
					getWizard().performFinish();
				}
				if (getNextPage() != null) {
					goToNextPage();
				}
			}
		});

		this.eClassSelection.getFilterText().addModifyListener(
				new ModifyListener() {

					public void modifyText(final ModifyEvent event) {
						updateButton();
					}
				});

		// avoid the page being "complete" when still on a previous page
		filteredList.setSelection(new int[0]);
		setPageComplete(false);

		setControl(this.eClassSelection);
	}

	protected void goToNextPage() {
		getContainer().showPage(getNextPage());
	}

	protected void updateButton() {
		if ((getContainer() != null) && (getContainer().getCurrentPage() != null)) {
			getContainer().updateButtons();
		}
	}

	public T getSelectedEClassifier() {
		return this.eClassSelection.getSelectedEClassifier();
	}

	/**
	 * Select the <code>eclassifierName</code> into the list.
	 * 
	 * @param eclassifierName
	 *            the name of the classifier to select.
	 */
	public void selectEClassifier(final String eclassifierName) {
		DebugUtils.debug(DEBUG);
		boolean doItNow = true;
		// We get all the jobs
		for (final Job job : Job.getJobManager().find(null)) {
			// We check if the job of 'FilteredList' is ended. If not, we
			// recursively call this method to check again without interfering
			// with the 'FilteredList' job (doing an asynchrony call).
			if (job.getClass().getName()
					.startsWith(FilteredList.class.getName())) {
				asyncSelectionClassifier(eclassifierName);
				DebugUtils.debug(DEBUG, "Selection defered."); //$NON-NLS-1$
				doItNow = false;
				break;
			}
		}

		// If the job 'FilteredList' is done, we can do the selection.
		if (doItNow) {
			internalSelectEClassifier(eclassifierName);
		}
	}

	private void asyncSelectionClassifier(final String eclassifierName) {
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				selectEClassifier(eclassifierName);
			}
		});
	}

	private void internalSelectEClassifier(final String packageName) {
		final FilteredList filteredList = this.eClassSelection
				.getFilteredList();
		filteredList.setSelection(new String[] { packageName });
		if (getSelectedEClassifier() == null) {
			throw new SelectedEClassifierRuntimeException();
		}
		DebugUtils.debug(DEBUG, "Selection setting finished."); //$NON-NLS-1$
		DebugUtils.debug(DEBUG,
				"selectedEClassifier=" + getSelectedEClassifier()); //$NON-NLS-1$
	}
}

Back to the top