diff options
| author | Belqassim Djafer | 2014-11-25 10:27:37 +0000 |
|---|---|---|
| committer | Belqassim Djafer | 2015-01-07 14:44:47 +0000 |
| commit | 87435bee99fa27421d4d1f59a16bec7f4ae979b9 (patch) | |
| tree | 76998bdbbfee4be6c2d5d6ce7c4d1eb5f9241fe9 | |
| parent | cb8499c09408259c240f11574c166c742b81434c (diff) | |
| download | org.eclipse.sirius-87435bee99fa27421d4d1f59a16bec7f4ae979b9.tar.gz org.eclipse.sirius-87435bee99fa27421d4d1f59a16bec7f4ae979b9.tar.xz org.eclipse.sirius-87435bee99fa27421d4d1f59a16bec7f4ae979b9.zip | |
[448344] Fix creating a ModelingProject with an existing name
Creating a ModelingProject fails when another project having the same
name in a different case on Windows and Mac.
Bug: 448344
Change-Id: I4cdec058eddcc963c0e671cfa40303cef9c25d00
Signed-off-by: Belqassim Djafer <belqassim.djafer@obeo.fr>
5 files changed, 206 insertions, 3 deletions
diff --git a/plugins/org.eclipse.sirius.editor/src/org/eclipse/sirius/editor/tools/internal/wizards/ViewpointSpecificationProjectWizard.java b/plugins/org.eclipse.sirius.editor/src/org/eclipse/sirius/editor/tools/internal/wizards/ViewpointSpecificationProjectWizard.java index 2c89dd5622..ec26e9f7c7 100644 --- a/plugins/org.eclipse.sirius.editor/src/org/eclipse/sirius/editor/tools/internal/wizards/ViewpointSpecificationProjectWizard.java +++ b/plugins/org.eclipse.sirius.editor/src/org/eclipse/sirius/editor/tools/internal/wizards/ViewpointSpecificationProjectWizard.java @@ -27,6 +27,7 @@ import org.eclipse.jface.wizard.Wizard; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.sirius.editor.editorPlugin.SiriusEditorPlugin; import org.eclipse.sirius.ui.tools.api.project.ViewpointSpecificationProject; +import org.eclipse.sirius.ui.tools.internal.wizards.pages.NewModelingProjectCreationWizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; @@ -121,7 +122,7 @@ public class ViewpointSpecificationProjectWizard extends Wizard implements INewW */ @Override public void addPages() { - newProjectPage = new WizardNewProjectCreationPage(SiriusEditorPlugin.getPlugin().getString("_UI_ViewpointSpecificationProjectWizard_label")); //$NON-NLS-1$ + newProjectPage = new NewModelingProjectCreationWizardPage(SiriusEditorPlugin.getPlugin().getString("_UI_ViewpointSpecificationProjectWizard_label")); //$NON-NLS-1$ newProjectPage.setInitialProjectName(MODULE_NAME_PREFIX); newProjectPage.setTitle(SiriusEditorPlugin.getPlugin().getString("_UI_ViewpointSpecificationProjectWizard_label")); //$NON-NLS-1$ newProjectPage.setDescription(SiriusEditorPlugin.getPlugin().getString("_UI_ViewpointSpecificationProjectWizard_description")); //$NON-NLS-1$ diff --git a/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/editor/vsm/ViewpointSpecificationProjectCreationTest.java b/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/editor/vsm/ViewpointSpecificationProjectCreationTest.java index cd81152b36..2bcdd83cff 100644 --- a/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/editor/vsm/ViewpointSpecificationProjectCreationTest.java +++ b/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/editor/vsm/ViewpointSpecificationProjectCreationTest.java @@ -23,6 +23,7 @@ import org.eclipse.sirius.tests.support.api.TestsUtil; import org.eclipse.sirius.tests.swtbot.support.api.AbstractSiriusSwtBotGefTestCase; import org.eclipse.sirius.tests.swtbot.support.api.condition.ItemEnabledCondition; import org.eclipse.sirius.tests.swtbot.support.utils.SWTBotUtils; +import org.eclipse.sirius.ui.tools.api.views.modelexplorerview.IModelExplorerView; import org.eclipse.swtbot.eclipse.gef.finder.SWTGefBot; import org.eclipse.swtbot.swt.finder.waits.Conditions; import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; @@ -49,6 +50,8 @@ public class ViewpointSpecificationProjectCreationTest extends AbstractSiriusSwt private static final String WIZARD_NEW = "New"; + private static final String WIZARD_CANCEL = "Cancel"; + private static final String WIZARD_PROJECT_NAME = "Project name:"; private static final String WIZARD_VIEWPOINT_SPECIFICATION_MODEL_NAME = "Viewpoint Specification Model name:"; @@ -122,6 +125,45 @@ public class ViewpointSpecificationProjectCreationTest extends AbstractSiriusSwt } + /** + * Ensure that it is not possible to create a modeling project when another + * existing project has the same name in different case on Windows and Mac. + */ + public void testCreatingProjectWithExistingName() { + if (isMacOrWindowsOS()) { + // Create a VSP + IProject project = createViewpointSpecificationProject(bot, VSM_PROJECT_NAME, VSM); + assertTrue("The created VSM project should exist.", ResourcesPlugin.getWorkspace().getRoot().getProject(VSM_PROJECT_NAME).exists()); + assertTrue("The created VSM project should be open.", ResourcesPlugin.getWorkspace().getRoot().getProject(VSM_PROJECT_NAME).isOpen()); + + // Try to create an other project with the same existing name in + // different case into the workspace and check the error message + tryToCreateProjectWithExistingProjectName("A project with " + VSM_PROJECT_NAME + " name should exist in workspace and the Next button should not be enabled"); + + // Delete the project only from workspace + closeAllEditors(); + try { + project.delete(false, true, new NullProgressMonitor()); + } catch (CoreException e) { + fail("Cannot delete the VSM Project"); + } + SWTBotUtils.waitAllUiEvents(); + + // Try to create an other project with the same existing name in + // different case on disk and check the error message + tryToCreateProjectWithExistingProjectName("A project with " + VSM_PROJECT_NAME + " name should exist on the disk and the Next button should not be enabled"); + } + } + + private void tryToCreateProjectWithExistingProjectName(String msgIfCreationIsPossible) { + bot.viewById(IModelExplorerView.ID).setFocus(); + bot.menu(WIZARD_FILE).menu(WIZARD_NEW).menu(WIZARD_VIEWPOINT_SPECIFICATION_PROJECT).click(); + bot.waitUntilWidgetAppears(Conditions.shellIsActive(WIZARD_NEW + " " + WIZARD_VIEWPOINT_SPECIFICATION_PROJECT)); + bot.textWithLabel(WIZARD_PROJECT_NAME).setText(VSM_PROJECT_NAME.toUpperCase()); + assertEquals(msgIfCreationIsPossible, false, bot.button(WIZARD_NEXT).isEnabled()); + bot.button(WIZARD_CANCEL).click(); + } + private void checkNatures(IProject project) { // Check the natures IProjectNature nature = null; @@ -153,7 +195,13 @@ public class ViewpointSpecificationProjectCreationTest extends AbstractSiriusSwt * Create a Sirius Specification Project and wait until the creation is * done. * + * @param bot + * the editor + * * @param vsmProjectName + * VSP name + * @param vsmFileName + * VSM file name * * @return the created project. */ @@ -194,4 +242,8 @@ public class ViewpointSpecificationProjectCreationTest extends AbstractSiriusSwt return ResourcesPlugin.getWorkspace().getRoot().getProject(vsmProjectName); } + private static boolean isMacOrWindowsOS() { + return System.getProperty("os.name").contains("Windows") || System.getProperty("os.name").contains("Mac"); + } + } diff --git a/plugins/org.eclipse.sirius.ui/META-INF/MANIFEST.MF b/plugins/org.eclipse.sirius.ui/META-INF/MANIFEST.MF index e4db4d4ca0..ba617ebd1a 100644 --- a/plugins/org.eclipse.sirius.ui/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.sirius.ui/META-INF/MANIFEST.MF @@ -110,7 +110,8 @@ Require-Bundle: com.google.guava;bundle-version="[11.0.2,16.0)", org.eclipse.ui.navigator;bundle-version="3.5.200", org.eclipse.ui.views.properties.tabbed;bundle-version="3.5.300", org.eclipse.core.expressions;bundle-version="3.4.400", - org.eclipse.ui.editors;bundle-version="3.8.0" + org.eclipse.ui.editors;bundle-version="3.8.0", + org.eclipse.core.filesystem;bundle-version="1.3.200" Eclipse-LazyStart: true Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-ActivationPolicy: lazy diff --git a/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/tools/internal/wizards/ModelingProjectWizard.java b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/tools/internal/wizards/ModelingProjectWizard.java index 80a6074d7f..b817b3bb83 100644 --- a/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/tools/internal/wizards/ModelingProjectWizard.java +++ b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/tools/internal/wizards/ModelingProjectWizard.java @@ -21,6 +21,7 @@ import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.sirius.ui.tools.api.project.ModelingProjectManager; +import org.eclipse.sirius.ui.tools.internal.wizards.pages.NewModelingProjectCreationWizardPage; import org.eclipse.sirius.viewpoint.provider.SiriusEditPlugin; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.INewWizard; @@ -117,7 +118,7 @@ public class ModelingProjectWizard extends Wizard implements INewWizard { */ @Override public void addPages() { - newProjectPage = new WizardNewProjectCreationPage(SiriusEditPlugin.getPlugin().getString("_UI_ModelingProjectWizard_label")); //$NON-NLS-1$ + newProjectPage = new NewModelingProjectCreationWizardPage(SiriusEditPlugin.getPlugin().getString("_UI_ModelingProjectWizard_label")); //$NON-NLS-1$ newProjectPage.setInitialProjectName(""); newProjectPage.setTitle(SiriusEditPlugin.getPlugin().getString("_UI_ModelingProjectWizard_label")); //$NON-NLS-1$ newProjectPage.setDescription(SiriusEditPlugin.getPlugin().getString("_UI_ModelingProjectWizard_description")); //$NON-NLS-1$ diff --git a/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/tools/internal/wizards/pages/NewModelingProjectCreationWizardPage.java b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/tools/internal/wizards/pages/NewModelingProjectCreationWizardPage.java new file mode 100644 index 0000000000..0122d2d37e --- /dev/null +++ b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/tools/internal/wizards/pages/NewModelingProjectCreationWizardPage.java @@ -0,0 +1,148 @@ +/******************************************************************************* + * Copyright (c) 2014 Obeo. + * 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: + * Obeo - initial API and implementation + *******************************************************************************/ + +package org.eclipse.sirius.ui.tools.internal.wizards.pages; + +import java.io.File; + +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.internal.resources.Project; +import org.eclipse.core.internal.utils.Messages; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.Path; +import org.eclipse.osgi.util.NLS; +import org.eclipse.sirius.common.tools.api.util.StringUtil; +import org.eclipse.ui.dialogs.WizardNewProjectCreationPage; +import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; +import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; + +/** + * Override {@link WizardNewProjectCreationPage} to avoid ErrorException when + * another project having the same project name exists on disk or workspace in a + * different case. + * + * @see org.eclipse.ui.dialogs.WizardNewProjectCreationPage + * @author <a href="mailto:belqassim.djafer@obeo.fr">Belqassim Djafer</a> + * + */ +@SuppressWarnings("restriction") +public class NewModelingProjectCreationWizardPage extends WizardNewProjectCreationPage { + + /** + * {@inheritDoc}. + * + * @see org.eclipse.ui.dialogs.WizardNewProjectCreationPage. + * WizardNewProjectCreationPage(String). + */ + public NewModelingProjectCreationWizardPage(String pageName) { + super(pageName); + } + + /* + * Prevent the user when another project having the same project name exists + * on disk or workspace in a different case and avoid an ErrorException. + * @see org.eclipse.ui.dialogs.WizardNewProjectCreationPage#validatePage() + */ + @Override + protected boolean validatePage() { + setErrorMessage(null); + setMessage(null); + boolean validProjectName = true; + if (resourceExistsInWorkspace()) { + validProjectName = false; + } + // check for collision with existing folder of different case on disk + if (!StringUtil.isEmpty(getProjectName()) && validProjectName) { + if (resourceExistsOnDisk()) { + validProjectName = false; + } + } + return validProjectName; + } + + /** + * Return true if another project having the same project name exists on + * disk (in a different case or not) + */ + private boolean resourceExistsOnDisk() { + IFileStore store = ((Project) getProjectHandle()).getStore(); + String name = store.fetchInfo().getName(); + File folder = new File(getLocationPath().toOSString()); + // Return true if the project location have the same project name with + // same case + if (folder.getName().equals(getProjectName())) { + String msg = NLS.bind(Messages.localstore_fileExists, new Path(folder.toString()).removeLastSegments(1).append(folder.getName()).toOSString()); + // Set a warning message + setMessage(msg, 2); + } else { + // Return true if the project location have the same project name in + // different case + if (folder.getName().toUpperCase().equals(getProjectName().toUpperCase()) && isMacOrWindowsOS()) { + String msg = NLS.bind(Messages.resources_existsLocalDifferentCase, new Path(folder.toString()).removeLastSegments(1).append(folder.getName()).toOSString()); + setErrorMessage(msg); + return true; + } + } + return resourceExistsInFolderLocation(store, name, folder); + } + + /** + * Return true if another project having the same project name exists on the + * given folder (in a different case or not) + */ + private boolean resourceExistsInFolderLocation(IFileStore store, String name, File folder) { + boolean projectNameExistsInSameCase = false; + if (folder.listFiles() != null) { + for (int i = 0; i < folder.listFiles().length && !projectNameExistsInSameCase; i++) { + if (name != null && folder.listFiles()[i].getName().equals(getProjectName())) { + String msg = NLS.bind(Messages.localstore_fileExists, new Path(folder.toString()).append(folder.listFiles()[i].getName()).toOSString()); + setMessage(msg, 2); + projectNameExistsInSameCase = true; + } else { + if (name != null && folder.listFiles()[i].getName().toUpperCase().equals(getProjectName().toUpperCase()) && isMacOrWindowsOS()) { + String msg = NLS.bind(Messages.resources_existsLocalDifferentCase, new Path(folder.toString()).append(folder.listFiles()[i].getName()).toOSString()); + setErrorMessage(msg); + return true; + } + } + } + } + return false; + } + + /** + * Return true if another project having the same project name exists in + * workspace + */ + private boolean resourceExistsInWorkspace() { + boolean existsInWorkspace = false; + if (!getProjectName().isEmpty()) { + IProject[] projects = IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getProjects(); + for (int i = 0; i < projects.length && !existsInWorkspace; i++) { + if (getProjectName().toUpperCase().equals(projects[i].getName().toUpperCase()) && isMacOrWindowsOS()) { + setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage); + existsInWorkspace = true; + } else { + if (getProjectName().equals(projects[i].getName())) { + setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage); + return true; + } + } + } + } + return existsInWorkspace; + } + + private static boolean isMacOrWindowsOS() { + return System.getProperty("os.name").contains("Windows") || System.getProperty("os.name").contains("Mac"); + // return false; + } +} |
