Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa04498cc46ae56c6845f9de77b6653c30dbb9b5 (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
/*******************************************************************************
 * Copyright (c) 2011 AtoS
 * 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:
 *    Anass RADOUANI (AtoS)
 *******************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.navigation.handlers;

import java.util.Collections;
import java.util.List;
import java.util.Vector;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.core.editor.CoreMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.utils.OpenDiagramCommand;
import org.eclipse.papyrus.infra.gmfdiag.common.DiagramsUtil;
import org.eclipse.papyrus.infra.gmfdiag.navigation.Activator;
import org.eclipse.papyrus.infra.gmfdiag.navigation.utils.MultiDiagramDialog;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.window.Window;

/**
 * handler for Up command
 * 
 */
public class TopNavigateHandler extends AbstractHandler {

	private IEditorPart editor;

	public Object execute(ExecutionEvent event) throws ExecutionException {
		editor = HandlerUtil.getActiveEditor(event);
		if(editor instanceof CoreMultiDiagramEditor) {
			CoreMultiDiagramEditor papyrusEditor = (CoreMultiDiagramEditor)editor;
			EObject parent = papyrusEditor.getDiagram().getElement().eContainer();
			List<List<Diagram>> diagramsTemp = new Vector<List<Diagram>>();
			List<Diagram> diagrams = Collections.emptyList();
			List<Diagram> associatedDiag = Collections.emptyList();
			if(parent != null) {
				while(parent != null) {
					associatedDiag = DiagramsUtil.getAssociatedDiagrams(parent, null);
					if(!associatedDiag.isEmpty()) {
						diagramsTemp.add(associatedDiag);
					}
					parent = parent.eContainer();
				}

				while(diagrams.size() == 0 && diagramsTemp.size() != 0) {
					diagrams = diagramsTemp.get(diagramsTemp.size() - 1);
					diagramsTemp.remove(diagramsTemp.size() - 1);

				}
				openDialog(papyrusEditor, diagrams);

			}
		}
		return null;
	}

	/**
	 * open the diagram if there is just one or open a dialog to choose the
	 * diagram to open if there is more than one
	 * 
	 * @param papyrusEditor
	 * @param diagrams
	 */
	private void openDialog(CoreMultiDiagramEditor papyrusEditor, List<Diagram> diagrams) {
		if(diagrams != null) {
			if(diagrams.size() == 1) {
				openDiagram(papyrusEditor, diagrams.get(0));
			} else if(diagrams.size() > 1) {
				MultiDiagramDialog multiDiagramDialog = new MultiDiagramDialog(Activator.getActiveWorkbenchShell(), diagrams);
				if(multiDiagramDialog.open() == Window.OK) {
					openDiagram(papyrusEditor, diagrams.get(multiDiagramDialog.getSelectedDiagram()));
				}
			}
		}
	}

	/**
	 * open a dialog to choose which diagram have to be opened
	 * 
	 * @param papyrusEditor
	 * @param diagram
	 */
	private void openDiagram(CoreMultiDiagramEditor papyrusEditor, Diagram diagram) {
		if(papyrusEditor.getEditingDomain() instanceof TransactionalEditingDomain) {
			TransactionalEditingDomain domain = (TransactionalEditingDomain)papyrusEditor.getEditingDomain();
			OpenDiagramCommand openDiagramCommand = new OpenDiagramCommand(domain, diagram);
			domain.getCommandStack().execute(new GMFtoEMFCommandWrapper(openDiagramCommand));
		}
	}

}

Back to the top