/
AnalysisView.java
270 lines (233 loc) · 7.2 KB
/
AnalysisView.java
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
/***********************************************************************
* Copyright (c) 2007 Anyware Technologies
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Anyware Technologies - initial API and implementation
*
* $Id: AnalysisView.java,v 1.5 2008/05/19 09:26:31 jlescot Exp $
**********************************************************************/
package org.eclipse.emf.ecoretools.ui.views;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecoretools.ui.Activator;
import org.eclipse.emf.ecoretools.ui.actions.RefreshAction;
import org.eclipse.emf.ecoretools.ui.actions.ToggleSynchronizeAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
/**
* This class implements the basic behavior of an analysis view for Ecore.
*
* @author <a href="david.sciamma@anyware-tech.com">David Sciamma</a>
*/
public abstract class AnalysisView extends ViewPart implements ISelectionListener {
private static final Transfer TRANSFER = LocalSelectionTransfer.getTransfer();
private EObject lastValidObject;
private EObject analyzedObject;
private boolean isSynchronized = false;
private ToggleSynchronizeAction toggleSynchroAction;
private RefreshAction refreshAction;
/**
* Constructor
*/
public AnalysisView() {
ISelectionService selService = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getSelectionService();
selService.addSelectionListener(this);
toggleSynchroAction = new ToggleSynchronizeAction(this);
refreshAction = new RefreshAction(this);
}
/**
* @see org.eclipse.ui.part.WorkbenchPart#dispose()
*/
@Override
public void dispose() {
ISelectionService selService = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getSelectionService();
selService.removeSelectionListener(this);
super.dispose();
}
/**
* Return the analyzed object
*
* @return the EObject analyzed by this view
*/
protected EObject getAnalyzedObject() {
return analyzedObject;
}
/**
* Changes the analyzed object and refresh the view
*
* @param obj
* the EObject that must be analyzed
*/
public void setAnalyzedObject(EObject obj) {
if (obj != null) {
analyzedObject = obj;
refresh(analyzedObject);
}
}
/**
* Returns the synchronization state of this view
*
* @return <code>true</code> if the view is synchronized
*/
public boolean isSynchronized() {
return isSynchronized;
}
/**
* Changes the synchronization of the view
*
* @param synchro
* the new synchronization state
*/
public void setSynchronized(boolean synchro) {
isSynchronized = synchro;
}
/**
* Returns the current selection of the workbench
*
* @return the selected EObject
*/
protected EObject getActiveSelection() {
ISelectionService selService = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getSelectionService();
if (selService.getSelection() instanceof IStructuredSelection) {
return getSelection((IStructuredSelection) selService.getSelection());
}
return null;
}
/**
* Returns the selected EObject from the given selection
*
* @param selection
* the selection which contains the EObject
* @return the selected EObject
*/
protected EObject getSelection(IStructuredSelection selection) {
Object selectedObject = selection.getFirstElement();
if (selectedObject != null) {
if (selectedObject instanceof EObject) {
return (EObject) selectedObject;
}
if (selectedObject instanceof IAdaptable) {
Object adaptedObj = ((IAdaptable) selectedObject).getAdapter(EObject.class);
if (adaptedObj instanceof EObject) {
return (EObject) adaptedObj;
}
}
IAdapterManager adapterManager = Platform.getAdapterManager();
if (adapterManager.hasAdapter(selectedObject, EObject.class.getName())) {
Object adaptedObj = adapterManager.loadAdapter(selectedObject, EObject.class.getName());
if (adaptedObj instanceof EObject) {
return (EObject) adaptedObj;
}
}
}
return null;
}
/**
* @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
* org.eclipse.jface.viewers.ISelection)
*/
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if (selection instanceof IStructuredSelection) {
EObject selectedObject = getSelection((IStructuredSelection) selection);
if (selectedObject != null) {
lastValidObject = selectedObject;
}
if (isSynchronized()) {
setAnalyzedObject(selectedObject);
}
}
}
/**
* @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
*/
@Override
public void createPartControl(Composite parent) {
fillActionBars();
}
/**
* @see org.eclipse.ui.part.WorkbenchPart#setFocus()
*/
@Override
public void setFocus() {
// Do nothing
}
/**
* Build the action bars of the view
*/
protected void fillActionBars() {
IActionBars actionBars = getViewSite().getActionBars();
setGlobalActionHandlers(actionBars);
fillToolBar(actionBars.getToolBarManager());
fillViewMenu(actionBars.getMenuManager());
}
/**
* Registers the global action handler
*
* @param actionBars
* the view action bars
*/
protected void setGlobalActionHandlers(IActionBars actionBars) {
// Do nothing
}
/**
* Build the view toolbar
*
* @param toolBar
* the view toolbar
*/
protected void fillToolBar(IToolBarManager toolBar) {
toolBar.add(toggleSynchroAction);
toolBar.add(refreshAction);
}
/**
* Build the view menu
*
* @param menu
* the view menu
*/
protected void fillViewMenu(IMenuManager menu) {
// Do nothing
}
/**
* Refresh the view for the current selected object
*/
public void refresh() {
setAnalyzedObject(lastValidObject);
}
protected static void prepareViewerForDragToSirius(TreeViewer viewer) {
/* Configure viewer drag and drop behavior */
final Transfer[] transfers = new Transfer[] { TRANSFER };
final int dndOperations = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
// final Transfer[] transfers = new Transfer[] {
// LocalTransfer.getInstance() };
viewer.addDragSupport(dndOperations, transfers, new EcoreToolsViewsDragTargetAdapter(viewer));
}
/**
* Refresh the analysis view
*
* @param object
* the analyzed object
*/
protected abstract void refresh(EObject object);
}