KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > IntroduceIndirectionInputPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Text;
28
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.jface.wizard.IWizardPage;
31
32 import org.eclipse.ui.PlatformUI;
33
34 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
35 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
36
37 import org.eclipse.jdt.core.IJavaElement;
38 import org.eclipse.jdt.core.IJavaProject;
39 import org.eclipse.jdt.core.IType;
40 import org.eclipse.jdt.core.search.IJavaSearchConstants;
41 import org.eclipse.jdt.core.search.IJavaSearchScope;
42 import org.eclipse.jdt.core.search.SearchEngine;
43
44 import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceIndirectionRefactoring;
45
46 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
47 import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog;
48 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
49 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
50 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor;
51 import org.eclipse.jdt.internal.ui.util.SWTUtil;
52
53 /**
54  * @since 3.2
55  */

56 public class IntroduceIndirectionInputPage extends UserInputWizardPage {
57
58     /**
59      * The name of the intermediary method to be created.
60      */

61     private Text fIntermediaryMethodName;
62     
63     private Combo fIntermediaryTypeName;
64     private static final int INTERMEDIARY_TYPE_COUNT= 10;
65     private static List JavaDoc fgIntermediaryTypes= new ArrayList JavaDoc(INTERMEDIARY_TYPE_COUNT);
66
67     /**
68      * Constructor for IntroduceIndirectionInputPage.
69      * @param name the name of the page
70      */

71     public IntroduceIndirectionInputPage(String JavaDoc name) {
72         super(name);
73     }
74
75     private Text createIntermediaryNameCombo(Composite result) {
76         final Text textField= new Text(result, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
77         textField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
78         TextFieldNavigationHandler.install(textField);
79         return textField;
80     }
81     
82     private Combo createIntermediaryTypeCombo(Composite composite) {
83         final Combo textCombo= new Combo(composite, SWT.SINGLE | SWT.BORDER);
84         textCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
85         textCombo.setItems((String JavaDoc[]) fgIntermediaryTypes.toArray(new String JavaDoc[fgIntermediaryTypes.size()]));
86         textCombo.setVisibleItemCount(INTERMEDIARY_TYPE_COUNT);
87         
88         JavaTypeCompletionProcessor processor= new JavaTypeCompletionProcessor(false, false, true);
89         processor.setPackageFragment(getIntroduceIndirectionRefactoring().getInvocationPackage());
90         ControlContentAssistHelper.createComboContentAssistant(textCombo, processor);
91         TextFieldNavigationHandler.install(textCombo);
92         return textCombo;
93     }
94
95     /**
96      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
97      */

98     public void createControl(Composite parent) {
99         Composite result= new Composite(parent, SWT.NONE);
100
101         setControl(result);
102
103         GridLayout layout= new GridLayout();
104         layout.numColumns= 2;
105         result.setLayout(layout);
106
107         Label methNameLabel= new Label(result, SWT.NONE);
108         methNameLabel.setText(RefactoringMessages.IntroduceIndirectionInputPage_new_method_name);
109
110         fIntermediaryMethodName= createIntermediaryNameCombo(result);
111
112         final Label intermediaryTypeLabel= new Label(result, SWT.NONE);
113         intermediaryTypeLabel.setText(RefactoringMessages.IntroduceIndirectionInputPage_declaring_class);
114
115         Composite inner= new Composite(result, SWT.NONE);
116         GridLayout innerLayout= new GridLayout();
117         innerLayout.marginHeight= 0;
118         innerLayout.marginWidth= 0;
119         innerLayout.numColumns= 2;
120         inner.setLayout(innerLayout);
121         inner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
122
123         fIntermediaryTypeName= createIntermediaryTypeCombo(inner);
124         fIntermediaryTypeName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
125
126         final Button browseTypes= new Button(inner, SWT.PUSH);
127         browseTypes.setText(RefactoringMessages.IntroduceIndirectionInputPage_browse);
128         GridData gd= new GridData();
129         gd.horizontalAlignment= GridData.END;
130         gd.widthHint= SWTUtil.getButtonWidthHint(browseTypes);
131         browseTypes.setLayoutData(gd);
132
133         final Button enableReferencesCheckBox= new Button(result, SWT.CHECK);
134         enableReferencesCheckBox.setText(RefactoringMessages.IntroduceIndirectionInputPage_update_references);
135         gd= new GridData(GridData.FILL_HORIZONTAL);
136         gd.horizontalSpan= 2;
137         gd.verticalIndent= 2;
138         enableReferencesCheckBox.setLayoutData(gd);
139         
140         fIntermediaryMethodName.setText(getIntroduceIndirectionRefactoring().getIntermediaryMethodName());
141         fIntermediaryTypeName.setText(getIntroduceIndirectionRefactoring().getIntermediaryClassName());
142
143         fIntermediaryMethodName.addModifyListener(new ModifyListener() {
144             public void modifyText(ModifyEvent e) {
145                 validateInput();
146             }
147         });
148         
149         enableReferencesCheckBox.addSelectionListener(new SelectionAdapter() {
150             public void widgetSelected(SelectionEvent e) {
151                 getIntroduceIndirectionRefactoring().setEnableUpdateReferences(enableReferencesCheckBox.getSelection());
152             }
153         });
154
155         fIntermediaryTypeName.addModifyListener(new ModifyListener() {
156             public void modifyText(ModifyEvent e) {
157                 validateInput();
158             }
159         });
160         
161         browseTypes.addSelectionListener(new SelectionAdapter() {
162             public void widgetSelected(SelectionEvent e) {
163                 IType intermediaryType= chooseIntermediaryClass();
164
165                 if (intermediaryType == null)
166                     return;
167
168                 fIntermediaryTypeName.setText(intermediaryType.getFullyQualifiedName());
169             }
170         });
171
172         if (getIntroduceIndirectionRefactoring().canEnableUpdateReferences())
173             enableReferencesCheckBox.setSelection(true);
174         else {
175             enableReferencesCheckBox.setSelection(false);
176             enableReferencesCheckBox.setEnabled(false);
177             getIntroduceIndirectionRefactoring().setEnableUpdateReferences(false);
178         }
179         
180         fIntermediaryMethodName.setFocus();
181         fIntermediaryMethodName.selectAll();
182         validateInput();
183
184         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.INTRODUCE_INDIRECTION_WIZARD_PAGE);
185     }
186
187     private IType chooseIntermediaryClass() {
188         IJavaProject proj= getIntroduceIndirectionRefactoring().getProject();
189
190         if (proj == null)
191             return null;
192
193         IJavaElement[] elements= new IJavaElement[] { proj };
194         IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
195
196         FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(getShell(), false, getWizard().getContainer(), scope, IJavaSearchConstants.CLASS);
197
198         dialog.setTitle(RefactoringMessages.IntroduceIndirectionInputPage_dialog_choose_declaring_class);
199         dialog.setMessage(RefactoringMessages.IntroduceIndirectionInputPage_dialog_choose_declaring_class_long);
200
201         if (dialog.open() == Window.OK) {
202             return (IType) dialog.getFirstResult();
203         }
204         return null;
205     }
206
207     private IntroduceIndirectionRefactoring getIntroduceIndirectionRefactoring() {
208         return (IntroduceIndirectionRefactoring) getRefactoring();
209     }
210
211     private void validateInput() {
212         RefactoringStatus merged= new RefactoringStatus();
213         merged.merge(getIntroduceIndirectionRefactoring().setIntermediaryClassName(fIntermediaryTypeName.getText()));
214         merged.merge(getIntroduceIndirectionRefactoring().setIntermediaryMethodName(fIntermediaryMethodName.getText()));
215
216         setPageComplete(!merged.hasError());
217         int severity= merged.getSeverity();
218         String JavaDoc message= merged.getMessageMatchingSeverity(severity);
219         if (severity >= RefactoringStatus.INFO) {
220             setMessage(message, severity);
221         } else {
222             setMessage("", NONE); //$NON-NLS-1$
223
}
224     }
225     
226     protected boolean performFinish() {
227         storeIntermediaryTypeName();
228         return super.performFinish();
229     }
230
231     public IWizardPage getNextPage() {
232         storeIntermediaryTypeName();
233         return super.getNextPage();
234     }
235
236     private void storeIntermediaryTypeName() {
237         String JavaDoc destination= fIntermediaryTypeName.getText();
238         if (!fgIntermediaryTypes.remove(destination) && fgIntermediaryTypes.size() >= INTERMEDIARY_TYPE_COUNT)
239             fgIntermediaryTypes.remove(fgIntermediaryTypes.size() - 1);
240         fgIntermediaryTypes.add(0, destination);
241     }
242 }
243
Popular Tags