KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.IStatus;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Group;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Table;
34 import org.eclipse.swt.widgets.TableColumn;
35 import org.eclipse.swt.widgets.Text;
36
37 import org.eclipse.jface.dialogs.IDialogSettings;
38 import org.eclipse.jface.dialogs.IInputValidator;
39 import org.eclipse.jface.dialogs.IMessageProvider;
40 import org.eclipse.jface.dialogs.InputDialog;
41 import org.eclipse.jface.preference.IPreferenceStore;
42 import org.eclipse.jface.resource.JFaceResources;
43 import org.eclipse.jface.viewers.CellEditor;
44 import org.eclipse.jface.viewers.CellLabelProvider;
45 import org.eclipse.jface.viewers.CheckStateChangedEvent;
46 import org.eclipse.jface.viewers.CheckboxTableViewer;
47 import org.eclipse.jface.viewers.ColumnViewer;
48 import org.eclipse.jface.viewers.ColumnWeightData;
49 import org.eclipse.jface.viewers.EditingSupport;
50 import org.eclipse.jface.viewers.ICheckStateListener;
51 import org.eclipse.jface.viewers.ISelection;
52 import org.eclipse.jface.viewers.ISelectionChangedListener;
53 import org.eclipse.jface.viewers.IStructuredContentProvider;
54 import org.eclipse.jface.viewers.IStructuredSelection;
55 import org.eclipse.jface.viewers.SelectionChangedEvent;
56 import org.eclipse.jface.viewers.TableViewer;
57 import org.eclipse.jface.viewers.TableViewerColumn;
58 import org.eclipse.jface.viewers.TextCellEditor;
59 import org.eclipse.jface.viewers.Viewer;
60 import org.eclipse.jface.viewers.ViewerCell;
61 import org.eclipse.jface.window.Window;
62
63 import org.eclipse.jface.text.Document;
64 import org.eclipse.jface.text.IDocument;
65
66 import org.eclipse.ltk.core.refactoring.Refactoring;
67 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
68 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
69
70 import org.eclipse.jdt.core.IJavaProject;
71 import org.eclipse.jdt.core.IType;
72 import org.eclipse.jdt.core.JavaConventions;
73 import org.eclipse.jdt.core.JavaCore;
74 import org.eclipse.jdt.core.JavaModelException;
75
76 import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo;
77 import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureRefactoring;
78 import org.eclipse.jdt.internal.corext.refactoring.structure.IntroduceParameterObjectRefactoring;
79 import org.eclipse.jdt.internal.corext.util.Messages;
80
81 import org.eclipse.jdt.ui.JavaElementLabels;
82 import org.eclipse.jdt.ui.PreferenceConstants;
83 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
84
85 import org.eclipse.jdt.internal.ui.JavaPlugin;
86 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
87 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
88 import org.eclipse.jdt.internal.ui.util.PixelConverter;
89 import org.eclipse.jdt.internal.ui.util.SWTUtil;
90 import org.eclipse.jdt.internal.ui.util.TableLayoutComposite;
91
92 public class IntroduceParameterObjectWizard extends RefactoringWizard {
93
94     public IntroduceParameterObjectWizard(Refactoring refactoring) {
95         super(refactoring, DIALOG_BASED_USER_INTERFACE);
96         setDefaultPageTitle(RefactoringMessages.IntroduceParameterObjectWizard_wizardpage_title);
97     }
98
99     protected void addUserInputPages() {
100         addPage(new IntroduceParameterObjectInputPage());
101     }
102
103     private static class IntroduceParameterObjectInputPage extends UserInputWizardPage {
104
105         private static final String JavaDoc CREATE_TOP_LEVEL_SETTING= "CreateTopLevel"; //$NON-NLS-1$
106
private static final String JavaDoc CREATE_SETTERS_SETTING= "CreateSetters"; //$NON-NLS-1$
107
private static final String JavaDoc CREATE_GETTERS_SETTING= "CreateGetters"; //$NON-NLS-1$
108

109         private final class ParameterObjectCreatorContentProvider implements IStructuredContentProvider {
110             public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
111             }
112
113             public void dispose() {
114             }
115
116             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
117                 if (inputElement instanceof IntroduceParameterObjectRefactoring) {
118                     IntroduceParameterObjectRefactoring refactoring= (IntroduceParameterObjectRefactoring) inputElement;
119                     List JavaDoc parameterInfos= refactoring.getParameterInfos();
120                     List JavaDoc result= new ArrayList JavaDoc(parameterInfos.size());
121                     for (Iterator JavaDoc iter= parameterInfos.iterator(); iter.hasNext();) {
122                         ParameterInfo pi= (ParameterInfo) iter.next();
123                         if (!pi.isAdded())
124                             result.add(pi);
125                     }
126                     return result.toArray();
127                 }
128                 return null;
129             }
130         }
131
132         private abstract class ParameterInfoLabelProvider extends CellLabelProvider {
133             public void update(ViewerCell cell) {
134                 ParameterInfo pi= (ParameterInfo) cell.getElement();
135                 cell.setText(doGetValue(pi));
136             }
137
138             protected abstract String JavaDoc doGetValue(ParameterInfo pi);
139         }
140
141         private abstract class ParameterInfoEditingSupport extends EditingSupport {
142             private CellEditor fTextEditor;
143
144             private ParameterInfoEditingSupport(CellEditor textEditor, ColumnViewer tv) {
145                 super(tv);
146                 fTextEditor= textEditor;
147             }
148
149             protected void setValue(Object JavaDoc element, Object JavaDoc value) {
150                 if (element instanceof ParameterInfo) {
151                     ParameterInfo pi= (ParameterInfo) element;
152                     doSetValue(pi, value.toString());
153                     getViewer().update(element, null);
154                 }
155                 validateRefactoring();
156                 updateSignaturePreview();
157             }
158
159             public abstract void doSetValue(ParameterInfo pi, String JavaDoc string);
160
161             protected Object JavaDoc getValue(Object JavaDoc element) {
162                 if (element instanceof ParameterInfo) {
163                     ParameterInfo pi= (ParameterInfo) element;
164                     return doGetValue(pi);
165                 }
166                 return null;
167             }
168
169             public abstract String JavaDoc doGetValue(ParameterInfo pi);
170
171             protected CellEditor getCellEditor(Object JavaDoc element) {
172                 return fTextEditor;
173             }
174
175             protected boolean canEdit(Object JavaDoc element) {
176                 if (element instanceof ParameterInfo) {
177                     ParameterInfo pi= (ParameterInfo) element;
178                     return fTextEditor!=null && pi.isCreateField();
179                 }
180                 return false;
181             }
182         }
183
184         private IntroduceParameterObjectRefactoring fRefactoring;
185         private JavaSourceViewer fSignaturePreview;
186         private IDocument fSignaturePreviewDocument= new Document();
187         private Button fLeaveDelegateCheckBox;
188         private Button fDeprecateDelegateCheckBox;
189
190         public IntroduceParameterObjectInputPage() {
191             super(RefactoringMessages.IntroduceParameterObjectWizard_wizardpage_name);
192             setTitle(RefactoringMessages.IntroduceParameterObjectWizard_wizardpage_title);
193             setDescription(RefactoringMessages.IntroduceParameterObjectWizard_wizardpage_description);
194         }
195
196         public void createControl(Composite parent) {
197             initializeDialogUnits(parent);
198             fRefactoring= (IntroduceParameterObjectRefactoring) getRefactoring();
199             Composite result= new Composite(parent, SWT.NONE);
200             result.setLayout(new GridLayout(2, false));
201             Group group= createGroup(result, RefactoringMessages.IntroduceParameterObjectWizard_type_group);
202
203             createClassNameInput(group);
204             createLocationInput(group);
205
206             createTable(group);
207             createGetterInput(group);
208             createSetterInput(group);
209
210             group= createGroup(result, RefactoringMessages.IntroduceParameterObjectWizard_method_group);
211             createParameterNameInput(group);
212             createDelegateInput(group);
213             createSignaturePreview(group);
214
215             validateRefactoring();
216             
217             setControl(result);
218         }
219
220         private void createParameterNameInput(Group group) {
221             Label l= new Label(group, SWT.NONE);
222             l.setText(RefactoringMessages.IntroduceParameterObjectWizard_parameterfield_label);
223             final Text text= new Text(group, SWT.BORDER);
224             text.setText(fRefactoring.getParameterName());
225             text.addModifyListener(new ModifyListener() {
226
227                 public void modifyText(ModifyEvent e) {
228                     fRefactoring.setParameterName(text.getText());
229                     updateSignaturePreview();
230                     validateRefactoring();
231                 }
232
233             });
234             text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
235         }
236
237         protected void validateRefactoring() {
238             List JavaDoc names= new ArrayList JavaDoc();
239             boolean oneChecked= false;
240             setMessage(null);
241             setErrorMessage(null);
242             setPageComplete(true);
243             IJavaProject project= fRefactoring.getMethod().getJavaProject();
244             String JavaDoc sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true);
245             String JavaDoc compliance= project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
246             List JavaDoc parameterInfos= fRefactoring.getParameterInfos();
247             for (Iterator JavaDoc iter= parameterInfos.iterator(); iter.hasNext();) {
248                 ParameterInfo pi= (ParameterInfo) iter.next();
249                 if (names.contains(pi.getNewName())) {
250                     setErrorMessage(Messages.format(RefactoringMessages.IntroduceParameterObjectWizard_parametername_check_notunique, pi.getNewName()));
251                     setPageComplete(false);
252                     return;
253                 }
254                 names.add(pi.getNewName());
255                 IStatus validateIdentifier= JavaConventions.validateIdentifier(pi.getNewName(), sourceLevel, compliance);
256                 if (isErrorMessage(validateIdentifier))
257                     return;
258                 if (pi.isCreateField())
259                     oneChecked= true;
260             }
261             if (!oneChecked) {
262                 setErrorMessage(RefactoringMessages.IntroduceParameterObjectWizard_parametername_check_atleastoneparameter);
263                 setPageComplete(false);
264                 return;
265             }
266             IStatus validateJavaTypeName= JavaConventions.validateJavaTypeName(fRefactoring.getClassName(), sourceLevel, compliance);
267             if (isErrorMessage(validateJavaTypeName))
268                 return;
269             if (fRefactoring.getClassName().indexOf('.') != -1) {
270                 setErrorMessage(RefactoringMessages.IntroduceParameterObjectWizard_dot_not_allowed_error);
271                 setPageComplete(false);
272             }
273             if (!"".equals(fRefactoring.getPackage())) { //$NON-NLS-1$
274
IStatus validatePackageName= JavaConventions.validatePackageName(fRefactoring.getPackage(), sourceLevel, compliance);
275                 if (isErrorMessage(validatePackageName))
276                     return;
277             }
278             try {
279                 IType type= project.findType(fRefactoring.getNewTypeName());
280                 if (type != null) {
281                     StringBuffer JavaDoc packageName= new StringBuffer JavaDoc();
282                     JavaElementLabels.getPackageFragmentLabel(type.getPackageFragment(), JavaElementLabels.ALL_DEFAULT, packageName);
283                     if (fRefactoring.isCreateAsTopLevel()) {
284                         setErrorMessage(Messages.format(RefactoringMessages.IntroduceParameterObjectWizard_type_already_exists_in_package_info, new Object JavaDoc[] {fRefactoring.getClassName(), packageName.toString() }));
285                         setPageComplete(false);
286                         return;
287                     } else {
288                         setErrorMessage(Messages.format(RefactoringMessages.IntroduceParameterObjectWizard_parametername_check_alreadyexists, new Object JavaDoc[] { fRefactoring.getClassName(), type.getCompilationUnit().getElementName()}));
289                         setPageComplete(false);
290                         return;
291                     }
292                 }
293             } catch (JavaModelException e) {
294                 // Don't care. The error will popup later anyway..
295
}
296         }
297
298         private boolean isErrorMessage(IStatus validationStatus) {
299             if (!validationStatus.isOK()) {
300                 if (validationStatus.getSeverity() == IStatus.ERROR) {
301                     setErrorMessage(validationStatus.getMessage());
302                     setPageComplete(false);
303                     return true;
304                 } else {
305                     if (validationStatus.getSeverity() == IStatus.INFO)
306                         setMessage(validationStatus.getMessage(), IMessageProvider.INFORMATION);
307                     else
308                         setMessage(validationStatus.getMessage(), IMessageProvider.WARNING);
309                 }
310             }
311             return false;
312         }
313
314         private void createSignaturePreview(Composite composite) {
315             Label previewLabel= new Label(composite, SWT.NONE);
316             previewLabel.setText(RefactoringMessages.IntroduceParameterObjectWizard_signaturepreview_label);
317             GridData gridData= new GridData(GridData.FILL_HORIZONTAL);
318             gridData.horizontalSpan= 2;
319             previewLabel.setLayoutData(gridData);
320
321             // //XXX: use ViewForm to draw a flat border. Beware of common
322
// problems
323
// with wrapping layouts
324
// //inside GridLayout. GridData must be constrained to force
325
// wrapping.
326
// See bug 9866 et al.
327
// ViewForm border= new ViewForm(composite, SWT.BORDER | SWT.FLAT);
328

329             IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
330             fSignaturePreview= new JavaSourceViewer(composite, null, null, false, SWT.READ_ONLY | SWT.V_SCROLL | SWT.WRAP, store);
331             fSignaturePreview.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null));
332             fSignaturePreview.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
333             fSignaturePreview.getTextWidget().setBackground(composite.getBackground());
334             fSignaturePreview.setDocument(fSignaturePreviewDocument);
335             fSignaturePreview.setEditable(false);
336
337             // Layouting problems with wrapped text: see
338
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=9866
339
Control signaturePreviewControl= fSignaturePreview.getControl();
340             PixelConverter pixelConverter= new PixelConverter(signaturePreviewControl);
341             GridData gdata= new GridData(GridData.FILL_BOTH);
342             gdata.widthHint= pixelConverter.convertWidthInCharsToPixels(50);
343             gdata.heightHint= pixelConverter.convertHeightInCharsToPixels(2);
344             gdata.horizontalSpan= 2;
345             signaturePreviewControl.setLayoutData(gdata);
346
347             updateSignaturePreview();
348             // //XXX must force JavaSourceViewer text widget to wrap:
349
// border.setContent(signaturePreviewControl);
350
// GridData borderData= new GridData(GridData.FILL_BOTH);
351
// borderData.widthHint= gdata.widthHint;
352
// borderData.heightHint= gdata.heightHint;
353
// border.setLayoutData(borderData);
354
}
355
356         private void createDelegateInput(Group group) {
357             fLeaveDelegateCheckBox= DelegateUIHelper.generateLeaveDelegateCheckbox(group, getRefactoring(), false);
358             GridData gridData= new GridData(GridData.FILL_HORIZONTAL);
359             gridData.horizontalSpan= 2;
360             fLeaveDelegateCheckBox.setLayoutData(gridData);
361             if (fLeaveDelegateCheckBox != null) {
362                 fDeprecateDelegateCheckBox= new Button(group, SWT.CHECK);
363                 GridData data= new GridData();
364                 data.horizontalAlignment= GridData.FILL;
365                 GridLayout layout= (GridLayout) group.getLayout();
366                 data.horizontalIndent= (layout.marginWidth + fDeprecateDelegateCheckBox.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
367                 data.horizontalSpan= 2;
368                 fDeprecateDelegateCheckBox.setLayoutData(data);
369                 fDeprecateDelegateCheckBox.setText(DelegateUIHelper.getDeprecateDelegateCheckBoxTitle());
370                 final ChangeSignatureRefactoring refactoring= fRefactoring;
371                 fDeprecateDelegateCheckBox.setSelection(DelegateUIHelper.loadDeprecateDelegateSetting(refactoring));
372                 refactoring.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
373                 fDeprecateDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
374                     public void widgetSelected(SelectionEvent e) {
375                         refactoring.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
376                         validateRefactoring();
377                     }
378                 });
379                 fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
380                 fLeaveDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
381                     public void widgetSelected(SelectionEvent e) {
382                         fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
383                     }
384                 });
385             }
386         }
387
388         private Group createGroup(Composite result, String JavaDoc caption) {
389             Group group= new Group(result, SWT.None);
390             group.setLayout(new GridLayout(2, false));
391             group.setText(caption);
392             GridData gridData= new GridData(GridData.FILL_BOTH);
393             gridData.horizontalSpan= 2;
394             group.setLayoutData(gridData);
395             return group;
396         }
397
398         private void createLocationInput(Composite parent) {
399             Label l= new Label(parent, SWT.NONE);
400             l.setText(RefactoringMessages.IntroduceParameterObjectWizard_destination_label);
401
402             Composite composite= new Composite(parent, SWT.None);
403             GridLayout gridLayout= new GridLayout(2, false);
404             gridLayout.marginHeight= 0;
405             gridLayout.marginWidth= 0;
406             composite.setLayout(gridLayout);
407             GridData gridData= new GridData(GridData.FILL_HORIZONTAL);
408             composite.setLayoutData(gridData);
409
410             final Button topLvlRadio= new Button(composite, SWT.RADIO);
411             topLvlRadio.setText(RefactoringMessages.IntroduceParameterObjectWizard_createastoplevel_radio);
412             topLvlRadio.addSelectionListener(new SelectionAdapter() {
413                 public void widgetSelected(SelectionEvent e) {
414                     boolean fAsTopLevel= topLvlRadio.getSelection();
415                     fRefactoring.setCreateAsTopLevel(fAsTopLevel);
416                     updateSignaturePreview();
417                     validateRefactoring();
418                 }
419             });
420
421             Button nestedRadio= new Button(composite, SWT.RADIO);
422             nestedRadio.setText(Messages.format(RefactoringMessages.IntroduceParameterObjectWizard_createasnestedclass_radio, fRefactoring.getContainingClass().getName()));
423             boolean createAsTopLevel= getBooleanSetting(CREATE_TOP_LEVEL_SETTING, fRefactoring.isCreateAsTopLevel());
424             fRefactoring.setCreateAsTopLevel(createAsTopLevel);
425             topLvlRadio.setSelection(createAsTopLevel);
426             nestedRadio.setSelection(!createAsTopLevel);
427
428         }
429
430         private void createTable(Composite parent) {
431             Composite result= new Composite(parent, SWT.NONE);
432             GridLayout layout= new GridLayout(2, false);
433             layout.marginHeight= 0;
434             layout.marginWidth= 0;
435             result.setLayout(layout);
436             GridData gridData= new GridData(GridData.FILL_BOTH);
437             gridData.horizontalSpan= 2;
438             result.setLayoutData(gridData);
439
440             Label l= new Label(result, SWT.NONE);
441             l.setText(RefactoringMessages.IntroduceParameterObjectWizard_fields_selection_label);
442             gridData= new GridData(GridData.FILL_HORIZONTAL);
443             gridData.horizontalSpan= 2;
444             gridData.verticalIndent= 5;
445             l.setLayoutData(gridData);
446             
447             TableLayoutComposite layoutComposite= new TableLayoutComposite(result, SWT.NONE);
448             layoutComposite.addColumnData(new ColumnWeightData(40, convertWidthInCharsToPixels(20), true));
449             layoutComposite.addColumnData(new ColumnWeightData(60, convertWidthInCharsToPixels(20), true));
450             final CheckboxTableViewer tv= CheckboxTableViewer.newCheckList(layoutComposite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
451             tv.setContentProvider(new ParameterObjectCreatorContentProvider());
452             createColumns(tv);
453
454             Table table= tv.getTable();
455             table.setLinesVisible(true);
456             table.setHeaderVisible(true);
457             gridData= new GridData(GridData.FILL_BOTH);
458             table.setLayoutData(gridData);
459             tv.setInput(fRefactoring);
460             List JavaDoc parameterInfos= fRefactoring.getParameterInfos();
461             for (Iterator JavaDoc iter= parameterInfos.iterator(); iter.hasNext();) {
462                 ParameterInfo pi= (ParameterInfo) iter.next();
463                 tv.setChecked(pi, pi.isCreateField());
464             }
465             tv.refresh(true);
466             gridData= new GridData(GridData.FILL_BOTH);
467             gridData.heightHint= SWTUtil.getTableHeightHint(table, parameterInfos.size());
468             layoutComposite.setLayoutData(gridData);
469             Composite controls= new Composite(result, SWT.NONE);
470             gridData= new GridData(GridData.FILL, GridData.FILL, false, false);
471             controls.setLayoutData(gridData);
472             GridLayout gridLayout= new GridLayout();
473             gridLayout.marginHeight= 0;
474             gridLayout.marginWidth= 0;
475             controls.setLayout(gridLayout);
476
477             final Button upButton= new Button(controls, SWT.NONE);
478             upButton.setText(RefactoringMessages.IntroduceParameterObjectWizard_moveentryup_button);
479             gridData= new GridData(GridData.FILL_HORIZONTAL);
480             upButton.setLayoutData(gridData);
481             SWTUtil.setButtonDimensionHint(upButton);
482             upButton.setEnabled(false);
483
484             final Button downButton= new Button(controls, SWT.NONE);
485             downButton.setText(RefactoringMessages.IntroduceParameterObjectWizard_moventrydown_button);
486             gridData= new GridData(GridData.FILL_HORIZONTAL);
487             downButton.setLayoutData(gridData);
488             SWTUtil.setButtonDimensionHint(downButton);
489             downButton.setEnabled(false);
490
491             addSpacer(controls);
492
493             final Button editButton= new Button(controls, SWT.NONE);
494             editButton.setText(RefactoringMessages.IntroduceParameterObjectWizard_edit_button);
495             editButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
496             editButton.setEnabled(!tv.getSelection().isEmpty());
497             SWTUtil.setButtonDimensionHint(editButton);
498             editButton.addSelectionListener(new SelectionAdapter() {
499                 public void widgetSelected(SelectionEvent e) {
500                     ISelection selection= tv.getSelection();
501                     if (selection instanceof IStructuredSelection) {
502                         IStructuredSelection ss= (IStructuredSelection) selection;
503                         ParameterInfo selected= (ParameterInfo) ss.getFirstElement();
504                         String JavaDoc message= RefactoringMessages.IntroduceParameterObjectWizard_fieldname_message;
505                         String JavaDoc title= RefactoringMessages.IntroduceParameterObjectWizard_fieldname_title;
506                         InputDialog inputDialog= new InputDialog(getShell(), title, message, selected.getNewName(), new IInputValidator() {
507
508                             public String JavaDoc isValid(String JavaDoc newText) {
509                                 IJavaProject project= fRefactoring.getCompilationUnit().getJavaProject();
510                                 String JavaDoc sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true);
511                                 String JavaDoc compliance= project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
512                                 IStatus status= JavaConventions.validateIdentifier(newText, sourceLevel, compliance);
513                                 if (!status.isOK())
514                                     return status.getMessage();
515                                 return null;
516                             }
517
518                         });
519                         if (inputDialog.open() == Window.OK) {
520                             selected.setNewName(inputDialog.getValue());
521                             tv.refresh(selected);
522                             updateSignaturePreview();
523                         }
524                             
525                     }
526                 }
527             });
528
529             downButton.addSelectionListener(new SelectionAdapter() {
530                 public void widgetSelected(SelectionEvent e) {
531                     ISelection selection= tv.getSelection();
532                     if (selection instanceof IStructuredSelection) {
533                         IStructuredSelection ss= (IStructuredSelection) selection;
534                         ParameterInfo selected= (ParameterInfo) ss.getFirstElement();
535                         fRefactoring.moveFieldDown(selected);
536                         tv.refresh();
537                         updateButtons(tv, upButton, downButton, editButton);
538                     }
539                 }
540
541             });
542             upButton.addSelectionListener(new SelectionAdapter() {
543                 public void widgetSelected(SelectionEvent e) {
544                     ISelection selection= tv.getSelection();
545                     if (selection instanceof IStructuredSelection) {
546                         IStructuredSelection ss= (IStructuredSelection) selection;
547                         ParameterInfo selected= (ParameterInfo) ss.getFirstElement();
548                         fRefactoring.moveFieldUp(selected);
549                         tv.refresh();
550                         updateButtons(tv, upButton, downButton, editButton);
551                     }
552                 }
553
554             });
555             tv.addCheckStateListener(new ICheckStateListener() {
556                 Map JavaDoc fLastNames=new HashMap JavaDoc();
557                 public void checkStateChanged(CheckStateChangedEvent event) {
558                     ParameterInfo element= (ParameterInfo) event.getElement();
559                     element.setCreateField(event.getChecked());
560                     if (element.isCreateField()){
561                         String JavaDoc lastName= (String JavaDoc) fLastNames.get(element);
562                         if (lastName==null){
563                             lastName=fRefactoring.getFieldName(element);
564                         }
565                         element.setNewName(lastName);
566                     } else {
567                         fLastNames.put(element, element.getNewName());
568                         element.setNewName(element.getOldName());
569                     }
570                     tv.update(element, null);
571                     updateButtons(tv, upButton, downButton, editButton);
572                     validateRefactoring();
573                 }
574
575             });
576             tv.addSelectionChangedListener(new ISelectionChangedListener() {
577                 public void selectionChanged(SelectionChangedEvent event) {
578                     updateButtons(tv, upButton, downButton, editButton);
579                 }
580             });
581         }
582
583         private void addSpacer(Composite parent) {
584             Label label= new Label(parent, SWT.NONE);
585             GridData gd= new GridData(GridData.FILL_HORIZONTAL);
586             gd.heightHint= 5;
587             label.setLayoutData(gd);
588         }
589
590         private void createColumns(final CheckboxTableViewer tv) {
591             TextCellEditor cellEditor= new TextCellEditor(tv.getTable());
592
593             TableViewerColumn viwerColumn= new TableViewerColumn(tv, SWT.LEAD);
594             viwerColumn.setLabelProvider(new ParameterInfoLabelProvider() {
595                 protected String JavaDoc doGetValue(ParameterInfo pi) {
596                     return pi.getNewTypeName();
597                 }
598             });
599
600             TableColumn column= viwerColumn.getColumn();
601             column.setText(RefactoringMessages.IntroduceParameterObjectWizard_type_column);
602             viwerColumn= new TableViewerColumn(tv, SWT.LEAD);
603             viwerColumn.setLabelProvider(new ParameterInfoLabelProvider() {
604                 protected String JavaDoc doGetValue(ParameterInfo pi) {
605                     return pi.getNewName();
606                 }
607             });
608             viwerColumn.setEditingSupport(new ParameterInfoEditingSupport(cellEditor, tv) {
609                 public String JavaDoc doGetValue(ParameterInfo pi) {
610                     return pi.getNewName();
611                 }
612                 public void doSetValue(ParameterInfo pi, String JavaDoc string) {
613                     pi.setNewName(string);
614                 }
615             });
616             column= viwerColumn.getColumn();
617             column.setText(RefactoringMessages.IntroduceParameterObjectWizard_name_column);
618         }
619
620         private void createGetterInput(Composite result) {
621             Composite buttons= new Composite(result, SWT.NONE);
622             GridLayout gridLayout= new GridLayout(2, true);
623             gridLayout.marginHeight= 0;
624             gridLayout.marginWidth= 0;
625             buttons.setLayout(gridLayout);
626             GridData gridData= new GridData();
627             gridData.horizontalSpan= 2;
628             buttons.setLayoutData(gridData);
629
630             final Button button= new Button(buttons, SWT.CHECK);
631             button.setText(RefactoringMessages.IntroduceParameterObjectWizard_creategetter_checkbox);
632             button.addSelectionListener(new SelectionAdapter() {
633
634                 public void widgetSelected(SelectionEvent e) {
635                     fRefactoring.setCreateGetter(button.getSelection());
636                     validateRefactoring();
637                 }
638
639             });
640             button.setSelection(getBooleanSetting(CREATE_GETTERS_SETTING, fRefactoring.isCreateGetter()));
641             fRefactoring.setCreateGetter(button.getSelection());
642             gridData= new GridData();
643             button.setLayoutData(gridData);
644         }
645
646         private void createSetterInput(Composite result) {
647             final Button button= new Button(result, SWT.CHECK);
648             button.setText(RefactoringMessages.IntroduceParameterObjectWizard_createsetter_checkbox);
649             button.addSelectionListener(new SelectionAdapter() {
650
651                 public void widgetSelected(SelectionEvent e) {
652                     fRefactoring.setCreateSetter(button.getSelection());
653                     validateRefactoring();
654                 }
655
656             });
657             button.setSelection(getBooleanSetting(CREATE_SETTERS_SETTING, fRefactoring.isCreateSetter()));
658             fRefactoring.setCreateSetter(button.getSelection());
659             GridData gridData= new GridData();
660             button.setLayoutData(gridData);
661         }
662
663         private void createClassNameInput(Composite result) {
664             Label label= new Label(result, SWT.LEAD);
665             label.setText(RefactoringMessages.IntroduceParameterObjectWizard_classnamefield_label);
666             final Text text= new Text(result, SWT.SINGLE | SWT.BORDER);
667             text.setText(fRefactoring.getClassName());
668             text.selectAll();
669             text.setFocus();
670             text.addModifyListener(new ModifyListener() {
671
672                 public void modifyText(ModifyEvent e) {
673                     fRefactoring.setClassName(text.getText());
674                     updateSignaturePreview();
675                     validateRefactoring();
676                 }
677
678             });
679             text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
680         }
681
682         private void updateButtons(final TableViewer tv, Button upButton, Button downButton, Button editButton) {
683             IStructuredSelection selection= (IStructuredSelection) tv.getSelection();
684             ParameterInfo firstElement= (ParameterInfo) selection.getFirstElement();
685             if (selection.isEmpty()) {
686                 upButton.setEnabled(false);
687                 downButton.setEnabled(false);
688                 editButton.setEnabled(false);
689             } else {
690                 int selectionIndex= tv.getTable().getSelectionIndex();
691                 upButton.setEnabled(selectionIndex != 0);
692                 downButton.setEnabled(selectionIndex != tv.getTable().getItemCount() - 1);
693                 editButton.setEnabled(firstElement.isCreateField());
694             }
695             fRefactoring.updateParameterPosition();
696             updateSignaturePreview();
697         }
698
699         private void updateSignaturePreview() {
700             try {
701                 int top= fSignaturePreview.getTextWidget().getTopPixel();
702                 fSignaturePreviewDocument.set(fRefactoring.getNewMethodSignature());
703                 fSignaturePreview.getTextWidget().setTopPixel(top);
704             } catch (JavaModelException e) {
705                 ExceptionHandler.handle(e, RefactoringMessages.IntroduceParameterObjectWizard_error_title, RefactoringMessages.IntroduceParameterObjectWizard_error_description);
706             }
707         }
708         
709         public void dispose() {
710             DelegateUIHelper.saveDeprecateDelegateSetting(fDeprecateDelegateCheckBox);
711             DelegateUIHelper.saveLeaveDelegateSetting(fLeaveDelegateCheckBox);
712             IDialogSettings settings= getRefactoringSettings();
713             settings.put(IntroduceParameterObjectInputPage.CREATE_GETTERS_SETTING, fRefactoring.isCreateGetter());
714             settings.put(IntroduceParameterObjectInputPage.CREATE_SETTERS_SETTING, fRefactoring.isCreateSetter());
715             settings.put(IntroduceParameterObjectInputPage.CREATE_TOP_LEVEL_SETTING, fRefactoring.isCreateAsTopLevel());
716             super.dispose();
717         }
718         
719         protected boolean getBooleanSetting(String JavaDoc key, boolean defaultValue) {
720             String JavaDoc update= getRefactoringSettings().get(key);
721             if (update != null)
722                 return Boolean.valueOf(update).booleanValue();
723             else
724                 return defaultValue;
725         }
726     }
727
728 }
729
Popular Tags