Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e94856f6e764ce772ab2ddcfb2aff36af33230ca (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
package org.eclipse.papyrus.emf.embeddededitor;

import java.util.EventObject;
import java.util.Set;

import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.provider.EcoreItemProviderAdapterFactory;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
import org.eclipse.emf.transaction.TransactionalCommandStack;
import org.eclipse.emf.transaction.impl.TransactionalCommandStackImpl;
import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.papyrus.emf.embeddededitor.editor.EmbeddedEditor;
import org.eclipse.papyrus.properties.contexts.View;
import org.eclipse.papyrus.properties.runtime.ConfigurationManager;
import org.eclipse.papyrus.properties.runtime.EmbeddedDisplayEngine;
import org.eclipse.papyrus.widgets.Activator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
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.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;


public class SandboxDialog extends TrayDialog {

	protected EmbeddedEditor editor;

	private String shellTitle = "Sample Embedded Editor"; //$NON-NLS-1$

	public static String APPLY_LABEL = "Apply";

	public static int APPLY_ID = 50; //Avoid conflicts with IDialogConstants

	protected SandboxDialog(Shell shell) {
		super(shell);
	}

	@Override
	public void create() {
		super.create();

		SashForm self = new SashForm(getDialogArea(), SWT.NONE);
		self.setOrientation(SWT.VERTICAL);
		self.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Composite topPart = new Composite(self, SWT.NONE);
		topPart.setLayout(new FillLayout());

		SashForm topSash = new SashForm(topPart, SWT.NONE);
		topSash.setOrientation(SWT.HORIZONTAL);
		topSash.setSashWidth(3);

		Composite bottomPart = new Composite(self, SWT.NONE);
		bottomPart.setLayout(new GridLayout(1, true));

		createSelectionPart(topSash);
		createEditorPart(topSash);
		createPropertiesPart(bottomPart);

		getShell().setImage(Activator.getDefault().getImage("icons/papyrus.png")); //$NON-NLS-1$
		getShell().setText(shellTitle);

		self.setWeights(new int[]{ 30, 20 });
		topSash.setWeights(new int[]{ 20, 60 });

		getShell().setSize(750, 500);
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		//The OK button still has the initial focus, but does not
		//automatically get it back when another non-button widget is selected
		//(default = false)
		//This will prevent the dialog to close when a text-widget is
		//validated with the "Enter" key
		getShell().setDefaultButton(null);
		getButton(IDialogConstants.OK_ID).setFocus();

		Button apply = createButton(parent, APPLY_ID, APPLY_LABEL, false);
		apply.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent event) {
				editor.save();
				updateTitle();
			}
		});
	}

	protected void updateTitle() {
		if(editor.isSaveNeeded()) {
			getShell().setText(shellTitle + "*"); //$NON-NLS-1$
		} else {
			getShell().setText(shellTitle);
		}
	}

	@Override
	protected int getShellStyle() {
		int style = super.getShellStyle();
		return style;
	}

	private void createSelectionPart(Composite topPart) {
		Tree tree = new Tree(topPart, SWT.MULTI | SWT.BORDER);
		TreeItem item = new TreeItem(tree, SWT.NONE);
		{
			TreeItem subItem = new TreeItem(item, SWT.NONE);
			subItem.setText("subItem");
			TreeItem subItem2 = new TreeItem(item, SWT.NONE);
			{
				TreeItem subItem3 = new TreeItem(subItem2, SWT.NONE);
				subItem3.setText("subItem3");
			}
			subItem2.setText("subItem2");
		}
		item.setText("item");
		TreeItem item2 = new TreeItem(tree, SWT.NONE);
		{
			TreeItem subItem = new TreeItem(item2, SWT.NONE);
			subItem.setText("subItem");
			TreeItem subItem2 = new TreeItem(item2, SWT.NONE);
			subItem2.setText("subItem2");
		}
		item2.setText("item2");
		TreeItem item3 = new TreeItem(tree, SWT.NONE);
		item3.setText("item3");

		GridData data = new GridData(SWT.FILL, SWT.FILL, false, true);
		data.widthHint = 100;
		tree.setLayoutData(data);
	}

	private void createButtons(Composite topPart) {
		Composite buttonsPart = new Composite(topPart, SWT.NONE);
		GridData data = new GridData(SWT.FILL, SWT.CENTER, false, true);
		buttonsPart.setLayoutData(data);
		buttonsPart.setLayout(new GridLayout());

		Button right = new Button(buttonsPart, SWT.PUSH);
		right.setImage(Activator.getDefault().getImage("org.eclipse.papyrus.widgets", "/icons/arrow_right.gif"));

		Button left = new Button(buttonsPart, SWT.PUSH);
		left.setImage(Activator.getDefault().getImage("org.eclipse.papyrus.widgets", "/icons/arrow_left.gif"));
	}

	private void createEditorPart(Composite topPart) {
		try {
			Composite editorAndButtonsPart = new Composite(topPart, SWT.NONE);
			GridLayout layout = new GridLayout(2, false);
			layout.marginHeight = 0;
			layout.marginWidth = 0;
			layout.horizontalSpacing = 2;
			editorAndButtonsPart.setLayout(layout);
			editorAndButtonsPart.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
			createButtons(editorAndButtonsPart);

			Composite editorPart = new Composite(editorAndButtonsPart, SWT.BORDER);
			FillLayout fillLayout = new FillLayout();
			fillLayout.marginWidth = 0;
			editorPart.setLayout(fillLayout);
			editorPart.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

			editor = createEditor(editorPart);

			editor.addCommandStackListener(new CommandStackListener() {

				public void commandStackChanged(EventObject event) {
					updateTitle();
				}

			});
		} catch (Exception ex) {
			Activator.log.error(ex);
		}
	}

	private EmbeddedEditor createEditor(Composite parent) {
		ResourceSet resourceSet = new ResourceSetImpl();

		//Simple editor
		//			resourceSet.getResource(URI.createPlatformResourceURI("org.eclipse.papyrus.newchild/Model/NewchildConfiguration.xmi", true), true);

		//UML Editor with ExtendedType support
		resourceSet.getResource(URI.createPlatformResourceURI("Test/elementType.uml", true), true);
		TransactionalCommandStack commandStack = new TransactionalCommandStackImpl();
		AdapterFactory adapterFactory = createAdapterFactory();
		TransactionalEditingDomainImpl editingDomain = new TransactionalEditingDomainImpl(adapterFactory, commandStack, resourceSet);


		editor = new EmbeddedEditor(resourceSet, adapterFactory, editingDomain);
		editor.createWidget(parent);

		return editor;
	}

	private AdapterFactory createAdapterFactory() {
		ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

		adapterFactory.addAdapterFactory(new ResourceItemProviderAdapterFactory());
		adapterFactory.addAdapterFactory(new EcoreItemProviderAdapterFactory());
		adapterFactory.addAdapterFactory(new ReflectiveItemProviderAdapterFactory());

		return adapterFactory;
	}

	private void createPropertiesPart(Composite bottomPart) {
		final ScrolledComposite scrolled = new ScrolledComposite(bottomPart, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolled.getVerticalBar().setIncrement(8);
		scrolled.setBackground(scrolled.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		scrolled.setBackgroundMode(SWT.INHERIT_DEFAULT);

		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 120;
		scrolled.setLayoutData(data);

		final Composite properties = new Composite(scrolled, SWT.NONE);
		final EmbeddedDisplayEngine displayEngine = new EmbeddedDisplayEngine();
		properties.setLayout(new FillLayout());

		data = new GridData(SWT.FILL, SWT.FILL, true, false);
		data.heightHint = 120;
		properties.setLayoutData(data);

		scrolled.setContent(properties);

		editor.getViewer().addSelectionChangedListener(new ISelectionChangedListener() {

			public void selectionChanged(SelectionChangedEvent event) {
				ISelection selection = event.getSelection();
				Set<View> views = ConfigurationManager.instance.constraintEngine.getViews(selection);
				displayEngine.display(views, properties, selection, SWT.TOP);
				refreshDisplay(properties, scrolled);
			}

		});

		scrolled.addControlListener(new ControlListener() {

			public void controlMoved(ControlEvent e) {
				//Nothing
			}

			public void controlResized(ControlEvent e) {
				refreshDisplay(properties, scrolled);
			}

		});
	}

	private void refreshDisplay(Composite properties, Composite scrolled) {
		Point size = properties.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		if(scrolled.getSize().x > 0) {
			size.x = scrolled.getSize().x - 21;
		}
		properties.setSize(size);
		properties.layout();
		//		scrolled.layout();
	}

	@Override
	protected void okPressed() {
		editor.save();
		super.okPressed();
	}

	@Override
	protected void cancelPressed() {
		handleShellCloseEvent();
	}

	@Override
	protected void handleShellCloseEvent() {
		if(editor.isSaveNeeded()) {
			MessageDialog dialog = new MessageDialog(getShell(), "Cancel ?", null, "Unsaved changes", MessageDialog.WARNING, new String[]{ "Save", "Don't save", "Cancel" }, 2);
			dialog.open();
			switch(dialog.getReturnCode()) {
			case 0: //Save & quit
				editor.save();
				//No break
			case 1: //Quit
				super.handleShellCloseEvent();
				break;
			case 2:
				return;
			}
		} else {
			super.handleShellCloseEvent();
		}
	}

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

	@Override
	protected boolean isResizable() {
		return true;
	}

	@Override
	protected Composite createDialogArea(Composite parent) {
		Composite control = (Composite)super.createDialogArea(parent);
		//		control.setBackgroundMode(SWT.INHERIT_DEFAULT);
		//		control.setBackground(control.getDisplay().getSystemColor(SWT.COLOR_WHITE));

		return control;
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		shell.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
	}
}

Back to the top