001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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 org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.events.ModifyEvent;
016: import org.eclipse.swt.events.ModifyListener;
017: import org.eclipse.swt.events.SelectionAdapter;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.graphics.Image;
020: import org.eclipse.swt.layout.GridData;
021: import org.eclipse.swt.layout.GridLayout;
022: import org.eclipse.swt.widgets.Button;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Label;
025: import org.eclipse.swt.widgets.Table;
026: import org.eclipse.swt.widgets.TableColumn;
027: import org.eclipse.swt.widgets.Text;
028:
029: import org.eclipse.jface.dialogs.Dialog;
030: import org.eclipse.jface.viewers.ArrayContentProvider;
031: import org.eclipse.jface.viewers.ColumnWeightData;
032: import org.eclipse.jface.viewers.ISelectionChangedListener;
033: import org.eclipse.jface.viewers.IStructuredSelection;
034: import org.eclipse.jface.viewers.ITableLabelProvider;
035: import org.eclipse.jface.viewers.SelectionChangedEvent;
036: import org.eclipse.jface.viewers.StructuredSelection;
037: import org.eclipse.jface.viewers.TableViewer;
038:
039: import org.eclipse.ui.PlatformUI;
040:
041: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
042: import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
043: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
044:
045: import org.eclipse.jdt.core.dom.IVariableBinding;
046:
047: import org.eclipse.jdt.internal.corext.dom.Bindings;
048: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
049: import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor;
050: import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodRefactoring;
051: import org.eclipse.jdt.internal.corext.util.Messages;
052:
053: import org.eclipse.jdt.ui.JavaElementLabels;
054:
055: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
056: import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
057: import org.eclipse.jdt.internal.ui.util.SWTUtil;
058: import org.eclipse.jdt.internal.ui.util.TableLayoutComposite;
059: import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
060:
061: /**
062: * Refactoring wizard for the 'move instance method' refactoring.
063: */
064: public final class MoveInstanceMethodWizard extends RefactoringWizard {
065:
066: /**
067: * The input wizard page of the 'move instance method' refactoring.
068: */
069: public final class MoveInstanceMethodPage extends
070: UserInputWizardPage {
071:
072: /** The page name */
073: protected static final String PAGE_NAME = "MoveInstanceMethodPage"; //$NON-NLS-1$
074:
075: /** The "leave delegate" checkbox */
076: protected Button fLeaveDelegateCheckBox = null;
077:
078: /** The "deprecate delegate" checkbox */
079: protected Button fDeprecateDelegateCheckBox = null;
080:
081: /** The method name text field */
082: protected Text fMethodNameField = null;
083:
084: /** The current method name status */
085: protected RefactoringStatus fMethodNameStatus = new RefactoringStatus();
086:
087: /** The target name text field */
088: protected Text fTargetNameField = null;
089:
090: /** The target name label */
091: protected Label fTargetNameLabel = null;
092:
093: /** The current target name status */
094: protected RefactoringStatus fTargetNameStatus = new RefactoringStatus();
095:
096: /** The current target type status */
097: protected RefactoringStatus fTargetTypeStatus = new RefactoringStatus();
098:
099: /**
100: * Creates a new move instance method page.
101: */
102: public MoveInstanceMethodPage() {
103: super (PAGE_NAME);
104: }
105:
106: /*
107: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
108: */
109: public void createControl(final Composite parent) {
110: Assert.isNotNull(parent);
111: final Composite control = new Composite(parent, SWT.NONE);
112: setControl(control);
113:
114: final GridLayout layout = new GridLayout();
115: layout.numColumns = 2;
116: control.setLayout(layout);
117:
118: Label label = new Label(control, SWT.SINGLE);
119: label
120: .setText(Messages
121: .format(
122: RefactoringMessages.MoveInstanceMethodPage_New_receiver,
123: JavaElementLabels
124: .getElementLabel(
125: fProcessor
126: .getMethod(),
127: JavaElementLabels.ALL_DEFAULT
128: | JavaElementLabels.M_PRE_RETURNTYPE
129: | JavaElementLabels.M_PRE_TYPE_PARAMETERS
130: | JavaElementLabels.M_PARAMETER_NAMES)));
131:
132: GridData data = new GridData();
133: data.horizontalSpan = 2;
134: label.setLayoutData(data);
135:
136: final TableLayoutComposite composite = new TableLayoutComposite(
137: control, SWT.NULL);
138: composite.addColumnData(new ColumnWeightData(40, true));
139: composite.addColumnData(new ColumnWeightData(60, true));
140:
141: final Table table = new Table(composite, SWT.SINGLE
142: | SWT.BORDER | SWT.FULL_SELECTION);
143: table.setHeaderVisible(true);
144: table.setLinesVisible(false);
145:
146: TableColumn column = new TableColumn(table, SWT.NONE);
147: column
148: .setText(RefactoringMessages.MoveInstanceMethodPage_Name);
149: column.setResizable(true);
150:
151: column = new TableColumn(table, SWT.NONE);
152: column
153: .setText(RefactoringMessages.MoveInstanceMethodPage_Type);
154: column.setResizable(true);
155:
156: final TableViewer viewer = new TableViewer(table);
157: viewer.setContentProvider(new ArrayContentProvider());
158: viewer.setLabelProvider(new TargetLabelProvider());
159:
160: final IVariableBinding[] candidateTargets = fProcessor
161: .getCandidateTargets();
162: viewer.setInput(candidateTargets);
163: final IVariableBinding[] possibleTargets = fProcessor
164: .getPossibleTargets();
165: viewer.setSelection(new StructuredSelection(
166: new Object[] { possibleTargets[0] }));
167:
168: viewer
169: .addSelectionChangedListener(new ISelectionChangedListener() {
170:
171: public final void selectionChanged(
172: final SelectionChangedEvent event) {
173: final Object element = ((IStructuredSelection) event
174: .getSelection()).getFirstElement();
175: if (element instanceof IVariableBinding) {
176: final IVariableBinding target = (IVariableBinding) element;
177: final IVariableBinding[] targets = fProcessor
178: .getPossibleTargets();
179: boolean success = false;
180: for (int index = 0; index < targets.length; index++) {
181: if (Bindings.equals(target,
182: targets[index])) {
183: handleTargetChanged(target);
184: success = true;
185: break;
186: }
187: }
188: if (!success)
189: fTargetTypeStatus = RefactoringStatus
190: .createWarningStatus(Messages
191: .format(
192: RefactoringMessages.MoveInstanceMethodPage_invalid_target,
193: target
194: .getName()));
195: else
196: fTargetTypeStatus = new RefactoringStatus();
197: handleStatusChanged();
198: }
199: }
200: });
201:
202: data = new GridData(GridData.FILL_HORIZONTAL
203: | GridData.GRAB_HORIZONTAL);
204: data.heightHint = SWTUtil.getTableHeightHint(table, 7);
205: data.horizontalSpan = 2;
206: composite.setLayoutData(data);
207:
208: label = new Label(control, SWT.SINGLE);
209: label
210: .setText(RefactoringMessages.MoveInstanceMethodPage_Method_name);
211: label.setLayoutData(new GridData());
212:
213: fMethodNameField = new Text(control, SWT.SINGLE
214: | SWT.BORDER);
215: fMethodNameField.setText(fProcessor.getMethodName());
216: fMethodNameField.selectAll();
217: fMethodNameField.setLayoutData(new GridData(
218: GridData.FILL_HORIZONTAL));
219: fMethodNameField.setFocus();
220: fMethodNameField.addModifyListener(new ModifyListener() {
221:
222: public final void modifyText(final ModifyEvent event) {
223: fMethodNameStatus = fProcessor
224: .setMethodName(fMethodNameField.getText());
225: handleStatusChanged();
226: }
227: });
228: TextFieldNavigationHandler.install(fMethodNameField);
229:
230: fTargetNameLabel = new Label(control, SWT.SINGLE);
231: fTargetNameLabel
232: .setText(RefactoringMessages.MoveInstanceMethodPage_Target_name);
233: fTargetNameLabel.setLayoutData(new GridData());
234:
235: fTargetNameField = new Text(control, SWT.SINGLE
236: | SWT.BORDER);
237: final String name = fProcessor.getTargetName();
238: if (name != null && name.length() > 0)
239: fTargetNameField.setText(fProcessor.getTargetName());
240: else {
241: setPageComplete(RefactoringStatus
242: .createInfoStatus(RefactoringCoreMessages.Checks_Choose_name));
243: setPageComplete(false);
244: }
245: fTargetNameField.setLayoutData(new GridData(
246: GridData.FILL_HORIZONTAL));
247: fTargetNameField.addModifyListener(new ModifyListener() {
248:
249: public final void modifyText(final ModifyEvent event) {
250: fTargetNameStatus = fProcessor
251: .setTargetName(fTargetNameField.getText());
252: handleStatusChanged();
253: }
254: });
255: TextFieldNavigationHandler.install(fTargetNameField);
256:
257: label = new Label(control, SWT.NONE);
258:
259: data = new GridData();
260: data.horizontalSpan = 2;
261: label.setLayoutData(data);
262:
263: fLeaveDelegateCheckBox = DelegateUIHelper
264: .generateLeaveDelegateCheckbox(control,
265: getRefactoring(), false);
266: if (fLeaveDelegateCheckBox != null) {
267: fDeprecateDelegateCheckBox = new Button(control,
268: SWT.CHECK);
269: data = new GridData();
270: data.horizontalAlignment = GridData.FILL;
271: data.horizontalIndent = (layout.marginWidth + fDeprecateDelegateCheckBox
272: .computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
273: data.horizontalSpan = 2;
274: fDeprecateDelegateCheckBox.setLayoutData(data);
275: fDeprecateDelegateCheckBox.setText(DelegateUIHelper
276: .getDeprecateDelegateCheckBoxTitle());
277: fDeprecateDelegateCheckBox
278: .setSelection(DelegateUIHelper
279: .loadDeprecateDelegateSetting(fProcessor));
280: fProcessor
281: .setDeprecateDelegates(fDeprecateDelegateCheckBox
282: .getSelection());
283: fDeprecateDelegateCheckBox
284: .addSelectionListener(new SelectionAdapter() {
285: public void widgetSelected(SelectionEvent e) {
286: fProcessor
287: .setDeprecateDelegates(fDeprecateDelegateCheckBox
288: .getSelection());
289: }
290: });
291: fDeprecateDelegateCheckBox
292: .setEnabled(fLeaveDelegateCheckBox
293: .getSelection());
294: fLeaveDelegateCheckBox
295: .addSelectionListener(new SelectionAdapter() {
296: public void widgetSelected(SelectionEvent e) {
297: fDeprecateDelegateCheckBox
298: .setEnabled(fLeaveDelegateCheckBox
299: .getSelection());
300: }
301: });
302: }
303:
304: fProcessor.setInlineDelegator(!fLeaveDelegateCheckBox
305: .getSelection());
306: fProcessor.setRemoveDelegator(!fLeaveDelegateCheckBox
307: .getSelection());
308:
309: handleTargetChanged(possibleTargets[0]);
310:
311: Dialog.applyDialogFont(control);
312: PlatformUI.getWorkbench().getHelpSystem().setHelp(
313: getControl(),
314: IJavaHelpContextIds.MOVE_MEMBERS_WIZARD_PAGE);
315: }
316:
317: public void dispose() {
318: DelegateUIHelper
319: .saveLeaveDelegateSetting(fLeaveDelegateCheckBox);
320: DelegateUIHelper
321: .saveDeprecateDelegateSetting(fDeprecateDelegateCheckBox);
322: super .dispose();
323: }
324:
325: /**
326: * Handles the status changed event.
327: */
328: protected final void handleStatusChanged() {
329: final RefactoringStatus status = new RefactoringStatus();
330: status.merge(fMethodNameStatus);
331: status.merge(fTargetNameStatus);
332: status.merge(fTargetTypeStatus);
333: if (!fTargetTypeStatus.isOK())
334: setPageComplete(false);
335: else
336: setPageComplete(status);
337: }
338:
339: /**
340: * Handles the target changed event.
341: *
342: * @param target the changed target
343: */
344: protected final void handleTargetChanged(
345: final IVariableBinding target) {
346: Assert.isNotNull(target);
347: fProcessor.setTarget(target);
348: fTargetNameField.setEnabled(fProcessor.needsTargetNode());
349: fTargetNameLabel.setEnabled(fProcessor.needsTargetNode());
350: }
351: }
352:
353: /**
354: * Table label provider for the target selection table.
355: */
356: public static class TargetLabelProvider extends
357: BindingLabelProvider implements ITableLabelProvider {
358:
359: /*
360: * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
361: */
362: public Image getColumnImage(final Object element,
363: final int column) {
364: if (column == 0)
365: return getImage(element);
366: return null;
367: }
368:
369: /*
370: * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
371: */
372: public String getColumnText(final Object element,
373: final int column) {
374: final IVariableBinding binding = (IVariableBinding) element;
375: switch (column) {
376: case 0:
377: return getText(binding);
378: case 1:
379: return getText(binding.getType());
380: default:
381: return null;
382: }
383: }
384: }
385:
386: /** The default create delegator setting */
387: protected static boolean DEFAULT_CREATE_DELEGATOR_SETTING = false;
388:
389: /** The default deprecate delegator setting */
390: protected static boolean DEFAULT_DEPRECATE_DELEGATOR_SETTING = false;
391:
392: /** The associated move instance method processor */
393: protected final MoveInstanceMethodProcessor fProcessor;
394:
395: /**
396: * Creates a new move instance method wizard.
397: *
398: * @param refactoring the refactoring to host
399: */
400: public MoveInstanceMethodWizard(
401: final MoveInstanceMethodRefactoring refactoring) {
402: super (refactoring, DIALOG_BASED_USER_INTERFACE);
403: fProcessor = refactoring.getMoveMethodProcessor();
404: setDefaultPageTitle(RefactoringMessages.MoveInstanceMethodWizard_Move_Method);
405: }
406:
407: /*
408: * @see RefactoringWizard#addUserInputPages
409: */
410: protected void addUserInputPages() {
411: addPage(new MoveInstanceMethodPage());
412: }
413: }
|