KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.IType;
29 import org.eclipse.jdt.core.JavaModelException;
30
31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
34
35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
38 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
39 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
40 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
41 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
43
44 /**
45  * Action to pull up method and fields into a superclass.
46  * <p>
47  * Action is applicable to selections containing elements of type
48  * <code>IType</code> (top-level types only), <code>IField</code> and
49  * <code>IMethod</code>.
50  *
51  * <p>
52  * This class may be instantiated; it is not intended to be subclassed.
53  * </p>
54  *
55  * @since 2.0
56  */

57 public class PullUpAction extends SelectionDispatchAction {
58
59     private static IMember[] getSelectedMembers(IStructuredSelection selection) {
60         if (selection.isEmpty())
61             return null;
62         if (selection.size() == 1) {
63             try {
64                 final IType type= RefactoringAvailabilityTester.getSingleSelectedType(selection);
65                 if (type != null)
66                     return new IType[] { type};
67             } catch (JavaModelException exception) {
68                 JavaPlugin.log(exception);
69             }
70         }
71         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
72             if (!(iter.next() instanceof IMember))
73                 return null;
74         }
75         Set JavaDoc memberSet= new HashSet JavaDoc();
76         memberSet.addAll(Arrays.asList(selection.toArray()));
77         return (IMember[]) memberSet.toArray(new IMember[memberSet.size()]);
78     }
79
80     private JavaEditor fEditor;
81
82     /**
83      * Note: This constructor is for internal use only. Clients should not call
84      * this constructor.
85      *
86      * @param editor
87      * the java editor
88      */

89     public PullUpAction(JavaEditor editor) {
90         this(editor.getEditorSite());
91         fEditor= editor;
92         setEnabled(SelectionConverter.canOperateOn(fEditor));
93     }
94
95     /**
96      * Creates a new <code>PullUpAction</code>. The action requires that the
97      * selection provided by the site's selection provider is of type <code>
98      * org.eclipse.jface.viewers.IStructuredSelection</code>.
99      *
100      * @param site
101      * the site providing context information for this action
102      */

103     public PullUpAction(IWorkbenchSite site) {
104         super(site);
105         setText(RefactoringMessages.RefactoringGroup_pull_Up_label);
106         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.PULL_UP_ACTION);
107     }
108
109     private IMember getSelectedMemberFromEditor() throws JavaModelException {
110         IJavaElement element= SelectionConverter.resolveEnclosingElement(fEditor, (ITextSelection) fEditor.getSelectionProvider().getSelection());
111         if (element == null || !(element instanceof IMember))
112             return null;
113         return (IMember) element;
114     }
115
116     /**
117      * {@inheritDoc}
118      */

119     public void run(IStructuredSelection selection) {
120         try {
121             IMember[] members= getSelectedMembers(selection);
122             if (RefactoringAvailabilityTester.isPullUpAvailable(members) && ActionUtil.isEditable(getShell(), members[0]))
123                 RefactoringExecutionStarter.startPullUpRefactoring(members, getShell());
124         } catch (JavaModelException e) {
125             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
126         }
127     }
128
129     /**
130      * {@inheritDoc}
131      */

132     public void run(ITextSelection selection) {
133         try {
134             if (!ActionUtil.isEditable(fEditor))
135                 return;
136             IMember member= getSelectedMemberFromEditor();
137             IMember[] array= new IMember[] { member};
138             if (member != null && RefactoringAvailabilityTester.isPullUpAvailable(array)) {
139                 RefactoringExecutionStarter.startPullUpRefactoring(array, getShell());
140             } else {
141                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.PullUpAction_unavailable);
142             }
143         } catch (JavaModelException e) {
144             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
145         }
146     }
147
148     /**
149      * {@inheritDoc}
150      */

151     public void selectionChanged(IStructuredSelection selection) {
152         try {
153             setEnabled(RefactoringAvailabilityTester.isPullUpAvailable(selection));
154         } catch (JavaModelException e) {
155             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
156
if (JavaModelUtil.isExceptionToBeLogged(e))
157                 JavaPlugin.log(e);
158             setEnabled(false);// no UI
159
}
160     }
161
162     /**
163      * {@inheritDoc}
164      */

165     public void selectionChanged(ITextSelection selection) {
166         setEnabled(true);
167     }
168
169     /**
170      * Note: This method is for internal use only. Clients should not call this
171      * method.
172      */

173     public void selectionChanged(JavaTextSelection selection) {
174         try {
175             setEnabled(RefactoringAvailabilityTester.isPullUpAvailable(selection));
176         } catch (JavaModelException e) {
177             setEnabled(false);
178         }
179     }
180 }
Popular Tags