Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal')
-rwxr-xr-xplugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/adapter/TextDocumentViewAdapterFactory.java61
-rwxr-xr-xplugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/handlers/RenameTextDocumentEditorHandler.java187
-rwxr-xr-xplugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/menu/TextDocumentDynamicContribution.java110
-rwxr-xr-xplugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/messages/Messages.java34
-rwxr-xr-xplugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/messages/messages.properties2
-rwxr-xr-xplugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/modelresource/TextDocumentSharedModelResource.java105
-rwxr-xr-xplugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/CreateTextDocumentViewTypeCommandHelper.java134
-rwxr-xr-xplugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/ExtendedViewPrototype.java41
-rwxr-xr-xplugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/TextDocumentViewPrototype.java176
9 files changed, 850 insertions, 0 deletions
diff --git a/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/adapter/TextDocumentViewAdapterFactory.java b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/adapter/TextDocumentViewAdapterFactory.java
new file mode 100755
index 00000000000..0f5d93057c5
--- /dev/null
+++ b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/adapter/TextDocumentViewAdapterFactory.java
@@ -0,0 +1,61 @@
+/*****************************************************************************
+ * Copyright (c) 2021 CEA LIST and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) <vincent.lorenzo@cea.fr> - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.textedit.ui.internal.adapter;
+
+import org.eclipse.core.runtime.IAdapterFactory;
+import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.IOpenable;
+import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.IOpenableWithContainer;
+import org.eclipse.papyrus.infra.textedit.textdocument.TextDocument;
+
+/**
+ * Adapter factory converting TextDocument to IOpenable.
+ *
+ */
+public class TextDocumentViewAdapterFactory implements IAdapterFactory {
+
+ /**
+ *
+ * @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object, java.lang.Class)
+ *
+ * @param adaptableObject
+ * @param adapterType
+ * @return
+ */
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Override
+ public Object getAdapter(Object adaptableObject, Class adapterType) {
+ if (adapterType == IOpenable.class) {
+ if (adaptableObject instanceof TextDocument) {
+ final TextDocument document = (TextDocument) adaptableObject;
+ return new IOpenableWithContainer.Openable(adaptableObject, document.getSemanticContext());
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ *
+ * @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
+ *
+ * @return
+ */
+ @Override
+ public Class<?>[] getAdapterList() {
+ return new Class[] { IOpenable.class };
+ }
+
+}
diff --git a/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/handlers/RenameTextDocumentEditorHandler.java b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/handlers/RenameTextDocumentEditorHandler.java
new file mode 100755
index 00000000000..3badb8b7d97
--- /dev/null
+++ b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/handlers/RenameTextDocumentEditorHandler.java
@@ -0,0 +1,187 @@
+/*****************************************************************************
+ * Copyright (c) 2021 CEA LIST and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) <vincent.lorenzo@cea.fr> - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.textedit.ui.internal.handlers;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.expressions.IEvaluationContext;
+import org.eclipse.emf.common.command.Command;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.window.Window;
+import org.eclipse.papyrus.infra.core.services.ServiceException;
+import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
+import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
+import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
+import org.eclipse.papyrus.infra.textedit.textdocument.TextDocument;
+import org.eclipse.papyrus.infra.textedit.textdocument.TextDocumentPackage;
+import org.eclipse.papyrus.infra.textedit.ui.Activator;
+import org.eclipse.papyrus.infra.textedit.ui.internal.messages.Messages;
+import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
+import org.eclipse.papyrus.infra.ui.util.ServiceUtilsForHandlers;
+import org.eclipse.papyrus.infra.ui.util.ServiceUtilsForIEvaluationContext;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IEditorPart;
+
+/**
+ * This handler allows to rename a Papyrus TextDocument. The handler is activated when
+ * the active editor provide an adapter to {@link TextDocument}
+ *
+ */
+public class RenameTextDocumentEditorHandler extends AbstractHandler {
+
+ private static final String NEW_TEXTDOCUMENT_NAME = Messages.RenameTextDocumentEditorHandler_NewName;
+
+ private static final String RENAME_AN_EXISTING_TEXTDOCUMENT = Messages.RenameTextDocumentEditorHandler_RenameAnExistingDocument;
+
+ /**
+ * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
+ * @param event
+ * @return
+ * @throws ExecutionException
+ *
+ */
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ executeTransaction(event);
+
+ return null;
+ }
+
+ /**
+ * Execute as transaction
+ *
+ * @param event
+ */
+ private void executeTransaction(ExecutionEvent event) {
+ final TransactionalEditingDomain domain = getEditingDomain(event);
+ final TextDocument textDocument = lookupTextDocument(event);
+ if (textDocument == null || domain == null) {
+ return;
+ }
+
+ // TODO : manage Internationalization (look at RenameDiagramHandler, to know how to code it)
+ // TODO: currently Internationalization works only for Diagram and Table
+ final String currentName = textDocument.getName();
+ String newName = null;
+ InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), RENAME_AN_EXISTING_TEXTDOCUMENT, NEW_TEXTDOCUMENT_NAME, currentName, null);
+ if (dialog.open() == Window.OK) {
+ newName = dialog.getValue();
+ }
+ if (newName != null && !newName.equals(currentName)) {
+ final SetRequest request = new SetRequest(domain, textDocument, TextDocumentPackage.eINSTANCE.getTextDocument_Name(), newName);
+ final IElementEditService commandProvider = ElementEditServiceUtils.getCommandProvider(textDocument);
+ Command command = null;
+ if (commandProvider != null) {
+ command = GMFtoEMFCommandWrapper.wrap(commandProvider.getEditCommand(request));
+ }
+ if (command != null) {
+ domain.getCommandStack().execute(command);
+ }
+ }
+ }
+
+
+
+ /**
+ *
+ * @param event
+ * an execution event
+ * @return
+ * the associated {@link IEvaluationContext} or <code>null</code>
+ */
+ private IEvaluationContext getIEvaluationContext(final ExecutionEvent event) {
+ if (event.getApplicationContext() instanceof IEvaluationContext) {
+ return (IEvaluationContext) event.getApplicationContext();
+ }
+ return null;
+ }
+
+ /**
+ *
+ * @param event
+ * an excution event
+ * @return
+ * the editing domain
+ */
+ private TransactionalEditingDomain getEditingDomain(final ExecutionEvent event) {
+ TransactionalEditingDomain domain = null;
+ try {
+ domain = ServiceUtilsForHandlers.getInstance().getTransactionalEditingDomain(event);
+ } catch (ServiceException e) {
+ Activator.log.error("EditingDomain not found", e); //$NON-NLS-1$
+ }
+ return domain;
+ }
+
+ /**
+ *
+ * @param event
+ * an ExceutionEvent
+ * @return
+ * the {@link TextDocument} wrapped by the current editor, or <code>null</code>
+ */
+ private TextDocument lookupTextDocument(final ExecutionEvent event) {
+ TextDocument textDocument = null;
+ final IEvaluationContext context = getIEvaluationContext(event);
+ if (context != null) {
+ try {
+ IEditorPart editor = ServiceUtilsForIEvaluationContext.getInstance().getService(IMultiDiagramEditor.class, context);
+ textDocument = editor.getAdapter(TextDocument.class);
+ } catch (ServiceException e) {
+ Activator.log.error("The current editor can't be found", e); //$NON-NLS-1$
+ }
+ }
+ return textDocument;
+ }
+
+ /**
+ * Get the TextDocument model element. This method can be used from {@link #execute(ExecutionEvent)} or {@link #setEnabled(Object)}.
+ *
+ * @return The current table
+ * @throws ServiceException
+ */
+ private TextDocument lookupTextDocument(final IEvaluationContext context) throws ServiceException {
+ IEditorPart editor = ServiceUtilsForIEvaluationContext.getInstance().getService(IMultiDiagramEditor.class, context);
+ final TextDocument textDocument = editor.getAdapter(TextDocument.class);
+ return textDocument;
+ }
+
+ /**
+ * Called by framework. Need to set the enabled flag.
+ *
+ * @see org.eclipse.core.commands.AbstractHandler#setEnabled(java.lang.Object)
+ *
+ * @param evaluationContext
+ */
+ @Override
+ public void setEnabled(Object evaluationContext) {
+ boolean enable = false;
+ if (evaluationContext instanceof IEvaluationContext) {
+ IEvaluationContext context = (IEvaluationContext) evaluationContext;
+ try {
+ // Try to get the TextDocument
+ enable = lookupTextDocument(context) != null;
+ } catch (ServiceException e) {
+ // Can't find ServiceRegistry: disable
+ }
+ }
+ // In all other cases
+ setBaseEnabled(enable);
+ }
+}
diff --git a/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/menu/TextDocumentDynamicContribution.java b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/menu/TextDocumentDynamicContribution.java
new file mode 100755
index 00000000000..c2cf9ec1128
--- /dev/null
+++ b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/menu/TextDocumentDynamicContribution.java
@@ -0,0 +1,110 @@
+/*****************************************************************************
+ * Copyright (c) 2021 CEA LIST and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) <vincent.lorenzo@cea.fr> - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.textedit.ui.internal.menu;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.ActionContributionItem;
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.papyrus.infra.textedit.representation.TextDocumentRepresentation;
+import org.eclipse.papyrus.infra.viewpoints.policy.DynamicContribution;
+import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker;
+import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype;
+
+/**
+ * Represent the dynamic contribution of the Papyrus TextDocument menu.
+ */
+public class TextDocumentDynamicContribution extends DynamicContribution {
+
+ /**
+ * Constructor.
+ */
+ public TextDocumentDynamicContribution() {
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param id
+ */
+ public TextDocumentDynamicContribution(String id) {
+ super(id);
+ }
+
+ /**
+ * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
+ *
+ * @return
+ */
+ @Override
+ protected IContributionItem[] getContributionItems() {
+ final EObject selection = getSelection();
+ if (selection == null) {
+ return new IContributionItem[0];
+ }
+
+ // build a list of all the available prototypes
+ List<ViewPrototype> data = new ArrayList<>();
+ for (final ViewPrototype proto : PolicyChecker.getFor(selection).getPrototypesFor(selection)) {
+ if (!(proto.getRepresentationKind() instanceof TextDocumentRepresentation)) {
+ continue;
+ }
+ data.add(proto);
+ }
+
+ // sort them
+ Collections.sort(data, new ViewPrototype.Comp());
+
+ // build the full labels
+ List<String> labels = new ArrayList<>(data.size());
+ String last = null;
+ boolean first = true;
+ for (ViewPrototype item : data) {
+ String label = item.getLabel();
+ if (last != null && last.equals(label)) {
+ // name collision
+ if (first) {
+ labels.set(labels.size() - 1, data.get(labels.size() - 1).getFullLabel());
+ first = false;
+ }
+ labels.add(item.getFullLabel());
+ } else {
+ labels.add(label);
+ last = label;
+ first = true;
+ }
+ }
+
+ // build the menu
+ List<IContributionItem> items = new ArrayList<>(data.size());
+ for (int i = 0; i != data.size(); i++) {
+ final ViewPrototype proto = data.get(i);
+ String label = labels.get(i);
+ items.add(new ActionContributionItem(new Action(label, proto.getIconDescriptor()) {
+ @Override
+ public void run() {
+ proto.instantiateOn(selection);
+ }
+ }));
+ }
+ return items.toArray(new IContributionItem[items.size()]);
+ }
+
+}
diff --git a/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/messages/Messages.java b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/messages/Messages.java
new file mode 100755
index 00000000000..ffbf36f9dd5
--- /dev/null
+++ b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/messages/Messages.java
@@ -0,0 +1,34 @@
+/*****************************************************************************
+ * Copyright (c) 2021 CEA LIST and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) <vincent.lorenzo@cea.fr> - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.textedit.ui.internal.messages;
+
+import org.eclipse.osgi.util.NLS;
+
+/**
+ *
+ */
+public class Messages extends NLS {
+ private static final String BUNDLE_NAME = Messages.class.getPackageName() + ".messages"; //$NON-NLS-1$
+ public static String RenameTextDocumentEditorHandler_NewName;
+ public static String RenameTextDocumentEditorHandler_RenameAnExistingDocument;
+ static {
+ // initialize resource bundle
+ NLS.initializeMessages(BUNDLE_NAME, Messages.class);
+ }
+
+ private Messages() {
+ }
+}
diff --git a/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/messages/messages.properties b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/messages/messages.properties
new file mode 100755
index 00000000000..5b1c2e18719
--- /dev/null
+++ b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/messages/messages.properties
@@ -0,0 +1,2 @@
+RenameTextDocumentEditorHandler_NewName=New name:
+RenameTextDocumentEditorHandler_RenameAnExistingDocument=Rename an existing TextDocument
diff --git a/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/modelresource/TextDocumentSharedModelResource.java b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/modelresource/TextDocumentSharedModelResource.java
new file mode 100755
index 00000000000..2a740b1906b
--- /dev/null
+++ b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/modelresource/TextDocumentSharedModelResource.java
@@ -0,0 +1,105 @@
+/*****************************************************************************
+ * Copyright (c) 2021 CEA LIST and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) <vincent.lorenzo@cea.fr> - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.textedit.ui.internal.modelresource;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.papyrus.infra.core.resource.AbstractModelWithSharedResource;
+import org.eclipse.papyrus.infra.core.resource.IModel;
+import org.eclipse.papyrus.infra.textedit.textdocument.TextDocument;
+
+/**
+ * This class is used to save {@link TextDocument} in the notation file
+ */
+public class TextDocumentSharedModelResource extends AbstractModelWithSharedResource<TextDocument> implements IModel {
+
+
+ /**
+ * File extension used for notation.
+ */
+ public static final String NOTATION_FILE_EXTENSION = "notation"; //$NON-NLS-1$
+
+ /**
+ * Model ID.
+ */
+ public static final String MODEL_ID = "org.eclipse.papyrus.infra.textedit.ui.modelresource.TextDocumentSharedModelResource"; //$NON-NLS-1$
+
+
+ /**
+ *
+ * Constructor.
+ *
+ */
+ public TextDocumentSharedModelResource() {
+
+ }
+
+
+ /**
+ * Get the file extension used for this model.
+ *
+ * @see org.eclipse.papyrus.infra.core.resource.AbstractBaseModel#getModelFileExtension()
+ *
+ * @return
+ */
+ @Override
+ protected String getModelFileExtension() {
+ return NOTATION_FILE_EXTENSION;
+ }
+
+ /**
+ * Get the identifier used to register this model.
+ *
+ * @see org.eclipse.papyrus.infra.core.resource.AbstractBaseModel#getIdentifier()
+ *
+ * @return
+ */
+ @Override
+ public String getIdentifier() {
+ return MODEL_ID;
+ }
+
+ /**
+ * Add a new initialized {@link TextDocument} to the model.
+ *
+ * @param textDocument
+ * The textDocument to remove.
+ */
+ public void addTextDocument(final TextDocument textDocument) {
+ getResource().getContents().add(textDocument);
+ }
+
+ /**
+ * remove an existing {@link TextDocument} to the model.
+ *
+ * @param textDocument
+ * The textDocument to remove.
+ */
+ public void removeTextDocument(final TextDocument textDocument) {
+ getResource().getContents().remove(textDocument);
+ }
+
+ /**
+ *
+ * @see org.eclipse.papyrus.infra.core.resource.AbstractModelWithSharedResource#isModelRoot(org.eclipse.emf.ecore.EObject)
+ *
+ * @param object
+ * @return
+ */
+ @Override
+ protected boolean isModelRoot(EObject object) {
+ return false;
+ }
+} \ No newline at end of file
diff --git a/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/CreateTextDocumentViewTypeCommandHelper.java b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/CreateTextDocumentViewTypeCommandHelper.java
new file mode 100755
index 00000000000..af9a3698c31
--- /dev/null
+++ b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/CreateTextDocumentViewTypeCommandHelper.java
@@ -0,0 +1,134 @@
+/*****************************************************************************
+ * Copyright (c) 2021 CEA LIST.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.textedit.ui.internal.viewpoint;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
+import org.eclipse.papyrus.infra.architecture.representation.PapyrusRepresentationKind;
+import org.eclipse.papyrus.infra.textedit.representation.TextDocumentRepresentation;
+import org.eclipse.papyrus.infra.textedit.representation.command.ICreateTextDocumentEditorCommand;
+import org.eclipse.papyrus.infra.textedit.textdocument.TextDocument;
+import org.eclipse.papyrus.infra.textedit.ui.Activator;
+import org.eclipse.papyrus.infra.tools.util.ClassLoaderHelper;
+import org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper;
+import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker;
+import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype;
+
+/**
+ * Represents a helper for the handling of Papyrus TextDocument View Type creation commands.
+ *
+ */
+public class CreateTextDocumentViewTypeCommandHelper implements IViewTypeHelper {
+
+ /**
+ * The cache of prototypes
+ */
+ private Map<PapyrusRepresentationKind, TextDocumentViewPrototype> cache;
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper#isSupported(org.eclipse.emf.ecore.EClass)
+ *
+ * @param type
+ * @return
+ */
+ @Override
+ public boolean isSupported(EClass type) {
+ return EcoreUtil.equals(type, org.eclipse.papyrus.infra.textedit.representation.RepresentationPackage.eINSTANCE.getTextDocumentRepresentation());
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper#isSupported(org.eclipse.emf.ecore.EObject)
+ *
+ * @param view
+ * @return
+ */
+ @Override
+ public boolean isSupported(EObject view) {
+ return (view instanceof TextDocument);
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper#getPrototypeFor(org.eclipse.papyrus.infra.architecture.representation.PapyrusRepresentationKind)
+ *
+ * @param kind
+ * @return
+ */
+ @Override
+ public ViewPrototype getPrototypeFor(PapyrusRepresentationKind kind) {
+ if (!(kind instanceof TextDocumentRepresentation)) {
+ return null;
+ }
+
+ final TextDocumentRepresentation textDocumentRepresentation = (TextDocumentRepresentation) kind;
+ if (cache == null) {
+ cache = new HashMap<>();
+ }
+ if (cache.containsKey(textDocumentRepresentation)) {
+ return cache.get(textDocumentRepresentation);
+ }
+ String creationCommandClassName = textDocumentRepresentation.getCreationCommandClass();
+ if (creationCommandClassName == null || creationCommandClassName.isEmpty()) {
+ return null;
+ }
+
+ Class<?> creationCommandClass = ClassLoaderHelper.loadClass(creationCommandClassName);
+ if (creationCommandClass != null) {
+ try {
+ final Constructor<?> constructor = creationCommandClass.getDeclaredConstructor(new Class[0]);
+ Object newInstance = constructor.newInstance();
+ if (newInstance instanceof ICreateTextDocumentEditorCommand) {
+ ICreateTextDocumentEditorCommand command = (ICreateTextDocumentEditorCommand) newInstance;
+ TextDocumentViewPrototype proto = new TextDocumentViewPrototype(textDocumentRepresentation, command);
+ cache.put(textDocumentRepresentation, proto);
+ return proto;
+ }
+ } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException e) {
+ Activator.log.error(e);
+ } catch (IllegalArgumentException e) {
+ Activator.log.error(e);
+ } catch (InvocationTargetException e) {
+ Activator.log.error(e);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper#getPrototypeOf(org.eclipse.emf.ecore.EObject)
+ *
+ * @param view
+ * @return
+ */
+ @Override
+ public ViewPrototype getPrototypeOf(EObject view) {
+ if (!isSupported(view)) {
+ return null;
+ }
+ PolicyChecker checker = PolicyChecker.getFor(view);
+ ArchitectureDomainManager manager = ArchitectureDomainManager.getInstance();
+ TextDocumentRepresentation repKind = (TextDocumentRepresentation) manager.getRepresentationKindById(((TextDocument) view).getKindId());// getRepresentationKindById(((Document) view)
+ if (null != repKind && checker.isInViewpoint(repKind)) {
+ return getPrototypeFor(repKind);
+ }
+ return ViewPrototype.UNAVAILABLE_VIEW;
+ }
+
+}
diff --git a/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/ExtendedViewPrototype.java b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/ExtendedViewPrototype.java
new file mode 100755
index 00000000000..5788f3d7eed
--- /dev/null
+++ b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/ExtendedViewPrototype.java
@@ -0,0 +1,41 @@
+/*****************************************************************************
+ * Copyright (c) 2021 CEA LIST and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.textedit.ui.internal.viewpoint;
+
+import org.eclipse.emf.ecore.EObject;
+
+/**
+ * Interface for ViewPrototype to get the result of the instantiation instead of a boolean
+ * TODO : propose me to Papyrus
+ */
+public interface ExtendedViewPrototype<T extends EObject> {
+
+ /**
+ *
+ * @param semanticOwner
+ * the semantic owner of the instantiated view
+ * @param graphicalOwner
+ * the graphical owner of the instantiated view
+ * @param name
+ * the name of the instantiated view
+ * @param openCreatedView
+ * if <code>true</code> the created view will be open
+ * @return
+ * the instantiated view
+ */
+ public T instantiateOn(EObject semanticOwner, EObject graphicalOwner, String name, boolean openCreatedView);
+
+}
diff --git a/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/TextDocumentViewPrototype.java b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/TextDocumentViewPrototype.java
new file mode 100755
index 00000000000..3dc1f59a1c1
--- /dev/null
+++ b/plugins/infra/textedit/org.eclipse.papyrus.infra.textedit.ui/src/org/eclipse/papyrus/infra/textedit/ui/internal/viewpoint/TextDocumentViewPrototype.java
@@ -0,0 +1,176 @@
+/*****************************************************************************
+ * Copyright (c) 2021 CEA LIST.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.textedit.ui.internal.viewpoint;
+
+import org.eclipse.emf.common.command.Command;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
+import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
+import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
+import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
+import org.eclipse.papyrus.infra.textedit.representation.TextDocumentRepresentation;
+import org.eclipse.papyrus.infra.textedit.representation.command.ICreateTextDocumentEditorCommand;
+import org.eclipse.papyrus.infra.textedit.textdocument.TextDocument;
+import org.eclipse.papyrus.infra.textedit.textdocument.TextDocumentPackage;
+import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype;
+
+
+/**
+ * Represents a prototype of Text Document Editor View for the viewpoints infrastructure.
+ */
+public class TextDocumentViewPrototype extends ViewPrototype implements ExtendedViewPrototype<TextDocument> {
+
+ /**
+ * The command used to create a TextDocument Editor
+ */
+ private final ICreateTextDocumentEditorCommand command;
+
+ /**
+ * Constructor.
+ *
+ * @param prototype
+ * The TextDocument representation
+ */
+ public TextDocumentViewPrototype(final TextDocumentRepresentation prototype, final ICreateTextDocumentEditorCommand command) {
+ super(prototype);
+ this.command = command;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#isOwnerReassignable()
+ *
+ * @return
+ */
+ @Override
+ public boolean isOwnerReassignable() {
+ // Users can always move documents that are part of their viewpoint
+ return true;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#instantiateOn(org.eclipse.emf.ecore.EObject, java.lang.String, boolean)
+ *
+ * @param owner
+ * @param name
+ * @param openCreatedView
+ * @return
+ */
+ @Override
+ public boolean instantiateOn(EObject owner, String name, boolean openCreatedView) {
+ return instantiateOn(owner, owner, name, openCreatedView) != null;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#instantiateOn(org.eclipse.emf.ecore.EObject)
+ *
+ * @param owner
+ * @return
+ */
+ @Override
+ public boolean instantiateOn(EObject owner) {
+ return instantiateOn(owner, null);
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#instantiateOn(org.eclipse.emf.ecore.EObject, java.lang.String)
+ *
+ * @param owner
+ * @param name
+ * @return
+ */
+ @Override
+ public boolean instantiateOn(EObject owner, String name) {
+ return instantiateOn(owner, name, true);
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getCommandChangeOwner(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject)
+ *
+ * @param view
+ * @param target
+ * @return
+ */
+ @Override
+ public Command getCommandChangeOwner(final EObject view, final EObject target) {
+ // change the graphical context
+ if (view instanceof TextDocument) {
+ final SetRequest request = new SetRequest(view, TextDocumentPackage.eINSTANCE.getTextDocument_GraphicalContext(), target);
+ final IElementEditService documentProvider = ElementEditServiceUtils.getCommandProvider(view);
+ if (documentProvider != null) {
+ return GMFtoEMFCommandWrapper.wrap(documentProvider.getEditCommand(request));
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getCommandChangeRoot(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject)
+ *
+ * @param view
+ * @param target
+ * @return
+ */
+ @Override
+ public Command getCommandChangeRoot(EObject view, EObject target) {
+ // change the semantic context
+ if (view instanceof TextDocument) {
+ final SetRequest request = new SetRequest(view, TextDocumentPackage.eINSTANCE.getTextDocument_SemanticContext(), target);
+ final IElementEditService documentProvider = ElementEditServiceUtils.getCommandProvider(view);
+ if (documentProvider != null) {
+ return GMFtoEMFCommandWrapper.wrap(documentProvider.getEditCommand(request));
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getOwnerOf(org.eclipse.emf.ecore.EObject)
+ *
+ * @param view
+ * @return
+ */
+ @Override
+ public EObject getOwnerOf(EObject view) {
+ // it is graphical context
+ return ((TextDocument) view).getGraphicalContext();
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getRootOf(org.eclipse.emf.ecore.EObject)
+ *
+ * @param view
+ * @return
+ */
+ @Override
+ public EObject getRootOf(EObject view) {
+ // it is semantic context
+ return ((TextDocument) view).getSemanticContext();
+ }
+
+ /**
+ * @see org.eclipse.papyrus.model2doc.integration.emf.documentstructuretemplate.ui.internal.viewpoint.ExtendedViewPrototype#instantiateOn(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject, java.lang.String, boolean)
+ *
+ * @param semanticOwner
+ * @param graphicalOwner
+ * @param name
+ * @param openCreatedView
+ * @return
+ */
+ @Override
+ public TextDocument instantiateOn(final EObject semanticOwner, final EObject graphicalOwner, final String name, final boolean openCreatedView) {
+ return command.execute(this, name, semanticOwner, graphicalOwner, openCreatedView);
+ }
+
+}

Back to the top