KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > ui > refactoring > UndoRefactoringAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.refactoring;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17
18 import org.eclipse.swt.widgets.Shell;
19
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.viewers.ISelection;
23
24 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
25
26 import org.eclipse.ltk.core.refactoring.IUndoManager;
27 import org.eclipse.ltk.core.refactoring.IValidationCheckResultQuery;
28 import org.eclipse.ltk.core.refactoring.RefactoringCore;
29 import org.eclipse.ltk.core.refactoring.UndoManagerAdapter;
30 import org.eclipse.ltk.internal.ui.refactoring.Messages;
31 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
32 import org.eclipse.ltk.internal.ui.refactoring.UndoManagerAction;
33
34 /**
35  * The refactoring undo action. When executed the action performs
36  * the top most change from the refactoring undo manager's undo
37  * stack.
38  * <p>
39  * The action is typically added to a global refactoring menu via
40  * the <code>org.eclipse.ui.actionSets</code> extension point.
41  * </p>
42  * <p>
43  * Note: this class isn't intended to be subclassed. Clients are only
44  * allowed to instantiate the class or to reference it from an action
45  * set.
46  * </p>
47  *
48  * @deprecated This action is now longer needed. Undo is now performed via the
49  * global undo/redo stack provided by <code>org.eclipse.core.commands</code>.
50  *
51  * @since 3.0
52  */

53 public class UndoRefactoringAction extends UndoManagerAction implements IWorkbenchWindowActionDelegate {
54
55     private int fPatternLength;
56
57     /**
58      * Creates a new undo refactoring action.
59      */

60     public UndoRefactoringAction() {
61     }
62
63     /* (non-Javadoc)
64      * Method declared in UndoManagerAction
65      */

66     protected String JavaDoc getName() {
67         // PR: 1GEWDUH: ITPJCORE:WINNT - Refactoring - Unable to undo refactoring change
68
return RefactoringUIMessages.UndoRefactoringAction_name;
69     }
70     
71     /* (non-Javadoc)
72      * Method declared in UndoManagerAction
73      */

74     protected IRunnableWithProgress createOperation(Shell parent) {
75         final IValidationCheckResultQuery query= new Query(parent, RefactoringUIMessages.UndoRefactoringAction_error_title) {
76             protected String JavaDoc getFullMessage(String JavaDoc errorMessage) {
77                 return Messages.format(
78                     RefactoringUIMessages.UndoRefactoringAction_error_message,
79                     errorMessage);
80             }
81         };
82         return new IRunnableWithProgress(){
83             public void run(IProgressMonitor pm) throws InvocationTargetException JavaDoc {
84                 try {
85                     RefactoringCore.getUndoManager().performUndo(query, pm);
86                 } catch (CoreException e) {
87                     throw new InvocationTargetException JavaDoc(e);
88                 }
89             }
90         };
91     }
92     
93     /* (non-Javadoc)
94      * Method declared in UndoManagerAction
95      */

96     protected UndoManagerAdapter createUndoManagerListener() {
97         return new UndoManagerAdapter() {
98             public void undoStackChanged(IUndoManager manager) {
99                 IAction action= getAction();
100                 if (action == null)
101                     return;
102                 boolean enabled= false;
103                 String JavaDoc text= null;
104                 if (manager.anythingToUndo()) {
105                     enabled= true;
106                     text= getActionText();
107                 } else {
108                     text= RefactoringUIMessages.UndoRefactoringAction_label;
109                 }
110                 action.setEnabled(enabled);
111                 action.setText(text);
112             }
113         };
114     }
115     
116     /* (non-Javadoc)
117      * Method declared in IActionDelegate
118      */

119     public void selectionChanged(IAction action, ISelection s) {
120         if (!isHooked()) {
121             hookListener(action);
122             fPatternLength= RefactoringUIMessages.UndoRefactoringAction_extendedLabel.length();
123             IUndoManager undoManager = RefactoringCore.getUndoManager();
124             if (undoManager.anythingToUndo()) {
125                 if (undoManager.peekUndoName() != null)
126                     action.setText(getActionText());
127                 action.setEnabled(true);
128             } else {
129                 action.setEnabled(false);
130             }
131         }
132     }
133     
134     private String JavaDoc getActionText() {
135         return shortenText(Messages.format(
136             RefactoringUIMessages.UndoRefactoringAction_extendedLabel,
137             RefactoringCore.getUndoManager().peekUndoName()), fPatternLength);
138     }
139 }
140
Popular Tags