Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f891841e0e2d4abd83ae7dd93ef3ad94111ed4d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*****************************************************************************
 * Copyright (c) 2010 Atos Origin.
 *
 *
 * 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:
 *  Mathieu Velten (Atos Origin) mathieu.velten@atosorigin.com - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.navigation;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.papyrus.commands.ICreationCommand;
import org.eclipse.papyrus.commands.OpenDiagramCommand;
import org.eclipse.papyrus.infra.core.resource.ModelSet;

public class NavigationHelper {

	private static class NavigationHelperHolder {

		public static final NavigationHelper instance = new NavigationHelper();
	}

	public static NavigationHelper getInstance() {
		return NavigationHelperHolder.instance;
	}

	private static final String EXTENSION_ID = "org.eclipse.papyrus.infra.gmfdiag.navigation.navigationRules";

	private static final String NAVIGATIONRULE_ID = "navigationRule";

	private static final String RULE_ID = "rule";

	private List<INavigationRule> navigationRules = new LinkedList<INavigationRule>();

	private NavigationHelper() {
		// Reading data from plugins
		IConfigurationElement[] configElements = Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_ID);
		for (IConfigurationElement configElement : configElements) {
			if (NAVIGATIONRULE_ID.equals(configElement.getName())) {
				try {
					Object obj = configElement.createExecutableExtension(RULE_ID);
					if (obj instanceof INavigationRule) {
						navigationRules.add((INavigationRule) obj);
					}
				} catch (CoreException e) {
				}
			}
		}
	}

	private void addNextNavigableElements(NavigableElement currentNavElement, List<NavigableElement> navElements) {

		navElements.add(currentNavElement);

		for (INavigationRule navigationRule : navigationRules) {
			if (navigationRule.handle(currentNavElement.getElement())) {
				List<NavigableElement> nextNavigableElements = navigationRule.getNextPossibleElements(currentNavElement);
				for (NavigableElement nextNavigableElement : nextNavigableElements) {
					addNextNavigableElements(nextNavigableElement, navElements);
				}
			}
		}
	}

	/**
	 * get all navigation possibilities (existing or "to create" elements) from
	 * the specified element.
	 *
	 * @param element
	 *            the element
	 * @return a list of navigation possibilities
	 */
	public List<NavigableElement> getAllNavigableElements(EObject element) {
		ArrayList<NavigableElement> navElements = new ArrayList<NavigableElement>();
		if (element != null) {
			addNextNavigableElements(new ExistingNavigableElement(element, null), navElements);
		}
		return navElements;
	}

	/**
	 * This method link the created element with its parent and browse the
	 * hierarchy to link the parents between them. Ultimately the hierarchy will
	 * be attached to an {@link ExistingNavigableElement}, so to the original
	 * model.
	 *
	 * @param createdNavElement
	 */
	public static void linkToModel(CreatedNavigableElement createdNavElement) {
		NavigableElement navElement = createdNavElement;
		while (navElement instanceof CreatedNavigableElement) {
			CreatedNavigableElement cne = (CreatedNavigableElement) navElement;
			cne.linkToModel();
			navElement = cne.getPreviousNavigableElement();
		}
	}

	/**
	 * Same behavior as {@link NavigationHelper#linkToModel(CreatedNavigableElement)} but set the
	 * base name of the elements.
	 *
	 * @param createdNavElement
	 * @param base
	 */
	public static void setBaseName(CreatedNavigableElement createdNavElement, String base) {
		NavigableElement navElement = createdNavElement;
		while (navElement instanceof CreatedNavigableElement) {
			CreatedNavigableElement cne = (CreatedNavigableElement) navElement;
			cne.setBaseName(base);
			navElement = cne.getPreviousNavigableElement();
		}
	}

	public static CompositeCommand getLinkCreateAndOpenNavigableDiagramCommand(final NavigableElement navElement, ICreationCommand creationCommandInterface, final String diagramName, ModelSet modelSet) {
		CompositeCommand compositeCommand = new CompositeCommand("Create diagram");

		if (navElement instanceof CreatedNavigableElement) {
			compositeCommand.add(new AbstractTransactionalCommand(modelSet.getTransactionalEditingDomain(), "Create hierarchy", null) {

				@Override
				protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
					NavigationHelper.linkToModel((CreatedNavigableElement) navElement);
					NavigationHelper.setBaseName((CreatedNavigableElement) navElement, "");
					return CommandResult.newOKCommandResult();
				}
			});
		}

		ICommand createDiagCommand = creationCommandInterface.getCreateDiagramCommand(modelSet, navElement.getElement(), diagramName);
		compositeCommand.add(createDiagCommand);
		compositeCommand.add(new OpenDiagramCommand(modelSet.getTransactionalEditingDomain(), createDiagCommand));

		return compositeCommand;
	}
}

Back to the top