KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > UndoManagerAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ltk.internal.ui.refactoring;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.OperationCanceledException;
16
17 import org.eclipse.core.resources.ResourcesPlugin;
18
19 import org.eclipse.swt.widgets.Shell;
20
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27
28 import org.eclipse.ui.IWorkbenchWindow;
29 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
30 import org.eclipse.ui.PlatformUI;
31
32 import org.eclipse.ltk.core.refactoring.IValidationCheckResultQuery;
33 import org.eclipse.ltk.core.refactoring.RefactoringCore;
34 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
35 import org.eclipse.ltk.core.refactoring.UndoManagerAdapter;
36 import org.eclipse.ltk.ui.refactoring.RefactoringUI;
37
38 public abstract class UndoManagerAction implements IWorkbenchWindowActionDelegate {
39
40     private static final int MAX_LENGTH= 30;
41
42     private IAction fAction;
43     private IWorkbenchWindow fWorkbenchWindow;
44     private UndoManagerAdapter fUndoManagerListener;
45     
46     protected static abstract class Query implements IValidationCheckResultQuery {
47         private Shell fParent;
48         private String JavaDoc fTitle;
49         public Query(Shell parent, String JavaDoc title) {
50             fParent= parent;
51             fTitle= title;
52         }
53         public boolean proceed(RefactoringStatus status) {
54             final Dialog dialog= RefactoringUI.createRefactoringStatusDialog(status, fParent, fTitle, false);
55             final int[] result= new int[1];
56             Runnable JavaDoc r= new Runnable JavaDoc() {
57                 public void run() {
58                     result[0]= dialog.open();
59                 }
60             };
61             fParent.getDisplay().syncExec(r);
62             return result[0] == IDialogConstants.OK_ID;
63         }
64         public void stopped(final RefactoringStatus status) {
65             Runnable JavaDoc r= new Runnable JavaDoc() {
66                 public void run() {
67                     String JavaDoc message= status.getMessageMatchingSeverity(RefactoringStatus.FATAL);
68                     MessageDialog.openWarning(fParent, fTitle, getFullMessage(message));
69                 }
70             };
71             fParent.getDisplay().syncExec(r);
72         }
73         protected abstract String JavaDoc getFullMessage(String JavaDoc errorMessage);
74     }
75
76     protected UndoManagerAction() {
77     }
78     
79     protected abstract IRunnableWithProgress createOperation(Shell parent);
80     
81     protected abstract UndoManagerAdapter createUndoManagerListener();
82     
83     protected abstract String JavaDoc getName();
84     
85     protected IWorkbenchWindow getWorkbenchWindow() {
86         return fWorkbenchWindow;
87     }
88     
89     protected IAction getAction() {
90         return fAction;
91     }
92     
93     protected boolean isHooked() {
94         return fAction != null;
95     }
96     
97     protected void hookListener(IAction action) {
98         if (isHooked())
99             return;
100         fAction= action;
101         fUndoManagerListener= createUndoManagerListener();
102         RefactoringCore.getUndoManager().addListener(fUndoManagerListener);
103     }
104     
105     protected String JavaDoc shortenText(String JavaDoc text, int patternLength) {
106         int length= text.length();
107         final int finalLength = MAX_LENGTH + patternLength;
108         if (text.length() <= finalLength)
109             return text;
110         StringBuffer JavaDoc result= new StringBuffer JavaDoc();
111         int mid= finalLength / 2;
112         result.append(text.substring(0, mid));
113         result.append("..."); //$NON-NLS-1$
114
result.append(text.substring(length - mid));
115         return result.toString();
116     }
117             
118     /* (non-Javadoc)
119      * Method declared in IActionDelegate
120      */

121     public void dispose() {
122         if (fUndoManagerListener != null)
123             RefactoringCore.getUndoManager().removeListener(fUndoManagerListener);
124         fWorkbenchWindow= null;
125         fAction= null;
126         fUndoManagerListener= null;
127     }
128     
129     /* (non-Javadoc)
130      * Method declared in IActionDelegate
131      */

132     public void init(IWorkbenchWindow window) {
133         fWorkbenchWindow= window;
134     }
135     
136     /* (non-Javadoc)
137      * Method declared in IActionDelegate
138      */

139     public void run(IAction action) {
140         Shell parent= fWorkbenchWindow.getShell();
141         IRunnableWithProgress op= createOperation(parent);
142         try {
143             PlatformUI.getWorkbench().getProgressService().runInUI(
144                 new ProgressMonitorDialog(fWorkbenchWindow.getShell()),
145                 op, ResourcesPlugin.getWorkspace().getRoot());
146         } catch (InvocationTargetException JavaDoc e) {
147             RefactoringCore.getUndoManager().flush();
148             ExceptionHandler.handle(e,
149                 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
150                 RefactoringUIMessages.UndoManagerAction_internal_error_title,
151                 RefactoringUIMessages.UndoManagerAction_internal_error_message);
152         } catch (InterruptedException JavaDoc e) {
153             // Operation isn't cancelable.
154
} catch (OperationCanceledException e) {
155             // the waiting dialog got canceled.
156
}
157     }
158 }
159
Popular Tags