KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > code > ExtractMethodInputPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.code;
12
13 import java.util.Iterator 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.events.SelectionListener;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Combo;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Text;
30
31 import org.eclipse.jface.dialogs.Dialog;
32 import org.eclipse.jface.dialogs.IDialogSettings;
33 import org.eclipse.jface.preference.IPreferenceStore;
34 import org.eclipse.jface.resource.JFaceResources;
35
36 import org.eclipse.jface.text.Document;
37
38 import org.eclipse.ui.PlatformUI;
39
40 import org.eclipse.jdt.core.dom.ASTNode;
41 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
42 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
43 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
44 import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
45 import org.eclipse.jdt.core.dom.Modifier;
46
47 import org.eclipse.jdt.ui.PreferenceConstants;
48 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
49
50 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
51 import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo;
52 import org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring;
53 import org.eclipse.jdt.internal.corext.util.Messages;
54
55 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
56 import org.eclipse.jdt.internal.ui.JavaPlugin;
57 import org.eclipse.jdt.internal.ui.JavaPluginImages;
58 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
59 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
60 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
61 import org.eclipse.jdt.internal.ui.refactoring.ChangeParametersControl;
62 import org.eclipse.jdt.internal.ui.refactoring.IParameterListChangeListener;
63 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
64 import org.eclipse.jdt.internal.ui.util.PixelConverter;
65 import org.eclipse.jdt.internal.ui.util.RowLayouter;
66
67 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
68 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
69
70 public class ExtractMethodInputPage extends UserInputWizardPage {
71
72     public static final String JavaDoc PAGE_NAME= "ExtractMethodInputPage";//$NON-NLS-1$
73

74     private ExtractMethodRefactoring fRefactoring;
75     private Text fTextField;
76     private boolean fFirstTime;
77     private JavaSourceViewer fSignaturePreview;
78     private Document fSignaturePreviewDocument;
79     private IDialogSettings fSettings;
80     
81     private static final String JavaDoc DESCRIPTION = RefactoringMessages.ExtractMethodInputPage_description;
82     private static final String JavaDoc THROW_RUNTIME_EXCEPTIONS= "ThrowRuntimeExceptions"; //$NON-NLS-1$
83
private static final String JavaDoc GENERATE_JAVADOC= "GenerateJavadoc"; //$NON-NLS-1$
84

85     public ExtractMethodInputPage() {
86         super(PAGE_NAME);
87         setImageDescriptor(JavaPluginImages.DESC_WIZBAN_REFACTOR_CU);
88         setDescription(DESCRIPTION);
89         fFirstTime= true;
90         fSignaturePreviewDocument= new Document();
91     }
92
93     public void createControl(Composite parent) {
94         fRefactoring= (ExtractMethodRefactoring)getRefactoring();
95         loadSettings();
96         
97         Composite result= new Composite(parent, SWT.NONE);
98         setControl(result);
99         GridLayout layout= new GridLayout();
100         layout.numColumns= 2;
101         result.setLayout(layout);
102         RowLayouter layouter= new RowLayouter(2);
103         GridData gd= null;
104         
105         initializeDialogUnits(result);
106         
107         Label label= new Label(result, SWT.NONE);
108         label.setText(getLabelText());
109         
110         fTextField= createTextInputField(result, SWT.BORDER);
111         fTextField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
112         
113         layouter.perform(label, fTextField, 1);
114         
115         ASTNode[] destinations= fRefactoring.getDestinations();
116         if (destinations.length > 1) {
117             label= new Label(result, SWT.NONE);
118             label.setText(RefactoringMessages.ExtractMethodInputPage_destination_type);
119             final Combo combo= new Combo(result, SWT.READ_ONLY | SWT.DROP_DOWN);
120             for (int i= 0; i < destinations.length; i++) {
121                 ASTNode declaration= destinations[i];
122                 combo.add(getLabel(declaration));
123             }
124             combo.select(0);
125             combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
126             combo.addSelectionListener(new SelectionListener() {
127                 public void widgetSelected(SelectionEvent e) {
128                     fRefactoring.setDestination(combo.getSelectionIndex());
129                 }
130                 public void widgetDefaultSelected(SelectionEvent e) {
131                     // nothing
132
}
133             });
134         }
135         
136         label= new Label(result, SWT.NONE);
137         label.setText(RefactoringMessages.ExtractMethodInputPage_access_Modifiers);
138         
139         Composite group= new Composite(result, SWT.NONE);
140         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
141         layout= new GridLayout();
142         layout.numColumns= 4; layout.marginWidth= 0;
143         group.setLayout(layout);
144         
145         String JavaDoc[] labels= new String JavaDoc[] {
146             RefactoringMessages.ExtractMethodInputPage_public,
147             RefactoringMessages.ExtractMethodInputPage_protected,
148             RefactoringMessages.ExtractMethodInputPage_default,
149             RefactoringMessages.ExtractMethodInputPage_private
150         };
151         Integer JavaDoc[] data= new Integer JavaDoc[] {new Integer JavaDoc(Modifier.PUBLIC), new Integer JavaDoc(Modifier.PROTECTED), new Integer JavaDoc(Modifier.NONE), new Integer JavaDoc(Modifier.PRIVATE)};
152         Integer JavaDoc visibility= new Integer JavaDoc(fRefactoring.getVisibility());
153         for (int i= 0; i < labels.length; i++) {
154             Button radio= new Button(group, SWT.RADIO);
155             radio.setText(labels[i]);
156             radio.setData(data[i]);
157             if (data[i].equals(visibility))
158                 radio.setSelection(true);
159             radio.addSelectionListener(new SelectionAdapter() {
160                 public void widgetSelected(SelectionEvent event) {
161                     setVisibility((Integer JavaDoc)event.widget.getData());
162                 }
163             });
164         }
165         layouter.perform(label, group, 1);
166         
167         if (!fRefactoring.getParameterInfos().isEmpty()) {
168             ChangeParametersControl cp= new ChangeParametersControl(result, SWT.NONE,
169                 RefactoringMessages.ExtractMethodInputPage_parameters,
170                 new IParameterListChangeListener() {
171                 public void parameterChanged(ParameterInfo parameter) {
172                     parameterModified();
173                 }
174                 public void parameterListChanged() {
175                     parameterModified();
176                 }
177                 public void parameterAdded(ParameterInfo parameter) {
178                     updatePreview(getText());
179                 }
180             }, ChangeParametersControl.Mode.EXTRACT_METHOD);
181             gd= new GridData(GridData.FILL_BOTH);
182             gd.horizontalSpan= 2;
183             cp.setLayoutData(gd);
184             cp.setInput(fRefactoring.getParameterInfos());
185         }
186         
187         Button checkBox= new Button(result, SWT.CHECK);
188         checkBox.setText(RefactoringMessages.ExtractMethodInputPage_throwRuntimeExceptions);
189         checkBox.setSelection(fSettings.getBoolean(THROW_RUNTIME_EXCEPTIONS));
190         checkBox.addSelectionListener(new SelectionAdapter() {
191             public void widgetSelected(SelectionEvent e) {
192                 setRethrowRuntimeException(((Button)e.widget).getSelection());
193             }
194         });
195         layouter.perform(checkBox);
196         
197         checkBox= new Button(result, SWT.CHECK);
198         checkBox.setText(RefactoringMessages.ExtractMethodInputPage_generateJavadocComment);
199         boolean generate= computeGenerateJavadoc();
200         setGenerateJavadoc(generate);
201         checkBox.setSelection(generate);
202         checkBox.addSelectionListener(new SelectionAdapter() {
203             public void widgetSelected(SelectionEvent e) {
204                 setGenerateJavadoc(((Button)e.widget).getSelection());
205             }
206         });
207         layouter.perform(checkBox);
208         
209         int duplicates= fRefactoring.getNumberOfDuplicates();
210         checkBox= new Button(result, SWT.CHECK);
211         if (duplicates == 0) {
212             checkBox.setText(RefactoringMessages.ExtractMethodInputPage_duplicates_none);
213         } else if (duplicates == 1) {
214             checkBox.setText(RefactoringMessages.ExtractMethodInputPage_duplicates_single);
215         } else {
216             checkBox.setText(Messages.format(
217                 RefactoringMessages.ExtractMethodInputPage_duplicates_multi,
218                 new Integer JavaDoc(duplicates)));
219         }
220         checkBox.setSelection(fRefactoring.getReplaceDuplicates());
221         checkBox.setEnabled(duplicates > 0);
222         checkBox.addSelectionListener(new SelectionAdapter() {
223             public void widgetSelected(SelectionEvent e) {
224                 fRefactoring.setReplaceDuplicates(((Button)e.widget).getSelection());
225             }
226         });
227         layouter.perform(checkBox);
228         
229         label= new Label(result, SWT.SEPARATOR | SWT.HORIZONTAL);
230         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
231         layouter.perform(label);
232         
233         createSignaturePreview(result, layouter);
234         
235         Dialog.applyDialogFont(result);
236         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.EXTRACT_METHOD_WIZARD_PAGE);
237     }
238     
239     private String JavaDoc getLabel(ASTNode node) {
240         if (node instanceof AbstractTypeDeclaration) {
241             return ((AbstractTypeDeclaration)node).getName().getIdentifier();
242         } else if (node instanceof AnonymousClassDeclaration) {
243             if (node.getLocationInParent() == ClassInstanceCreation.ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
244                 ClassInstanceCreation creation= (ClassInstanceCreation)node.getParent();
245                 return Messages.format(
246                     RefactoringMessages.ExtractMethodInputPage_anonymous_type_label,
247                     ASTNodes.asString(creation.getType()));
248             } else if (node.getLocationInParent() == EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
249                 EnumConstantDeclaration decl= (EnumConstantDeclaration)node.getParent();
250                 return decl.getName().getIdentifier();
251             }
252         }
253         return "UNKNOWN"; //$NON-NLS-1$
254
}
255
256     private Text createTextInputField(Composite parent, int style) {
257         Text result= new Text(parent, style);
258         result.addModifyListener(new ModifyListener() {
259             public void modifyText(ModifyEvent e) {
260                 textModified(getText());
261             }
262         });
263         TextFieldNavigationHandler.install(result);
264         return result;
265     }
266     
267     private String JavaDoc getText() {
268         if (fTextField == null)
269             return null;
270         return fTextField.getText();
271     }
272     
273     private String JavaDoc getLabelText(){
274         return RefactoringMessages.ExtractMethodInputPage_label_text;
275     }
276     
277     private void setVisibility(Integer JavaDoc visibility) {
278         fRefactoring.setVisibility(visibility.intValue());
279         updatePreview(getText());
280     }
281     
282     private void setRethrowRuntimeException(boolean value) {
283         fSettings.put(THROW_RUNTIME_EXCEPTIONS, value);
284         fRefactoring.setThrowRuntimeExceptions(value);
285         updatePreview(getText());
286     }
287     
288     private boolean computeGenerateJavadoc() {
289         boolean result= fRefactoring.getGenerateJavadoc();
290         if (result)
291             return result;
292         return fSettings.getBoolean(GENERATE_JAVADOC);
293     }
294     
295     private void setGenerateJavadoc(boolean value) {
296         fSettings.put(GENERATE_JAVADOC, value);
297         fRefactoring.setGenerateJavadoc(value);
298     }
299     
300     private void createSignaturePreview(Composite composite, RowLayouter layouter) {
301         Label previewLabel= new Label(composite, SWT.NONE);
302         previewLabel.setText(RefactoringMessages.ExtractMethodInputPage_signature_preview);
303         layouter.perform(previewLabel);
304         
305         IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
306         fSignaturePreview= new JavaSourceViewer(composite, null, null, false, SWT.READ_ONLY | SWT.V_SCROLL | SWT.WRAP /*| SWT.BORDER*/, store);
307         fSignaturePreview.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null));
308         fSignaturePreview.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
309         fSignaturePreview.getTextWidget().setBackground(composite.getBackground());
310         fSignaturePreview.setDocument(fSignaturePreviewDocument);
311         fSignaturePreview.setEditable(false);
312         
313         //Layouting problems with wrapped text: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=9866
314
Control signaturePreviewControl= fSignaturePreview.getControl();
315         PixelConverter pixelConverter= new PixelConverter(signaturePreviewControl);
316         GridData gdata= new GridData(GridData.FILL_BOTH);
317         gdata.widthHint= pixelConverter.convertWidthInCharsToPixels(50);
318         gdata.heightHint= pixelConverter.convertHeightInCharsToPixels(2);
319         signaturePreviewControl.setLayoutData(gdata);
320         layouter.perform(signaturePreviewControl);
321     }
322     
323     private void updatePreview(String JavaDoc text) {
324         if (fSignaturePreview == null)
325             return;
326             
327         if (text.length() == 0)
328             text= "someMethodName"; //$NON-NLS-1$
329

330         int top= fSignaturePreview.getTextWidget().getTopPixel();
331         String JavaDoc signature;
332         try {
333             signature= fRefactoring.getSignature(text);
334         } catch (IllegalArgumentException JavaDoc e) {
335             signature= ""; //$NON-NLS-1$
336
}
337         fSignaturePreviewDocument.set(signature);
338         fSignaturePreview.getTextWidget().setTopPixel(top);
339     }
340     
341     private void loadSettings() {
342         fSettings= getDialogSettings().getSection(ExtractMethodWizard.DIALOG_SETTING_SECTION);
343         if (fSettings == null) {
344             fSettings= getDialogSettings().addNewSection(ExtractMethodWizard.DIALOG_SETTING_SECTION);
345             fSettings.put(THROW_RUNTIME_EXCEPTIONS, false);
346             fSettings.put(GENERATE_JAVADOC, JavaPreferencesSettings.getCodeGenerationSettings(fRefactoring.getCompilationUnit().getJavaProject()).createComments);
347         }
348         fRefactoring.setThrowRuntimeExceptions(fSettings.getBoolean(THROW_RUNTIME_EXCEPTIONS));
349     }
350     
351     //---- Input validation ------------------------------------------------------
352

353     public void setVisible(boolean visible) {
354         if (visible) {
355             if (fFirstTime) {
356                 fFirstTime= false;
357                 setPageComplete(false);
358                 updatePreview(getText());
359                 fTextField.setFocus();
360             } else {
361                 setPageComplete(validatePage(true));
362             }
363         }
364         super.setVisible(visible);
365     }
366     
367     private void textModified(String JavaDoc text) {
368         fRefactoring.setMethodName(text);
369         RefactoringStatus status= validatePage(true);
370         if (!status.hasFatalError()) {
371             updatePreview(text);
372         } else {
373             fSignaturePreviewDocument.set(""); //$NON-NLS-1$
374
}
375         setPageComplete(status);
376     }
377     
378     private void parameterModified() {
379         updatePreview(getText());
380         setPageComplete(validatePage(false));
381     }
382     
383     private RefactoringStatus validatePage(boolean text) {
384         RefactoringStatus result= new RefactoringStatus();
385         if (text) {
386             result.merge(validateMethodName());
387             result.merge(validateParameters());
388         } else {
389             result.merge(validateParameters());
390             result.merge(validateMethodName());
391         }
392         return result;
393     }
394     
395     private RefactoringStatus validateMethodName() {
396         RefactoringStatus result= new RefactoringStatus();
397         String JavaDoc text= getText();
398         if ("".equals(text)) { //$NON-NLS-1$
399
result.addFatalError(RefactoringMessages.ExtractMethodInputPage_validation_emptyMethodName);
400             return result;
401         }
402         result.merge(fRefactoring.checkMethodName());
403         return result;
404     }
405     
406     private RefactoringStatus validateParameters() {
407         RefactoringStatus result= new RefactoringStatus();
408         List JavaDoc parameters= fRefactoring.getParameterInfos();
409         for (Iterator JavaDoc iter= parameters.iterator(); iter.hasNext();) {
410             ParameterInfo info= (ParameterInfo) iter.next();
411             if ("".equals(info.getNewName())) { //$NON-NLS-1$
412
result.addFatalError(RefactoringMessages.ExtractMethodInputPage_validation_emptyParameterName);
413                 return result;
414             }
415         }
416         result.merge(fRefactoring.checkParameterNames());
417         result.merge(fRefactoring.checkVarargOrder());
418         return result;
419     }
420 }
421
Popular Tags