Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0d28ae96430be76fe9224c77c66917806a25a191 (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
/*****************************************************************************
 * Copyright (c) 2012 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:
 *  Tristan FAURE  tristan.faure@atos.net - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.core.editor;

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

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.EditorManager;


/**
 * Methods to manage one editor 
 * @author Tristan FAURE
 *
 */
@SuppressWarnings("restriction")
public final class OneInstanceUtils {
	
	public static IWorkbenchWindow getWindow () {
		if (PlatformUI.getWorkbench() != null){
			if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null){
				return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
			}
		}
		return null ;
	}
	
	/**
	 * Determine if Papyrus is already opened
	 * @param reference
	 * @return
	 */
	public static boolean isPapyrusOpen(CoreMultiDiagramEditor reference) {
		return isPapyrusOpen(getWindow(),reference);
	}
	
	/**
	 * Determine if Papyrus is already opened
	 * @param reference
	 * @return
	 */
	public static boolean isPapyrusOpen(IWorkbenchWindow window, CoreMultiDiagramEditor reference) {
		return false;
//		return getAllEdtiorsExceptReference(window,reference).size() > 0 ;
	}
	
	/**
	 * get all the papyrus editors except reference parameter
	 * @param window
	 * @param reference
	 * @return
	 */
	public static Iterable<CoreMultiDiagramEditor> getAllEdtiorsExceptReference (CoreMultiDiagramEditor reference){
		return getAllEdtiorsExceptReference(getWindow(),reference);
	}
	
	/**
	 * get all the papyrus editors except reference parameter
	 * @param window
	 * @param reference
	 * @return
	 */
	public static List<CoreMultiDiagramEditor> getAllEdtiorsExceptReference (IWorkbenchWindow window, CoreMultiDiagramEditor reference){
		List<CoreMultiDiagramEditor> result = new LinkedList<CoreMultiDiagramEditor>();
		if (window != null)	{
			for (IWorkbenchPage p : window.getPages()) {
				for (IEditorReference r : p.getEditorReferences()) {
					IEditorPart editor = r.getEditor(false);
					if (editor != reference && editor instanceof CoreMultiDiagramEditor) {
						result.add((CoreMultiDiagramEditor)editor);
					}
				}
			}
		}
		return result ;
	}
	
	/**
	 * Save all the instances (normally one) of the Papyrus Editors opened
	 * @param window
	 * @param reference
	 * @param list
	 * @return
	 */
	public static boolean saveAllPapyrusOpened (IWorkbenchWindow window, CoreMultiDiagramEditor reference, List<CoreMultiDiagramEditor> list){
		if (list == null) {
			list = getAllEdtiorsExceptReference(window, reference);
		}
		return EditorManager.saveAll(list, true, true, false, window);
	}
	
	/**
	 * return false if the operation is canceled
	 * @param window
	 * @param reference
	 * @return
	 */
	public static boolean closeAllPapyrusOpened (IWorkbenchWindow window, CoreMultiDiagramEditor reference){
		final List<CoreMultiDiagramEditor> parts = getAllEdtiorsExceptReference(window,reference);
		if (saveAllPapyrusOpened(window, reference, parts)) {
			Display.getDefault().asyncExec(new Runnable() {
				
				public void run() {
					for (IEditorPart p : parts){
						p.getEditorSite().getPage().closeEditor(p, false);
					}
				}
			});
			
		}
		else {
			// the user selects cancel to the operation so return false 
			return false ;
		}
		return true ;
	}
	
	/**
	 * return false if the operation is canceled
	 * @param window
	 * @param reference
	 * @return
	 */
	public static boolean closeAllPapyrusOpened (CoreMultiDiagramEditor reference){
		return closeAllPapyrusOpened(getWindow(),reference);
	}
	
}

Back to the top