Skip to content

Package: ModelExplorerView$2

ModelExplorerView$2

nameinstructionbranchcomplexitylinemethod
postShutdown(IWorkbench)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
preShutdown(IWorkbench, boolean)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011 Eike Stepper (Berlin, Germany) and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eike Stepper - initial API and implementation
13: *******************************************************************************/
14: package org.eclipse.emf.ecp.ui.views;
15:
16: import org.eclipse.core.runtime.CoreException;
17: import org.eclipse.core.runtime.IConfigurationElement;
18: import org.eclipse.core.runtime.Platform;
19: import org.eclipse.emf.ecore.EObject;
20: import org.eclipse.emf.ecp.core.ECPProject;
21: import org.eclipse.emf.ecp.core.util.ECPContainer;
22: import org.eclipse.emf.ecp.core.util.ECPUtil;
23: import org.eclipse.emf.ecp.internal.ui.model.ModelContentProvider;
24: import org.eclipse.emf.ecp.spi.ui.UIProvider;
25: import org.eclipse.emf.ecp.spi.ui.UIProviderRegistry;
26: import org.eclipse.emf.ecp.spi.ui.util.ECPHandlerHelper;
27: import org.eclipse.emf.ecp.ui.common.ECPViewerFactory;
28: import org.eclipse.emf.ecp.ui.e3.Messages;
29: import org.eclipse.emf.ecp.ui.linkedView.ILinkedWithEditorView;
30: import org.eclipse.emf.ecp.ui.linkedView.LinkedWithEditorPartListener;
31: import org.eclipse.emf.ecp.ui.platform.Activator;
32: import org.eclipse.emf.ecp.ui.tester.ECPSavePropertySource;
33: import org.eclipse.emf.ecp.ui.tester.SaveButtonEnablementObserver;
34: import org.eclipse.jface.action.Action;
35: import org.eclipse.jface.action.IMenuManager;
36: import org.eclipse.jface.action.IToolBarManager;
37: import org.eclipse.jface.dialogs.IDialogSettings;
38: import org.eclipse.jface.viewers.DoubleClickEvent;
39: import org.eclipse.jface.viewers.IDoubleClickListener;
40: import org.eclipse.jface.viewers.ISelectionChangedListener;
41: import org.eclipse.jface.viewers.IStructuredSelection;
42: import org.eclipse.jface.viewers.SelectionChangedEvent;
43: import org.eclipse.jface.viewers.StructuredSelection;
44: import org.eclipse.jface.viewers.TreeViewer;
45: import org.eclipse.jface.viewers.ViewerSorter;
46: import org.eclipse.swt.SWT;
47: import org.eclipse.swt.events.FocusEvent;
48: import org.eclipse.swt.events.FocusListener;
49: import org.eclipse.swt.widgets.Composite;
50: import org.eclipse.ui.IEditorPart;
51: import org.eclipse.ui.IEditorReference;
52: import org.eclipse.ui.IPartListener2;
53: import org.eclipse.ui.IViewSite;
54: import org.eclipse.ui.IWorkbench;
55: import org.eclipse.ui.IWorkbenchListener;
56: import org.eclipse.ui.PartInitException;
57: import org.eclipse.ui.PlatformUI;
58: import org.eclipse.ui.services.IEvaluationService;
59:
60: /**
61: * @author Eike Stepper
62: * @author Eugen Neufeld
63: */
64: public class ModelExplorerView extends TreeView implements ILinkedWithEditorView {
65:         private static final String VIEW_SORTER = "viewSorter"; //$NON-NLS-1$
66:
67:         private static final String LINK_WITH_EDITOR_SET = "LinkWithEditorSet"; //$NON-NLS-1$
68:
69:         private static final String LINK_WITH_EDITOR = "LinkWithEditor"; //$NON-NLS-1$
70:
71:         /**
72:          * @author Jonas
73:          * @author Eugen Neufeld
74:          */
75:         public class DoubleClickListener implements IDoubleClickListener {
76:
77:                 /**
78:                  * Opens an EObject using the ActionHelper or opens a closed ECPProject. {@inheritDoc}
79:                  */
80:                 @Override
81:                 public void doubleClick(DoubleClickEvent event) {
82:                         if (event.getSelection() instanceof IStructuredSelection) {
83:                                 final IStructuredSelection structuredSelection = (IStructuredSelection) event.getSelection();
84:                                 final Object firstElement = structuredSelection.getFirstElement();
85:
86:                                 if (firstElement instanceof ECPProject) {
87:                                         final ECPProject project = (ECPProject) firstElement;
88:                                         if (!project.isOpen()) {
89:                                                 project.open();
90:                                         }
91:                                 } else {
92:                                         final ECPContainer context = ECPUtil
93:                                                 .getModelContext(contentProvider, structuredSelection.toArray());
94:                                         ECPHandlerHelper.openModelElement(firstElement, (ECPProject) context);
95:                                 }
96:                         }
97:                 }
98:         }
99:
100:         /**
101:          * The id of this view.
102:          */
103:         public static final String ID = "org.eclipse.emf.ecp.ui.ModelExplorerView"; //$NON-NLS-1$
104:
105:         private final IPartListener2 linkWithEditorPartListener = new LinkedWithEditorPartListener(this);
106:
107:         private boolean linkingActive = true;
108:
109:         private ModelContentProvider contentProvider;
110:
111:         private TreeViewer viewer;
112:
113:         private Action linkWithEditorAction;
114:
115:         private ECPSavePropertySource ecpSavePropertySource;
116:
117:         /**
118:          * Default Constructor.
119:          */
120:         public ModelExplorerView() {
121:                 super(ID);
122:         }
123:
124:         /**
125:          * {@inheritDoc}
126:          *
127:          * @see org.eclipse.emf.ecp.ui.views.TreeView#init(org.eclipse.ui.IViewSite)
128:          */
129:         @Override
130:         public void init(IViewSite site) throws PartInitException {
131:                 super.init(site);
132:                 @SuppressWarnings("cast")
133:                 // Ignore the warning and keep the cast to stay SRC compatible with Luna
134:                 final IEvaluationService evaluationService = (IEvaluationService) site.getService(IEvaluationService.class);
135:                 ecpSavePropertySource = new ECPSavePropertySource();
136:                 evaluationService.addSourceProvider(ecpSavePropertySource);
137:         }
138:
139:         /**
140:          * {@inheritDoc}
141:          *
142:          * @see org.eclipse.ui.part.WorkbenchPart#dispose()
143:          */
144:         @Override
145:         public void dispose() {
146:                 @SuppressWarnings("cast")
147:                 // Ignore the warning and keep the cast to stay SRC compatible with Luna
148:                 final IEvaluationService evaluationService = (IEvaluationService) getSite()
149:                         .getService(IEvaluationService.class);
150:                 evaluationService.removeSourceProvider(ecpSavePropertySource);
151:                 super.dispose();
152:         }
153:
154:         @Override
155:         protected TreeViewer createViewer(final Composite parent) {
156:                 viewer = ECPViewerFactory.createModelExplorerViewer(parent, true, createLabelDecorator());
157:                 contentProvider = (ModelContentProvider) viewer.getContentProvider();
158:                 viewer.addDoubleClickListener(new DoubleClickListener());
159:                 viewer.addSelectionChangedListener(new ModelExplorerViewSelectionListener());
160:                 viewer.getControl().addFocusListener(new FocusListener() {
161:                         @Override
162:                         public void focusGained(FocusEvent event) {
163:                         }
164:
165:                         @Override
166:                         public void focusLost(FocusEvent event) {
167:                                 ECPUtil.getECPObserverBus().notify(SaveButtonEnablementObserver.class)
168:                                         .notifyChangeButtonState(null, false);
169:                         }
170:
171:                 });
172:
173:                 addViewerSorter(viewer);
174:
175:                 getSite().getWorkbenchWindow().getWorkbench().addWorkbenchListener(new IWorkbenchListener() {
176:
177:                         @Override
178:                         public boolean preShutdown(IWorkbench workbench, boolean forced) {
179:
180:                                 return ECPHandlerHelper.showDirtyProjectsDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
181:                                         .getShell());
182:                         }
183:
184:                         @Override
185:                         public void postShutdown(IWorkbench workbench) {
186:                                 // do nothing
187:                         }
188:                 });
189:
190:                 return viewer;
191:         }
192:
193:         @SuppressWarnings("deprecation")
194:         private void addViewerSorter(TreeViewer viewer) {
195:                 final IConfigurationElement[] modelExplorerSettings = Platform.getExtensionRegistry()
196:                         .getConfigurationElementsFor("org.eclipse.emf.ecp.ui.modelExplorerSettings"); //$NON-NLS-1$
197:                 if (modelExplorerSettings.length == 1) {
198:                         if (modelExplorerSettings[0].getAttribute(VIEW_SORTER) != null) {
199:                                 try {
200:                                         final ViewerSorter sorter = (ViewerSorter) modelExplorerSettings[0]
201:                                                 .createExecutableExtension(VIEW_SORTER);
202:                                         viewer.setSorter(sorter);
203:                                 } catch (final CoreException e) {
204:                                         Activator.log(e);
205:                                 }
206:
207:                         }
208:                 }
209:         }
210:
211:         @Override
212:         protected void fillLocalToolBar(IToolBarManager manager) {
213:                 if (getDialogSettings().getBoolean(LINK_WITH_EDITOR_SET)) {
214:                         linkingActive = getDialogSettings().getBoolean(LINK_WITH_EDITOR);
215:                 }
216:                 if (linkingActive) {
217:                         getSite().getPage().addPartListener(linkWithEditorPartListener);
218:                 }
219:
220:                 linkWithEditorAction = new Action(Messages.ModelExplorerView_LinkWithEditor, SWT.TOGGLE) {
221:
222:                         @Override
223:                         public void run() {
224:                                 if (linkingActive) {
225:                                         linkingActive = false;
226:                                         getSite().getPage().removePartListener(linkWithEditorPartListener);
227:                                 } else {
228:                                         linkingActive = true;
229:                                         getSite().getPage().addPartListener(linkWithEditorPartListener);
230:                                         final IEditorPart editor = getSite().getPage().getActiveEditor();
231:                                         if (editor != null) {
232:                                                 editorActivated(editor);
233:                                         }
234:                                 }
235:
236:                                 getDialogSettings().put(LINK_WITH_EDITOR, isChecked());
237:                                 getDialogSettings().put(LINK_WITH_EDITOR_SET, true);
238:                         }
239:
240:                 };
241:
242:                 linkWithEditorAction.setImageDescriptor(Activator.getImageDescriptor("icons/link_with_editor.gif")); //$NON-NLS-1$
243:                 linkWithEditorAction.setToolTipText(Messages.ModelExplorerView_LinkWithEditor);
244:                 linkWithEditorAction.setChecked(getDialogSettings().getBoolean(LINK_WITH_EDITOR_SET) ? getDialogSettings()
245:                         .getBoolean(LINK_WITH_EDITOR) : true);
246:                 manager.add(linkWithEditorAction);
247:         }
248:
249:         private IDialogSettings getDialogSettings() {
250:                 return Activator.getDefault().getDialogSettings();
251:         }
252:
253:         @Override
254:         protected void fillContextMenu(IMenuManager manager) {
255:                 final Object[] elements = getSelection().toArray();
256:
257:                 final ECPContainer context = ECPUtil.getModelContext(contentProvider, elements);
258:                 if (context != null) {
259:                         final UIProvider provider = UIProviderRegistry.INSTANCE.getUIProvider(context);
260:                         if (provider != null) {
261:                                 provider.fillContextMenu(manager, context, elements);
262:                         }
263:                 }
264:
265:                 super.fillContextMenu(manager);
266:         }
267:
268:         /** {@inheritDoc} */
269:         @Override
270:         public void editorActivated(IEditorPart activatedEditor) {
271:                 if (!linkingActive || !getViewSite().getPage().isPartVisible(this)) {
272:                         return;
273:                 }
274:                 final Object input = activatedEditor.getEditorInput().getAdapter(EObject.class);
275:                 if (input != null) {
276:                         viewer.setSelection(new StructuredSelection(input), true);
277:                 }
278:         }
279:
280:         /**
281:          *
282:          * @author jfaltermeier
283:          *
284:          */
285:         private class ModelExplorerViewSelectionListener implements ISelectionChangedListener {
286:
287:                 @Override
288:                 public void selectionChanged(SelectionChangedEvent event) {
289:                         notifyAboutSaveButtonState(event);
290:                         if (linkingActive) {
291:                                 final Object selected = ((IStructuredSelection) event.getSelection()).getFirstElement();
292:                                 if (selected instanceof EObject) {
293:                                         for (final IEditorReference editorRef : getSite().getPage().getEditorReferences()) {
294:                                                 Object editorInput = null;
295:                                                 try {
296:
297:                                                         editorInput = editorRef.getEditorInput().getAdapter(EObject.class);
298:                                                 } catch (final PartInitException e) {
299:                                                         e.printStackTrace();
300:                                                 }
301:                                                 if (selected.equals(editorInput)) {
302:                                                         getSite().getPage().bringToTop(editorRef.getPart(true));
303:                                                         return;
304:                                                 }
305:                                         }
306:                                 }
307:                         }
308:                 }
309:
310:                 private void notifyAboutSaveButtonState(SelectionChangedEvent event) {
311:                         final boolean selectedProjectIsDirty;
312:
313:                         final Object selected = ((IStructuredSelection) event.getSelection()).getFirstElement();
314:                         final ECPProject project = ECPUtil.getECPProjectManager().getProject(selected);
315:
316:                         if (project == null) {
317:                                 selectedProjectIsDirty = false;
318:                         } else {
319:                                 selectedProjectIsDirty = project.hasDirtyContents();
320:                         }
321:
322:                         ECPUtil.getECPObserverBus().notify(SaveButtonEnablementObserver.class)
323:                                 .notifyChangeButtonState(project, selectedProjectIsDirty);
324:                 }
325:         }
326: }