KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.ui.refactoring;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.ModifyEvent;
15 import org.eclipse.swt.events.ModifyListener;
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.Combo;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.TabFolder;
26 import org.eclipse.swt.widgets.TabItem;
27 import org.eclipse.swt.widgets.Text;
28
29 import org.eclipse.jface.dialogs.Dialog;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.resource.JFaceResources;
32
33 import org.eclipse.jface.text.Document;
34
35 import org.eclipse.ui.PlatformUI;
36
37 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
38 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
39 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
40
41 import org.eclipse.jdt.core.JavaModelException;
42 import org.eclipse.jdt.core.dom.Modifier;
43
44 import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo;
45 import org.eclipse.jdt.internal.corext.refactoring.StubTypeContext;
46 import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureRefactoring;
47 import org.eclipse.jdt.internal.corext.util.JdtFlags;
48
49 import org.eclipse.jdt.ui.PreferenceConstants;
50 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
51
52 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
53 import org.eclipse.jdt.internal.ui.JavaPlugin;
54 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
55 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
56 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
57 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor;
58 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
59 import org.eclipse.jdt.internal.ui.util.PixelConverter;
60
61 public class ChangeSignatureWizard extends RefactoringWizard {
62
63     public ChangeSignatureWizard(ChangeSignatureRefactoring ref) {
64         super(ref, DIALOG_BASED_USER_INTERFACE);
65         setDefaultPageTitle(RefactoringMessages.ChangeSignatureRefactoring_modify_Parameters);
66     }
67
68     protected void addUserInputPages(){
69         addPage(new ChangeSignatureInputPage());
70     }
71     
72     private static class ChangeSignatureInputPage extends UserInputWizardPage {
73
74         public static final String JavaDoc PAGE_NAME= "ChangeSignatureInputPage"; //$NON-NLS-1$
75
private JavaSourceViewer fSignaturePreview;
76         private Document fSignaturePreviewDocument;
77         private Button fLeaveDelegateCheckBox;
78         private Button fDeprecateDelegateCheckBox;
79         
80         public ChangeSignatureInputPage() {
81             super(PAGE_NAME);
82             setMessage(RefactoringMessages.ChangeSignatureInputPage_change);
83             fSignaturePreviewDocument= new Document();
84         }
85     
86         /*
87          * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
88          */

89         public void createControl(Composite parent) {
90             Composite composite= new Composite(parent, SWT.NONE);
91             final GridLayout layout= new GridLayout();
92             composite.setLayout(layout);
93             initializeDialogUnits(composite);
94         
95             try {
96                 createHeadControls(composite);
97
98                 createParameterExceptionsFolder(composite);
99                 fLeaveDelegateCheckBox= DelegateUIHelper.generateLeaveDelegateCheckbox(composite, getRefactoring(), false);
100                 if (fLeaveDelegateCheckBox != null) {
101                     fDeprecateDelegateCheckBox= new Button(composite, 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                     data.horizontalSpan= 2;
106                     fDeprecateDelegateCheckBox.setLayoutData(data);
107                     fDeprecateDelegateCheckBox.setText(DelegateUIHelper.getDeprecateDelegateCheckBoxTitle());
108                     final ChangeSignatureRefactoring refactoring= getChangeMethodSignatureRefactoring();
109                     fDeprecateDelegateCheckBox.setSelection(DelegateUIHelper.loadDeprecateDelegateSetting(refactoring));
110                     refactoring.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
111                     fDeprecateDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
112                         public void widgetSelected(SelectionEvent e) {
113                             refactoring.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
114                         }
115                     });
116                     fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
117                     fLeaveDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
118                         public void widgetSelected(SelectionEvent e) {
119                             fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
120                         }
121                     });
122                 }
123                 Label sep= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
124                 sep.setLayoutData((new GridData(GridData.FILL_HORIZONTAL)));
125                 createSignaturePreview(composite);
126                 
127                 update(false);
128                 setControl(composite);
129                 Dialog.applyDialogFont(composite);
130             } catch (JavaModelException e) {
131                 ExceptionHandler.handle(e, RefactoringMessages.ChangeSignatureInputPage_Change_Signature, RefactoringMessages.ChangeSignatureInputPage_Internal_Error);
132             }
133             PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.MODIFY_PARAMETERS_WIZARD_PAGE);
134         }
135
136         private void createHeadControls(Composite parent) throws JavaModelException {
137             //must create controls column-wise to get mnemonics working:
138
Composite composite= new Composite(parent, SWT.NONE);
139             composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
140             GridLayout layout= new GridLayout(3, false);
141             layout.marginHeight= 0;
142             layout.marginWidth= 0;
143             composite.setLayout(layout);
144             
145             createAccessControl(composite);
146             createReturnTypeControl(composite);
147             createNameControl(composite);
148         }
149
150         private void createAccessControl(Composite parent) throws JavaModelException {
151             Composite access= new Composite(parent, SWT.NONE);
152             GridLayout layout= new GridLayout();
153             layout.marginHeight= 0;
154             layout.marginWidth= 0;
155             access.setLayout(layout);
156             
157             final int[] availableVisibilities= getChangeMethodSignatureRefactoring().getAvailableVisibilities();
158             int currentVisibility= getChangeMethodSignatureRefactoring().getVisibility();
159                         
160             Label label= new Label(access, SWT.NONE);
161             label.setText(RefactoringMessages.ChangeSignatureInputPage_access_modifier);
162
163             final Combo combo= new Combo(access, SWT.DROP_DOWN | SWT.READ_ONLY);
164             if (availableVisibilities.length == 0) {
165                 combo.setEnabled(false);
166             } else {
167                 for (int i= 0; i < availableVisibilities.length; i++) {
168                     combo.add(getAccessModifierString(availableVisibilities[i]));
169                 }
170                 combo.addSelectionListener(new SelectionAdapter() {
171                     public void widgetSelected(SelectionEvent e) {
172                         int newVisibility= availableVisibilities[combo.getSelectionIndex()];
173                         getChangeMethodSignatureRefactoring().setVisibility(newVisibility);
174                         update(true);
175                     }
176                 });
177             }
178             combo.setText(getAccessModifierString(currentVisibility));
179             combo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
180             
181             // ensure that "Access modifier:" and "Return type:" Labels are not too close:
182
Dialog.applyDialogFont(access);
183             access.pack();
184             int minLabelWidth= label.getSize().x + 3 * layout.horizontalSpacing;
185             if (minLabelWidth > combo.getSize().x)
186                 label.setLayoutData(new GridData(minLabelWidth, label.getSize().y));
187         }
188         
189         private String JavaDoc getAccessModifierString(int modifier) {
190             switch (modifier) {
191                 case Modifier.PUBLIC :
192                     return JdtFlags.VISIBILITY_STRING_PUBLIC;
193                 case Modifier.PROTECTED :
194                     return JdtFlags.VISIBILITY_STRING_PROTECTED;
195                 case Modifier.NONE :
196                     return RefactoringMessages.ChangeSignatureInputPage_default;
197                 case Modifier.PRIVATE :
198                     return JdtFlags.VISIBILITY_STRING_PRIVATE;
199                 default :
200                     throw new IllegalArgumentException JavaDoc("\"" + modifier + "\" is not a Modifier constant"); //$NON-NLS-1$ //$NON-NLS-2$
201
}
202         }
203
204         private void createReturnTypeControl(Composite parent) {
205             Composite returnType= new Composite(parent, SWT.NONE);
206             returnType.setLayoutData(new GridData(GridData.FILL_BOTH));
207             GridLayout layout= new GridLayout(1, false);
208             layout.marginHeight= 0;
209             layout.marginWidth= 0;
210             returnType.setLayout(layout);
211
212             Label label= new Label(returnType, SWT.NONE);
213             label.setText(RefactoringMessages.ChangeSignatureInputPage_return_type);
214
215             final Text text= new Text(returnType, SWT.BORDER);
216             text.setText(getChangeMethodSignatureRefactoring().getReturnTypeString());
217             text.setLayoutData((new GridData(GridData.FILL_HORIZONTAL)));
218             TextFieldNavigationHandler.install(text);
219
220             if (getChangeMethodSignatureRefactoring().canChangeNameAndReturnType()) {
221                 text.addModifyListener(new ModifyListener(){
222                     public void modifyText(ModifyEvent e) {
223                         getChangeMethodSignatureRefactoring().setNewReturnTypeName(text.getText());
224                         update(true);
225                     }
226                 });
227             } else {
228                 text.setEnabled(false);
229             }
230             
231             JavaTypeCompletionProcessor processor= new JavaTypeCompletionProcessor(true, true);
232             StubTypeContext stubTypeContext= getChangeMethodSignatureRefactoring().getStubTypeContext();
233             processor.setCompletionContext(stubTypeContext.getCuHandle(), stubTypeContext.getBeforeString(), stubTypeContext.getAfterString());
234             ControlContentAssistHelper.createTextContentAssistant(text, processor);
235         }
236
237         private void createNameControl(Composite parent) {
238             Composite name= new Composite(parent, SWT.NONE);
239             name.setLayoutData(new GridData(GridData.FILL_BOTH));
240             GridLayout layout= new GridLayout(1, false);
241             layout.marginHeight= 0;
242             layout.marginWidth= 0;
243             name.setLayout(layout);
244
245             Label label= new Label(name, SWT.NONE);
246             label.setText(RefactoringMessages.ChangeSignatureInputPage_method_name);
247             
248             final Text text= new Text(name, SWT.BORDER);
249             text.setText(getChangeMethodSignatureRefactoring().getMethodName());
250             text.setLayoutData((new GridData(GridData.FILL_HORIZONTAL)));
251             TextFieldNavigationHandler.install(text);
252
253             if (getChangeMethodSignatureRefactoring().canChangeNameAndReturnType()) {
254                 text.addModifyListener(new ModifyListener(){
255                     public void modifyText(ModifyEvent e) {
256                         getChangeMethodSignatureRefactoring().setNewMethodName(text.getText());
257                         update(true);
258                     }
259                 });
260             } else {
261                 text.setEnabled(false);
262             }
263         }
264
265         private void createParameterExceptionsFolder(Composite composite) {
266             TabFolder folder= new TabFolder(composite, SWT.TOP);
267             folder.setLayoutData(new GridData(GridData.FILL_BOTH));
268             
269             TabItem item= new TabItem(folder, SWT.NONE);
270             item.setText(RefactoringMessages.ChangeSignatureInputPage_parameters);
271             item.setControl(createParameterTableControl(folder));
272             
273             TabItem itemEx= new TabItem(folder, SWT.NONE);
274             itemEx.setText(RefactoringMessages.ChangeSignatureInputPage_exceptions);
275             itemEx.setControl(createExceptionsTableControl(folder));
276
277             folder.addSelectionListener(new SelectionAdapter() {
278                 public void widgetSelected(SelectionEvent e) {
279                     ((TabItem) e.item).getControl().setFocus();
280                 }
281             });
282         }
283     
284         private Control createParameterTableControl(Composite composite) {
285             Composite border= new Composite(composite, SWT.NONE);
286             border.setLayout(new GridLayout());
287             
288             String JavaDoc labelText= null; //no label
289
ChangeParametersControl cp= new ChangeParametersControl(border, SWT.NONE, labelText, new IParameterListChangeListener() {
290                 public void parameterChanged(ParameterInfo parameter) {
291                     update(true);
292                 }
293                 public void parameterListChanged() {
294                     update(true);
295                 }
296                 public void parameterAdded(ParameterInfo parameter) {
297                     update(true);
298                 }
299             }, ChangeParametersControl.Mode.CHANGE_METHOD_SIGNATURE, getChangeMethodSignatureRefactoring().getStubTypeContext());
300             cp.setLayoutData(new GridData(GridData.FILL_BOTH));
301             cp.setInput(getChangeMethodSignatureRefactoring().getParameterInfos());
302             return border;
303         }
304         
305         private Control createExceptionsTableControl(Composite parent) {
306             Composite border= new Composite(parent, SWT.NONE);
307             border.setLayout(new GridLayout());
308             
309             ChangeExceptionsControl cp= new ChangeExceptionsControl(border, SWT.NONE, new IExceptionListChangeListener() {
310                 public void exceptionListChanged() {
311                     update(true);
312                 }
313             }, getChangeMethodSignatureRefactoring().getMethod().getJavaProject());
314             cp.setLayoutData(new GridData(GridData.FILL_BOTH));
315             cp.setInput(getChangeMethodSignatureRefactoring().getExceptionInfos());
316             return border;
317         }
318
319         public void dispose() {
320             DelegateUIHelper.saveLeaveDelegateSetting(fLeaveDelegateCheckBox);
321             DelegateUIHelper.saveDeprecateDelegateSetting(fDeprecateDelegateCheckBox);
322             super.dispose();
323         }
324         
325         private void createSignaturePreview(Composite composite) {
326             Label previewLabel= new Label(composite, SWT.NONE);
327             previewLabel.setText(RefactoringMessages.ChangeSignatureInputPage_method_Signature_Preview);
328             
329 // //XXX: use ViewForm to draw a flat border. Beware of common problems with wrapping layouts
330
// //inside GridLayout. GridData must be constrained to force wrapping. See bug 9866 et al.
331
// ViewForm border= new ViewForm(composite, SWT.BORDER | SWT.FLAT);
332

333             IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
334             fSignaturePreview= new JavaSourceViewer(composite, null, null, false, SWT.READ_ONLY | SWT.V_SCROLL | SWT.WRAP /*| SWT.BORDER*/, store);
335             fSignaturePreview.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null));
336             fSignaturePreview.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
337             fSignaturePreview.getTextWidget().setBackground(composite.getBackground());
338             fSignaturePreview.setDocument(fSignaturePreviewDocument);
339             fSignaturePreview.setEditable(false);
340             
341             //Layouting problems with wrapped text: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=9866
342
Control signaturePreviewControl= fSignaturePreview.getControl();
343             PixelConverter pixelConverter= new PixelConverter(signaturePreviewControl);
344             GridData gdata= new GridData(GridData.FILL_BOTH);
345             gdata.widthHint= pixelConverter.convertWidthInCharsToPixels(50);
346             gdata.heightHint= pixelConverter.convertHeightInCharsToPixels(2);
347             signaturePreviewControl.setLayoutData(gdata);
348             
349 // //XXX must force JavaSourceViewer text widget to wrap:
350
// border.setContent(signaturePreviewControl);
351
// GridData borderData= new GridData(GridData.FILL_BOTH);
352
// borderData.widthHint= gdata.widthHint;
353
// borderData.heightHint= gdata.heightHint;
354
// border.setLayoutData(borderData);
355
}
356
357         private ChangeSignatureRefactoring getChangeMethodSignatureRefactoring(){
358             return (ChangeSignatureRefactoring)getRefactoring();
359         }
360
361         private void update(boolean displayErrorMessage){
362             updateStatus(displayErrorMessage);
363             updateSignaturePreview();
364         }
365
366         private void updateStatus(boolean displayErrorMessage) {
367             try{
368                 if (getChangeMethodSignatureRefactoring().isSignatureSameAsInitial()){
369                     if (displayErrorMessage)
370                         setErrorMessage(RefactoringMessages.ChangeSignatureInputPage_unchanged);
371                     else
372                         setErrorMessage(null);
373                     setPageComplete(false);
374                     return;
375                 }
376                 RefactoringStatus nameCheck= getChangeMethodSignatureRefactoring().checkSignature();
377                 if (displayErrorMessage) {
378                     setPageComplete(nameCheck);
379                 } else {
380                     setErrorMessage(null);
381                     setPageComplete(true);
382                 }
383             } catch (JavaModelException e){
384                 setErrorMessage(RefactoringMessages.ChangeSignatureInputPage_Internal_Error);
385                 setPageComplete(false);
386                 JavaPlugin.log(e);
387             }
388         }
389
390         private void updateSignaturePreview() {
391             try{
392                 int top= fSignaturePreview.getTextWidget().getTopPixel();
393                 fSignaturePreviewDocument.set(getChangeMethodSignatureRefactoring().getNewMethodSignature());
394                 fSignaturePreview.getTextWidget().setTopPixel(top);
395             } catch (JavaModelException e){
396                 ExceptionHandler.handle(e, RefactoringMessages.ChangeSignatureRefactoring_modify_Parameters, RefactoringMessages.ChangeSignatureInputPage_exception);
397             }
398         }
399     }
400 }
401
Popular Tags