KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > RenameAction


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.jdt.ui.actions;
12
13 import org.eclipse.jface.dialogs.MessageDialog;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.viewers.SelectionChangedEvent;
17
18 import org.eclipse.jface.text.ITextSelection;
19
20 import org.eclipse.ui.IWorkbenchSite;
21 import org.eclipse.ui.PlatformUI;
22
23 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
24 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
25 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
26 import org.eclipse.jdt.internal.ui.refactoring.actions.RenameJavaElementAction;
27 import org.eclipse.jdt.internal.ui.refactoring.actions.RenameResourceAction;
28
29 /**
30  * Renames a Java element or workbench resource.
31  * <p>
32  * Action is applicable to selections containing elements of type
33  * <code>IJavaElement</code> or <code>IResource</code>.
34  *
35  * <p>
36  * This class may be instantiated; it is not intended to be subclassed.
37  * </p>
38  *
39  * @since 2.0
40  */

41 public class RenameAction extends SelectionDispatchAction {
42
43     private RenameJavaElementAction fRenameJavaElement;
44     private RenameResourceAction fRenameResource;
45
46     /**
47      * Creates a new <code>RenameAction</code>. The action requires
48      * that the selection provided by the site's selection provider is of type <code>
49      * org.eclipse.jface.viewers.IStructuredSelection</code>.
50      *
51      * @param site the site providing context information for this action
52      */

53     public RenameAction(IWorkbenchSite site) {
54         super(site);
55         setText(RefactoringMessages.RenameAction_text);
56         fRenameJavaElement= new RenameJavaElementAction(site);
57         fRenameJavaElement.setText(getText());
58         fRenameResource= new RenameResourceAction(site);
59         fRenameResource.setText(getText());
60         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.RENAME_ACTION);
61     }
62     
63     /**
64      * Note: This constructor is for internal use only. Clients should not call this constructor.
65      * @param editor the java editor
66      */

67     public RenameAction(JavaEditor editor) {
68         this(editor.getEditorSite());
69         fRenameJavaElement= new RenameJavaElementAction(editor);
70     }
71     
72     /*
73      * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
74      */

75     public void selectionChanged(SelectionChangedEvent event) {
76         fRenameJavaElement.selectionChanged(event);
77         if (fRenameResource != null)
78             fRenameResource.selectionChanged(event);
79         setEnabled(computeEnabledState());
80     }
81
82     /*
83      * @see SelectionDispatchAction#update(ISelection)
84      */

85     public void update(ISelection selection) {
86         fRenameJavaElement.update(selection);
87         
88         if (fRenameResource != null)
89             fRenameResource.update(selection);
90     
91         setEnabled(computeEnabledState());
92     }
93     
94     private boolean computeEnabledState(){
95         if (fRenameResource != null) {
96             return fRenameJavaElement.isEnabled() || fRenameResource.isEnabled();
97         } else {
98             return fRenameJavaElement.isEnabled();
99         }
100     }
101     
102     public void run(IStructuredSelection selection) {
103         if (fRenameJavaElement.isEnabled())
104             fRenameJavaElement.run(selection);
105         if (fRenameResource != null && fRenameResource.isEnabled())
106             fRenameResource.run(selection);
107     }
108
109     public void run(ITextSelection selection) {
110         if (fRenameJavaElement.canRunInEditor())
111             fRenameJavaElement.run(selection);
112         else
113             MessageDialog.openInformation(getShell(), RefactoringMessages.RenameAction_rename, RefactoringMessages.RenameAction_unavailable);
114     }
115 }
116
Popular Tags