Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org')
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/Activator.java79
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/DynamicCommandHandler.java68
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/DynamicContribution.java77
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/IProfileHelper.java47
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/IViewTypeHelper.java60
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ModelAddData.java195
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/NotationUtils.java168
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PolicyChecker.java978
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PreferenceConstants.java73
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PreferenceInitializer.java41
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ProfileUtils.java86
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/UnavailableViewPrototype.java135
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewPrototype.java440
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewPrototypeContentProvider.java255
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewpointExplorer.java90
-rwxr-xr-xplugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/WeightedConfiguration.java123
16 files changed, 2915 insertions, 0 deletions
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/Activator.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/Activator.java
new file mode 100755
index 00000000000..f695b17a236
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/Activator.java
@@ -0,0 +1,79 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import org.eclipse.papyrus.infra.core.log.LogHelper;
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The activator class controls the plug-in life cycle
+ */
+public class Activator extends AbstractUIPlugin {
+
+ // The plug-in ID
+ public static final String PLUGIN_ID = "org.eclipse.papyrus.infra.viewpoints.policy"; //$NON-NLS-1$
+
+ // The shared instance
+ private static Activator plugin;
+
+ // The log for this plugin
+ private LogHelper log;
+
+ /**
+ * Gets the Papyrus log associated with this plugin
+ *
+ * @return The Papyrus log for this plugin
+ */
+ public LogHelper getPapyrusLog() {
+ return log;
+ }
+
+ /**
+ * The constructor
+ */
+ public Activator() {
+ this.log = new LogHelper(this);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
+ */
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ plugin = this;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+ */
+ public void stop(BundleContext context) throws Exception {
+ plugin = null;
+ super.stop(context);
+ }
+
+ /**
+ * Returns the shared instance
+ *
+ * @return the shared instance
+ */
+ public static Activator getDefault() {
+ return plugin;
+ }
+
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/DynamicCommandHandler.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/DynamicCommandHandler.java
new file mode 100755
index 00000000000..c50c7553249
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/DynamicCommandHandler.java
@@ -0,0 +1,68 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.IHandler;
+import org.eclipse.core.commands.IHandlerListener;
+
+
+/**
+ * Represents the default handler for commands about the creation of dynamic views.
+ * This class is used to activate the UI elements that rely on the corresponding commands.
+ *
+ * @author Laurent Wouters
+ */
+public class DynamicCommandHandler implements IHandler {
+
+ /**
+ * @see org.eclipse.core.commands.IHandler#addHandlerListener(org.eclipse.core.commands.IHandlerListener)
+ */
+ public void addHandlerListener(IHandlerListener handlerListener) {
+ }
+
+ /**
+ * @see org.eclipse.core.commands.IHandler#dispose()
+ */
+ public void dispose() {
+ }
+
+ /**
+ * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
+ */
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ return null;
+ }
+
+ /**
+ * @see org.eclipse.core.commands.IHandler#isEnabled()
+ */
+ public boolean isEnabled() {
+ return true;
+ }
+
+ /**
+ * @see org.eclipse.core.commands.IHandler#isHandled()
+ */
+ public boolean isHandled() {
+ return true;
+ }
+
+ /**
+ * @see org.eclipse.core.commands.IHandler#removeHandlerListener(org.eclipse.core.commands.IHandlerListener)
+ */
+ public void removeHandlerListener(IHandlerListener handlerListener) {
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/DynamicContribution.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/DynamicContribution.java
new file mode 100755
index 00000000000..088fcadb681
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/DynamicContribution.java
@@ -0,0 +1,77 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.actions.CompoundContributionItem;
+
+/**
+ * Represent the dynamic contribution of a policy to some UI elements
+ *
+ * @author Laurent Wouters
+ */
+public abstract class DynamicContribution extends CompoundContributionItem {
+
+ /**
+ * Creation message prefix
+ */
+ protected static final String CREATE_MESSAGE = "Create a new ";
+
+ /**
+ * Constructor.
+ */
+ protected DynamicContribution() {
+ }
+
+ /**
+ * Constructor.
+ */
+ protected DynamicContribution(String id) {
+ }
+
+ /**
+ * @see org.eclipse.ui.actions.CompoundContributionItem#isDynamic()
+ */
+ @Override
+ public boolean isDynamic() {
+ return true;
+ }
+
+ /**
+ * Gets the current selection as an EObject
+ *
+ * @return The current selection, or <code>null</code> if it is not an EObject
+ */
+ protected EObject getSelection() {
+ IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (window == null)
+ return null;
+ ISelection selection = window.getSelectionService().getSelection();
+ if (selection == null)
+ return null;
+ if (selection.isEmpty())
+ return null;
+ if (selection instanceof IStructuredSelection) {
+ IStructuredSelection struct = (IStructuredSelection) selection;
+ Object obj = struct.getFirstElement();
+ return EMFHelper.getEObject(obj);
+ }
+ return null;
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/IProfileHelper.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/IProfileHelper.java
new file mode 100755
index 00000000000..cf96c652625
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/IProfileHelper.java
@@ -0,0 +1,47 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import java.util.Collection;
+
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EPackage;
+
+/**
+ * Represents a helper for the Policy Checker that is able to check the application of UML profiles
+ *
+ * @author Laurent Wouters
+ */
+public interface IProfileHelper {
+
+ /**
+ * Gets a collection of the profiles (as EPackage-s) applied on the given model
+ *
+ * @param model
+ * The model to inspect
+ * @return A collection of profiles (as EPackage)
+ */
+ Collection<EPackage> getAppliedProfiles(EObject model);
+
+ /**
+ * Gets a collection of the stereotypes (as EClass-es) applied to the given object
+ *
+ * @param object
+ * The object to inspect
+ * @return A collection of stereotypes (as EClass)
+ */
+ Collection<EClass> getAppliedStereotypes(EObject object);
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/IViewTypeHelper.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/IViewTypeHelper.java
new file mode 100755
index 00000000000..1efe4f7bdf4
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/IViewTypeHelper.java
@@ -0,0 +1,60 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView;
+
+
+/**
+ * Represents a helper for the handling of view creation commands
+ *
+ * @author Laurent Wouters
+ */
+public interface IViewTypeHelper {
+ /**
+ * Determines whether the given type of view is supported by this helper
+ *
+ * @return <code>true</code> if the type of view is supported by this helper
+ */
+ boolean isSupported(EClass type);
+
+ /**
+ * Determines whether the given object represents a view that is supported by this helper
+ *
+ * @param view
+ * The object to enquire
+ * @return <code>true</code> if the object represents a view supported by this helper
+ */
+ boolean isSupported(EObject view);
+
+ /**
+ * Gets the prototype for the given configuration element
+ *
+ * @param configuration
+ * A view configuration element
+ * @return The associated prototype
+ */
+ ViewPrototype getPrototypeFor(PapyrusView configuration);
+
+ /**
+ * Gets the prototype of the given view
+ *
+ * @param view
+ * The view
+ * @return The prototype of the given view
+ */
+ ViewPrototype getPrototypeOf(EObject view);
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ModelAddData.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ModelAddData.java
new file mode 100755
index 00000000000..96255381264
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ModelAddData.java
@@ -0,0 +1,195 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PathElement;
+
+/**
+ * Represents the set of data related to the insertion of a new model element in a view
+ *
+ * @author Laurent Wouters
+ */
+public class ModelAddData {
+ private boolean permit;
+ private List<EReference> path;
+ private EObject undoOrigin;
+ private EReference undoReference;
+ private EObject undoValuePrevious;
+ private EObject undoValueNext;
+
+ /**
+ * Determines whether the insertion is permitted
+ *
+ * @return <code>true</code> if it is permitted
+ */
+ public boolean isPermitted() {
+ return permit;
+ }
+
+ /**
+ * Determines whether an insertion path for the new element has been defined
+ *
+ * @return <code>true</code> if an insertion path has been defined
+ */
+ public boolean isPathDefined() {
+ return (path != null);
+ }
+
+ /**
+ * Initializes this set of data without an insertion path
+ *
+ * @param permit
+ * Is the insertion permitted?
+ */
+ public ModelAddData(boolean permit) {
+ this.permit = permit;
+ }
+
+ /**
+ * Initializes this set of data
+ *
+ * @param permit
+ * Is the insertion permitted?
+ * @param insertionPath
+ * The insertion path
+ */
+ public ModelAddData(boolean permit, EList<PathElement> insertionPath) {
+ this.permit = permit;
+ if (insertionPath != null && !insertionPath.isEmpty()) {
+ this.path = new ArrayList<EReference>(insertionPath.size());
+ for (int i = 0; i != insertionPath.size(); i++)
+ path.add(insertionPath.get(i).getFeature());
+ }
+ }
+
+ /**
+ * Execute the insertion represented by this object on the given origin object
+ *
+ * @param origin
+ * The origin object
+ * @param target
+ * The object to insert
+ * @return <code>true</code> if the insertion was successful
+ */
+ public boolean execute(EObject origin, EObject target) {
+ EObject current = origin;
+ int index = 0;
+ while (index < path.size() - 1) {
+ current = buildPathStep(current, path.get(index));
+ if (current == null)
+ return false;
+ index++;
+ }
+ EReference feature = path.get(path.size() - 1);
+ if (feature.isMany()) {
+ EList list = (EList) current.eGet(feature);
+ list.add(target);
+ setUndo(current, feature, null, target);
+ } else {
+ setUndo(current, feature, (EObject) current.eGet(feature), target);
+ current.eSet(feature, target);
+ }
+ return true;
+ }
+
+ /**
+ * Undo the effect of a previous execute
+ */
+ public void undoExecute() {
+ if (undoReference.isMany()) {
+ ((EList) undoOrigin.eGet(undoReference)).remove(undoValueNext);
+ } else {
+ undoOrigin.eSet(undoReference, undoValuePrevious);
+ }
+ }
+
+ /**
+ * Re-execute the insertion represented by this object
+ */
+ public void redoExecute() {
+ if (undoReference.isMany()) {
+ ((EList) undoOrigin.eGet(undoReference)).add(undoValueNext);
+ } else {
+ undoOrigin.eSet(undoReference, undoValueNext);
+ }
+ }
+
+ /**
+ * Executes a step in the insertion path
+ *
+ * @param current
+ * The current object
+ * @param feature
+ * The feature to resolve on the object
+ * @return The resolved object at this step that is now contained in current.feature
+ */
+ private EObject buildPathStep(EObject current, EReference feature) {
+ Object value = current.eGet(feature);
+ if (value == null) {
+ // Try to create an instance of the type
+ EClass type = feature.getEReferenceType();
+ if (type.isAbstract())
+ return null; // Too bad, we can't fix this
+ EObject inst = EcoreUtil.create(type);
+ current.eSet(feature, inst);
+ setUndo(current, feature, null, inst);
+ return inst;
+ } else if (value instanceof EList) {
+ EList list = (EList) value;
+ // If we got at least one element, return the first one
+ if (!list.isEmpty()) {
+ return (EObject) list.get(0);
+ }
+ // We have to create one
+ EClass type = feature.getEReferenceType();
+ if (type.isAbstract())
+ return null; // Too bad, we can't fix this
+ EObject inst = EcoreUtil.create(type);
+ list.add(inst);
+ setUndo(current, feature, null, inst);
+ return inst;
+ } else {
+ return (EObject) value;
+ }
+ }
+
+ /**
+ * Sets up the undo data if necessary
+ *
+ * @param origin
+ * The modified object
+ * @param feature
+ * The modified feature
+ * @param previous
+ * The previous value
+ * @param next
+ * The target value
+ */
+ private void setUndo(EObject origin, EReference feature, EObject previous, EObject next) {
+ if (undoReference == null)
+ return;
+ undoOrigin = origin;
+ undoReference = feature;
+ undoValuePrevious = previous;
+ undoValueNext = next;
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/NotationUtils.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/NotationUtils.java
new file mode 100755
index 00000000000..06858ca5ebe
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/NotationUtils.java
@@ -0,0 +1,168 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import java.util.Iterator;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.ui.PlatformUI;
+
+
+/**
+ * Helper methods for the .notation related resources
+ *
+ * @author Laurent Wouters
+ */
+public class NotationUtils {
+
+ /**
+ * Gets the roots of the notations resources related to the given object
+ *
+ * @param element
+ * The object from which to retrieve the notation resources
+ * @return An iterator of notation resources' roots, or <code>null</code> if none cannot be resolved
+ */
+ public static Iterator<EObject> getNotationRoots(EObject element) {
+ Iterator<Resource> notations = getNotationResources(element);
+ if (notations == null)
+ return null;
+ return new RootsIterator(notations);
+ }
+
+ /**
+ * Represents an iterator on all the roots of the notations resources of a ResourceSet
+ *
+ * @author Laurent Wouters
+ */
+ private static class RootsIterator implements Iterator<EObject> {
+ private Iterator<Resource> notations;
+ private Iterator<EObject> inner;
+
+ public RootsIterator(Iterator<Resource> notations) {
+ this.notations = notations;
+ if (notations.hasNext())
+ inner = notations.next().getAllContents();
+ }
+
+ public boolean hasNext() {
+ if (inner == null)
+ return false;
+ if (inner.hasNext())
+ return true;
+ while (notations.hasNext()) {
+ inner = notations.next().getAllContents();
+ if (inner.hasNext())
+ return true;
+ }
+ inner = null;
+ return false;
+ }
+
+ public EObject next() {
+ if (inner == null)
+ return null;
+ if (inner.hasNext())
+ return inner.next();
+ while (notations.hasNext()) {
+ inner = notations.next().getAllContents();
+ if (inner.hasNext())
+ return inner.next();
+ }
+ inner = null;
+ return null;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+
+ /**
+ * Gets the notation resources related to the given object
+ *
+ * @param element
+ * The object from which to retrieve the notation resources
+ * @return An iterator of notation resources, or <code>null</code> if none cannot be resolved
+ */
+ public static Iterator<Resource> getNotationResources(EObject element) {
+ Iterator<Resource> result = tryGetNotationResources(element);
+ if (result != null)
+ return result;
+ IAdaptable input = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getInput();
+ if (input != null) {
+ EObject obj = (EObject) input.getAdapter(EObject.class);
+ return tryGetNotationResources(obj);
+ }
+ return null;
+ }
+
+ /**
+ * Tries to get the notation resources related to the given object
+ *
+ * @param element
+ * The object from which to retrieve the notation resources
+ * @return An iterator of notation resources, or <code>null</code> if none cannot be resolved
+ */
+ private static Iterator<Resource> tryGetNotationResources(EObject element) {
+ if (element == null)
+ return null;
+ if (element.eResource() == null)
+ return null;
+ return new NotationsIterator(element.eResource().getResourceSet());
+ }
+
+
+ /**
+ * Represents an iterator over the notation resources of a ResourceSet
+ *
+ * @author Laurent Wouters
+ */
+ private static class NotationsIterator implements Iterator<Resource> {
+ private Iterator<Resource> inner;
+ private Resource next;
+
+ public NotationsIterator(ResourceSet set) {
+ inner = set.getResources().iterator();
+ next = getNextNotation();
+ }
+
+ private Resource getNextNotation() {
+ while (inner.hasNext()) {
+ Resource resource = inner.next();
+ String uri = resource.getURI().toString();
+ if (uri.endsWith(".notation"))
+ return resource;
+ }
+ return null;
+ }
+
+ public boolean hasNext() {
+ return (next != null);
+ }
+
+ public Resource next() {
+ Resource result = next;
+ next = getNextNotation();
+ return result;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PolicyChecker.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PolicyChecker.java
new file mode 100755
index 00000000000..81156d18a14
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PolicyChecker.java
@@ -0,0 +1,978 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExtension;
+import org.eclipse.core.runtime.IExtensionPoint;
+import org.eclipse.core.runtime.IExtensionRegistry;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EPackage;
+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.ecore.util.EcoreUtil;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.papyrus.infra.core.pluginexplorer.Plugin;
+import org.eclipse.papyrus.infra.core.pluginexplorer.PluginEntry;
+import org.eclipse.papyrus.infra.viewpoints.configuration.ChildRule;
+import org.eclipse.papyrus.infra.viewpoints.configuration.ModelRule;
+import org.eclipse.papyrus.infra.viewpoints.configuration.OwningRule;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PaletteRule;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusConfiguration;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusDiagram;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusViewpoint;
+import org.eclipse.papyrus.infra.viewpoints.iso42010.ArchitectureViewpoint;
+import org.eclipse.papyrus.infra.viewpoints.iso42010.ModelKind;
+import org.eclipse.papyrus.infra.viewpoints.iso42010.Stakeholder;
+
+/**
+ * The <code>PolicyChecker</code> enforces the viewpoints configuration as a policy in the user interface
+ *
+ * @author Laurent Wouters
+ */
+public class PolicyChecker {
+ /**
+ * ID of the extension point defining the configurations
+ */
+ private static final String EXTENSION_ID = "org.eclipse.papyrus.infra.viewpoints.policy.custom";
+ /**
+ * The cache of loaded configurations
+ */
+ private static final Map<String, PapyrusConfiguration> CONFIGURATIONS_CACHE = new HashMap<String, PapyrusConfiguration>();
+ /**
+ * The default built-in configuration
+ */
+ private static final PapyrusConfiguration CONFIG_BUILTIN_DEFAULT = loadDefaultConfiguration("builtin/default.configuration");
+ /**
+ * Default result when the current policy cannot determine whether an element can be added to a view
+ */
+ private static final boolean DEFAULT_POLICY_UNKNOWN_CHILD = false;
+ /**
+ * Default result when the current policy cannot determine whether a palette item should be exposed
+ */
+ private static final boolean DEFAULT_POLICY_UNKNWON_PALETTE = true;
+
+
+ /**
+ * Policy check result allowing an action
+ */
+ private static final int RESULT_PERMIT = 1;
+ /**
+ * Policy check result neither allowing not denying an action
+ */
+ private static final int RESULT_UNKNOWN = 0;
+ /**
+ * Policy check result denying an action
+ */
+ private static final int RESULT_DENY = -1;
+
+ /**
+ * Loads a built-in configuration
+ *
+ * @param name
+ * The name of the configuration
+ * @return The loaded configuration
+ */
+ private static PapyrusConfiguration loadDefaultConfiguration(String name) {
+ Plugin me = new Plugin(Activator.getDefault().getBundle());
+ PluginEntry entry = me.getEntry(name);
+ return loadConfigurationFrom(entry.getLogicalPath());
+ }
+
+ /**
+ * Loads a viewpoints configuration from the given location.
+ *
+ * @param location
+ * The location from where to load the configuration
+ * @return The loaded configuration, or <code>null</code> if the operation failed
+ */
+ public static PapyrusConfiguration loadConfigurationFrom(String location) {
+ try {
+ if (location == null)
+ return null;
+ if (location.isEmpty())
+ return null;
+ URI uri = null;
+ if (location.startsWith("platform:/")) {
+ uri = URI.createURI(location);
+ } else {
+ uri = URI.createFileURI(location);
+ }
+ location = uri.toString();
+ PapyrusConfiguration config = CONFIGURATIONS_CACHE.get(location);
+ if (config != null)
+ return config;
+ ResourceSet set = new ResourceSetImpl();
+ Resource res = set.getResource(uri, true);
+ config = (PapyrusConfiguration) res.getContents().get(0);
+ CONFIGURATIONS_CACHE.put(location, config);
+ return config;
+ } catch (NullPointerException e) {
+ }
+ return null;
+ }
+
+ /**
+ * Gets the default configuration
+ *
+ * @return The default configuration
+ */
+ public static PapyrusConfiguration getDefaultConfiguration() {
+ return CONFIG_BUILTIN_DEFAULT;
+ }
+
+
+ private static Map<String, Collection<String>> CONTRIBUTIONS_DEFINITON = null;
+ private static Map<PapyrusConfiguration, Collection<PapyrusConfiguration>> CONTRIBUTIONS_CACHE = null;
+
+ /**
+ * Gets the viewpoints contributing to the given viewpoint
+ *
+ * @param viewpoint
+ * A viewpoint
+ * @return A collection of the other viewpoints contributing to the given one
+ */
+ private static Collection<PapyrusViewpoint> getContributionsTo(PapyrusViewpoint viewpoint) {
+ Collection<PapyrusViewpoint> result = new ArrayList<PapyrusViewpoint>();
+ Collection<PapyrusConfiguration> contributions = getContributionsTo((PapyrusConfiguration) viewpoint.eContainer());
+ for (PapyrusConfiguration contrib : contributions) {
+ for (ArchitectureViewpoint vp : contrib.getViewpoints()) {
+ if (vp instanceof PapyrusViewpoint && vp.getName().equals(viewpoint.getName())) {
+ result.add((PapyrusViewpoint) vp);
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Gets the configurations contributing to the given configuration
+ *
+ * @param config
+ * A configuration
+ * @return A collection of the contributing configurations
+ */
+ private static Collection<PapyrusConfiguration> getContributionsTo(PapyrusConfiguration config) {
+ if (CONTRIBUTIONS_CACHE == null)
+ loadContributions();
+ Collection<PapyrusConfiguration> result = CONTRIBUTIONS_CACHE.get(config);
+ if (result != null)
+ return result;
+ result = new ArrayList<PapyrusConfiguration>();
+ CONTRIBUTIONS_CACHE.put(config, result);
+ for (Map.Entry<String, PapyrusConfiguration> entry : CONFIGURATIONS_CACHE.entrySet()) {
+ if (entry.getValue() == config) {
+ Collection<String> contribURIs = CONTRIBUTIONS_DEFINITON.get(entry.getKey());
+ if (contribURIs != null)
+ for (String uri : contribURIs)
+ result.add(loadConfigurationFrom(uri));
+ return result;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Loads the viewpoints contribution data from the extension points
+ */
+ private static void loadContributions() {
+ CONTRIBUTIONS_DEFINITON = new HashMap<String, Collection<String>>();
+ CONTRIBUTIONS_CACHE = new HashMap<PapyrusConfiguration, Collection<PapyrusConfiguration>>();
+
+ IExtensionRegistry registry = Platform.getExtensionRegistry();
+ IExtensionPoint point = registry.getExtensionPoint(EXTENSION_ID);
+ IExtension[] extensions = point.getExtensions();
+
+ for (int i = 0; i != extensions.length; i++) {
+ String plugin = extensions[i].getContributor().getName();
+ IConfigurationElement[] elements = extensions[i].getConfigurationElements();
+ for (int j = 0; j != elements.length; j++) {
+ if (elements[j].getName().equals("contribution")) {
+ String uriOriginal = getCanonicalURI(plugin, elements[j].getAttribute("original"));
+ String uriContrib = getCanonicalURI(plugin, elements[j].getAttribute("file"));
+ Collection<String> list = CONTRIBUTIONS_DEFINITON.get(uriOriginal);
+ if (list == null) {
+ list = new ArrayList<String>();
+ CONTRIBUTIONS_DEFINITON.put(uriOriginal, list);
+ }
+ list.add(uriContrib);
+ }
+ }
+ }
+ }
+
+ /**
+ * Gets the canonical (absolute) URI from a potentially plugin-relative URI
+ *
+ * @param plugin
+ * The container plugin
+ * @param uri
+ * The URI to canonicalize
+ * @return The canonical (absolute) URI
+ */
+ private static String getCanonicalURI(String plugin, String uri) {
+ if (uri.startsWith(PreferenceConstants.P_CONF_PATH_SCHEME_PLUGIN_VALUE))
+ return uri;
+ return PreferenceConstants.P_CONF_PATH_SCHEME_PLUGIN_VALUE + plugin + "/" + uri;
+ }
+
+
+ /**
+ * Gets the preference store for the viewpoints-related preferences
+ *
+ * @return The preference store for the viewpoints-related preferences
+ */
+ public static IPreferenceStore getPreferences() {
+ return Activator.getDefault().getPreferenceStore();
+ }
+
+
+ /**
+ * The current (global) instance
+ */
+ private static PolicyChecker currentPolicyChecker;
+
+ /**
+ * Gets the policy checker currently enforcing the viewpoints configuration
+ *
+ * @return The current policy checker
+ */
+ public static PolicyChecker getCurrent() {
+ if (currentPolicyChecker == null)
+ currentPolicyChecker = new PolicyChecker();
+ return currentPolicyChecker;
+ }
+
+ /**
+ * Sets the policy checker currently enforcing the viewpoints configuration
+ *
+ * @param pc
+ * The new policy checker
+ */
+ public static void setCurrent(PolicyChecker pc) {
+ currentPolicyChecker = pc;
+ }
+
+
+ /**
+ * The checker's configuration
+ */
+ private PapyrusConfiguration configuration;
+
+ /**
+ * Force only one type of view per model element
+ */
+ private boolean oneViewPerElem;
+
+ /**
+ * The current stakeholder
+ */
+ private Stakeholder selectedStakeholder;
+
+ /**
+ * The current viewpoint
+ */
+ private PapyrusViewpoint selectedViewpoint;
+
+ /**
+ * The cache of applicable viewpoints, with all contributing configurations taken into account
+ */
+ private Collection<PapyrusViewpoint> applicableViewpoints;
+
+ /**
+ * The current profile helper
+ */
+ private IProfileHelper profileHelper;
+
+ /**
+ * Gets the configuration enforced by this object
+ *
+ * @return The configuration
+ */
+ public PapyrusConfiguration getConfiguration() {
+ return configuration;
+ }
+
+ /**
+ * Gets the stakeholder represented by this object
+ *
+ * @return The stakeholder
+ */
+ public Stakeholder getStakeholder() {
+ return selectedStakeholder;
+ }
+
+ /**
+ * Gets the viewpoint enforced by this object
+ *
+ * @return The enforced viewpoint
+ */
+ public PapyrusViewpoint getViewpoint() {
+ return selectedViewpoint;
+ }
+
+ /**
+ * Initializes this instance from the current preferences
+ */
+ private PolicyChecker() {
+ IPreferenceStore store = getPreferences();
+ String prefType = store.getString(PreferenceConstants.P_CONF_TYPE);
+ String prefForce = store.getString(PreferenceConstants.P_FORCE_MULTIPLICITY);
+ String prefViewpoint = store.getString(PreferenceConstants.P_VIEWPOINT);
+
+ this.profileHelper = ProfileUtils.getProfileHelper();
+ this.oneViewPerElem = "true".equals(prefForce);
+ if (PreferenceConstants.P_CONF_TYPE_DEFAULT_VALUE.equals(prefType)) {
+ this.configuration = CONFIG_BUILTIN_DEFAULT;
+ } else if (PreferenceConstants.P_CONF_TYPE_EXTENSION_VALUE.equals(prefType)) {
+ WeightedConfiguration wc = WeightedConfiguration.getTopConfiguration();
+ this.configuration = (wc != null ? wc.getConfiguration() : null);
+ } else {
+ String scheme = store.getString(PreferenceConstants.P_CONF_PATH_SCHEME);
+ String path = store.getString(PreferenceConstants.P_CONF_PATH);
+ if (PreferenceConstants.P_CONF_PATH_SCHEME_WORKSPACE_VALUE.equals(scheme))
+ path = PreferenceConstants.P_CONF_PATH_SCHEME_WORKSPACE_VALUE + path;
+ this.configuration = loadConfigurationFrom(path);
+ }
+ if (this.configuration == null)
+ this.configuration = CONFIG_BUILTIN_DEFAULT;
+
+ if (prefViewpoint != null && !prefViewpoint.isEmpty()) {
+ for (Stakeholder stakeholder : this.configuration.getStakeholders()) {
+ for (ArchitectureViewpoint vp : stakeholder.getViewpoints()) {
+ if (prefViewpoint.equals(vp.getName())) {
+ this.selectedStakeholder = stakeholder;
+ this.selectedViewpoint = (PapyrusViewpoint) vp;
+ buildApplicableViewpoints();
+ return;
+ }
+ }
+ }
+ }
+ this.selectedStakeholder = this.configuration.getDefaultStakeholder();
+ this.selectedViewpoint = (PapyrusViewpoint) this.selectedStakeholder.getViewpoints().get(0);
+ buildApplicableViewpoints();
+ }
+
+ /**
+ * Initializes this policy checker with the default configuration and viewpoint
+ *
+ * @param oneViewPerElem
+ * Force only one type of view per model element
+ */
+ public PolicyChecker(boolean oneViewPerElem) {
+ this.configuration = CONFIG_BUILTIN_DEFAULT;
+ this.oneViewPerElem = oneViewPerElem;
+ this.selectedStakeholder = this.configuration.getDefaultStakeholder();
+ this.selectedViewpoint = (PapyrusViewpoint) this.selectedStakeholder.getViewpoints().get(0);
+ this.profileHelper = ProfileUtils.getProfileHelper();
+ buildApplicableViewpoints();
+ }
+
+ /**
+ * Initializes this policy checker with the given configuration and viewpoint
+ *
+ * @param config
+ * The configuration to enforce
+ * @param viewpoint
+ * The viewpoint to enforce
+ * @param oneViewPerElem
+ * Force only one type of view per model element
+ */
+ public PolicyChecker(PapyrusConfiguration config, PapyrusViewpoint viewpoint, boolean oneViewPerElem) {
+ this.configuration = config;
+ this.oneViewPerElem = oneViewPerElem;
+ this.selectedViewpoint = viewpoint;
+ this.profileHelper = ProfileUtils.getProfileHelper();
+ buildApplicableViewpoints();
+ for (Stakeholder stakeholder : configuration.getStakeholders()) {
+ if (stakeholder.getViewpoints().contains(viewpoint)) {
+ this.selectedStakeholder = stakeholder;
+ return;
+ }
+ }
+ }
+
+
+ /**
+ * Builds the <code>applicableViewpoints</code> member from the selected viewpoint and all its contribution
+ */
+ private void buildApplicableViewpoints() {
+ applicableViewpoints = new ArrayList<PapyrusViewpoint>();
+ buildApplicableViewpoints(selectedViewpoint);
+ for (PapyrusViewpoint vp : getContributionsTo(selectedViewpoint))
+ buildApplicableViewpoints(vp);
+ }
+
+ /**
+ * Builds the <code>applicableViewpoints</code> member from the given viewpoint by adding it, as well as its parent
+ *
+ * @param from
+ * The top viewpoint
+ */
+ private void buildApplicableViewpoints(PapyrusViewpoint from) {
+ PapyrusViewpoint vp = from;
+ while (vp != null) {
+ applicableViewpoints.add(vp);
+ vp = vp.getParent();
+ }
+ }
+
+
+ /**
+ * Determines whether the given element can own the given view
+ *
+ * @param element
+ * The possible owner
+ * @param prototype
+ * The view prototype
+ * @return <code>true</code> if the element can own the view
+ */
+ public boolean canOwnNewView(EObject element, ViewPrototype prototype) {
+ if (prototype == null)
+ return false;
+ if (!matchesProfiles(prototype.configuration, profileHelper.getAppliedProfiles(element)))
+ return false;
+ return (matchesOwnerMultiplicity(prototype.configuration, element.eClass(), profileHelper.getAppliedStereotypes(element), getOwnedViewCount(element, prototype)));
+ }
+
+ /**
+ * Determines whether the given element can be the root of a view owned by the given object
+ *
+ * @param element
+ * The possible root element
+ * @param owner
+ * The possible owner
+ * @param prototype
+ * The view prototype
+ * @return <code>true</code> if the element can be the root
+ */
+ public boolean canHaveNewView(EObject element, EObject owner, ViewPrototype prototype) {
+ if (prototype == null)
+ return false;
+ if (!matchesProfiles(prototype.configuration, profileHelper.getAppliedProfiles(owner)))
+ return false;
+ if (!matchesProfiles(prototype.configuration, profileHelper.getAppliedProfiles(element)))
+ return false;
+ if (!matchesOwnerMultiplicity(prototype.configuration, owner.eClass(), profileHelper.getAppliedStereotypes(owner), getOwnedViewCount(owner, prototype)))
+ return false;
+ if (!matchesRootMultiplicity(prototype.configuration, element.eClass(), profileHelper.getAppliedStereotypes(element), getViewCountOn(element, prototype)))
+ return false;
+ return true;
+ }
+
+ /**
+ * Gets the insertion data of the given element in the given diagram
+ *
+ * @param diagram
+ * The diagram
+ * @param parent
+ * The element to be edited
+ * @param child
+ * The element to be added through the diagram
+ * @return The insertion data
+ */
+ public ModelAddData getChildAddData(Diagram diagram, EObject parent, EObject child) {
+ ViewPrototype prototype = ViewPrototype.get(diagram);
+ if (prototype == null)
+ // This diagram is not in the current policy
+ return new ModelAddData(false);
+
+ PapyrusDiagram config = (PapyrusDiagram) prototype.configuration;
+ Collection<EClass> stereotypes = profileHelper.getAppliedStereotypes(child);
+ while (config != null) {
+ for (ChildRule rule : config.getChildRules()) {
+ int result = allows(rule, parent.eClass(), child.eClass(), stereotypes);
+ if (result != RESULT_UNKNOWN)
+ return new ModelAddData((result == RESULT_PERMIT), rule.getInsertionPath());
+ }
+ config = (PapyrusDiagram) config.getParent();
+ }
+ return new ModelAddData(DEFAULT_POLICY_UNKNOWN_CHILD);
+ }
+
+ /**
+ * Gets the insertion data of the given element in the given diagram
+ *
+ * @param diagram
+ * The diagram
+ * @param parentType
+ * The type of the element to be edited
+ * @param childType
+ * The type of element to be added through the diagram
+ * @return The insertion data
+ */
+ public ModelAddData getChildAddData(Diagram diagram, EClass parentType, EClass childType) {
+ ViewPrototype prototype = ViewPrototype.get(diagram);
+ if (prototype == null)
+ // This diagram is not in the current policy
+ return new ModelAddData(false);
+
+ PapyrusDiagram config = (PapyrusDiagram) prototype.configuration;
+ while (config != null) {
+ for (ChildRule rule : config.getChildRules()) {
+ int result = allows(rule, parentType, childType, new ArrayList<EClass>(0));
+ if (result != RESULT_UNKNOWN)
+ return new ModelAddData((result == RESULT_PERMIT), rule.getInsertionPath());
+ }
+ config = (PapyrusDiagram) config.getParent();
+ }
+ return new ModelAddData(DEFAULT_POLICY_UNKNOWN_CHILD);
+ }
+
+ /**
+ * Determines whether the given diagram can have the palette element with the given entry ID
+ *
+ * @param diagram
+ * The diagram
+ * @param entryID
+ * A palette element entry ID
+ * @return <code>true</code> if the palette element is allowed
+ */
+ public boolean isInPalette(Diagram diagram, String entryID) {
+ ViewPrototype prototype = ViewPrototype.get(diagram);
+ if (prototype == null)
+ // This diagram is not in the current policy
+ return false;
+
+ PapyrusDiagram config = (PapyrusDiagram) prototype.configuration;
+ while (config != null) {
+ for (PaletteRule rule : config.getPaletteRules()) {
+ int result = allows(rule, entryID);
+ if (result != RESULT_UNKNOWN)
+ return (result == RESULT_PERMIT);
+ }
+ config = (PapyrusDiagram) config.getParent();
+ }
+ return DEFAULT_POLICY_UNKNWON_PALETTE;
+ }
+
+ /**
+ * Determines whether the given view configuration element is part of the current viewpoint
+ *
+ * @param config
+ * A view configuration element
+ * @return <code>true</code> if the element is part of the current viewpoint
+ */
+ public boolean isInViewpoint(PapyrusView config) {
+ for (PapyrusViewpoint viewpoint : applicableViewpoints) {
+ for (ModelKind kind : viewpoint.getModelKinds()) {
+ PapyrusView view = (PapyrusView) kind;
+ if (EcoreUtil.equals(view, config))
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Gets a collection of all the view prototypes allowed by the active policy
+ *
+ * @return A collection of view prototypes
+ */
+ public Collection<ViewPrototype> getAllPrototypes() {
+ Collection<ViewPrototype> result = new ArrayList<ViewPrototype>();
+ for (PapyrusViewpoint viewpoint : applicableViewpoints) {
+ for (ModelKind kind : viewpoint.getModelKinds()) {
+ PapyrusView view = (PapyrusView) kind;
+ ViewPrototype proto = ViewPrototype.get(view);
+ if (proto != null)
+ result.add(proto);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Gets a list of the prototypes that can be instantiated with the given element as owner according to the policy
+ *
+ * @param element
+ * The element to test
+ * @return A list of the prototypes that can be instantiated
+ */
+ public Collection<ViewPrototype> getPrototypesFor(EObject element) {
+ Collection<ViewPrototype> result = new ArrayList<ViewPrototype>();
+ Collection<EPackage> profiles = profileHelper.getAppliedProfiles(element);
+ Collection<EClass> stereotypes = profileHelper.getAppliedStereotypes(element);
+ for (PapyrusViewpoint viewpoint : applicableViewpoints) {
+ for (ModelKind kind : viewpoint.getModelKinds()) {
+ PapyrusView view = (PapyrusView) kind;
+ if (!matchesProfiles(view, profiles))
+ continue;
+ ViewPrototype proto = ViewPrototype.get(view);
+ if (proto == null)
+ continue;
+ int count = getOwnedViewCount(element, proto);
+ if (!matchesOwnerMultiplicity(view, element.eClass(), stereotypes, count))
+ continue;
+ result.add(proto);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Gets the number of views of a given type owned by a given object
+ *
+ * @param element
+ * The object to count views for
+ * @param prototype
+ * The prototype of view to counts
+ * @return The number of owned views
+ */
+ private int getOwnedViewCount(EObject element, ViewPrototype prototype) {
+ int count = 0;
+ Iterator<EObject> roots = NotationUtils.getNotationRoots(element);
+ if (roots == null)
+ return count;
+ while (roots.hasNext()) {
+ EObject view = roots.next();
+ ViewPrototype proto = ViewPrototype.get(view);
+ if (prototype == proto) {
+ EObject owner = proto.getOwnerOf(view);
+ if (EcoreUtil.equals(owner, element))
+ count++;
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Gets the number of views of a given type on a given object
+ *
+ * @param element
+ * The object to count views on
+ * @param prototype
+ * The prototype of view to counts
+ * @return The number of views on the object
+ */
+ private int getViewCountOn(EObject element, ViewPrototype prototype) {
+ int count = 0;
+ Iterator<EObject> roots = NotationUtils.getNotationRoots(element);
+ if (roots == null)
+ return count;
+ while (roots.hasNext()) {
+ EObject view = roots.next();
+ ViewPrototype proto = ViewPrototype.get(view);
+ if (prototype == proto) {
+ EObject root = proto.getRootOf(view);
+ if (EcoreUtil.equals(root, element))
+ count++;
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Tries to match a view configuration from the given info
+ *
+ * @param implem
+ * The implementation ID
+ * @param owner
+ * The owner
+ * @param root
+ * The root element
+ * @return The matching view, or <code>null</code> if none was found
+ */
+ protected PapyrusView getViewFrom(String implem, EObject owner, EObject root) {
+ for (PapyrusViewpoint viewpoint : applicableViewpoints) {
+ for (ModelKind kind : viewpoint.getModelKinds()) {
+ PapyrusView view = (PapyrusView) kind;
+ if (matches(view, implem, owner, root))
+ return view;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Tries to match a view configuration with the given info
+ *
+ * @param view
+ * A view configuration
+ * @param implem
+ * The implementation ID
+ * @param owner
+ * The owner
+ * @param root
+ * The root element
+ * @return <code>true</code> if the configuration matches
+ */
+ private boolean matches(PapyrusView view, String implem, EObject owner, EObject root) {
+ if (!ViewPrototype.isNatural(view))
+ return false;
+ if (!view.getImplementationID().equals(implem))
+ return false;
+ if (owner != null) {
+ if (!matchesProfiles(view, profileHelper.getAppliedProfiles(owner)))
+ return false;
+ if (!matchesOwner(view, owner.eClass(), profileHelper.getAppliedStereotypes(owner)))
+ return false;
+ }
+ if (root != null) {
+ if (!matchesProfiles(view, profileHelper.getAppliedProfiles(root)))
+ return false;
+ if (!matchesRoot(view, root.eClass(), profileHelper.getAppliedStereotypes(root)))
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Checks whether the given set of profiles matches the requirements of the given view
+ *
+ * @param view
+ * The view to check against
+ * @param profiles
+ * The applied profiles
+ * @return <code>true</code> if the prototype is matching
+ */
+ private boolean matchesProfiles(PapyrusView view, Collection<EPackage> profiles) {
+ PapyrusView current = view;
+ while (current != null) {
+ for (EPackage profile : view.getProfiles())
+ if (!profiles.contains(profile))
+ return false;
+ current = current.getParent();
+ }
+ return true;
+ }
+
+ /**
+ * Checks whether the given type of owning element with applied stereotypes is allowed for the given view
+ *
+ * @param view
+ * The view to check against
+ * @param type
+ * The owning element's type
+ * @param stereotypes
+ * The stereotypes applied on the owning element
+ * @return <code>true</code> if the prototype is matching
+ */
+ private boolean matchesOwner(PapyrusView view, EClass type, Collection<EClass> stereotypes) {
+ PapyrusView current = view;
+ while (current != null) {
+ for (OwningRule rule : current.getOwningRules()) {
+ int result = allows(rule, type, stereotypes);
+ if (result == RESULT_DENY)
+ return false;
+ if (result == RESULT_PERMIT)
+ return true;
+ }
+ current = current.getParent();
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the given view can be owned by an element of the given type, applied with the given stereotypes if the cardinality is already the given amount
+ *
+ * @param view
+ * The view to check against
+ * @param type
+ * The owning element's type
+ * @param stereotypes
+ * The stereotypes applied on the owning element
+ * @param count
+ * The current cardinality for the owning element
+ * @return <code>true</code> if the prototype is matching
+ */
+ private boolean matchesOwnerMultiplicity(PapyrusView view, EClass type, Collection<EClass> stereotypes, int count) {
+ PapyrusView current = view;
+ while (current != null) {
+ for (OwningRule rule : current.getOwningRules()) {
+ int allow = allows(rule, type, stereotypes);
+ if (allow == RESULT_DENY)
+ return false;
+ if (allow == RESULT_UNKNOWN)
+ continue;
+ int multiplicity = rule.getMultiplicity();
+ if (multiplicity == -1)
+ return true;
+ return (count < multiplicity);
+ }
+ current = current.getParent();
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the given type of root element with applied stereotypes is allowed for the given view
+ *
+ * @param view
+ * The view to check against
+ * @param type
+ * The root element's type
+ * @param stereotypes
+ * The stereotypes applied on the root element
+ * @return <code>true</code> if the prototype is matching
+ */
+ private boolean matchesRoot(PapyrusView view, EClass type, Collection<EClass> stereotypes) {
+ PapyrusView current = view;
+ while (current != null) {
+ for (ModelRule rule : current.getModelRules()) {
+ int result = allows(rule, type, stereotypes);
+ if (result == RESULT_DENY)
+ return false;
+ if (result == RESULT_PERMIT)
+ return true;
+ }
+ current = current.getParent();
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the given view can have the given root element of the given type, applied with the given stereotypes if the cardinality is already the given amount
+ *
+ * @param view
+ * The view to check against
+ * @param type
+ * The root element's type
+ * @param stereotypes
+ * The stereotypes applied on the root element
+ * @param count
+ * The current cardinality for the root element
+ * @return <code>true</code> if the prototype is matching
+ */
+ private boolean matchesRootMultiplicity(PapyrusView view, EClass type, Collection<EClass> stereotypes, int count) {
+ PapyrusView current = view;
+ while (current != null) {
+ for (ModelRule rule : current.getModelRules()) {
+ int allow = allows(rule, type, stereotypes);
+ if (allow == RESULT_DENY)
+ return false;
+ if (allow == RESULT_UNKNOWN)
+ continue;
+ int multiplicity = (oneViewPerElem ? 1 : rule.getMultiplicity());
+ if (multiplicity == -1)
+ return true;
+ return (count < multiplicity);
+ }
+ current = current.getParent();
+ }
+ return false;
+ }
+
+ /**
+ * Checks an owner's type against a rule
+ *
+ * @param rule
+ * The owning rule
+ * @param owner
+ * The owner's type
+ * @param stereotypes
+ * The stereotypes applied on <code>owner</code>
+ * @return The check result
+ */
+ private int allows(OwningRule rule, EClass owner, Collection<EClass> stereotypes) {
+ EClass c = rule.getElement();
+ if (c == null || c.isSuperTypeOf(owner)) {
+ // matching type => check the application of the required stereotypes
+ for (EClass stereotype : rule.getStereotypes())
+ if (!stereotypes.contains(stereotype))
+ return RESULT_UNKNOWN;
+ return rule.isPermit() ? RESULT_PERMIT : RESULT_DENY;
+ } else {
+ // type is not matching => unknown
+ return RESULT_UNKNOWN;
+ }
+ }
+
+ /**
+ * Checks a root element's type against a rule
+ *
+ * @param rule
+ * The modeling rule
+ * @param element
+ * The root element's type
+ * @param stereotypes
+ * The stereotypes applied on <code>element</code>
+ * @return The check result
+ */
+ private int allows(ModelRule rule, EClass element, Collection<EClass> stereotypes) {
+ EClass c = rule.getElement();
+ if (c == null || c.isSuperTypeOf(element)) {
+ // matching type => check the application of the required stereotypes
+ for (EClass stereotype : rule.getStereotypes())
+ if (!stereotypes.contains(stereotype))
+ return RESULT_UNKNOWN;
+ return rule.isPermit() ? RESULT_PERMIT : RESULT_DENY;
+ } else {
+ // type is not matching => unknown
+ return RESULT_UNKNOWN;
+ }
+ }
+
+ /**
+ * Checks a child insertion against a rule
+ *
+ * @param rule
+ * The insertion rule
+ * @param origin
+ * The root element's type
+ * @param element
+ * The child's type
+ * @param stereotypes
+ * The stereotypes applied on <code>element</code>
+ * @return The check result
+ */
+ private int allows(ChildRule rule, EClass origin, EClass element, Collection<EClass> stereotypes) {
+ EClass ce = rule.getElement();
+ EClass co = rule.getOrigin();
+ if ((ce == null || ce.isSuperTypeOf(element))
+ && (co == null || co.isSuperTypeOf(origin))) {
+ // matching type => check the application of the required stereotypes
+ for (EClass stereotype : rule.getStereotypes())
+ if (!stereotypes.contains(stereotype))
+ return RESULT_UNKNOWN;
+ return rule.isPermit() ? RESULT_PERMIT : RESULT_DENY;
+ } else {
+ // type is not matching => unknown
+ return RESULT_UNKNOWN;
+ }
+ }
+
+ /**
+ * Checks a palette element against a rule
+ *
+ * @param rule
+ * The palette rule
+ * @param toolID
+ * The palette element's ID
+ * @return The check result
+ */
+ private int allows(PaletteRule rule, String toolID) {
+ String elem = rule.getElement();
+ boolean applies = (elem == null);
+ applies = applies || (elem.length() == 0);
+ applies = applies || (elem != null && elem.equals(toolID));
+ applies = applies || (elem != null && elem.endsWith("*") && toolID.startsWith(elem.substring(0, elem.length() - 1)));
+ if (applies)
+ return rule.isPermit() ? RESULT_PERMIT : RESULT_DENY;
+ return RESULT_UNKNOWN;
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PreferenceConstants.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PreferenceConstants.java
new file mode 100755
index 00000000000..1e433ad793e
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PreferenceConstants.java
@@ -0,0 +1,73 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+/**
+ * Contains all the constants for the management of the viewpoints-related configuration preferences
+ *
+ * @author Laurent Wouters
+ */
+public class PreferenceConstants {
+
+ /**
+ * Preference for the type of configuration selection
+ */
+ public static final String P_CONF_TYPE = "papyrusViewpointsConfigurationType";
+
+ /**
+ * Preference for the scheme of the path to a custom configuration
+ */
+ public static final String P_CONF_PATH_SCHEME = "papyrusViewpointsUserDefinedScheme";
+
+ /**
+ * Preference for the path to a custom configuration
+ */
+ public static final String P_CONF_PATH = "papyrusViewpointsUserDefinedConfiguration";
+
+ /**
+ * Preference for the configuration's stakeholder
+ */
+ public static final String P_STAKEHOLDER = "papyrusViewpointsSelectedStakeholder";
+
+ /**
+ * Preference for the configuration's viewpoint
+ */
+ public static final String P_VIEWPOINT = "papyrusViewpointsSelectedViewpoint";
+
+ /**
+ * Preference for the forced multiplicity
+ */
+ public static final String P_FORCE_MULTIPLICITY = "papyrusViewpointsForceMultiplicity";
+
+
+ public static final String P_CONF_TYPE_DEFAULT_LABEL = "Default (Papyrus built-in configuration, allows all)";
+ public static final String P_CONF_TYPE_DEFAULT_VALUE = "default";
+
+ public static final String P_CONF_TYPE_EXTENSION_LABEL = "Deployed through the extension point";
+ public static final String P_CONF_TYPE_EXTENSION_VALUE = "extension";
+
+ public static final String P_CONF_TYPE_USER_DEFINED_LABEL = "Manual configuration selection";
+ public static final String P_CONF_TYPE_USER_DEFINED_VALUE = "custom";
+
+ public static final String P_CONF_PATH_SCHEME_FILE_LABEL = "Absolute path";
+ public static final String P_CONF_PATH_SCHEME_FILE_VALUE = "file";
+
+ public static final String P_CONF_PATH_SCHEME_WORKSPACE_LABEL = "Workspace file";
+ public static final String P_CONF_PATH_SCHEME_WORKSPACE_VALUE = "platform:/resource/";
+
+ public static final String P_CONF_PATH_SCHEME_PLUGIN_LABEL = "Embedded in a plugin";
+ public static final String P_CONF_PATH_SCHEME_PLUGIN_VALUE = "platform:/plugin/";
+
+ public static final String P_FORCE_MULTIPLICITY_LABEL = "Force at most one view of each type per model element";
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PreferenceInitializer.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PreferenceInitializer.java
new file mode 100755
index 00000000000..7d9613edc3f
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/PreferenceInitializer.java
@@ -0,0 +1,41 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
+import org.eclipse.jface.preference.IPreferenceStore;
+
+/**
+ * Initializes the default preferences for the viewpoints
+ *
+ * @author Laurent Wouters
+ */
+public class PreferenceInitializer extends AbstractPreferenceInitializer {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences()
+ */
+ public void initializeDefaultPreferences() {
+ IPreferenceStore store = Activator.getDefault().getPreferenceStore();
+ store.setDefault(PreferenceConstants.P_CONF_TYPE, PreferenceConstants.P_CONF_TYPE_EXTENSION_VALUE);
+ store.setDefault(PreferenceConstants.P_CONF_PATH_SCHEME, PreferenceConstants.P_CONF_PATH_SCHEME_FILE_VALUE);
+ store.setDefault(PreferenceConstants.P_CONF_PATH, "");
+ store.setDefault(PreferenceConstants.P_STAKEHOLDER, "");
+ store.setDefault(PreferenceConstants.P_VIEWPOINT, "");
+ store.setDefault(PreferenceConstants.P_FORCE_MULTIPLICITY, "false");
+ }
+
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ProfileUtils.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ProfileUtils.java
new file mode 100755
index 00000000000..de66e9d2aaf
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ProfileUtils.java
@@ -0,0 +1,86 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExtension;
+import org.eclipse.core.runtime.IExtensionPoint;
+import org.eclipse.core.runtime.IExtensionRegistry;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EPackage;
+
+
+/**
+ * Helper class for the ProfileHelper extension point
+ *
+ * @author Laurent Wouters
+ */
+class ProfileUtils {
+ /**
+ * ID of the extension point
+ */
+ private static final String EXTENSION_ID = "org.eclipse.papyrus.infra.viewpoints.policy.profilehelper";
+
+ /**
+ * Gets a instance of the <code>IProfileHelper</code> interface
+ *
+ * @return an instance of <code>IProfileHelper</code>
+ */
+ public static IProfileHelper getProfileHelper() {
+ IExtensionRegistry registry = Platform.getExtensionRegistry();
+ IExtensionPoint point = registry.getExtensionPoint(EXTENSION_ID);
+ IExtension[] extensions = point.getExtensions();
+
+ for (int i = 0; i != extensions.length; i++) {
+ IConfigurationElement[] elements = extensions[i].getConfigurationElements();
+ for (int j = 0; j != elements.length; j++) {
+ try {
+ IProfileHelper instance = (IProfileHelper) elements[j].createExecutableExtension("class");
+ if (instance != null)
+ return instance;
+ } catch (CoreException e) {
+ }
+ }
+ }
+ return new DefaultProfileHelper();
+ }
+
+ /**
+ * Represents a default implementation of the profile helper
+ *
+ * @author Laurent Wouters
+ */
+ private static class DefaultProfileHelper implements IProfileHelper {
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.IProfileHelper#getAppliedProfiles(org.eclipse.emf.ecore.EObject)
+ */
+ public Collection<EPackage> getAppliedProfiles(EObject model) {
+ return new ArrayList<EPackage>(0);
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.IProfileHelper#getAppliedStereotypes(org.eclipse.emf.ecore.EObject)
+ */
+ public Collection<EClass> getAppliedStereotypes(EObject object) {
+ return new ArrayList<EClass>(0);
+ }
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/UnavailableViewPrototype.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/UnavailableViewPrototype.java
new file mode 100755
index 00000000000..80a2ac3fce5
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/UnavailableViewPrototype.java
@@ -0,0 +1,135 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import org.eclipse.emf.common.command.Command;
+import org.eclipse.emf.ecore.EObject;
+
+
+/**
+ * Represents a view prototype that is not available in the current viewpoints configuration
+ *
+ * @author Laurent Wouters
+ */
+public class UnavailableViewPrototype extends ViewPrototype {
+
+ private String name;
+
+ private String icon;
+
+ /**
+ * Constructor.
+ */
+ protected UnavailableViewPrototype(String name, String icon) {
+ super(null);
+ this.name = name;
+ this.icon = icon;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#isNatural()
+ */
+ @Override
+ public boolean isNatural() {
+ return true;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getImplementation()
+ */
+ @Override
+ public String getImplementation() {
+ return "PapyrusUnavailableView";
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getLabel()
+ */
+ @Override
+ public String getLabel() {
+ return "Unavailable " + name;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getQualifiedName()
+ */
+ @Override
+ public String getQualifiedName() {
+ return "Default Papyrus Viewpoint :: " + getLabel();
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getIconURI()
+ */
+ @Override
+ public String getIconURI() {
+ return "platform:/plugin/org.eclipse.papyrus.infra.viewpoints.policy/icons/Unavailable" + icon + ".gif";
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#isOwnerReassignable()
+ */
+ @Override
+ public boolean isOwnerReassignable() {
+ return false;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#instantiateOn(org.eclipse.emf.ecore.EObject)
+ */
+ @Override
+ public boolean instantiateOn(EObject owner) {
+ return false;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#instantiateOn(org.eclipse.emf.ecore.EObject, java.lang.String)
+ */
+ @Override
+ public boolean instantiateOn(EObject owner, String name) {
+ return false;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getCommandChangeOwner(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject)
+ */
+ @Override
+ public Command getCommandChangeOwner(EObject view, EObject target) {
+ return null;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getCommandChangeRoot(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject)
+ */
+ @Override
+ public Command getCommandChangeRoot(EObject view, EObject target) {
+ return null;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getOwnerOf(org.eclipse.emf.ecore.EObject)
+ */
+ @Override
+ public EObject getOwnerOf(EObject view) {
+ return null;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype#getRootOf(org.eclipse.emf.ecore.EObject)
+ */
+ @Override
+ public EObject getRootOf(EObject view) {
+ return null;
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewPrototype.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewPrototype.java
new file mode 100755
index 00000000000..1d35e6fad90
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewPrototype.java
@@ -0,0 +1,440 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExtension;
+import org.eclipse.core.runtime.IExtensionPoint;
+import org.eclipse.core.runtime.IExtensionRegistry;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.emf.common.command.Command;
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.papyrus.infra.viewpoints.configuration.Category;
+import org.eclipse.papyrus.infra.viewpoints.configuration.ModelRule;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusDiagram;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusSyncTable;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusTable;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusViewpoint;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PathElement;
+import org.eclipse.swt.graphics.Image;
+
+
+/**
+ * Represents a prototype of view in Papyrus
+ *
+ * @author Laurent Wouters
+ */
+public abstract class ViewPrototype {
+ /**
+ * Singleton for unavailable views
+ */
+ public static final ViewPrototype UNAVAILABLE_VIEW = new UnavailableViewPrototype("View", "View");
+ /**
+ * Singleton for unavailable views (diagrams)
+ */
+ public static final ViewPrototype UNAVAILABLE_DIAGRAM = new UnavailableViewPrototype("Diagram", "Diagram");
+
+ /**
+ * Singleton for unavailable views (tables)
+ */
+ public static final ViewPrototype UNAVAILABLE_TABLE = new UnavailableViewPrototype("Table", "Table");
+
+ /**
+ * ID of the extension point for Papyrus diagrams
+ */
+ protected static final String EXTENSION_ID = "org.eclipse.papyrus.infra.viewpoints.policy.viewType";
+
+ /**
+ * The collection of helpers
+ */
+ private static final Collection<IViewTypeHelper> HELPERS = getCommandHelpers();
+
+ /**
+ * Retrieves the helpers from the extensions
+ *
+ * @return The helpers
+ */
+ private static Collection<IViewTypeHelper> getCommandHelpers() {
+ IExtensionRegistry registry = Platform.getExtensionRegistry();
+ IExtensionPoint point = registry.getExtensionPoint(EXTENSION_ID);
+ IExtension[] extensions = point.getExtensions();
+
+ Collection<IViewTypeHelper> result = new ArrayList<IViewTypeHelper>();
+ for (int i = 0; i != extensions.length; i++) {
+ IConfigurationElement[] elements = extensions[i].getConfigurationElements();
+ for (int j = 0; j != elements.length; j++) {
+ try {
+ IViewTypeHelper instance = (IViewTypeHelper) elements[j].createExecutableExtension("class");
+ if (instance != null)
+ result.add(instance);
+ } catch (CoreException e) {
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Determines whether the given configuration element applies on a natural view
+ *
+ * @param config
+ * The configuration element
+ * @return <code>true</code> if the configuration applies on a natural view
+ */
+ public static boolean isNatural(PapyrusView config) {
+ return config.getName() == null && config.getIcon() == null;
+ }
+
+ /**
+ * Determines whether the given object is a supported view object
+ *
+ * @param object
+ * The object to inspect
+ * @return <code>true</code> if the object is a supported view
+ */
+ public static boolean isViewObject(EObject object) {
+ for (IViewTypeHelper helper : HELPERS)
+ if (helper.isSupported(object))
+ return true;
+ return false;
+ }
+
+ /**
+ * Gets the prototype for the given configuration element
+ *
+ * @param config
+ * The configuration element
+ * @return The prototype
+ */
+ public static ViewPrototype get(PapyrusView config) {
+ for (IViewTypeHelper helper : HELPERS) {
+ if (helper.isSupported(config.eClass())) {
+ ViewPrototype proto = helper.getPrototypeFor(config);
+ if (proto != null)
+ return proto;
+ }
+ }
+ if (config instanceof PapyrusDiagram)
+ return UNAVAILABLE_DIAGRAM;
+ else if (config instanceof PapyrusTable)
+ return UNAVAILABLE_TABLE;
+ else if (config instanceof PapyrusSyncTable)
+ return UNAVAILABLE_TABLE;
+ else
+ return UNAVAILABLE_VIEW;
+ }
+
+ /**
+ * Gets the prototype for the the given object representing a view
+ *
+ * @param view
+ * The view for which a prototype is expected
+ * @return The view's prototype
+ */
+ public static ViewPrototype get(EObject view) {
+ for (IViewTypeHelper helper : HELPERS) {
+ if (helper.isSupported(view)) {
+ ViewPrototype proto = helper.getPrototypeOf(view);
+ if (proto != null)
+ return proto;
+ }
+ }
+ return UNAVAILABLE_VIEW;
+ }
+
+ /**
+ * Gets the prototype for the given implementation identifier
+ *
+ * @param implem
+ * The implementation ID
+ * @param owner
+ * The owner
+ * @param root
+ * The root element
+ * @return The prototype
+ */
+ public static ViewPrototype get(String implem, EObject owner, EObject root) {
+ PapyrusView view = PolicyChecker.getCurrent().getViewFrom(implem, owner, root);
+ if (view == null)
+ // The given implementation has been inhibited by the current viewpoint configuration
+ return UNAVAILABLE_VIEW;
+ return get(view);
+ }
+
+
+ /**
+ * The configuration element of this view
+ */
+ protected final PapyrusView configuration;
+
+ /**
+ * Constructor.
+ *
+ * @param configuration
+ * The configuration element
+ */
+ protected ViewPrototype(PapyrusView configuration) {
+ this.configuration = configuration;
+ }
+
+ /**
+ * Gets the configuration element for this view prototype
+ *
+ * @return The configuration element
+ */
+ public PapyrusView getConfiguration() {
+ return configuration;
+ }
+
+ /**
+ * Gets whether the represented view is the raw implementation
+ *
+ * @return <code>true</code> if this is a natural view
+ */
+ public boolean isNatural() {
+ return isNatural(configuration);
+ }
+
+ /**
+ * Gets the ID of the implementation of this prototype
+ *
+ * @return The implementation ID
+ */
+ public String getImplementation() {
+ return configuration.getImplementationID();
+ }
+
+ /**
+ * Gets the label
+ *
+ * @return The label
+ */
+ public String getLabel() {
+ return configuration.getName();
+ }
+
+ /**
+ * Gets the full label that is qualified with its possible root elements
+ *
+ * @return The full label
+ */
+ public String getFullLabel() {
+ StringBuilder builder = new StringBuilder(getLabel());
+ builder.append(" for ");
+ boolean first = true;
+ for (ModelRule rule : configuration.getModelRules()) {
+ if (rule.getStereotypes() != null && rule.getStereotypes().size() > 0) {
+ for (EClass stereotype : rule.getStereotypes()) {
+ if (!first) {
+ builder.append(", ");
+ first = false;
+ }
+ builder.append(stereotype.getName());
+ }
+ } else if (rule.getElement() != null) {
+ if (!first) {
+ builder.append(", ");
+ first = false;
+ }
+ builder.append(rule.getElement().getName());
+ } else {
+ continue;
+ }
+ }
+ return builder.toString();
+ }
+
+ /**
+ * Gets the qualified name of this prototype
+ *
+ * @return The qualified name
+ */
+ public String getQualifiedName() {
+ PapyrusViewpoint vp = (PapyrusViewpoint) configuration.eContainer();
+ return vp.getName() + " :: " + getLabel();
+ }
+
+ /**
+ * Gets the URI of this prototype's icon
+ *
+ * @return The icon's URI
+ */
+ public String getIconURI() {
+ return configuration.getIcon();
+ }
+
+ /**
+ * Gets the image descriptor of this prototype's icon
+ *
+ * @return The icon's descriptor
+ */
+ public ImageDescriptor getIconDescriptor() {
+ URL url = null;
+ try {
+ url = new URL(getIconURI());
+ } catch (MalformedURLException e) {
+ Activator.getDefault().getPapyrusLog().error("Cannot load icon at URI " + getIconURI(), e);
+ }
+ return ImageDescriptor.createFromURL(url);
+ }
+
+ /**
+ * Gets the image of this prototype's icon
+ *
+ * @return The icon's image
+ */
+ public Image getIcon() {
+ return getIconDescriptor().createImage();
+ }
+
+ /**
+ * Gets the categories of this view prototype
+ *
+ * @return The prototype's categories
+ */
+ public Collection<Category> getCategories() {
+ return configuration.getCategories();
+ }
+
+ /**
+ * Gets the initial view's root from the owner
+ *
+ * @param owner
+ * The initial view's owner
+ * @return The initial root
+ */
+ public EObject getRootFor(EObject owner) {
+ for (ModelRule rule : configuration.getModelRules()) {
+ if (rule.getAutoSelectPath() != null && rule.getAutoSelectPath().size() > 0) {
+ EObject result = traverse(owner, rule.getAutoSelectPath());
+ if (result != null)
+ return result;
+ }
+ }
+ return owner;
+ }
+
+ private EObject traverse(EObject from, EList<PathElement> path) {
+ EObject current = from;
+ for (PathElement elem : path) {
+ EReference feature = elem.getFeature();
+ if (current.eIsSet(feature)) {
+ Object temp = current.eGet(feature);
+ if (temp instanceof EList) {
+ current = ((EList<? extends EObject>) temp).get(0);
+ } else if (temp instanceof EObject) {
+ current = (EObject) temp;
+ } else {
+ return null;
+ }
+ } else {
+ return null;
+ }
+ }
+ return current;
+ }
+
+ /**
+ * Determines whether the instances of this prototype can change owners
+ *
+ * @return <code>true</code> if the owner is reassignable
+ */
+ public abstract boolean isOwnerReassignable();
+
+ /**
+ * Create a new view from this prototype with the given owner
+ *
+ * @param owner
+ * The new view's owner
+ * @return <code>true</code> if the instantiation succeeded
+ */
+ public abstract boolean instantiateOn(EObject owner);
+
+ /**
+ * Create a new view from this prototype with the given owner and name
+ *
+ * @param owner
+ * The new view's owner
+ * @param name
+ * The new view's name
+ * @return <code>true</code> if the instantiation succeeded
+ */
+ public abstract boolean instantiateOn(EObject owner, String name);
+
+ /**
+ * Gets the command for moving the given view to a new owner (target)
+ *
+ * @param view
+ * The view to be moved
+ * @param target
+ * The target owner
+ * @return The command, or <code>null</code> if the change is not allowed
+ */
+ public abstract Command getCommandChangeOwner(EObject view, EObject target);
+
+ /**
+ * Gets the command for changing the root element of the given view to a new element (target)
+ *
+ * @param view
+ * The view to change root
+ * @param target
+ * The target element
+ * @return The command, or <code>null</code> if the change is not allowed
+ */
+ public abstract Command getCommandChangeRoot(EObject view, EObject target);
+
+ /**
+ * Gets the object owning the given view
+ *
+ * @param view
+ * The view
+ * @return The view's owner
+ */
+ public abstract EObject getOwnerOf(EObject view);
+
+ /**
+ * Gets the object which is the root element of the given view
+ *
+ * @param view
+ * The view
+ * @return The views's root element
+ */
+ public abstract EObject getRootOf(EObject view);
+
+ /**
+ * Represents a comparator of prototypes
+ *
+ * @author Laurent Wouters
+ */
+ public static class Comp implements Comparator<ViewPrototype> {
+ /**
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ public int compare(ViewPrototype arg0, ViewPrototype arg1) {
+ return (arg0.getLabel().compareTo(arg1.getLabel()));
+ }
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewPrototypeContentProvider.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewPrototypeContentProvider.java
new file mode 100755
index 00000000000..a12c267315a
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewPrototypeContentProvider.java
@@ -0,0 +1,255 @@
+/*****************************************************************************
+ * Copyright (c) 2014 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.papyrus.infra.viewpoints.configuration.Category;
+import org.eclipse.papyrus.infra.viewpoints.configuration.ModelRule;
+import org.eclipse.papyrus.infra.viewpoints.configuration.OwningRule;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusViewpoint;
+import org.eclipse.swt.graphics.Image;
+
+/**
+ * Content provider of ViewPrototypes
+ *
+ * @author Laurent Wouters
+ *
+ */
+public class ViewPrototypeContentProvider implements ITreeContentProvider {
+
+ private static Image loadImage(String uri) {
+ URL url = null;
+ try {
+ url = new URL(uri);
+ } catch (MalformedURLException e) {
+ }
+ return ImageDescriptor.createFromURL(url).createImage();
+ }
+
+ public static class LP extends LabelProvider {
+
+ private Image imageCategory = loadImage("platform:/plugin/org.eclipse.papyrus.infra.viewpoints.configuration.edit/icons/full/obj16/PapyrusView.png");
+ private Image imageViewpoint = loadImage("platform:/plugin/org.eclipse.papyrus.infra.viewpoints.configuration.edit/icons/full/obj16/PapyrusViewpoint.png");
+ private Image imageProfile = loadImage("platform:/plugin/org.eclipse.papyrus.uml.profile/resources/icons/Profile.gif");
+ private Image imageModel = loadImage("platform:/plugin/org.eclipse.papyrus.infra.viewpoints.configuration.edit/icons/full/obj16/ModelRule.png");
+ private Image imageOwner = loadImage("platform:/plugin/org.eclipse.papyrus.infra.viewpoints.configuration.edit/icons/full/obj16/OwningRule.png");
+
+ @Override
+ public String getText(Object element) {
+ if (element instanceof Category)
+ return "Category " + ((Category) element).getName();
+ if (element instanceof ViewPrototype)
+ return ((ViewPrototype) element).getLabel();
+ if (element instanceof PapyrusViewpoint)
+ return "Available in viewpoint: " + ((PapyrusViewpoint) element).getName();
+ if (element instanceof EPackage)
+ return "Required profile: " + ((EPackage) element).getNsURI();
+ if (element instanceof ModelRule)
+ return getText((ModelRule) element);
+ if (element instanceof OwningRule)
+ return getText((OwningRule) element);
+ return element.toString();
+ }
+
+ private String getText(ModelRule rule) {
+ StringBuilder builder = new StringBuilder("Possible root: ");
+ boolean first = true;
+ for (EClass ste : rule.getStereotypes()) {
+ if (!first)
+ builder.append(", ");
+ builder.append("<<");
+ builder.append(ste.getEPackage().getNsPrefix());
+ builder.append("::");
+ builder.append(ste.getName());
+ builder.append(">>");
+ first = false;
+ }
+ if (rule.getElement() != null) {
+ if (!first)
+ builder.append(" ");
+ builder.append(rule.getElement().getEPackage().getNsPrefix());
+ builder.append("::");
+ builder.append(rule.getElement().getName());
+ }
+ return builder.toString();
+ }
+
+ private String getText(OwningRule rule) {
+ StringBuilder builder = new StringBuilder("Possible owner: ");
+ boolean first = true;
+ for (EClass ste : rule.getStereotypes()) {
+ if (!first)
+ builder.append(", ");
+ builder.append("<<");
+ builder.append(ste.getEPackage().getNsPrefix());
+ builder.append("::");
+ builder.append(ste.getName());
+ builder.append(">>");
+ first = false;
+ }
+ if (rule.getElement() != null) {
+ if (!first)
+ builder.append(" ");
+ builder.append(rule.getElement().getEPackage().getNsPrefix());
+ builder.append("::");
+ builder.append(rule.getElement().getName());
+ }
+ return builder.toString();
+ }
+
+ @Override
+ public Image getImage(Object element) {
+ if (element instanceof Category)
+ return imageCategory;
+ if (element instanceof ViewPrototype)
+ return ((ViewPrototype) element).getIcon();
+ if (element instanceof PapyrusViewpoint)
+ return imageViewpoint;
+ if (element instanceof EPackage)
+ return imageProfile;
+ if (element instanceof ModelRule)
+ return imageModel;
+ if (element instanceof OwningRule)
+ return imageOwner;
+ return null;
+ }
+ }
+
+ /**
+ * The root of all views
+ */
+ public static final Object treeRoot = new Object();
+
+ /**
+ * The views
+ */
+ private Map<Category, List<ViewPrototype>> views;
+
+ /**
+ * The categories
+ */
+ private List<Category> categories;
+
+ /**
+ * Creates the provider
+ *
+ * @param views
+ * The views to provide
+ */
+ public ViewPrototypeContentProvider() {
+ this.views = new HashMap<Category, List<ViewPrototype>>();
+ this.categories = new ArrayList<Category>();
+ for (ViewPrototype view : PolicyChecker.getCurrent().getAllPrototypes()) {
+ for (Category category : view.getCategories()) {
+ if (!categories.contains(category)) {
+ categories.add(category);
+ views.put(category, new ArrayList<ViewPrototype>());
+ }
+ views.get(category).add(view);
+ }
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jface.viewers.IContentProvider#dispose()
+ */
+ public void dispose() {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
+ */
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object)
+ */
+ public Object[] getElements(Object inputElement) {
+ return categories.toArray();
+ }
+
+
+ private Object[] getChildren(ViewPrototype view) {
+ if (view.getConfiguration() == null)
+ return new String[0];
+ List<Object> data = new ArrayList<Object>();
+ data.add(view.getConfiguration().eContainer());
+ data.addAll(view.getConfiguration().getProfiles());
+ data.addAll(view.getConfiguration().getModelRules());
+ data.addAll(view.getConfiguration().getOwningRules());
+ return data.toArray();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
+ */
+ public Object[] getChildren(Object element) {
+ if (element == treeRoot)
+ return categories.toArray();
+ if (element instanceof Category)
+ return views.get((Category) element).toArray();
+ if (element instanceof ViewPrototype)
+ return getChildren((ViewPrototype) element);
+ return new Object[0];
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
+ */
+ public Object getParent(Object element) {
+ if (element == treeRoot)
+ return null;
+ if (element instanceof Category)
+ return treeRoot;
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
+ */
+ public boolean hasChildren(Object element) {
+ if (element == treeRoot)
+ return true;
+ if (element instanceof Category)
+ return true;
+ if (element instanceof ViewPrototype)
+ return true;
+ return false;
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewpointExplorer.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewpointExplorer.java
new file mode 100755
index 00000000000..0198e5d56fb
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/ViewpointExplorer.java
@@ -0,0 +1,90 @@
+/*****************************************************************************
+ * Copyright (c) 2014 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerSorter;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.ui.part.ViewPart;
+
+/**
+ * Eclipse view for the user to explore the current viewpoint
+ *
+ * @author Laurent Wouters
+ *
+ */
+public class ViewpointExplorer extends ViewPart {
+
+ private TreeViewer tree;
+
+ public ViewpointExplorer() {
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
+ */
+ public void createPartControl(Composite parent) {
+ GridLayout gridLayout = new GridLayout();
+ gridLayout.numColumns = 1;
+ parent.setLayout(gridLayout);
+
+ Composite inner = new Composite(parent, SWT.NONE);
+ inner.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.FILL_VERTICAL));
+ inner.setLayout(new FillLayout());
+
+ tree = new TreeViewer(inner, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
+ tree.setLabelProvider(new ViewPrototypeContentProvider.LP());
+ tree.setSorter(getViewerSorter());
+ tree.setContentProvider(new ViewPrototypeContentProvider());
+ tree.setInput(ViewPrototypeContentProvider.treeRoot);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
+ */
+ @Override
+ public void setFocus() {
+
+ }
+
+
+ /**
+ * Gets the viewer sorter for this dialog
+ *
+ * @return A viewer sorter
+ */
+ private ViewerSorter getViewerSorter() {
+ return new ViewerSorter() {
+ @Override
+ public int compare(Viewer viewer, Object e1, Object e2) {
+ if (e1 instanceof ViewPrototype) {
+ ViewPrototype b1 = (ViewPrototype) e1;
+ ViewPrototype b2 = (ViewPrototype) e2;
+ return b1.getFullLabel().compareTo(b2.getFullLabel());
+ }
+ return super.compare(viewer, e1, e2);
+ }
+ };
+ }
+}
diff --git a/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/WeightedConfiguration.java b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/WeightedConfiguration.java
new file mode 100755
index 00000000000..97483b33144
--- /dev/null
+++ b/plugins/infra/viewpoints/org.eclipse.papyrus.infra.viewpoints.policy/src/org/eclipse/papyrus/infra/viewpoints/policy/WeightedConfiguration.java
@@ -0,0 +1,123 @@
+/*****************************************************************************
+ * 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:
+ * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.viewpoints.policy;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExtension;
+import org.eclipse.core.runtime.IExtensionPoint;
+import org.eclipse.core.runtime.IExtensionRegistry;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusConfiguration;
+
+
+/**
+ * Represents a Papyrus viewpoints configuration associated to a priority
+ *
+ * @author Laurent Wouters
+ */
+public class WeightedConfiguration {
+ /**
+ * ID of the extension point defining the configurations
+ */
+ private static final String EXTENSION_ID = "org.eclipse.papyrus.infra.viewpoints.policy.custom";
+
+ private String uri;
+ private int priority;
+ private PapyrusConfiguration config;
+
+ /**
+ * Gets the URI of the is configuration
+ *
+ * @return The configuration's URI
+ */
+ public String getURI() {
+ return uri;
+ }
+
+ /**
+ * Gets the Papyrus configuration represented by this object
+ *
+ * @return A Papyrus Configuration
+ */
+ public PapyrusConfiguration getConfiguration() {
+ if (this.config == null)
+ this.config = PolicyChecker.loadConfigurationFrom(uri);
+ return this.config;
+ }
+
+ /**
+ * Initializes the configuration from the given configuration element
+ *
+ * @param config
+ * The extension point configuration element
+ */
+ private WeightedConfiguration(String plugin, IConfigurationElement config) {
+ this.uri = config.getAttribute("file");
+ if (!this.uri.startsWith(PreferenceConstants.P_CONF_PATH_SCHEME_PLUGIN_VALUE))
+ this.uri = PreferenceConstants.P_CONF_PATH_SCHEME_PLUGIN_VALUE + plugin + "/" + this.uri;
+ try {
+ this.priority = Integer.parseInt(config.getAttribute("priority"));
+ } catch (NumberFormatException ex) {
+ }
+ }
+
+
+ /**
+ * Represents a comparator for weighted configurations
+ *
+ * @author Laurent Wouters
+ */
+ private static class Comparator implements java.util.Comparator<WeightedConfiguration>, Serializable {
+ /**
+ * Serial version
+ */
+ private static final long serialVersionUID = 9025582514350440832L;
+
+ /**
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ public int compare(WeightedConfiguration o1, WeightedConfiguration o2) {
+ return (o2.priority - o1.priority);
+ }
+ }
+
+ /**
+ * Gets the configuration with the top priority
+ *
+ * @return The appropriate configuration, or <code>null</code> if none was found
+ */
+ public static WeightedConfiguration getTopConfiguration() {
+ IExtensionRegistry registry = Platform.getExtensionRegistry();
+ IExtensionPoint point = registry.getExtensionPoint(EXTENSION_ID);
+ IExtension[] extensions = point.getExtensions();
+
+ List<WeightedConfiguration> configs = new ArrayList<WeightedConfiguration>();
+ for (int i = 0; i != extensions.length; i++) {
+ String plugin = extensions[i].getContributor().getName();
+ IConfigurationElement[] elements = extensions[i].getConfigurationElements();
+ for (int j = 0; j != elements.length; j++)
+ if (elements[j].getName().equals("configuration"))
+ configs.add(new WeightedConfiguration(plugin, elements[j]));
+ }
+ Collections.sort(configs, new Comparator());
+ if (configs.size() == 0)
+ return null;
+ return configs.get(0);
+ }
+}

Back to the top