KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.List JavaDoc;
14
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17
18 import org.eclipse.jface.text.ITextSelection;
19
20 import org.eclipse.ui.IWorkbenchSite;
21
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.JavaModelException;
24
25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
27 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
28
29 import org.eclipse.jdt.internal.ui.JavaPlugin;
30 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
31 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
32 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
33 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
34 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
35 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
36
37 /**
38  * Infers type arguments for raw references to generic types.
39  *
40  * <p>
41  * This class may be instantiated; it is not intended to be subclassed.
42  * </p>
43  *
44  * @since 3.1
45  */

46 public class InferTypeArgumentsAction extends SelectionDispatchAction {
47
48     private JavaEditor fEditor;
49
50     /**
51      * Note: This constructor is for internal use only. Clients should not call this constructor.
52      *
53      * @param editor the java editor
54      */

55     public InferTypeArgumentsAction(JavaEditor editor) {
56         this(editor.getEditorSite());
57         fEditor= editor;
58         setEnabled(SelectionConverter.canOperateOn(fEditor));
59     }
60     
61     /**
62      * Creates a new {@link InferTypeArgumentsAction}. The action requires
63      * that the selection provided by the site's selection provider is of type
64      * {@link org.eclipse.jface.viewers.IStructuredSelection}.
65      *
66      * @param site the site providing context information for this action
67      */

68     public InferTypeArgumentsAction(IWorkbenchSite site) {
69         super(site);
70         setText(RefactoringMessages.InferTypeArgumentsAction_label);
71     }
72     
73     /*
74      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
75      */

76     public void selectionChanged(ITextSelection selection) {
77         setEnabled(true);
78     }
79     
80     /*
81      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection)
82      */

83     public void selectionChanged(JavaTextSelection selection) {
84         if (selection.getLength() != 0)
85             setEnabled(false);
86         else
87             setEnabled(SelectionConverter.canOperateOn(fEditor));
88     }
89
90     /*
91      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
92      */

93     public void selectionChanged(IStructuredSelection selection) {
94         try {
95             setEnabled(RefactoringAvailabilityTester.isInferTypeArgumentsAvailable(selection));
96         } catch (JavaModelException e) {
97             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
98
if (JavaModelUtil.isExceptionToBeLogged(e))
99                 JavaPlugin.log(e);
100             setEnabled(false);//no UI
101
}
102     }
103
104     /*
105      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
106      */

107     public void run(IStructuredSelection selection) {
108         IJavaElement[] elements= getSelectedElements(selection);
109         try {
110             if (! ActionUtil.areProcessable(getShell(), elements))
111                 return;
112
113             if (RefactoringAvailabilityTester.isInferTypeArgumentsAvailable(elements)) {
114                 RefactoringExecutionStarter.startInferTypeArgumentsRefactoring(elements, getShell());
115             } else {
116                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.InferTypeArgumentsAction_unavailable);
117             }
118         } catch (JavaModelException e) {
119             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
120         }
121     }
122
123     /*
124      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.text.ITextSelection)
125      */

126     public void run(ITextSelection selection) {
127         if (!ActionUtil.isEditable(fEditor))
128             return;
129         IJavaElement element= SelectionConverter.getInput(fEditor);
130         IJavaElement[] array= new IJavaElement[] {element};
131         try {
132             if (element != null && RefactoringAvailabilityTester.isInferTypeArgumentsAvailable(array)){
133                 RefactoringExecutionStarter.startInferTypeArgumentsRefactoring(array, getShell());
134             } else {
135                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.InferTypeArgumentsAction_unavailable);
136             }
137         } catch (JavaModelException e) {
138             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
139         }
140     }
141     
142     private static IJavaElement[] getSelectedElements(IStructuredSelection selection){
143         List JavaDoc list= selection.toList();
144         IJavaElement[] elements= new IJavaElement[list.size()];
145         for (int i= 0; i < list.size(); i++) {
146             Object JavaDoc object= list.get(i);
147             if (object instanceof IJavaElement)
148                 elements[i]= (IJavaElement) object;
149             else
150                 return new IJavaElement[0];
151         }
152         return elements;
153     }
154 }
155
Popular Tags