KickJava   Java API By Example, From Geeks To Geeks.

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


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 push down methods and fields into subclasses.
46  * <p>
47  * Action is applicable to selections containing elements of type
48  * <code>IField</code> and <code>IMethod</code>.
49  *
50  * <p>
51  * This class may be instantiated; it is not intended to be subclassed.
52  * </p>
53  *
54  * @since 2.1
55  */

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

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

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

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

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

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

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

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