Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ed5b3c1fad3e9c131c211e2d771019a02efe5867 (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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 * 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:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.uml.diagram.internal.ui.wizards;

import static org.eclipse.papyrus.uml.diagram.wizards.utils.WizardsHelper.adapt;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.cdo.eresource.CDOResourceFolder;
import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.cdo.util.CDOURIUtil;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.DecoratingLabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.cdo.core.IPapyrusRepository;
import org.eclipse.papyrus.cdo.internal.core.IInternalPapyrusRepository;
import org.eclipse.papyrus.cdo.internal.ui.views.ModelRepositoryItemProvider;
import org.eclipse.papyrus.cdo.uml.diagram.internal.ui.l10n.Messages;
import org.eclipse.papyrus.uml.diagram.wizards.Activator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import com.google.common.base.Strings;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

/**
 * This is the NewModelPage type. Enjoy.
 */
public class NewModelPage extends WizardPage {

	public static final String PAGE_ID = "NewCDOModel"; //$NON-NLS-1$

	private Text folderText;

	private TreeViewer foldersTree;

	private Text nameText;

	private IPapyrusRepository repository;

	private CDOResourceNode selectedNode;

	private boolean synchronizingFolderSelection;

	public NewModelPage(IStructuredSelection selection, EventBus bus, String modelKindName) {

		super(PAGE_ID);

		setTitle(NLS.bind(Messages.NewModelPage_0, modelKindName));
		setDescription(NLS.bind(Messages.NewModelPage_1, modelKindName));

		if (!selection.isEmpty()) {
			selectedNode = adapt(selection.getFirstElement(), CDOResourceNode.class);
			if ((selectedNode != null) && !(selectedNode instanceof CDOResourceFolder)) {
				selectedNode = selectedNode.getFolder();
			}
		}

		bus.register(this);
	}

	@Override
	public void createControl(Composite parent) {
		Composite myComposite = new Composite(parent, SWT.NONE);
		GridDataFactory.fillDefaults().grab(true, true).applyTo(myComposite);
		myComposite.setLayout(new GridLayout(2, false));

		Label label = new Label(myComposite, SWT.NONE);
		label.setText(Messages.NewModelPage_2);
		GridDataFactory.swtDefaults().span(2, 1).applyTo(label);

		folderText = new Text(myComposite, SWT.BORDER);
		GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(folderText);
		updateFolderSelection();

		foldersTree = new TreeViewer(myComposite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		GridDataFactory.fillDefaults().grab(true, true).span(2, 1).applyTo(foldersTree.getControl());
		ModelRepositoryItemProvider itemProvider = new ModelRepositoryItemProvider(null);
		foldersTree.setContentProvider(itemProvider);
		foldersTree.setLabelProvider(new DecoratingLabelProvider(itemProvider, PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator()));
		foldersTree.setSorter(itemProvider);
		if (getRepository() != null) {
			foldersTree.setInput(getRepository());
		}
		if (selectedNode != null) {
			foldersTree.setSelection(new StructuredSelection(selectedNode));
		}

		new Label(myComposite, SWT.NONE).setText(Messages.NewModelPage_3);

		nameText = new Text(myComposite, SWT.BORDER);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);
		nameText.setText(suggestName("model", "di")); //$NON-NLS-1$ //$NON-NLS-2$

		setControl(myComposite);

		folderText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				findFolderSelection();
				validatePage();
			}
		});

		foldersTree.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				selectedNode = selection.isEmpty() ? null : adapt(selection.getFirstElement(), CDOResourceNode.class);
				updateFolderSelection();
				validatePage();
			}
		});

		nameText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				validatePage();
			}
		});

		validatePage();
	}

	@Subscribe
	public void setRepository(IPapyrusRepository repository) {
		this.repository = repository;

		if (foldersTree != null) {
			foldersTree.setInput(repository);
		}

		validatePage();
	}

	private IInternalPapyrusRepository getRepository() {
		return ((IInternalPapyrusRepository) repository);
	}

	CDOView getView() {
		IInternalPapyrusRepository repo = getRepository();
		return (repo == null) ? null : repo.getMasterView();
	}

	String getSelectedFolderPath() {
		return folderText.getText().trim();
	}

	void updateFolderSelection() {
		whileSynchronizingFolderSelection(new Runnable() {

			@Override
			public void run() {
				CDOResourceFolder selected = null;
				if (selectedNode instanceof CDOResourceFolder) {
					selected = (CDOResourceFolder) selectedNode;
				} else if (selectedNode != null) {
					// will be null if the selectedNode is contained by the root
					// resource
					selected = selectedNode.getFolder();
				}

				if (selected == null) {
					folderText.setText(""); //$NON-NLS-1$
				} else {
					folderText.setText(selected.getPath());
				}
			}
		});
	}

	void findFolderSelection() {
		whileSynchronizingFolderSelection(new Runnable() {

			@Override
			public void run() {
				String folder = folderText.getText().trim();
				CDOView view = getView();
				if (view != null) {
					if (folder.equals("")) { //$NON-NLS-1$
						foldersTree.setSelection(StructuredSelection.EMPTY);
					} else {
						try {
							foldersTree.setSelection(new StructuredSelection(view.getResourceNode(folder)));
						} catch (Exception e) {
							// normal occurrence when the folder doesn't exist
						}
					}
				}
			}
		});
	}

	private void whileSynchronizingFolderSelection(Runnable runnable) {
		if (!synchronizingFolderSelection) {
			synchronizingFolderSelection = true;

			try {
				runnable.run();
			} finally {
				synchronizingFolderSelection = false;
			}
		}
	}

	String suggestName(String baseName, String extension) {
		String result = String.format("%s.%s", baseName, extension); //$NON-NLS-1$
		CDOView view = getView();

		if (view != null) {
			for (int i = 1;; i++) {
				if (!view.hasResource(getNewResourcePath(result))) {
					break;
				} else {
					// use %s instead of %d to avoid any thousands separators
					// (hah! that there should be so many models)
					result = String.format("%s%s.%s", baseName, i, extension); //$NON-NLS-1$
				}
			}
		}

		return result;
	}

	String getNewResourcePath(String name) {
		String result = null;
		String path = getSelectedFolderPath();

		if (path.equals("")) { //$NON-NLS-1$
			// it's a resource in the root
			result = "/" + name; //$NON-NLS-1$
		} else {
			StringBuilder buf = new StringBuilder();

			if (!path.startsWith("/")) { //$NON-NLS-1$
				buf.append("/"); //$NON-NLS-1$
			}
			buf.append(path);
			if (!path.endsWith("/")) { //$NON-NLS-1$
				buf.append("/"); //$NON-NLS-1$
			}
			buf.append(name);

			result = buf.toString();
		}

		return result;
	}

	String getNewResourceName() {
		return (nameText == null) ? null : nameText.getText().trim();
	}

	void setNewResourceName(String newName) {
		nameText.setText(newName.trim());
	}

	public URI createNewModelResourceURI() {
		return CDOURIUtil.createResourceURI(getView(), getNewResourcePath(getNewResourceName()));
	}

	private String getExtension() {
		String result = null;
		String name = getNewResourceName();

		if (name != null) {
			// the proper extension is whatever follows the *last* '.',
			// but for our purposes we need e.g. "profile.uml" to be
			// an extension
			int dot = name.indexOf('.');
			if (dot >= 0) {
				result = name.substring(dot + 1);
			}
		}

		return result;
	}

	/**
	 * Diagram extension changed.
	 *
	 * @param newExtension
	 *            the new extension
	 * @return result of validation of the new extension
	 */
	public IStatus diagramExtensionChanged(String newExtension) {
		String currentExtension = getExtension();
		if (!newExtension.equals(currentExtension)) {

			String oldFileName = getNewResourceName();
			String base = oldFileName;
			if (currentExtension != null) {
				// take one off for the '.'
				base = base.substring(0, base.length() - currentExtension.length() - 1);
			}
			String newFileName = suggestName(base, newExtension);

			setNewResourceName(newFileName);

			String message1 = org.eclipse.papyrus.uml.diagram.wizards.Messages.NewModelFilePage_new_diagram_category_needs_specific_extension;
			String message2 = NLS.bind(org.eclipse.papyrus.uml.diagram.wizards.Messages.NewModelFilePage_diagram_file_was_renamed, oldFileName, newFileName);
			String message = message1 + message2;
			Status resultStatus = new Status(IStatus.INFO, Activator.PLUGIN_ID, message);

			String errorMessage = getErrorMessage();
			if (errorMessage != null) {
				resultStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, errorMessage);
			}
			return resultStatus;
		}
		return Status.OK_STATUS;
	}

	void validatePage() {
		setMessage(null, NONE);
		setPageComplete(true);

		String name = getNewResourceName();
		if (getView() == null) {
			setMessage(Messages.NewModelPage_16, ERROR);
			setPageComplete(false);
		} else if (Strings.isNullOrEmpty(name)) {
			setMessage(Messages.NewModelPage_17, ERROR);
			setPageComplete(false);
		} else {
			String path = getNewResourcePath(name);
			if (getView().hasResource(path)) {
				setMessage(NLS.bind(Messages.NewModelPage_18, path), ERROR);
				setPageComplete(false);
			} else {
				// check existence of folder (if any)
				String folderPath = getSelectedFolderPath();
				if (!Strings.isNullOrEmpty(folderPath)) {
					if (!getView().hasResource(folderPath)) {
						setMessage(Messages.NewModelPage_19, WARNING);
					}
				}
			}
		}
	}
}

Back to the top