Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f8a0436e93af645e20d86de05f0360e28ac5e627 (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
/*****************************************************************************
 * Copyright (c) 2010, 2014 CEA LIST 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 404874
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.creation;

import java.io.IOException;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.infra.properties.contexts.Context;
import org.eclipse.papyrus.infra.properties.contexts.Section;
import org.eclipse.papyrus.infra.properties.contexts.Tab;
import org.eclipse.papyrus.infra.properties.contexts.View;
import org.eclipse.papyrus.views.properties.Activator;
import org.eclipse.papyrus.views.properties.messages.Messages;
import org.eclipse.papyrus.views.properties.runtime.ConfigurationManager;
import org.eclipse.papyrus.views.properties.runtime.DefaultDisplayEngine;
import org.eclipse.papyrus.views.properties.runtime.DisplayEngine;
import org.eclipse.papyrus.views.properties.xwt.XWTSection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;


/**
 * A dialog used to display an edition form for a given object.
 * The form is described by the given {@link View}s
 *
 * @author Camille Letavernier
 */
// TODO : This dialog should use the Embedded Display Engine
public class EditionDialog extends SelectionDialog {

	private Set<View> views;

	private Object input;

	private Set<XWTSection> sections = new HashSet<XWTSection>();

	private boolean cancelable = false;

	/**
	 *
	 * Constructor.
	 *
	 * @param shell
	 *            The shell in which the dialog will be opened
	 * @param cancelable
	 *            Whether the cancel button is active or not. The values are data-binded, ie.
	 *            edited in real time, before the ok button is pressed. If the cancel button
	 *            is active, callers are responsible for undoing the changes.
	 */
	protected EditionDialog(Shell shell, boolean cancelable) {
		super(shell);
		this.cancelable = cancelable;
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param shell
	 *            The shell in which the dialog will be opened
	 */
	protected EditionDialog(Shell shell) {
		this(shell, false);
	}

	@Override
	public void create() {
		super.create();
		if (getShell().getText() == null || "".equals(getShell().getText())) { //$NON-NLS-1$
			setTitle(Messages.EditionDialog_CreateANewElement);
		}
		getShell().setImage(Activator.getDefault().getImage("org.eclipse.papyrus.infra.widgets", "/icons/papyrus.png")); //$NON-NLS-1$ //$NON-NLS-2$
		getShell().addDisposeListener(new DisposeListener() {

			public void widgetDisposed(DisposeEvent e) {
				dispose();
			}

		});

		display();

		// The values are data-binded, thus are edited in real time. It is not possible to cancel (However, Ctrl+Z should work)
		getButton(IDialogConstants.CANCEL_ID).setEnabled(cancelable);
	}

	@Override
	public Composite getDialogArea() {
		return (Composite) super.getDialogArea();
	}

	/**
	 * Sets the object being edited by this dialog
	 *
	 * @param input
	 */
	public void setInput(Object input) {
		this.input = input;
	}

	/**
	 * Sets the Views used to edit the input object
	 *
	 * @param views
	 */
	public void setViews(Set<View> views) {
		this.views = views;
	}

	/**
	 * Provide information about context and view, as well as the element for which the dialog
	 * should be provided. It will call setViews in turn.
	 *
	 * @param contextName The name of the context
	 * @param contextURI The URI of the context, tries to load context, if it has not been done yet
	 * @param viewName The name of the view
	 */
	public void setViewData(String contextName, String viewName) {
		setViewData(contextName, null, viewName);
	}

	/**
	 * Provide information about context and view, as well as the element for which the dialog
	 * should be provided.
	 *
	 * @param contextName The name of the context
 	 * @param contextURI The URI of the context. If the context is not available yet, the function uses this URI to load it.
 	 * @param viewName The name of the view
	 */
	public void setViewData(String contextName, URI contextURI, String viewName) {

		Context context = ConfigurationManager.getInstance().getContext(contextName);
		if ((context == null) && (contextURI != null)) {
			// might not have been loaded yet
			loadFromURI(contextURI);
			context = ConfigurationManager.getInstance().getContext(contextName);
		}

		Set<View> views = new HashSet<View>();

		if (context != null) {
			for (View view : context.getViews()) {
				if (view.getName().equals(viewName)) {
					views.add(view);
					break;
				}
			}
		}
		if (views.isEmpty()) {
			throw new RuntimeException (String.format(Messages.EditionDialog_CanNotFindview, viewName));
		}
		setViews(views);
	}

	/**
	 * Load the passed context into the configuration manager.
	 */
	protected void loadFromURI(URI uri) {
		try {
			ConfigurationManager.getInstance().addContext(uri);
		}
		catch (IOException io) {
			Activator.log.error(io);
		}
	}

	private void display() {
		DisplayEngine display = new DefaultDisplayEngine();

		IStructuredSelection selection = new StructuredSelection(input);

		Composite parent = new Composite(getDialogArea(), SWT.NONE);
		parent.setLayout(new FillLayout());
		parent.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		getShell().setSize(600, 400);

		final Set<Tab> tabsList = new LinkedHashSet<Tab>();

		for (View view : views) {
			for (Section section : view.getSections()) {
				tabsList.add(section.getTab());
			}
		}

		List<Tab> allTabs = new LinkedList<Tab>(tabsList);

		Collections.sort(allTabs, new Comparator<Tab>() {

			/**
			 * compares two tabs each other
			 *
			 * @param tab1
			 *            first tab to compare
			 * @param tab2
			 *            second tab to compare
			 * @return a negative integer if the first tab should be placed before the second tab
			 */
			public int compare(Tab tab1, Tab tab2) {
				int priority1 = getPriority(tab1);
				int priority2 = getPriority(tab2);

				if (priority1 < priority2) {
					return -1;
				}

				if (priority1 > priority2) {
					return 1;
				}

				// p1 == p2

				priority1 = getXWTTabPriority(tab1);
				priority2 = getXWTTabPriority(tab2);

				if (priority1 < priority2) {
					return -1;
				}

				if (priority1 > priority2) {
					return 1;
				}

				// p1 == p2

				String label1 = tab1.getLabel();
				String label2 = tab2.getLabel();

				return Collator.getInstance().compare(label1, label2);
			}

			private Tab getPreviousTab(Tab tab) {
				Tab afterTab = tab.getAfterTab();
				if (tabsList.contains(afterTab)) {
					return afterTab;
				}

				// not found. Return null
				return null;
			}

			private int getPriority(Tab tab) {
				Tab previousTab = getPreviousTab(tab);
				if (previousTab != null) {
					return getPriority(previousTab) + 1;
				}

				return getXWTTabPriority(tab);
			}

			private int getXWTTabPriority(Tab tab) {
				return tab.getPriority();
			}

		});

		Map<Tab, Composite> tabs = new LinkedHashMap<Tab, Composite>();
		if (allTabs.size() > 1) {
			CTabFolder tabFolder = new CTabFolder(parent, SWT.BOTTOM);
			tabFolder.setSelectionBackground(new Color[] { tabFolder.getDisplay().getSystemColor(SWT.COLOR_WHITE), tabFolder.getBackground() }, new int[] { 100 }, true);
			tabFolder.setLayout(new FillLayout());
			for (Tab tab : allTabs) {
				CTabItem item = new CTabItem(tabFolder, SWT.NONE);
				Composite tabControl = new Composite(tabFolder, SWT.NONE);
				item.setControl(tabControl);
				item.setText(tab.getLabel());
				tabs.put(tab, tabControl);
			}
		} else if (!allTabs.isEmpty()) {
			Tab tab = allTabs.get(0);
			tabs.put(tab, parent);
		}

		for (View view : views) {
			for (Section section : view.getSections()) {
				XWTSection xwtSection = new XWTSection(section, view, display);
				sections.add(xwtSection);

				xwtSection.createControls(tabs.get(section.getTab()), null);
				xwtSection.setInput(null, selection);
				xwtSection.refresh();
			}
		}

		getShell().pack();
		Point size = getShell().getSize();
		int minWidth = 600;
		if (size.x < minWidth) {
			size.x = minWidth;
		}
		getShell().setSize(size);
	}

	/**
	 * Disposes this dialog
	 */
	public void dispose() {
		for (XWTSection section : sections) {
			section.dispose();
		}
		sections.clear();
	}

	@Override
	protected void okPressed() {
		// Bug 404874: Work-around for Mac platform, where finishing the dialog does not trigger
		// focus-lost on the last edited control (which would commit its changes)
		getOkButton().forceFocus();
		super.okPressed();
	}
}

Back to the top