]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/refaktor-before/src/no/uio/ifi/refaktor/handlers/WorkspaceModificationHandler.java
Some talks, mostly identical.
[ifi-stolz-refaktor.git] / case-study / refaktor-before / src / no / uio / ifi / refaktor / handlers / WorkspaceModificationHandler.java
CommitLineData
1b2798f6
EK
1package no.uio.ifi.refaktor.handlers;
2
3import java.lang.reflect.InvocationTargetException;
4
5import no.uio.ifi.refaktor.change.changers.RefaktorChanger;
6
7import org.eclipse.core.commands.AbstractHandler;
8import org.eclipse.core.commands.ExecutionEvent;
9import org.eclipse.core.commands.ExecutionException;
10import org.eclipse.core.runtime.CoreException;
11import org.eclipse.core.runtime.IProgressMonitor;
12import org.eclipse.core.runtime.NullProgressMonitor;
13import org.eclipse.jface.dialogs.MessageDialog;
14import org.eclipse.ui.IWorkbenchWindow;
15import org.eclipse.ui.actions.WorkspaceModifyOperation;
16import org.eclipse.ui.handlers.HandlerUtil;
17
18public abstract class WorkspaceModificationHandler extends AbstractHandler {
19
20 private RefaktorChanger changer;
21
22 public WorkspaceModificationHandler() {
23 super();
24 }
25
26 protected abstract RefaktorChanger createChanger();
27 protected abstract String getTitle();
28 protected abstract String getMessage();
29
30 @Override
31 public Object execute(ExecutionEvent event) throws ExecutionException {
32 destroyChanger();
33 if (preconditionsAreMet(event) && userConfirms(event)) {
34 try {
35 modifyWorkspace();
36 } catch (InvocationTargetException e) {
37 e.printStackTrace();
38 makeAndThrowExecutionException(e);
39 } catch (InterruptedException e) {
40 e.printStackTrace();
41 makeAndThrowExecutionException(e);
42 }
43 }
44 return null;
45 }
46
47 private void makeAndThrowExecutionException(Throwable e)
48 throws ExecutionException {
49 throw new ExecutionException(this.getClass().getSimpleName() + " failed",e);
50 }
51
52 protected boolean preconditionsAreMet(ExecutionEvent event) throws ExecutionException {
53 return true;
54 }
55
56 public void modifyWorkspace() throws ExecutionException, InvocationTargetException, InterruptedException {
57 WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
58
59 @Override
60 protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
61 getChanger().execute(monitor);
62 }
63 };
64 op.run(new NullProgressMonitor());
65 }
66
67 protected boolean userConfirms(ExecutionEvent event) throws ExecutionException {
68 IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
69 return MessageDialog.openQuestion(window.getShell(), getTitle(), getMessage());
70 }
71
72 protected void destroyChanger() {
73 changer = null;
74 }
75
76 protected RefaktorChanger getChanger() {
77 if (changer == null)
78 changer = createChanger();
79 return changer;
80 }
81
82}