Skip to content

Package: OpenPreviewHandler$1

OpenPreviewHandler$1

nameinstructionbranchcomplexitylinemethod
run()
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 10 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-2014 EclipseSource Muenchen GmbH 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: * Alexandra Buzila- initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.model.internal.preview.actions;
15:
16: import org.eclipse.core.commands.ExecutionEvent;
17: import org.eclipse.core.commands.ExecutionException;
18: import org.eclipse.core.runtime.IStatus;
19: import org.eclipse.core.runtime.Status;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecore.resource.Resource;
22: import org.eclipse.emf.ecore.resource.ResourceSet;
23: import org.eclipse.emf.ecp.view.model.internal.preview.Activator;
24: import org.eclipse.emf.ecp.view.model.internal.preview.Messages;
25: import org.eclipse.emf.ecp.view.spi.model.VView;
26: import org.eclipse.emf.ecp.view.spi.model.reporting.StatusReport;
27: import org.eclipse.emf.ecp.view.spi.treemasterdetail.ui.swt.MasterDetailAction;
28: import org.eclipse.emfforms.spi.editor.IToolbarAction;
29: import org.eclipse.jface.action.Action;
30: import org.eclipse.jface.resource.ImageDescriptor;
31: import org.eclipse.jface.viewers.ISelectionProvider;
32: import org.eclipse.jface.viewers.IStructuredSelection;
33: import org.eclipse.ui.IWorkbenchPage;
34: import org.eclipse.ui.PartInitException;
35: import org.eclipse.ui.PlatformUI;
36: import org.eclipse.ui.handlers.HandlerUtil;
37: import org.osgi.framework.FrameworkUtil;
38:
39: /** Opens the {@link org.eclipse.emf.ecp.view.model.internal.preview.e3.views.PreviewView}. */
40: public class OpenPreviewHandler extends MasterDetailAction implements IToolbarAction {
41:         /**
42:          * {@inheritDoc}
43:          *
44:          * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
45:          */
46:         @Override
47:         public Object execute(ExecutionEvent event) throws ExecutionException {
48:                 final Object selection = ((IStructuredSelection) HandlerUtil.getActiveMenuSelection(event)).getFirstElement();
49:                 if (selection == null || !(selection instanceof EObject)) {
50:                         return null;
51:                 }
52:                 execute((EObject) selection);
53:                 return null;
54:         }
55:
56:         /**
57:          * {@inheritDoc}
58:          *
59:          * @see org.eclipse.emf.ecp.view.spi.treemasterdetail.ui.swt.MasterDetailAction#execute(EObject)
60:          */
61:         @Override
62:         public void execute(EObject object) {
63:                 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
64:
65:                 try {
66:                         page.showView("org.eclipse.emf.ecp.view.model.preview.e3.views.PreviewView", null, //$NON-NLS-1$
67:                                 IWorkbenchPage.VIEW_VISIBLE);
68:                 } catch (final PartInitException ex) {
69:                         Activator.getDefault().getReportService().report(
70:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
71:                 }
72:
73:         }
74:
75:         /**
76:          * {@inheritDoc}
77:          *
78:          * @see org.eclipse.emf.ecp.view.spi.treemasterdetail.ui.swt.MasterDetailAction#shouldShow(org.eclipse.emf.ecore.EObject)
79:          */
80:         @Override
81:         public boolean shouldShow(EObject eObject) {
82:                 return true;
83:         }
84:
85:         @Override
86:         public Action getAction(final Object currentObject, ISelectionProvider selectionProvider) {
87:                 final Action previewAction = new Action(Messages.OpenPreviewHandler_OpenPreview) {
88:                         @Override
89:                         public void run() {
90:                                 execute(((ResourceSet) currentObject).getResources().get(0).getAllContents().next());
91:                         }
92:                 };
93:                 previewAction.setImageDescriptor(ImageDescriptor.createFromURL(FrameworkUtil.getBundle(this.getClass())
94:                         .getResource("icons/preview.png"))); //$NON-NLS-1$
95:                 return previewAction;
96:         }
97:
98:         @Override
99:         public boolean canExecute(Object object) {
100:                 // We can't execute our Action on Objects other than ResourceSet
101:                 if (!(object instanceof ResourceSet)) {
102:                         return false;
103:                 }
104:                 // Check, if the ResourceSet contains a VView. If so, we can execute our action.
105:                 final ResourceSet resourceSet = (ResourceSet) object;
106:                 for (final Resource r : resourceSet.getResources()) {
107:                         if (r.getContents().size() > 0 && r.getContents().get(0) instanceof VView) {
108:                                 return true;
109:                         }
110:                 }
111:                 return false;
112:         }
113: }