Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 95168e01a75627b9accd184c62f2365608be550f (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
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST and others.
 * 
 * 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:
 *   Shuai Li (CEA LIST) <shuai.li@cea.fr> - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.editor;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IPage;
import org.eclipse.papyrus.infra.core.sashwindows.di.PageRef;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.INavigationLocation;
import org.eclipse.ui.NavigationLocation;

public class PapyrusNavigationLocation extends NavigationLocation {
	private URI pageURI;
	
	/**
	 * Constructor: Save editor selection, selected editor part, and selected page
	 *
	 */
	public PapyrusNavigationLocation(IEditorPart editorPart) {
		super(editorPart);
		if (editorPart instanceof PapyrusMultiDiagramEditor) {
			PapyrusMultiDiagramEditor papyrusEditor = (PapyrusMultiDiagramEditor) editorPart;
			// Important to set editorSelection and selectedEditorPart, otherwise navigation history breaks
			/*ISelection editorSelection = papyrusEditor.getEditorSite().getSelectionProvider().getSelection();//papyrusEditor.getSite().getSelectionProvider().getSelection()
			IEditorPart selectedEditorPart = papyrusEditor.getActiveEditor();*/
			IPage selectedPage = papyrusEditor.getISashWindowsContainer().getActiveSashWindowsPage();
			
			if (selectedPage != null && selectedPage.getRawModel() instanceof PageRef) {
				PageRef pageRef = (PageRef) selectedPage.getRawModel();
				if (pageRef != null && pageRef.getEmfPageIdentifier() != null) {
					pageURI = EcoreUtil.getURI(pageRef.getEmfPageIdentifier());
				}
			}
		}
	}

	/**
	 * @see org.eclipse.ui.INavigationLocation#restoreLocation()
	 *
	 */
	@Override
	public void restoreLocation() {
		IEditorPart editorPart = getEditorPart();
		if (editorPart instanceof PapyrusMultiDiagramEditor) {
			try {
				IPageManager pageManager = ServiceUtils.getInstance().getIPageManager(((PapyrusMultiDiagramEditor) editorPart).getServicesRegistry());
				EObject objectToOpen = ServiceUtils.getInstance().getModelSet(((PapyrusMultiDiagramEditor) editorPart).getServicesRegistry()).getEObject(pageURI, false);
				
				if (objectToOpen != null) {
					EObject context = EMFHelper.getEObject(objectToOpen);
					if (context != null) {
						try {
							pageManager = ServiceUtilsForEObject.getInstance().getIPageManager(context);
							if (pageManager.isOpen(objectToOpen)) {
								pageManager.selectPage(objectToOpen);
							} else {
								pageManager.openPage(objectToOpen);
							}
						} catch (Exception ex) {
							Activator.log.error(ex);
						}
					}
				}
				
			} catch (ServiceException e) {
				Activator.log.error(e);
			}
		}
	}

	/**
	 * If the selected editor part matches, then merge.
	 * 
	 * @see org.eclipse.ui.INavigationLocation#mergeInto(org.eclipse.ui.INavigationLocation)
	 *
	 * @param currentLocation
	 * @return
	 */
	@Override
	public boolean mergeInto(INavigationLocation currentLocation) {
		if (currentLocation instanceof PapyrusNavigationLocation) {
			if (((PapyrusNavigationLocation) currentLocation).getPageURI() != null && pageURI != null) {
				return ((PapyrusNavigationLocation) currentLocation).getPageURI().equals(pageURI);
			}
		}
		return false;
	}

	public URI getPageURI() {
		return this.pageURI;
	}

	/**
	 * @see org.eclipse.ui.INavigationLocation#saveState(org.eclipse.ui.IMemento)
	 *
	 * @param memento
	 */
	@Override
	public void saveState(IMemento memento) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see org.eclipse.ui.INavigationLocation#restoreState(org.eclipse.ui.IMemento)
	 *
	 * @param memento
	 */
	@Override
	public void restoreState(IMemento memento) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see org.eclipse.ui.INavigationLocation#update()
	 *
	 */
	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}
}

Back to the top