Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 772f3b50d35688e1847942649d485a9e0e9b7784 (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
/*****************************************************************************
 * Copyright (c) 2014, 2016 CEA LIST, Christian W. Damus, 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:
 *   Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation
 *   Christian W. Damus - bug 485220
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.export.utils;

import java.util.Collection;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.ILocalPageService;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForSelection;
import org.eclipse.papyrus.infra.gmfdiag.export.engine.ExportDiagramLocalPageService;
import org.eclipse.papyrus.uml.tools.model.UmlUtils;

/**
 * Helper to extract file from selection in Project Explorer.
 *
 * @author Gabriel Pascual
 *
 */
public class SelectionHelper {

	/**
	 * Constructor.
	 *
	 */
	private SelectionHelper() {

	}

	/**
	 * Try to retrieve the selected file from the given selection.
	 *
	 * @param selection
	 *            the selection
	 * @return the selected file
	 */
	public static IFile convertSelection2File(ISelection selection) {
		IFile foundFile = null;

		// get the selected diagrams file
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;

			// Only one file should be selected
			if (!structuredSelection.isEmpty() && structuredSelection.size() == 1) {
				Object selectedObject = structuredSelection.getFirstElement();
				foundFile = getIFile(selectedObject);
			}
		}
		return foundFile;
	}

	/**
	 * Return the IFile corresponding to the selection.
	 *
	 * @param selectedObj
	 *            selected file
	 * @return the i file
	 */
	public static IFile getIFile(Object selectedObj) {
		IFile result = null;
		if (selectedObj instanceof IFile) {
			result = (IFile) selectedObj;
		}
		// Try to adapt
		if (result == null && selectedObj instanceof IAdaptable) {
			result = ((IAdaptable) selectedObj).getAdapter(IFile.class);
		}
		// adapt in ifile
		if (result == null) {
			result = Platform.getAdapterManager().getAdapter(selectedObj, IFile.class);
		}
		if (result == null) {
			// try to check if it is a collection
			Collection<?> collec = Platform.getAdapterManager().getAdapter(selectedObj, Collection.class);
			if (collec != null) {
				for (Object o : collec) {
					if (o instanceof IFile) {
						IFile f = (IFile) o;
						if (DiModel.MODEL_FILE_EXTENSION.equals(f.getFileExtension())) {
							result = f;
							break;
						}
					}
				}
			}
		}
		return result != null && DiModel.MODEL_FILE_EXTENSION.equals(result.getFileExtension()) ? result
				: null;
	}


	/**
	 * Checks if selection is exportable model. To export a model to images, it must contain diagrams.
	 *
	 *
	 * @param receiver
	 *            the receiver
	 * @return true, if is exportable mosdel
	 */
	public static boolean isExportableModel(IStructuredSelection receiver) {


		IPageManager pageManager = null;
		try {
			pageManager = ServiceUtilsForSelection.getInstance().getService(IPageManager.class, receiver);
		} catch (ServiceException e) {
			// Ignore service exception
		}


		boolean isExportable = false;

		if (pageManager != null) {

			ModelSet modelSet = null;
			try {
				modelSet = ServiceUtilsForSelection.getInstance().getModelSet(receiver);
			} catch (ServiceException e) {
				// Ignore service exception
				return false;
			}

			// There is at least one diagram in resource
			Resource umlResource = UmlUtils.getUmlResource(modelSet);
			ILocalPageService service = new ExportDiagramLocalPageService(umlResource.getContents().get(0));
			List<Object> localPages = pageManager.allLocalPages(service);

			isExportable = !localPages.isEmpty();

		}



		return isExportable;
	}
}

Back to the top