KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.resource.JFaceResources;
28
29 import org.eclipse.jface.text.Document;
30
31 import org.eclipse.ui.PlatformUI;
32
33 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
34 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
35 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
36
37 import org.eclipse.jdt.core.JavaModelException;
38
39 import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo;
40 import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceParameterRefactoring;
41
42 import org.eclipse.jdt.ui.PreferenceConstants;
43 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
44
45 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
48 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
49 import org.eclipse.jdt.internal.ui.util.PixelConverter;
50
51 public class IntroduceParameterWizard extends RefactoringWizard {
52
53     public IntroduceParameterWizard(IntroduceParameterRefactoring ref) {
54         super(ref, DIALOG_BASED_USER_INTERFACE | PREVIEW_EXPAND_FIRST_NODE);
55         setDefaultPageTitle(RefactoringMessages.IntroduceParameterWizard_defaultPageTitle);
56     }
57
58     /* non java-doc
59      * @see RefactoringWizard#addUserInputPages
60      */

61     protected void addUserInputPages(){
62         addPage(new IntroduceParameterInputPage(getIntroduceParameterRefactoring().guessParameterNames()));
63     }
64     
65     private IntroduceParameterRefactoring getIntroduceParameterRefactoring(){
66         return (IntroduceParameterRefactoring)getRefactoring();
67     }
68     
69     private static class IntroduceParameterInputPage extends UserInputWizardPage {
70
71         private static final String JavaDoc DESCRIPTION = RefactoringMessages.IntroduceParameterInputPage_description;
72         public static final String JavaDoc PAGE_NAME= "IntroduceParameterInputPage";//$NON-NLS-1$
73
private String JavaDoc[] fParamNameProposals;
74         
75         private JavaSourceViewer fSignaturePreview;
76         private Document fSignaturePreviewDocument;
77         private Button fLeaveDelegateCheckBox;
78         private Button fDeprecateDelegateCheckBox;
79     
80         public IntroduceParameterInputPage(String JavaDoc[] tempNameProposals) {
81             super(PAGE_NAME);
82             setDescription(DESCRIPTION);
83             Assert.isNotNull(tempNameProposals);
84             fParamNameProposals= tempNameProposals;
85             fSignaturePreviewDocument= new Document();
86         }
87
88         private IntroduceParameterRefactoring getIntroduceParameterRefactoring(){
89             return (IntroduceParameterRefactoring)getRefactoring();
90         }
91
92         public void createControl(Composite parent) {
93             Composite result= new Composite(parent, SWT.NONE);
94             setControl(result);
95             GridLayout layout= new GridLayout();
96             result.setLayout(layout);
97             
98             createParameterTableControl(result);
99             fLeaveDelegateCheckBox= DelegateUIHelper.generateLeaveDelegateCheckbox(result, getRefactoring(), false);
100             if (fLeaveDelegateCheckBox != null) {
101                 fDeprecateDelegateCheckBox= new Button(result, SWT.CHECK);
102                 GridData data= new GridData();
103                 data.horizontalAlignment= GridData.FILL;
104                 data.horizontalIndent= (layout.marginWidth + fDeprecateDelegateCheckBox.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
105                 fDeprecateDelegateCheckBox.setLayoutData(data);
106                 fDeprecateDelegateCheckBox.setText(DelegateUIHelper.getDeprecateDelegateCheckBoxTitle());
107                 final IntroduceParameterRefactoring refactoring= getIntroduceParameterRefactoring();
108                 fDeprecateDelegateCheckBox.setSelection(DelegateUIHelper.loadDeprecateDelegateSetting(refactoring));
109                 refactoring.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
110                 fDeprecateDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
111                     public void widgetSelected(SelectionEvent e) {
112                         refactoring.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
113                     }
114                 });
115                 fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
116                 fLeaveDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
117                     public void widgetSelected(SelectionEvent e) {
118                         fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
119                     }
120                 });
121             }
122             Label sep= new Label(result, SWT.SEPARATOR | SWT.HORIZONTAL);
123             sep.setLayoutData((new GridData(GridData.FILL_HORIZONTAL)));
124             createSignaturePreview(result);
125             
126             update(false);
127             Dialog.applyDialogFont(result);
128             PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.INTRODUCE_PARAMETER_WIZARD_PAGE);
129         }
130
131         private ChangeParametersControl createParameterTableControl(Composite composite) {
132             String JavaDoc labelText= RefactoringMessages.IntroduceParameterWizard_parameters;
133             ChangeParametersControl cp= new ChangeParametersControl(composite, SWT.NONE, labelText, new IParameterListChangeListener() {
134                 public void parameterChanged(ParameterInfo parameter) {
135                     update(true);
136                 }
137                 public void parameterListChanged() {
138                     update(true);
139                 }
140                 public void parameterAdded(ParameterInfo parameter) {
141                     update(true);
142                 }
143             }, ChangeParametersControl.Mode.INTRODUCE_PARAMETER, fParamNameProposals);
144             cp.setLayoutData(new GridData(GridData.FILL_BOTH));
145             cp.setInput(getIntroduceParameterRefactoring().getParameterInfos());
146             cp.editParameter(getIntroduceParameterRefactoring().getAddedParameterInfo());
147             return cp;
148         }
149
150         public void dispose() {
151             DelegateUIHelper.saveLeaveDelegateSetting(fLeaveDelegateCheckBox);
152             DelegateUIHelper.saveDeprecateDelegateSetting(fDeprecateDelegateCheckBox);
153             super.dispose();
154         }
155     
156         private void createSignaturePreview(Composite composite) {
157             Label previewLabel= new Label(composite, SWT.NONE);
158             previewLabel.setText(RefactoringMessages.ChangeSignatureInputPage_method_Signature_Preview);
159             
160             IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
161             fSignaturePreview= new JavaSourceViewer(composite, null, null, false, SWT.READ_ONLY | SWT.V_SCROLL | SWT.WRAP /*| SWT.BORDER*/, store);
162             fSignaturePreview.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null));
163             fSignaturePreview.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
164             fSignaturePreview.getTextWidget().setBackground(composite.getBackground());
165             fSignaturePreview.setDocument(fSignaturePreviewDocument);
166             fSignaturePreview.setEditable(false);
167             
168             //Layouting problems with wrapped text: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=9866
169
Control signaturePreviewControl= fSignaturePreview.getControl();
170             PixelConverter pixelConverter= new PixelConverter(signaturePreviewControl);
171             GridData gdata= new GridData(GridData.FILL_BOTH);
172             gdata.widthHint= pixelConverter.convertWidthInCharsToPixels(50);
173             gdata.heightHint= pixelConverter.convertHeightInCharsToPixels(2);
174             signaturePreviewControl.setLayoutData(gdata);
175         }
176         
177         private void update(boolean displayErrorMessage){
178             updateStatus(displayErrorMessage);
179             updateSignaturePreview();
180         }
181
182         private void updateStatus(boolean displayErrorMessage) {
183             RefactoringStatus nameCheck= getIntroduceParameterRefactoring().validateInput();
184             if (displayErrorMessage) {
185                 setPageComplete(nameCheck);
186             } else {
187                 setErrorMessage(null);
188                 setPageComplete(true);
189             }
190         }
191
192         private void updateSignaturePreview() {
193             try{
194                 int top= fSignaturePreview.getTextWidget().getTopPixel();
195                 fSignaturePreviewDocument.set(getIntroduceParameterRefactoring().getMethodSignaturePreview());
196                 fSignaturePreview.getTextWidget().setTopPixel(top);
197             } catch (JavaModelException e){
198                 ExceptionHandler.handle(e, RefactoringMessages.IntroduceParameterWizard_defaultPageTitle, RefactoringMessages.ChangeSignatureInputPage_exception);
199             }
200         }
201     
202     }
203 }
204
205
Popular Tags