KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > ReorgMoveAction


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.reorg;
12
13 import java.util.List JavaDoc;
14
15
16 import org.eclipse.core.resources.IResource;
17
18 import org.eclipse.jface.viewers.IStructuredSelection;
19
20 import org.eclipse.jface.text.ITextSelection;
21
22 import org.eclipse.ui.IWorkbenchSite;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.actions.MoveProjectAction;
25
26
27 import org.eclipse.jdt.core.IJavaElement;
28 import org.eclipse.jdt.core.JavaModelException;
29
30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
32 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
34
35 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
36
37 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
40 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
41 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
42
43 public class ReorgMoveAction extends SelectionDispatchAction {
44     public ReorgMoveAction(IWorkbenchSite site) {
45         super(site);
46         setText(ReorgMessages.ReorgMoveAction_3);
47         setDescription(ReorgMessages.ReorgMoveAction_4);
48         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_ACTION);
49     }
50
51     public void selectionChanged(IStructuredSelection selection) {
52         if (!selection.isEmpty()) {
53             if (ReorgUtils.containsOnlyProjects(selection.toList())) {
54                 setEnabled(createWorkbenchAction(selection).isEnabled());
55                 return;
56             }
57             try {
58                 List JavaDoc elements= selection.toList();
59                 IResource[] resources= ReorgUtils.getResources(elements);
60                 IJavaElement[] javaElements= ReorgUtils.getJavaElements(elements);
61                 if (elements.size() != resources.length + javaElements.length)
62                     setEnabled(false);
63                 else
64                     setEnabled(RefactoringAvailabilityTester.isMoveAvailable(resources, javaElements));
65             } catch (JavaModelException e) {
66                 // no ui here - this happens on selection changes
67
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
68
if (JavaModelUtil.isExceptionToBeLogged(e))
69                     JavaPlugin.log(e);
70                 setEnabled(false);
71             }
72         } else
73             setEnabled(false);
74     }
75
76     public void selectionChanged(ITextSelection selection) {
77         setEnabled(true);
78     }
79
80     /**
81      * Note: This method is for internal use only. Clients should not call this method.
82      */

83     public void selectionChanged(JavaTextSelection selection) {
84         try {
85             setEnabled(RefactoringAvailabilityTester.isMoveAvailable(selection));
86         } catch (JavaModelException e) {
87             setEnabled(false);
88         }
89     }
90
91     private MoveProjectAction createWorkbenchAction(IStructuredSelection selection) {
92         MoveProjectAction action= new MoveProjectAction(getShell());
93         action.selectionChanged(selection);
94         return action;
95     }
96
97     public void run(IStructuredSelection selection) {
98         if (ReorgUtils.containsOnlyProjects(selection.toList())) {
99             createWorkbenchAction(selection).run();
100             return;
101         }
102         try {
103             List JavaDoc elements= selection.toList();
104             IResource[] resources= ReorgUtils.getResources(elements);
105             IJavaElement[] javaElements= ReorgUtils.getJavaElements(elements);
106             if (RefactoringAvailabilityTester.isMoveAvailable(resources, javaElements))
107                 RefactoringExecutionStarter.startMoveRefactoring(resources, javaElements, getShell());
108         } catch (JavaModelException e) {
109             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
110         }
111     }
112 }
113
Popular Tags