Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cece86b9fad278263f58ab3d1ff76a573f54889d (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
/**
 * Copyright (c) 2011 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 Guyomar (Mia-Software) - Bug 349546 - EMF Facet facetSet editor
 *  Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
 */
package org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.wizard.page;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.Messages;
import org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.exported.wizard.page.ICreateFacetSetWizardPage;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

public class CreateFacetSetWizardPage extends WizardNewFileCreationPage
		implements ICreateFacetSetWizardPage {

	private static final String FILE_EXTENSION = "efacet"; //$NON-NLS-1$
	private static final String MODEL_BASE = "My"; //$NON-NLS-1$

	public CreateFacetSetWizardPage(final String pageId,
			final IStructuredSelection selection) {
		super(pageId, selection);
		setTitle(Messages.CreateFacetSetWizardImpl_FacetModel);
		setDescription(Messages.CreateFacetSetWizardImpl_Create_new_facet_Set);
		setFileName(MODEL_BASE + "." + CreateFacetSetWizardPage.FILE_EXTENSION); //$NON-NLS-1$
		// Try and get the resource selection to determine a current directory
		// for the file dialog.
		if ((selection != null) && !selection.isEmpty()) {
			// Get the resource...
			getResource(selection);
		}
	}

	private void getResource(final IStructuredSelection selection) {
		final Object selectedElement = selection.iterator().next();
		if (selectedElement instanceof IResource) {
			// Get the resource parent, if its a file.
			IResource selectedResource = (IResource) selectedElement;
			if (selectedResource.getType() == IResource.FILE) {
				selectedResource = selectedResource.getParent();
			}
			// This gives us a directory...
			if ((selectedResource instanceof IFolder)
					|| (selectedResource instanceof IProject)) {
				// Set this for the container.
				setContainerFullPath(selectedResource.getFullPath());
				// Make up a unique new name here.
				final String modelFileExt = CreateFacetSetWizardPage.FILE_EXTENSION;
				String modelFilename = MODEL_BASE + "." + modelFileExt; //$NON-NLS-1$
				for (int i = 1; ((IContainer) selectedResource)
						.findMember(modelFilename) != null; ++i) {
					modelFilename = MODEL_BASE + i + "." + modelFileExt; //$NON-NLS-1$
				}
				setFileName(modelFilename);
			}
		}
	}

	@Override
	protected boolean validatePage() {
		boolean valid = super.validatePage();

		if (valid) {
			final String extension = new Path(getFileName()).getFileExtension();
			if ((extension == null)
					|| !CreateFacetSetWizardPage.FILE_EXTENSION
							.equalsIgnoreCase(extension)) {
				setErrorMessage(Messages.CreateFacetSetWizardImpl_File_extension_restriction);
				valid = false;
			}
			final IPath containerFullPath = getContainerFullPath();
			// try {
			// if (!PluginUtils.isInPluginProject(containerFullPath)) {
			// setMessage(
			// Messages.CreateFacetSetWizardPage_Not_a_Plugin_project,
			// IMessageProvider.WARNING);
			// }
			// } catch (final CoreException e) {
			// Logger.logError(e, Activator.getDefault());
			// }
		}

		return valid;
	}

	public IFile getModelFile() {
		return ResourcesPlugin.getWorkspace().getRoot()
				.getFile(getContainerFullPath().append(getFileName()));
	}
}

Back to the top