diff options
| author | Florian Barbin | 2017-07-13 14:52:51 +0000 |
|---|---|---|
| committer | Florian Barbin | 2017-07-20 13:23:25 +0000 |
| commit | 7d329c8189e2a7f9cceb87fea7ad28b7ab7d025d (patch) | |
| tree | ab8031cbf1f5dcc3a1ddaf41ce0972943979a85e | |
| parent | c0db050727c5c1113c2803976147ac419f30748c (diff) | |
| download | org.eclipse.sirius-7d329c8189e2a7f9cceb87fea7ad28b7ab7d025d.tar.gz org.eclipse.sirius-7d329c8189e2a7f9cceb87fea7ad28b7ab7d025d.tar.xz org.eclipse.sirius-7d329c8189e2a7f9cceb87fea7ad28b7ab7d025d.zip | |
[516669] Adds the "Update Representation location" functionality
* Adds an action on workspace Resource context menu (Folder, Project,
File etc.) to update the representation locations.
* This action delegates to the representationLocationManager to move
representations to the expected location.
Bug: 516669
Change-Id: I89966ccf9f870adbbefe87208e37c111d92ace1e
Signed-off-by: Florian Barbin <florian.barbin@obeo.fr>
6 files changed, 255 insertions, 13 deletions
diff --git a/plugins/org.eclipse.sirius.common/src/org/eclipse/sirius/common/tools/api/util/WorkspaceUtil.java b/plugins/org.eclipse.sirius.common/src/org/eclipse/sirius/common/tools/api/util/WorkspaceUtil.java index b2f4ce54c9..4ffff3058b 100644 --- a/plugins/org.eclipse.sirius.common/src/org/eclipse/sirius/common/tools/api/util/WorkspaceUtil.java +++ b/plugins/org.eclipse.sirius.common/src/org/eclipse/sirius/common/tools/api/util/WorkspaceUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 THALES GLOBAL SERVICES. + * Copyright (c) 2011, 2017 THALES GLOBAL SERVICES. * 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 @@ -16,7 +16,6 @@ import java.util.List; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; @@ -35,10 +34,10 @@ public final class WorkspaceUtil { } /** - * Get the files with a specific extension in the given projects. + * Get the files with a specific extension in the given containers (Project, Folder etc.). * - * @param projects - * the project in which to look for + * @param containers + * the containers in which to look for * * @param extension * the file extension @@ -47,14 +46,13 @@ public final class WorkspaceUtil { * * examples : * - * getFilesFromWorkspace (null) : return all files from the - * workspace getFilesFromWorkspaces ("java" : return all java - * sources files from the workspace + * getFilesFromWorkspace (null) : return all files from the workspace getFilesFromWorkspaces ("java" : + * return all java sources files from the workspace */ - public static List<IFile> getFilesFromWorkspace(final Collection<IProject> projects, final String extension) { + public static List<IFile> getFilesFromWorkspace(final Collection<IContainer> containers, final String extension) { List<IFile> allFiles = new ArrayList<IFile>(); try { - allFiles = getWorkspaceFiles(projects.toArray(new IProject[projects.size()])); + allFiles = getWorkspaceFiles(containers.toArray(new IContainer[containers.size()])); } catch (final CoreException e1) { // do nothing -- fail silently } @@ -78,8 +76,7 @@ public final class WorkspaceUtil { * @param resources * the resources to scan * @throws CoreException - * if {@link org.eclipse.core.resources.IContainer#members()} on - * a resource fails. Reasons include: + * if {@link org.eclipse.core.resources.IContainer#members()} on a resource fails. Reasons include: * <ul> * <li>This resource does not exist.</li> * <li>This resource is a project that is not open.</li> diff --git a/plugins/org.eclipse.sirius.ui/plugin.properties b/plugins/org.eclipse.sirius.ui/plugin.properties index 6438d87eb2..af672d1fa7 100644 --- a/plugins/org.eclipse.sirius.ui/plugin.properties +++ b/plugins/org.eclipse.sirius.ui/plugin.properties @@ -351,8 +351,10 @@ SpecificEditorInputTranformer_transformationFailure = Failing of EditorInput tra TraceabilityMarkerNavigationProvider_dialogMessage = Select the Representations (referencing {0}) you would like to open. TraceabilityMarkerNavigationProvider_dialogTitle = Open representations referencing {0} TraceabilityMarkerNavigationProvider_noSessionFoundError = We can''t find a session associated to the given editor "{0}" -TreeEditorDialogFactory_error = An error occured in {0} +TreeEditorDialogFactory_error = An error occurred in {0} TypedVariableValueDialog_title = Enter variable value(s) +UpdateRepresentationsLocationHandler_errorMsg = An error occurred during the {0} execution +UpdateRepresentationsLocationHandler_label = Update Representations Location UserSession_openRepresentationTask = Open representation... UserSession_representationNotFound = Cannot find representation: {0} UserSession_viewpointSelectionFailed = Cannot select viewpoints: {0}. @@ -390,6 +392,8 @@ ModelingModelProvider.name=Sirius Modeling Workspace category.siriusCommands.name = Sirius Commands command.control.name = Control command.uncontrol.name = Uncontrol +command.update.rep.location.name = Update Representations Location +command.update.rep.location.label = Update Representations Location commonFilter.danglingRepresentations.description = Hides the dangling representations, ie. whitout represented element or whose element has been detached or deleted. commonFilter.danglingRepresentations.name = Representations known as dangling commonFilter.emptyRepresentationType.description = Hides Representation descriptions without instances diff --git a/plugins/org.eclipse.sirius.ui/plugin.xml b/plugins/org.eclipse.sirius.ui/plugin.xml index e1765a7b83..bec0b114b4 100644 --- a/plugins/org.eclipse.sirius.ui/plugin.xml +++ b/plugins/org.eclipse.sirius.ui/plugin.xml @@ -284,6 +284,13 @@ properties="isSemanticResourceFileTester" class="org.eclipse.sirius.ui.tools.internal.views.common.FileHandledByOpenedSessionTester"> </propertyTester> + <propertyTester + class="org.eclipse.sirius.ui.business.internal.commands.UpdateRepresentationLocationCommandPropertyTester" + id="org.eclipse.sirius.ui.split.representation.propertyTester" + namespace="org.eclipse.sirius.ui" + properties="isContainingAirdFiles" + type="java.lang.Object"> + </propertyTester> </extension> <extension point="org.eclipse.ui.navigator.navigatorContent"> @@ -673,5 +680,37 @@ </visibleWhen> </command> </menuContribution> + <menuContribution + locationURI="popup:org.eclipse.ui.popup.any?after=additions"> + <command + commandId="org.eclipse.sirius.ui.splitrepresentations.command" + label="%command.update.rep.location.label" + style="push"> + <visibleWhen> + <iterate + ifEmpty="false" + operator="and"> + <and> + <instanceof + value="org.eclipse.core.resources.IResource"> + </instanceof> + <test + forcePluginActivation="true" + property="org.eclipse.sirius.ui.isContainingAirdFiles"> + </test> + </and> + </iterate> + </visibleWhen> + </command> + </menuContribution> </extension> + + <extension + point="org.eclipse.ui.commands"> + <command + defaultHandler="org.eclipse.sirius.ui.business.internal.commands.UpdateRepresentationsLocationHandler" + id="org.eclipse.sirius.ui.splitrepresentations.command" + name="%command.update.rep.location.name"> + </command> + </extension> </plugin> diff --git a/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/commands/UpdateRepresentationLocationCommandPropertyTester.java b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/commands/UpdateRepresentationLocationCommandPropertyTester.java new file mode 100644 index 0000000000..7f3d64b15e --- /dev/null +++ b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/commands/UpdateRepresentationLocationCommandPropertyTester.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2017 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.business.internal.commands; + +import java.util.Collections; + +import org.eclipse.core.expressions.PropertyTester; +import org.eclipse.core.resources.IResource; + +/** + * A property tester to check enablement of the Update Representation Location command. + * + * @author fbarbin + * + */ +public class UpdateRepresentationLocationCommandPropertyTester extends PropertyTester { + + @Override + public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { + if (Boolean.getBoolean("createLocalRepresentationInSeparateResource")) { //$NON-NLS-1$ + return receiver instanceof IResource && !UpdateRepresentationsLocationHandler.getAirdFiles(Collections.singletonList(receiver)).isEmpty(); + } else { + return false; + } + } +} diff --git a/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/commands/UpdateRepresentationsLocationHandler.java b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/commands/UpdateRepresentationsLocationHandler.java new file mode 100644 index 0000000000..49d804147d --- /dev/null +++ b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/commands/UpdateRepresentationsLocationHandler.java @@ -0,0 +1,162 @@ +/******************************************************************************* + * Copyright (c) 2017 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.business.internal.commands; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.text.MessageFormat; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Stream; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl.FactoryImpl; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.sirius.business.api.helper.SiriusUtil; +import org.eclipse.sirius.business.api.query.ResourceQuery; +import org.eclipse.sirius.business.internal.representation.DRepresentationLocationManager; +import org.eclipse.sirius.common.tools.api.util.WorkspaceUtil; +import org.eclipse.sirius.ui.tools.internal.actions.repair.RepresentationFilesNeedCloseSessionValidator; +import org.eclipse.sirius.viewpoint.DAnalysis; +import org.eclipse.sirius.viewpoint.DRepresentation; +import org.eclipse.sirius.viewpoint.DRepresentationDescriptor; +import org.eclipse.sirius.viewpoint.provider.Messages; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.WorkspaceModifyOperation; +import org.eclipse.ui.handlers.HandlerUtil; + +import com.google.common.collect.Lists; + +/** + * The class will handle the Update representation location command from a project, a folder or an aird context menu. + * + * @author fbarbin + * + */ +public class UpdateRepresentationsLocationHandler extends AbstractHandler { + + private static final String ERROR_MSG = MessageFormat.format(Messages.UpdateRepresentationsLocationHandler_errorMsg, Messages.UpdateRepresentationsLocationHandler_label); + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection selection = HandlerUtil.getCurrentSelection(event); + if (selection instanceof IStructuredSelection) { + Collection<IFile> files = getAirdFiles(((IStructuredSelection) selection).toList()); + files.stream().forEach(file -> handleIFile(file)); + } + return null; + + } + + private void handleIFile(IFile iFile) { + RepresentationFilesNeedCloseSessionValidator validator = new RepresentationFilesNeedCloseSessionValidator(Messages.UpdateRepresentationsLocationHandler_label); + if (validator.validate(iFile).isOK()) { + UpdateRepresentationLocationOperation operation = new UpdateRepresentationLocationOperation(iFile); + try { + PlatformUI.getWorkbench().getProgressService().run(true, false, operation); + } catch (InvocationTargetException | InterruptedException e) { + throw new RuntimeException(ERROR_MSG, e); + } + } + } + + /** + * Gets all aird files found in the given resource list (Folder, Project, File etc). + * + * @param selection + * the selected element. + * @return the collection of IFile with aird extension found + */ + public static Collection<IFile> getAirdFiles(List<?> selection) { + Collection<IFile> files = Lists.newArrayList(); + selection.iterator().forEachRemaining(el -> { + if (el instanceof IContainer) { + files.addAll(WorkspaceUtil.getFilesFromWorkspace(Collections.singleton((IContainer) el), SiriusUtil.SESSION_RESOURCE_EXTENSION)); + } else if (el instanceof IFile) { + files.add((IFile) el); + } + }); + return files; + } + + /** + * A WorkspaceModifyOperation to execute the update. + * + * @author fbarbin + * + */ + private class UpdateRepresentationLocationOperation extends WorkspaceModifyOperation { + IFile file; + + UpdateRepresentationLocationOperation(IFile file) { + this.file = file; + } + + @Override + protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { + monitor.beginTask(Messages.UpdateRepresentationsLocationHandler_label, IProgressMonitor.UNKNOWN); + + URI uri = URI.createPlatformResourceURI(file.getFullPath().toOSString(), true); + ResourceSet resourceSet = new ResourceSetImpl(); + TransactionalEditingDomain domain = FactoryImpl.INSTANCE.createEditingDomain(resourceSet); + Resource airdResource = resourceSet.getResource(uri, true); + + // We execute changes in a EMF Transaction to keep the consistency of the aird in case of error. + domain.getCommandStack().execute(new RecordingCommand(domain) { + + @Override + protected void doExecute() { + Stream<DRepresentationDescriptor> descriptors = airdResource.getContents().stream().filter(DAnalysis.class::isInstance).map(DAnalysis.class::cast) + .flatMap(d -> d.getOwnedViews().stream()).flatMap(v -> v.getOwnedRepresentationDescriptors().stream()); + descriptors.forEach(repDesc -> { + DRepresentation representation = repDesc.getRepresentation(); + DRepresentationLocationManager representationLocationManager; + representationLocationManager = new DRepresentationLocationManager(); + Resource resourceforRepresentation = representationLocationManager.getOrCreateRepresentationResource(representation, airdResource); + resourceSet.getResources().add(resourceforRepresentation); + resourceforRepresentation.getContents().add(representation); + repDesc.updateRepresentation(representation); + }); + try { + for (Resource resource : resourceSet.getResources()) { + ResourceQuery query = new ResourceQuery(resource); + if (query.isAirdOrSrmResource()) { + if (resource.getContents().isEmpty()) { + resource.delete(Collections.emptyMap()); + } else { + resource.save(Collections.emptyMap()); + } + } + } + } catch (IOException e) { + throw new RuntimeException(ERROR_MSG, e); + } + + } + }); + } + } +} diff --git a/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/viewpoint/provider/Messages.java b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/viewpoint/provider/Messages.java index 753d31cdca..f71225b4fc 100644 --- a/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/viewpoint/provider/Messages.java +++ b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/viewpoint/provider/Messages.java @@ -1191,6 +1191,12 @@ public final class Messages { @TranslatableMessage public static String GraphicalRepresentationHandler_disableViewpointButton_tooltip; + @TranslatableMessage + public static String UpdateRepresentationsLocationHandler_label; + + @TranslatableMessage + public static String UpdateRepresentationsLocationHandler_errorMsg; + // CHECKSTYLE:ON private Messages() { |
