Skip to content

Package: AbstractWorkspaceHandler$2

AbstractWorkspaceHandler$2

nameinstructionbranchcomplexitylinemethod
run(IProgressMonitor)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
{...}
M: 13 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.cdo.internal.ui.handlers;
15:
16: import java.lang.reflect.InvocationTargetException;
17:
18: import org.eclipse.core.commands.AbstractHandler;
19: import org.eclipse.core.commands.ExecutionEvent;
20: import org.eclipse.core.commands.ExecutionException;
21: import org.eclipse.core.runtime.IProgressMonitor;
22: import org.eclipse.core.runtime.IStatus;
23: import org.eclipse.core.runtime.Status;
24: import org.eclipse.core.runtime.jobs.Job;
25: import org.eclipse.emf.cdo.workspace.CDOWorkspace;
26: import org.eclipse.emf.ecp.cdo.internal.ui.Activator;
27: import org.eclipse.jface.operation.IRunnableWithProgress;
28: import org.eclipse.jface.viewers.ISelection;
29: import org.eclipse.jface.viewers.IStructuredSelection;
30: import org.eclipse.net4j.util.AdapterUtil;
31: import org.eclipse.ui.IWorkbenchWindow;
32: import org.eclipse.ui.PlatformUI;
33: import org.eclipse.ui.handlers.HandlerUtil;
34: import org.eclipse.ui.services.IEvaluationService;
35:
36: /**
37: * Abstract Handler for executing commands.
38: *
39: * @author Eike Stepper
40: */
41: public abstract class AbstractWorkspaceHandler extends AbstractHandler {
42:         private final String jobName;
43:
44:         /**
45:          * Constructor.
46:          *
47:          * @param jobName display job name
48:          */
49:         public AbstractWorkspaceHandler(String jobName) {
50:                 this.jobName = jobName;
51:         }
52:
53:         /**
54:          * Get the display name of the current job.
55:          *
56:          * @return the name
57:          */
58:         public final String getJobName() {
59:                 return jobName;
60:         }
61:
62:         /**
63:          * {@inheritDoc}
64:          *
65:          * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
66:          */
67:         @Override
68:         public final Object execute(final ExecutionEvent event) throws ExecutionException {
69:                 final ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
70:                 if (selection instanceof IStructuredSelection) {
71:                         final Object element = ((IStructuredSelection) selection).getFirstElement();
72:
73:                         final CDOWorkspace workspace = AdapterUtil.adapt(element, CDOWorkspace.class);
74:                         if (workspace != null) {
75:                                 try {
76:                                         if (jobName == null) {
77:                                                 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
78:                                                         @Override
79:                                                         public void run(IProgressMonitor monitor) throws InvocationTargetException,
80:                                                                 InterruptedException {
81:                                                                 try {
82:                                                                         execute(event, workspace, monitor);
83:                                                                 } catch (final ExecutionException ex) {
84:                                                                         Activator.log(ex);
85:                                                                 }
86:                                                         }
87:                                                 });
88:                                         } else {
89:                                                 new Job(jobName) {
90:                                                         @Override
91:                                                         protected IStatus run(IProgressMonitor monitor) {
92:                                                                 try {
93:                                                                         execute(event, workspace, monitor);
94:                                                                         return Status.OK_STATUS;
95:                                                                 } catch (final ExecutionException ex) {
96:                                                                         return new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex);
97:                                                                 }
98:                                                         }
99:                                                 }.schedule();
100:                                         }
101:                                 } catch (final InvocationTargetException ex) {
102:                                         throw new ExecutionException("Problem while handling " + element, ex); //$NON-NLS-1$
103:                                 } catch (final InterruptedException ex) {
104:                                         throw new ExecutionException("Problem while handling " + element, ex); //$NON-NLS-1$
105:                                 }
106:                         }
107:                 }
108:
109:                 return null;
110:         }
111:
112:         /**
113:          * Execute the given event.
114:          *
115:          * @param event the event
116:          * @param workspace the {@link CDOWorkspace}
117:          * @param monitor a progress monitor
118:          * @throws ExecutionException if execution fails
119:          */
120:         protected abstract void execute(ExecutionEvent event, CDOWorkspace workspace, IProgressMonitor monitor)
121:                 throws ExecutionException;
122:
123:         /**
124:          * Refresh the dirty state of the {@link CDOWorkspace}.
125:          *
126:          * @param event the event
127:          * @throws ExecutionException if refresh fails
128:          */
129:         protected static void refreshDirtyState(ExecutionEvent event) throws ExecutionException {
130:                 final IWorkbenchWindow ww = HandlerUtil.getActiveWorkbenchWindowChecked(event);
131:                 // We keep the cast, otherwise we loose SRC compatibility with Luna
132:                 @SuppressWarnings("cast")
133:                 final IEvaluationService service = (IEvaluationService) ww.getService(IEvaluationService.class);
134:                 if (service != null) {
135:                         service.requestEvaluation("org.eclipse.emf.cdo.workspace.dirty"); //$NON-NLS-1$
136:                 }
137:         }
138: }