001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Map;
018:
019: import org.eclipse.core.runtime.IStatus;
020:
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.events.ModifyEvent;
023: import org.eclipse.swt.events.ModifyListener;
024: import org.eclipse.swt.events.SelectionAdapter;
025: import org.eclipse.swt.events.SelectionEvent;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.layout.GridLayout;
028: import org.eclipse.swt.widgets.Button;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Group;
032: import org.eclipse.swt.widgets.Label;
033: import org.eclipse.swt.widgets.Table;
034: import org.eclipse.swt.widgets.TableColumn;
035: import org.eclipse.swt.widgets.Text;
036:
037: import org.eclipse.jface.dialogs.IDialogSettings;
038: import org.eclipse.jface.dialogs.IInputValidator;
039: import org.eclipse.jface.dialogs.IMessageProvider;
040: import org.eclipse.jface.dialogs.InputDialog;
041: import org.eclipse.jface.preference.IPreferenceStore;
042: import org.eclipse.jface.resource.JFaceResources;
043: import org.eclipse.jface.viewers.CellEditor;
044: import org.eclipse.jface.viewers.CellLabelProvider;
045: import org.eclipse.jface.viewers.CheckStateChangedEvent;
046: import org.eclipse.jface.viewers.CheckboxTableViewer;
047: import org.eclipse.jface.viewers.ColumnViewer;
048: import org.eclipse.jface.viewers.ColumnWeightData;
049: import org.eclipse.jface.viewers.EditingSupport;
050: import org.eclipse.jface.viewers.ICheckStateListener;
051: import org.eclipse.jface.viewers.ISelection;
052: import org.eclipse.jface.viewers.ISelectionChangedListener;
053: import org.eclipse.jface.viewers.IStructuredContentProvider;
054: import org.eclipse.jface.viewers.IStructuredSelection;
055: import org.eclipse.jface.viewers.SelectionChangedEvent;
056: import org.eclipse.jface.viewers.TableViewer;
057: import org.eclipse.jface.viewers.TableViewerColumn;
058: import org.eclipse.jface.viewers.TextCellEditor;
059: import org.eclipse.jface.viewers.Viewer;
060: import org.eclipse.jface.viewers.ViewerCell;
061: import org.eclipse.jface.window.Window;
062:
063: import org.eclipse.jface.text.Document;
064: import org.eclipse.jface.text.IDocument;
065:
066: import org.eclipse.ltk.core.refactoring.Refactoring;
067: import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
068: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
069:
070: import org.eclipse.jdt.core.IJavaProject;
071: import org.eclipse.jdt.core.IType;
072: import org.eclipse.jdt.core.JavaConventions;
073: import org.eclipse.jdt.core.JavaCore;
074: import org.eclipse.jdt.core.JavaModelException;
075:
076: import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo;
077: import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureRefactoring;
078: import org.eclipse.jdt.internal.corext.refactoring.structure.IntroduceParameterObjectRefactoring;
079: import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil;
080: import org.eclipse.jdt.internal.corext.util.Messages;
081:
082: import org.eclipse.jdt.ui.JavaElementLabels;
083: import org.eclipse.jdt.ui.PreferenceConstants;
084: import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
085:
086: import org.eclipse.jdt.internal.ui.JavaPlugin;
087: import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
088: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
089: import org.eclipse.jdt.internal.ui.util.PixelConverter;
090: import org.eclipse.jdt.internal.ui.util.SWTUtil;
091: import org.eclipse.jdt.internal.ui.util.TableLayoutComposite;
092:
093: public class IntroduceParameterObjectWizard extends RefactoringWizard {
094:
095: public IntroduceParameterObjectWizard(Refactoring refactoring) {
096: super (refactoring, DIALOG_BASED_USER_INTERFACE);
097: setDefaultPageTitle(RefactoringMessages.IntroduceParameterObjectWizard_wizardpage_title);
098: }
099:
100: protected void addUserInputPages() {
101: addPage(new IntroduceParameterObjectInputPage());
102: }
103:
104: private static class IntroduceParameterObjectInputPage extends
105: UserInputWizardPage {
106:
107: private static final String CREATE_TOP_LEVEL_SETTING = "CreateTopLevel"; //$NON-NLS-1$
108: private static final String CREATE_SETTERS_SETTING = "CreateSetters"; //$NON-NLS-1$
109: private static final String CREATE_GETTERS_SETTING = "CreateGetters"; //$NON-NLS-1$
110:
111: private final class ParameterObjectCreatorContentProvider
112: implements IStructuredContentProvider {
113: public void inputChanged(Viewer viewer, Object oldInput,
114: Object newInput) {
115: }
116:
117: public void dispose() {
118: }
119:
120: public Object[] getElements(Object inputElement) {
121: if (inputElement instanceof IntroduceParameterObjectRefactoring) {
122: IntroduceParameterObjectRefactoring refactoring = (IntroduceParameterObjectRefactoring) inputElement;
123: List parameterInfos = refactoring
124: .getParameterInfos();
125: List result = new ArrayList(parameterInfos.size());
126: for (Iterator iter = parameterInfos.iterator(); iter
127: .hasNext();) {
128: ParameterInfo pi = (ParameterInfo) iter.next();
129: if (!pi.isAdded())
130: result.add(pi);
131: }
132: return result.toArray();
133: }
134: return null;
135: }
136: }
137:
138: private abstract class ParameterInfoLabelProvider extends
139: CellLabelProvider {
140: public void update(ViewerCell cell) {
141: ParameterInfo pi = (ParameterInfo) cell.getElement();
142: cell.setText(doGetValue(pi));
143: }
144:
145: protected abstract String doGetValue(ParameterInfo pi);
146: }
147:
148: private abstract class ParameterInfoEditingSupport extends
149: EditingSupport {
150: private CellEditor fTextEditor;
151:
152: private ParameterInfoEditingSupport(CellEditor textEditor,
153: ColumnViewer tv) {
154: super (tv);
155: fTextEditor = textEditor;
156: }
157:
158: protected void setValue(Object element, Object value) {
159: if (element instanceof ParameterInfo) {
160: ParameterInfo pi = (ParameterInfo) element;
161: doSetValue(pi, value.toString());
162: getViewer().update(element, null);
163: }
164: validateRefactoring();
165: updateSignaturePreview();
166: }
167:
168: public abstract void doSetValue(ParameterInfo pi,
169: String string);
170:
171: protected Object getValue(Object element) {
172: if (element instanceof ParameterInfo) {
173: ParameterInfo pi = (ParameterInfo) element;
174: return doGetValue(pi);
175: }
176: return null;
177: }
178:
179: public abstract String doGetValue(ParameterInfo pi);
180:
181: protected CellEditor getCellEditor(Object element) {
182: return fTextEditor;
183: }
184:
185: protected boolean canEdit(Object element) {
186: if (element instanceof ParameterInfo) {
187: ParameterInfo pi = (ParameterInfo) element;
188: return fTextEditor != null && pi.isCreateField();
189: }
190: return false;
191: }
192: }
193:
194: private IntroduceParameterObjectRefactoring fRefactoring;
195: private JavaSourceViewer fSignaturePreview;
196: private IDocument fSignaturePreviewDocument = new Document();
197: private Button fLeaveDelegateCheckBox;
198: private Button fDeprecateDelegateCheckBox;
199:
200: public IntroduceParameterObjectInputPage() {
201: super (
202: RefactoringMessages.IntroduceParameterObjectWizard_wizardpage_name);
203: setTitle(RefactoringMessages.IntroduceParameterObjectWizard_wizardpage_title);
204: setDescription(RefactoringMessages.IntroduceParameterObjectWizard_wizardpage_description);
205: }
206:
207: public void createControl(Composite parent) {
208: initializeDialogUnits(parent);
209: fRefactoring = (IntroduceParameterObjectRefactoring) getRefactoring();
210: Composite result = new Composite(parent, SWT.NONE);
211: result.setLayout(new GridLayout(2, false));
212: Group group = createGroup(
213: result,
214: RefactoringMessages.IntroduceParameterObjectWizard_type_group);
215:
216: createClassNameInput(group);
217: createLocationInput(group);
218:
219: createTable(group);
220: createGetterInput(group);
221: createSetterInput(group);
222:
223: group = createGroup(
224: result,
225: RefactoringMessages.IntroduceParameterObjectWizard_method_group);
226: createParameterNameInput(group);
227: createDelegateInput(group);
228: createSignaturePreview(group);
229:
230: validateRefactoring();
231:
232: setControl(result);
233: }
234:
235: private void createParameterNameInput(Group group) {
236: Label l = new Label(group, SWT.NONE);
237: l
238: .setText(RefactoringMessages.IntroduceParameterObjectWizard_parameterfield_label);
239: final Text text = new Text(group, SWT.BORDER);
240: text.setText(fRefactoring.getParameterName());
241: text.addModifyListener(new ModifyListener() {
242:
243: public void modifyText(ModifyEvent e) {
244: fRefactoring.setParameterName(text.getText());
245: updateSignaturePreview();
246: validateRefactoring();
247: }
248:
249: });
250: text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
251: }
252:
253: protected void validateRefactoring() {
254: List names = new ArrayList();
255: boolean oneChecked = false;
256: setMessage(null);
257: setErrorMessage(null);
258: setPageComplete(true);
259: IJavaProject project = fRefactoring.getMethod()
260: .getJavaProject();
261: String sourceLevel = project.getOption(
262: JavaCore.COMPILER_SOURCE, true);
263: String compliance = project.getOption(
264: JavaCore.COMPILER_COMPLIANCE, true);
265: List parameterInfos = fRefactoring.getParameterInfos();
266: for (Iterator iter = parameterInfos.iterator(); iter
267: .hasNext();) {
268: ParameterInfo pi = (ParameterInfo) iter.next();
269: if (names.contains(pi.getNewName())) {
270: setErrorMessage(Messages
271: .format(
272: RefactoringMessages.IntroduceParameterObjectWizard_parametername_check_notunique,
273: pi.getNewName()));
274: setPageComplete(false);
275: return;
276: }
277: names.add(pi.getNewName());
278: IStatus validateIdentifier = JavaConventions
279: .validateIdentifier(pi.getNewName(),
280: sourceLevel, compliance);
281: if (isErrorMessage(validateIdentifier))
282: return;
283: if (pi.isCreateField())
284: oneChecked = true;
285: }
286: if (!oneChecked) {
287: setErrorMessage(RefactoringMessages.IntroduceParameterObjectWizard_parametername_check_atleastoneparameter);
288: setPageComplete(false);
289: return;
290: }
291: IStatus validateJavaTypeName = JavaConventions
292: .validateJavaTypeName(fRefactoring.getClassName(),
293: sourceLevel, compliance);
294: if (isErrorMessage(validateJavaTypeName))
295: return;
296: if (fRefactoring.getClassName().indexOf('.') != -1) {
297: setErrorMessage(RefactoringMessages.IntroduceParameterObjectWizard_dot_not_allowed_error);
298: setPageComplete(false);
299: }
300: if (!"".equals(fRefactoring.getPackage())) { //$NON-NLS-1$
301: IStatus validatePackageName = JavaConventions
302: .validatePackageName(fRefactoring.getPackage(),
303: sourceLevel, compliance);
304: if (isErrorMessage(validatePackageName))
305: return;
306: }
307: try {
308: IType type = project.findType(fRefactoring
309: .getNewTypeName());
310: if (type != null) {
311: StringBuffer packageName = new StringBuffer();
312: JavaElementLabels.getPackageFragmentLabel(type
313: .getPackageFragment(),
314: JavaElementLabels.ALL_DEFAULT, packageName);
315: if (fRefactoring.isCreateAsTopLevel()) {
316: setErrorMessage(Messages
317: .format(
318: RefactoringMessages.IntroduceParameterObjectWizard_type_already_exists_in_package_info,
319: new Object[] {
320: fRefactoring
321: .getClassName(),
322: packageName.toString() }));
323: setPageComplete(false);
324: return;
325: } else {
326: setErrorMessage(Messages
327: .format(
328: RefactoringMessages.IntroduceParameterObjectWizard_parametername_check_alreadyexists,
329: new Object[] {
330: fRefactoring
331: .getClassName(),
332: type
333: .getCompilationUnit()
334: .getElementName() }));
335: setPageComplete(false);
336: return;
337: }
338: }
339: } catch (JavaModelException e) {
340: // Don't care. The error will popup later anyway..
341: }
342: }
343:
344: private boolean isErrorMessage(IStatus validationStatus) {
345: if (!validationStatus.isOK()) {
346: if (validationStatus.getSeverity() == IStatus.ERROR) {
347: setErrorMessage(validationStatus.getMessage());
348: setPageComplete(false);
349: return true;
350: } else {
351: if (validationStatus.getSeverity() == IStatus.INFO)
352: setMessage(validationStatus.getMessage(),
353: IMessageProvider.INFORMATION);
354: else
355: setMessage(validationStatus.getMessage(),
356: IMessageProvider.WARNING);
357: }
358: }
359: return false;
360: }
361:
362: private void createSignaturePreview(Composite composite) {
363: Label previewLabel = new Label(composite, SWT.NONE);
364: previewLabel
365: .setText(RefactoringMessages.IntroduceParameterObjectWizard_signaturepreview_label);
366: GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
367: gridData.horizontalSpan = 2;
368: previewLabel.setLayoutData(gridData);
369:
370: // //XXX: use ViewForm to draw a flat border. Beware of common
371: // problems
372: // with wrapping layouts
373: // //inside GridLayout. GridData must be constrained to force
374: // wrapping.
375: // See bug 9866 et al.
376: // ViewForm border= new ViewForm(composite, SWT.BORDER | SWT.FLAT);
377:
378: IPreferenceStore store = JavaPlugin.getDefault()
379: .getCombinedPreferenceStore();
380: fSignaturePreview = new JavaSourceViewer(composite, null,
381: null, false, SWT.READ_ONLY | SWT.V_SCROLL
382: | SWT.WRAP, store);
383: fSignaturePreview
384: .configure(new JavaSourceViewerConfiguration(
385: JavaPlugin.getDefault().getJavaTextTools()
386: .getColorManager(), store, null,
387: null));
388: fSignaturePreview
389: .getTextWidget()
390: .setFont(
391: JFaceResources
392: .getFont(PreferenceConstants.EDITOR_TEXT_FONT));
393: fSignaturePreview.getTextWidget().setBackground(
394: composite.getBackground());
395: fSignaturePreview.setDocument(fSignaturePreviewDocument);
396: fSignaturePreview.setEditable(false);
397:
398: // Layouting problems with wrapped text: see
399: // https://bugs.eclipse.org/bugs/show_bug.cgi?id=9866
400: Control signaturePreviewControl = fSignaturePreview
401: .getControl();
402: PixelConverter pixelConverter = new PixelConverter(
403: signaturePreviewControl);
404: GridData gdata = new GridData(GridData.FILL_BOTH);
405: gdata.widthHint = pixelConverter
406: .convertWidthInCharsToPixels(50);
407: gdata.heightHint = pixelConverter
408: .convertHeightInCharsToPixels(2);
409: gdata.horizontalSpan = 2;
410: signaturePreviewControl.setLayoutData(gdata);
411:
412: updateSignaturePreview();
413: // //XXX must force JavaSourceViewer text widget to wrap:
414: // border.setContent(signaturePreviewControl);
415: // GridData borderData= new GridData(GridData.FILL_BOTH);
416: // borderData.widthHint= gdata.widthHint;
417: // borderData.heightHint= gdata.heightHint;
418: // border.setLayoutData(borderData);
419: }
420:
421: private void createDelegateInput(Group group) {
422: fLeaveDelegateCheckBox = DelegateUIHelper
423: .generateLeaveDelegateCheckbox(group,
424: getRefactoring(), false);
425: GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
426: gridData.horizontalSpan = 2;
427: fLeaveDelegateCheckBox.setLayoutData(gridData);
428: if (fLeaveDelegateCheckBox != null) {
429: fDeprecateDelegateCheckBox = new Button(group,
430: SWT.CHECK);
431: GridData data = new GridData();
432: data.horizontalAlignment = GridData.FILL;
433: GridLayout layout = (GridLayout) group.getLayout();
434: data.horizontalIndent = (layout.marginWidth + fDeprecateDelegateCheckBox
435: .computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
436: data.horizontalSpan = 2;
437: fDeprecateDelegateCheckBox.setLayoutData(data);
438: fDeprecateDelegateCheckBox.setText(DelegateUIHelper
439: .getDeprecateDelegateCheckBoxTitle());
440: final ChangeSignatureRefactoring refactoring = fRefactoring;
441: fDeprecateDelegateCheckBox
442: .setSelection(DelegateUIHelper
443: .loadDeprecateDelegateSetting(refactoring));
444: refactoring
445: .setDeprecateDelegates(fDeprecateDelegateCheckBox
446: .getSelection());
447: fDeprecateDelegateCheckBox
448: .addSelectionListener(new SelectionAdapter() {
449: public void widgetSelected(SelectionEvent e) {
450: refactoring
451: .setDeprecateDelegates(fDeprecateDelegateCheckBox
452: .getSelection());
453: validateRefactoring();
454: }
455: });
456: fDeprecateDelegateCheckBox
457: .setEnabled(fLeaveDelegateCheckBox
458: .getSelection());
459: fLeaveDelegateCheckBox
460: .addSelectionListener(new SelectionAdapter() {
461: public void widgetSelected(SelectionEvent e) {
462: fDeprecateDelegateCheckBox
463: .setEnabled(fLeaveDelegateCheckBox
464: .getSelection());
465: }
466: });
467: }
468: }
469:
470: private Group createGroup(Composite result, String caption) {
471: Group group = new Group(result, SWT.None);
472: group.setLayout(new GridLayout(2, false));
473: group.setText(caption);
474: GridData gridData = new GridData(GridData.FILL_BOTH);
475: gridData.horizontalSpan = 2;
476: group.setLayoutData(gridData);
477: return group;
478: }
479:
480: private void createLocationInput(Composite parent) {
481: Label l = new Label(parent, SWT.NONE);
482: l
483: .setText(RefactoringMessages.IntroduceParameterObjectWizard_destination_label);
484:
485: Composite composite = new Composite(parent, SWT.None);
486: GridLayout gridLayout = new GridLayout(2, false);
487: gridLayout.marginHeight = 0;
488: gridLayout.marginWidth = 0;
489: composite.setLayout(gridLayout);
490: GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
491: composite.setLayoutData(gridData);
492:
493: final Button topLvlRadio = new Button(composite, SWT.RADIO);
494: topLvlRadio
495: .setText(RefactoringMessages.IntroduceParameterObjectWizard_createastoplevel_radio);
496: topLvlRadio.addSelectionListener(new SelectionAdapter() {
497: public void widgetSelected(SelectionEvent e) {
498: boolean fAsTopLevel = topLvlRadio.getSelection();
499: fRefactoring.setCreateAsTopLevel(fAsTopLevel);
500: updateSignaturePreview();
501: validateRefactoring();
502: }
503: });
504:
505: Button nestedRadio = new Button(composite, SWT.RADIO);
506: nestedRadio
507: .setText(Messages
508: .format(
509: RefactoringMessages.IntroduceParameterObjectWizard_createasnestedclass_radio,
510: fRefactoring.getContainingClass()
511: .getName()));
512: boolean createAsTopLevel = getBooleanSetting(
513: CREATE_TOP_LEVEL_SETTING, fRefactoring
514: .isCreateAsTopLevel());
515: fRefactoring.setCreateAsTopLevel(createAsTopLevel);
516: topLvlRadio.setSelection(createAsTopLevel);
517: nestedRadio.setSelection(!createAsTopLevel);
518:
519: }
520:
521: private void createTable(Composite parent) {
522: Composite result = new Composite(parent, SWT.NONE);
523: GridLayout layout = new GridLayout(2, false);
524: layout.marginHeight = 0;
525: layout.marginWidth = 0;
526: result.setLayout(layout);
527: GridData gridData = new GridData(GridData.FILL_BOTH);
528: gridData.horizontalSpan = 2;
529: result.setLayoutData(gridData);
530:
531: Label l = new Label(result, SWT.NONE);
532: l
533: .setText(RefactoringMessages.IntroduceParameterObjectWizard_fields_selection_label);
534: gridData = new GridData(GridData.FILL_HORIZONTAL);
535: gridData.horizontalSpan = 2;
536: gridData.verticalIndent = 5;
537: l.setLayoutData(gridData);
538:
539: TableLayoutComposite layoutComposite = new TableLayoutComposite(
540: result, SWT.NONE);
541: layoutComposite.addColumnData(new ColumnWeightData(40,
542: convertWidthInCharsToPixels(20), true));
543: layoutComposite.addColumnData(new ColumnWeightData(60,
544: convertWidthInCharsToPixels(20), true));
545: final CheckboxTableViewer tv = CheckboxTableViewer
546: .newCheckList(layoutComposite, SWT.V_SCROLL
547: | SWT.H_SCROLL | SWT.BORDER
548: | SWT.FULL_SELECTION | SWT.MULTI);
549: tv
550: .setContentProvider(new ParameterObjectCreatorContentProvider());
551: createColumns(tv);
552:
553: Table table = tv.getTable();
554: table.setLinesVisible(true);
555: table.setHeaderVisible(true);
556: gridData = new GridData(GridData.FILL_BOTH);
557: table.setLayoutData(gridData);
558: tv.setInput(fRefactoring);
559: List parameterInfos = fRefactoring.getParameterInfos();
560: for (Iterator iter = parameterInfos.iterator(); iter
561: .hasNext();) {
562: ParameterInfo pi = (ParameterInfo) iter.next();
563: tv.setChecked(pi, pi.isCreateField());
564: }
565: tv.refresh(true);
566: gridData = new GridData(GridData.FILL_BOTH);
567: gridData.heightHint = SWTUtil.getTableHeightHint(table,
568: parameterInfos.size());
569: layoutComposite.setLayoutData(gridData);
570: Composite controls = new Composite(result, SWT.NONE);
571: gridData = new GridData(GridData.FILL, GridData.FILL,
572: false, false);
573: controls.setLayoutData(gridData);
574: GridLayout gridLayout = new GridLayout();
575: gridLayout.marginHeight = 0;
576: gridLayout.marginWidth = 0;
577: controls.setLayout(gridLayout);
578:
579: final Button upButton = new Button(controls, SWT.NONE);
580: upButton
581: .setText(RefactoringMessages.IntroduceParameterObjectWizard_moveentryup_button);
582: gridData = new GridData(GridData.FILL_HORIZONTAL);
583: upButton.setLayoutData(gridData);
584: SWTUtil.setButtonDimensionHint(upButton);
585: upButton.setEnabled(false);
586:
587: final Button downButton = new Button(controls, SWT.NONE);
588: downButton
589: .setText(RefactoringMessages.IntroduceParameterObjectWizard_moventrydown_button);
590: gridData = new GridData(GridData.FILL_HORIZONTAL);
591: downButton.setLayoutData(gridData);
592: SWTUtil.setButtonDimensionHint(downButton);
593: downButton.setEnabled(false);
594:
595: addSpacer(controls);
596:
597: final Button editButton = new Button(controls, SWT.NONE);
598: editButton
599: .setText(RefactoringMessages.IntroduceParameterObjectWizard_edit_button);
600: editButton.setLayoutData(new GridData(
601: GridData.FILL_HORIZONTAL));
602: editButton.setEnabled(!tv.getSelection().isEmpty());
603: SWTUtil.setButtonDimensionHint(editButton);
604: editButton.addSelectionListener(new SelectionAdapter() {
605: public void widgetSelected(SelectionEvent e) {
606: ISelection selection = tv.getSelection();
607: if (selection instanceof IStructuredSelection) {
608: IStructuredSelection ss = (IStructuredSelection) selection;
609: ParameterInfo selected = (ParameterInfo) ss
610: .getFirstElement();
611: String message = RefactoringMessages.IntroduceParameterObjectWizard_fieldname_message;
612: String title = RefactoringMessages.IntroduceParameterObjectWizard_fieldname_title;
613: InputDialog inputDialog = new InputDialog(
614: getShell(), title, message, selected
615: .getNewName(),
616: new IInputValidator() {
617:
618: public String isValid(String newText) {
619: IStatus status = JavaConventionsUtil
620: .validateIdentifier(
621: newText,
622: fRefactoring
623: .getCompilationUnit());
624: if (!status.isOK())
625: return status.getMessage();
626: return null;
627: }
628:
629: });
630: if (inputDialog.open() == Window.OK) {
631: selected.setNewName(inputDialog.getValue());
632: tv.refresh(selected);
633: updateSignaturePreview();
634: }
635:
636: }
637: }
638: });
639:
640: downButton.addSelectionListener(new SelectionAdapter() {
641: public void widgetSelected(SelectionEvent e) {
642: int[] indices = tv.getTable().getSelectionIndices();
643: Arrays.sort(indices);
644: for (int i = indices.length - 1; i >= 0; i--) {
645: int idx = indices[i];
646: ParameterInfo pi = (ParameterInfo) tv
647: .getElementAt(idx);
648: fRefactoring.moveFieldDown(pi);
649: }
650: tv.refresh();
651: updateButtons(tv, upButton, downButton, editButton);
652: }
653:
654: });
655: upButton.addSelectionListener(new SelectionAdapter() {
656: public void widgetSelected(SelectionEvent e) {
657: int[] indices = tv.getTable().getSelectionIndices();
658: Arrays.sort(indices);
659: for (int i = 0; i < indices.length; i++) {
660: int idx = indices[i];
661: ParameterInfo pi = (ParameterInfo) tv
662: .getElementAt(idx);
663: fRefactoring.moveFieldUp(pi);
664: }
665: tv.refresh();
666: updateButtons(tv, upButton, downButton, editButton);
667: }
668:
669: });
670: tv.addCheckStateListener(new ICheckStateListener() {
671: Map fLastNames = new HashMap();
672:
673: public void checkStateChanged(
674: CheckStateChangedEvent event) {
675: ParameterInfo element = (ParameterInfo) event
676: .getElement();
677: element.setCreateField(event.getChecked());
678: if (element.isCreateField()) {
679: String lastName = (String) fLastNames
680: .get(element);
681: if (lastName == null) {
682: lastName = fRefactoring
683: .getFieldName(element);
684: }
685: element.setNewName(lastName);
686: } else {
687: fLastNames.put(element, element.getNewName());
688: element.setNewName(element.getOldName());
689: }
690: tv.update(element, null);
691: updateButtons(tv, upButton, downButton, editButton);
692: validateRefactoring();
693: }
694:
695: });
696: tv
697: .addSelectionChangedListener(new ISelectionChangedListener() {
698: public void selectionChanged(
699: SelectionChangedEvent event) {
700: updateButtons(tv, upButton, downButton,
701: editButton);
702: }
703: });
704: }
705:
706: private void addSpacer(Composite parent) {
707: Label label = new Label(parent, SWT.NONE);
708: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
709: gd.heightHint = 5;
710: label.setLayoutData(gd);
711: }
712:
713: private void createColumns(final CheckboxTableViewer tv) {
714: TextCellEditor cellEditor = new TextCellEditor(tv
715: .getTable());
716:
717: TableViewerColumn viwerColumn = new TableViewerColumn(tv,
718: SWT.LEAD);
719: viwerColumn
720: .setLabelProvider(new ParameterInfoLabelProvider() {
721: protected String doGetValue(ParameterInfo pi) {
722: return pi.getNewTypeName();
723: }
724: });
725:
726: TableColumn column = viwerColumn.getColumn();
727: column
728: .setText(RefactoringMessages.IntroduceParameterObjectWizard_type_column);
729: viwerColumn = new TableViewerColumn(tv, SWT.LEAD);
730: viwerColumn
731: .setLabelProvider(new ParameterInfoLabelProvider() {
732: protected String doGetValue(ParameterInfo pi) {
733: return pi.getNewName();
734: }
735: });
736: viwerColumn
737: .setEditingSupport(new ParameterInfoEditingSupport(
738: cellEditor, tv) {
739: public String doGetValue(ParameterInfo pi) {
740: return pi.getNewName();
741: }
742:
743: public void doSetValue(ParameterInfo pi,
744: String string) {
745: pi.setNewName(string);
746: }
747: });
748: column = viwerColumn.getColumn();
749: column
750: .setText(RefactoringMessages.IntroduceParameterObjectWizard_name_column);
751: }
752:
753: private void createGetterInput(Composite result) {
754: Composite buttons = new Composite(result, SWT.NONE);
755: GridLayout gridLayout = new GridLayout(2, true);
756: gridLayout.marginHeight = 0;
757: gridLayout.marginWidth = 0;
758: buttons.setLayout(gridLayout);
759: GridData gridData = new GridData();
760: gridData.horizontalSpan = 2;
761: buttons.setLayoutData(gridData);
762:
763: final Button button = new Button(buttons, SWT.CHECK);
764: button
765: .setText(RefactoringMessages.IntroduceParameterObjectWizard_creategetter_checkbox);
766: button.addSelectionListener(new SelectionAdapter() {
767:
768: public void widgetSelected(SelectionEvent e) {
769: fRefactoring.setCreateGetter(button.getSelection());
770: validateRefactoring();
771: }
772:
773: });
774: button.setSelection(getBooleanSetting(
775: CREATE_GETTERS_SETTING, fRefactoring
776: .isCreateGetter()));
777: fRefactoring.setCreateGetter(button.getSelection());
778: gridData = new GridData();
779: button.setLayoutData(gridData);
780: }
781:
782: private void createSetterInput(Composite result) {
783: final Button button = new Button(result, SWT.CHECK);
784: button
785: .setText(RefactoringMessages.IntroduceParameterObjectWizard_createsetter_checkbox);
786: button.addSelectionListener(new SelectionAdapter() {
787:
788: public void widgetSelected(SelectionEvent e) {
789: fRefactoring.setCreateSetter(button.getSelection());
790: validateRefactoring();
791: }
792:
793: });
794: button.setSelection(getBooleanSetting(
795: CREATE_SETTERS_SETTING, fRefactoring
796: .isCreateSetter()));
797: fRefactoring.setCreateSetter(button.getSelection());
798: GridData gridData = new GridData();
799: button.setLayoutData(gridData);
800: }
801:
802: private void createClassNameInput(Composite result) {
803: Label label = new Label(result, SWT.LEAD);
804: label
805: .setText(RefactoringMessages.IntroduceParameterObjectWizard_classnamefield_label);
806: final Text text = new Text(result, SWT.SINGLE | SWT.BORDER);
807: text.setText(fRefactoring.getClassName());
808: text.selectAll();
809: text.setFocus();
810: text.addModifyListener(new ModifyListener() {
811:
812: public void modifyText(ModifyEvent e) {
813: fRefactoring.setClassName(text.getText());
814: updateSignaturePreview();
815: validateRefactoring();
816: }
817:
818: });
819: text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
820: }
821:
822: private void updateButtons(final TableViewer tv,
823: Button upButton, Button downButton, Button editButton) {
824: IStructuredSelection selection = (IStructuredSelection) tv
825: .getSelection();
826: ParameterInfo firstElement = (ParameterInfo) selection
827: .getFirstElement();
828: if (selection.isEmpty()) {
829: upButton.setEnabled(false);
830: downButton.setEnabled(false);
831: editButton.setEnabled(false);
832: } else {
833: int[] selectionIndex = tv.getTable()
834: .getSelectionIndices();
835: upButton.setEnabled(selectionIndex[0] != 0);
836: downButton
837: .setEnabled(selectionIndex[selectionIndex.length - 1] != tv
838: .getTable().getItemCount() - 1);
839: editButton.setEnabled(firstElement.isCreateField()
840: && selectionIndex.length == 1);
841: }
842: fRefactoring.updateParameterPosition();
843: updateSignaturePreview();
844: }
845:
846: private void updateSignaturePreview() {
847: try {
848: int top = fSignaturePreview.getTextWidget()
849: .getTopPixel();
850: fSignaturePreviewDocument.set(fRefactoring
851: .getNewMethodSignature());
852: fSignaturePreview.getTextWidget().setTopPixel(top);
853: } catch (JavaModelException e) {
854: ExceptionHandler
855: .handle(
856: e,
857: RefactoringMessages.IntroduceParameterObjectWizard_error_title,
858: RefactoringMessages.IntroduceParameterObjectWizard_error_description);
859: }
860: }
861:
862: public void dispose() {
863: DelegateUIHelper
864: .saveDeprecateDelegateSetting(fDeprecateDelegateCheckBox);
865: DelegateUIHelper
866: .saveLeaveDelegateSetting(fLeaveDelegateCheckBox);
867: IDialogSettings settings = getRefactoringSettings();
868: settings
869: .put(
870: IntroduceParameterObjectInputPage.CREATE_GETTERS_SETTING,
871: fRefactoring.isCreateGetter());
872: settings
873: .put(
874: IntroduceParameterObjectInputPage.CREATE_SETTERS_SETTING,
875: fRefactoring.isCreateSetter());
876: settings
877: .put(
878: IntroduceParameterObjectInputPage.CREATE_TOP_LEVEL_SETTING,
879: fRefactoring.isCreateAsTopLevel());
880: super .dispose();
881: }
882:
883: protected boolean getBooleanSetting(String key,
884: boolean defaultValue) {
885: String update = getRefactoringSettings().get(key);
886: if (update != null)
887: return Boolean.valueOf(update).booleanValue();
888: else
889: return defaultValue;
890: }
891: }
892:
893: }
|