KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20
21 import org.eclipse.jface.text.ITextSelection;
22
23 import org.eclipse.ui.IWorkbenchSite;
24 import org.eclipse.ui.PlatformUI;
25
26 import org.eclipse.jdt.core.IJavaElement;
27 import org.eclipse.jdt.core.IMember;
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.util.JavaModelUtil;
33
34 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
35
36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
37 import org.eclipse.jdt.internal.ui.JavaPlugin;
38 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
39 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
40 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
41 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
42 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
43 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
44
45 public class MoveStaticMembersAction extends SelectionDispatchAction{
46     
47     private JavaEditor fEditor;
48
49     public MoveStaticMembersAction(IWorkbenchSite site) {
50         super(site);
51         setText(RefactoringMessages.RefactoringGroup_move_label);
52         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_ACTION);
53     }
54
55     public MoveStaticMembersAction(JavaEditor editor) {
56         this(editor.getEditorSite());
57         fEditor= editor;
58         setEnabled(SelectionConverter.canOperateOn(fEditor));
59     }
60         
61     public void selectionChanged(IStructuredSelection selection) {
62         try {
63             setEnabled(RefactoringAvailabilityTester.isMoveStaticMembersAvailable(getSelectedMembers(selection)));
64         } catch (JavaModelException e) {
65             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
66
if (JavaModelUtil.isExceptionToBeLogged(e))
67                 JavaPlugin.log(e);
68             setEnabled(false);//no ui
69
}
70     }
71
72     public void selectionChanged(ITextSelection selection) {
73         setEnabled(true);
74     }
75     
76     /**
77      * Note: This method is for internal use only. Clients should not call this method.
78      */

79     public void selectionChanged(JavaTextSelection selection) {
80         try {
81             setEnabled(RefactoringAvailabilityTester.isMoveStaticAvailable(selection));
82         } catch (JavaModelException e) {
83             setEnabled(false);
84         }
85     }
86
87     public void run(IStructuredSelection selection) {
88         try {
89             IMember[] members= getSelectedMembers(selection);
90             for (int index= 0; index < members.length; index++) {
91                 if (!ActionUtil.isEditable(getShell(), members[index]))
92                     return;
93             }
94             if (RefactoringAvailabilityTester.isMoveStaticMembersAvailable(members))
95                 RefactoringExecutionStarter.startMoveStaticMembersRefactoring(members, getShell());
96         } catch (JavaModelException e) {
97             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
98         }
99     }
100
101     public void run(ITextSelection selection) {
102         try {
103             IMember member= getSelectedMemberFromEditor();
104             if (!ActionUtil.isEditable(fEditor, getShell(), member))
105                 return;
106             IMember[] array= new IMember[]{member};
107             if (member != null && RefactoringAvailabilityTester.isMoveStaticMembersAvailable(array)){
108                 RefactoringExecutionStarter.startMoveStaticMembersRefactoring(array, getShell());
109             } else {
110                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.MoveMembersAction_unavailable);
111             }
112         } catch (JavaModelException e) {
113             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
114         }
115     }
116     
117     private static IMember[] getSelectedMembers(IStructuredSelection selection){
118         if (selection.isEmpty())
119             return null;
120         
121         for (final Iterator JavaDoc iterator= selection.iterator(); iterator.hasNext(); ) {
122             if (! (iterator.next() instanceof IMember))
123                 return null;
124         }
125         Set JavaDoc memberSet= new HashSet JavaDoc();
126         memberSet.addAll(Arrays.asList(selection.toArray()));
127         return (IMember[]) memberSet.toArray(new IMember[memberSet.size()]);
128     }
129
130     private IMember getSelectedMemberFromEditor() throws JavaModelException{
131         IJavaElement element= SelectionConverter.getElementAtOffset(fEditor);
132         if (element == null || ! (element instanceof IMember))
133             return null;
134         return (IMember)element;
135     }
136 }
137
Popular Tags