Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0a0f65939079d8859fc20455936c426d2cd52db3 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*******************************************************************************
 * Copyright (c) 2013 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:
 *     Arthur Daussy - initial implementation
 *******************************************************************************/
package org.eclipse.papyrus.team.collaborative.strategy.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.gmf.runtime.common.ui.dialogs.SaveAllDirtyEditorsDialog;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.editor.CoreMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForWorkbenchPage;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.team.collaborative.core.IExtendedURI;
import org.eclipse.papyrus.team.collaborative.core.reports.CollabStatus;
import org.eclipse.papyrus.team.collaborative.core.reports.LogHelper;
import org.eclipse.papyrus.team.collaborative.core.utils.CollabFunctionsFactory;
import org.eclipse.papyrus.team.collaborative.core.utils.ModelsUtil;
import org.eclipse.papyrus.team.collaborative.strategy.Activator;
import org.eclipse.papyrus.team.collaborative.strategy.RefreshHelper;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerView;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;


/**
 * Utility class for User Interface interactions.
 * 
 * @author adaussy
 */
public class UIUtils {

	/** The resource manager. */
	public static LocalResourceManager RESOURCE_MANAGER = new LocalResourceManager(JFaceResources.getResources());

	/**
	 * Save all dirty editor.
	 * 
	 * @return the {@link IStatus}
	 */
	public static IStatus saveAllDirtyEditor() {
		//Force save
		IEditorPart[] dirtyEditors = SaveAllDirtyEditorsDialog.getDirtyEditors();
		if(dirtyEditors.length > 0) {
			SaveAllDirtyEditorsDialog dialog = new SaveAllDirtyEditorsDialog(Display.getDefault().getActiveShell());
			if(dialog.open() == SaveAllDirtyEditorsDialog.CANCEL) {
				return Status.CANCEL_STATUS;
			} else {
				for(int i = 0; i < dirtyEditors.length; i++) {
					IEditorPart editorI = dirtyEditors[i];
					editorI.doSave(new NullProgressMonitor());
				}
			}
		}
		return Status.OK_STATUS;
	}

	/**
	 * Gets the current resource set.
	 * 
	 * @return the current resource set
	 */
	public static ResourceSet getCurrentResourceSet() {
		IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if(editor instanceof CoreMultiDiagramEditor) {
			CoreMultiDiagramEditor papyrusEditor = (CoreMultiDiagramEditor)editor;
			return papyrusEditor.getEditingDomain().getResourceSet();
		}
		return null;
	}

	/**
	 * Return all the leaf from the semantic meta model
	 * 
	 * @param uris
	 *        Input set for the search of leaf
	 * @param resourceSet
	 * @return
	 */
	public static Collection<EObject> getLeafSemanticElement(Collection<IExtendedURI> uris, ResourceSet resourceSet) {
		Collection<IExtendedURI> semanticURIS = Collections2.filter(uris, new Predicate<IExtendedURI>() {

			public boolean apply(IExtendedURI arg0) {
				return UmlModel.UML_FILE_EXTENSION.equals(arg0.getUri().fileExtension());
			}
		});
		Collection<Collection<EObject>> eObjects = Collections2.transform(semanticURIS, CollabFunctionsFactory.getExtendedURIToEObjects(resourceSet));
		Set<EObject> semanticElements = new HashSet<EObject>();
		for(Collection<EObject> eos : eObjects) {
			semanticElements.addAll(eos);
		}
		return ModelsUtil.getRoots(semanticElements);
	}

	/**
	 * Gets the first selection from the workbench
	 * 
	 * @return the first selection
	 */
	public static EObject getFirstSelection() {
		Collection<EObject> selection = getSelection();
		if(selection.isEmpty()) {
			return null;
		}
		return selection.iterator().next();
	}

	/**
	 * Gets the selection from the workbench
	 * 
	 * @return the selection
	 */
	public static Collection<EObject> getSelection() {
		ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
		ISelection selection = selectionService.getSelection();
		if(selection != null) {
			if(selection instanceof IStructuredSelection) {
				List<EObject> result = new ArrayList<EObject>();
				Iterator<?> ite = ((IStructuredSelection)selection).iterator();
				while(ite.hasNext()) {
					Object next = ite.next();
					EObject eObject = EMFHelper.getEObject(next);
					if(eObject != null) {
						result.add(eObject);
					}
				}
				return result;
			}
		}
		return Collections.emptyList();
	}

	/**
	 * Refresh elements in model explorer view.
	 * 
	 * @param uris
	 *        {@link IExtendedURI} to refresh
	 * @param resourceSet
	 *        the resource set
	 */
	public static void refreshModelExplorer(Set<IExtendedURI> uris, ResourceSet resourceSet) {
		for(Collection<EObject> objects : Collections2.transform(uris, CollabFunctionsFactory.getExtendedURIToEObjects(resourceSet))) {
			RefreshHelper.refreshModelExplorer(objects);
		}
	}

	/**
	 * This reload the current editor
	 * WARNING : BE careful after this action the ancient resourceSet will deprecated you may use getCurrentResourceSet() to retrieve the new one.
	 */
	public static void reloadEditor() {
		Display.getCurrent().syncExec(new Runnable() {

			public void run() {
				IWorkbench wb = PlatformUI.getWorkbench();
				IWorkbenchPage page = wb.getActiveWorkbenchWindow().getActivePage();
				IEditorPart editor = page.getActiveEditor();
				IEditorInput input = editor.getEditorInput();
				page.closeEditor(editor, true);
				try {
					IEditorDescriptor desc = wb.getEditorRegistry().getDefaultEditor(input.getName());
					page.openEditor(input, desc.getId(), false);
				} catch (PartInitException e) {
					LogHelper.getInstance().logError("Error during reloading", Activator.PLUGIN_ID, e);
				}
			}
		});
	}

	/**
	 * Display an error dialog using a status.
	 * 
	 * @param errorStatus
	 *        the error status
	 * @param title
	 *        the title
	 */
	public static void errorDialog(IStatus errorStatus, String title) {
		ErrorDialog dialog = new ErrorDialog(Display.getDefault().getActiveShell(), title, errorStatus.getMessage(), errorStatus, Status.ERROR);
		dialog.open();
		LogHelper.getInstance().logError(errorStatus.getMessage(), Activator.PLUGIN_ID, errorStatus.getException());
	}

	/**
	 * Error dialog.
	 * 
	 * @param throwable
	 *        the throwable
	 * @param title
	 *        the title
	 * @param message
	 *        the message
	 */
	public static void errorDialog(Throwable throwable, String title, String message) {
		errorDialog(CollabStatus.createErrorStatus(message, throwable), title);
	}

	/** The adapter factory. */
	private static ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

	/**
	 * Try to get a correct label from a Object
	 * Handle: {@link EObject} {@link Resource}.
	 * 
	 * @param object
	 *        the object
	 * @return the label
	 */
	public static String getLabel(Object object) {
		if(object instanceof EObject) {
			EObject eObject = (EObject)object;
			IItemLabelProvider labelProvider = (IItemLabelProvider)adapterFactory.adapt(eObject, IItemLabelProvider.class);
			return labelProvider.getText(eObject);
		} else if(object instanceof Resource) {
			return "Resouce " + ((Resource)object).getURI();
		}
		return object.toString();
	}

	public static ILabelProvider getModelExplorerLavelProvider() {
		ILabelProvider labelProvider = getLabelProvider(ModelExplorerView.LABEL_PROVIDER_SERVICE_CONTEXT);
		if(labelProvider == null) {
			labelProvider = getLabelProvider();
		}
		return labelProvider;
	}

	public static ILabelProvider getLabelProvider() {
		return getLabelProvider(null);
	}

	public static ILabelProvider getLabelProvider(String context) {

		try {
			IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			ServicesRegistry serviceRegistry = ServiceUtilsForWorkbenchPage.getInstance().getServiceRegistry(activePage);
			LabelProviderService service = serviceRegistry.getService(LabelProviderService.class);
			if(context != null) {
				return service.getLabelProvider(context);
			} else {
				return service.getLabelProvider();
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}

		return null;
	}

}

Back to the top