]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | package no.uio.ifi.refaktor.handlers; |
2 | ||
3 | import java.lang.reflect.InvocationTargetException; | |
4 | ||
5 | import no.uio.ifi.refaktor.change.changers.RefaktorChanger; | |
6 | ||
7 | import org.eclipse.core.commands.AbstractHandler; | |
8 | import org.eclipse.core.commands.ExecutionEvent; | |
9 | import org.eclipse.core.commands.ExecutionException; | |
10 | import org.eclipse.core.runtime.CoreException; | |
11 | import org.eclipse.core.runtime.IProgressMonitor; | |
12 | import org.eclipse.core.runtime.NullProgressMonitor; | |
13 | import org.eclipse.jface.dialogs.MessageDialog; | |
14 | import org.eclipse.ui.IWorkbenchWindow; | |
15 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
16 | import org.eclipse.ui.handlers.HandlerUtil; | |
17 | ||
18 | public 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 | } |