KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > actions > RenameResourceAction


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.internal.ui.refactoring.actions;
12
13
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.core.resources.IResource;
17
18 import org.eclipse.jface.viewers.IStructuredSelection;
19
20 import org.eclipse.ui.IWorkbenchSite;
21
22 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
23 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
24
25 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
26
27 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
28 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
29
30 public class RenameResourceAction extends SelectionDispatchAction {
31
32     public RenameResourceAction(IWorkbenchSite site) {
33         super(site);
34     }
35     
36     public void selectionChanged(IStructuredSelection selection) {
37         IResource element= getResource(selection);
38         if (element == null)
39             setEnabled(false);
40         else
41             setEnabled(RefactoringAvailabilityTester.isRenameAvailable(element));
42     }
43
44     public void run(IStructuredSelection selection) {
45         IResource resource = getResource(selection);
46         if (!RefactoringAvailabilityTester.isRenameAvailable(resource))
47             return;
48         try {
49             RefactoringExecutionStarter.startRenameResourceRefactoring(resource, getShell());
50         } catch (CoreException e) {
51             ExceptionHandler.handle(e, RefactoringMessages.RenameJavaElementAction_name, RefactoringMessages.RenameJavaElementAction_exception);
52         }
53     }
54
55     private static IResource getResource(IStructuredSelection selection) {
56         if (selection.size() != 1)
57             return null;
58         Object JavaDoc first= selection.getFirstElement();
59         if (! (first instanceof IResource))
60             return null;
61         return (IResource)first;
62     }
63 }
64
Popular Tags