1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring;
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.graphics.Image;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Table;
25 import org.eclipse.swt.widgets.TableColumn;
26 import org.eclipse.swt.widgets.Text;
28 import org.eclipse.core.runtime.Assert;
30 import org.eclipse.jface.dialogs.Dialog;
31 import org.eclipse.jface.layout.TableColumnLayout;
32 import org.eclipse.jface.viewers.ArrayContentProvider;
33 import org.eclipse.jface.viewers.ColumnWeightData;
34 import org.eclipse.jface.viewers.ISelectionChangedListener;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.viewers.ITableLabelProvider;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.StructuredSelection;
39 import org.eclipse.jface.viewers.TableViewer;
41 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ltk.core.refactoring.Refactoring;
44 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
45 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
46 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
48 import org.eclipse.jdt.core.dom.IVariableBinding;
50 import org.eclipse.jdt.internal.corext.dom.Bindings;
51 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
52 import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor;
53 import org.eclipse.jdt.internal.corext.util.Messages;
55 import org.eclipse.jdt.ui.JavaElementLabels;
57 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
58 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
59 import org.eclipse.jdt.internal.ui.util.SWTUtil;
60 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
61 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
64 * Refactoring wizard for the 'move instance method' refactoring.
66 public final class MoveInstanceMethodWizard extends RefactoringWizard {
69 * The input wizard page of the 'move instance method' refactoring.
71 public final class MoveInstanceMethodPage extends UserInputWizardPage {
74 protected static final String PAGE_NAME= "MoveInstanceMethodPage"; //$NON-NLS-1$
76 /** The "leave delegate" checkbox */
77 protected Button fLeaveDelegateCheckBox= null;
79 /** The "deprecate delegate" checkbox */
80 protected Button fDeprecateDelegateCheckBox= null;
82 /** The method name text field */
83 protected Text fMethodNameField= null;
85 /** The current method name status */
86 protected RefactoringStatus fMethodNameStatus= new RefactoringStatus();
88 /** The target name text field */
89 protected Text fTargetNameField= null;
91 /** The target name label */
92 protected Label fTargetNameLabel= null;
94 /** The current target name status */
95 protected RefactoringStatus fTargetNameStatus= new RefactoringStatus();
97 /** The current target type status */
98 protected RefactoringStatus fTargetTypeStatus= new RefactoringStatus();
101 * Creates a new move instance method page.
103 public MoveInstanceMethodPage() {
108 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
110 public void createControl(final Composite parent) {
111 Assert.isNotNull(parent);
112 final Composite control= new Composite(parent, SWT.NONE);
115 final GridLayout layout= new GridLayout();
116 layout.numColumns= 2;
117 control.setLayout(layout);
119 Label label= new Label(control, SWT.SINGLE);
120 label.setText(Messages.format(RefactoringMessages.MoveInstanceMethodPage_New_receiver, JavaElementLabels.getElementLabel(fProcessor.getMethod(), JavaElementLabels.ALL_DEFAULT | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PRE_TYPE_PARAMETERS | JavaElementLabels.M_PARAMETER_NAMES)));
122 GridData data= new GridData();
123 data.horizontalSpan= 2;
124 label.setLayoutData(data);
126 final Composite composite= new Composite(control, SWT.NULL);
128 final Table table= new Table(composite, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
129 table.setHeaderVisible(true);
130 table.setLinesVisible(false);
132 TableColumn columnType= new TableColumn(table, SWT.NONE);
133 columnType.setText(RefactoringMessages.MoveInstanceMethodPage_Type);
134 columnType.setResizable(true);
136 TableColumn columnName= new TableColumn(table, SWT.NONE);
137 columnName.setText(RefactoringMessages.MoveInstanceMethodPage_Receiver);
138 columnName.setResizable(true);
140 TableColumnLayout tableColumnLayout= new TableColumnLayout();
141 composite.setLayout(tableColumnLayout);
142 tableColumnLayout.setColumnData(columnType, new ColumnWeightData(60, true));
143 tableColumnLayout.setColumnData(columnName, new ColumnWeightData(40, true));
145 final TableViewer viewer= new TableViewer(table);
146 viewer.setContentProvider(new ArrayContentProvider());
147 viewer.setLabelProvider(new TargetLabelProvider());
149 final IVariableBinding[] candidateTargets= fProcessor.getCandidateTargets();
150 viewer.setInput(candidateTargets);
151 final IVariableBinding[] possibleTargets= fProcessor.getPossibleTargets();
152 viewer.setSelection(new StructuredSelection(new Object[] { possibleTargets[0]}));
154 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
156 public final void selectionChanged(final SelectionChangedEvent event) {
157 final Object element= ((IStructuredSelection) event.getSelection()).getFirstElement();
158 if (element instanceof IVariableBinding) {
159 final IVariableBinding target= (IVariableBinding) element;
160 final IVariableBinding[] targets= fProcessor.getPossibleTargets();
161 boolean success= false;
162 for (int index= 0; index < targets.length; index++) {
163 if (Bindings.equals(target, targets[index])) {
164 handleTargetChanged(target);
170 fTargetTypeStatus= RefactoringStatus.createWarningStatus(Messages.format(RefactoringMessages.MoveInstanceMethodPage_invalid_target, BasicElementLabels.getJavaElementName(target.getName())));
172 fTargetTypeStatus= new RefactoringStatus();
173 handleStatusChanged();
178 data= new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
179 data.heightHint= SWTUtil.getTableHeightHint(table, 7);
180 data.widthHint= convertWidthInCharsToPixels(30);
181 data.horizontalSpan= 2;
182 composite.setLayoutData(data);
184 label= new Label(control, SWT.SINGLE);
185 label.setText(RefactoringMessages.MoveInstanceMethodPage_Method_name);
186 label.setLayoutData(new GridData());
188 fMethodNameField= new Text(control, SWT.SINGLE | SWT.BORDER);
189 fMethodNameField.setText(fProcessor.getMethodName());
190 fMethodNameField.selectAll();
191 fMethodNameField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
192 fMethodNameField.setFocus();
193 fMethodNameField.addModifyListener(new ModifyListener() {
195 public final void modifyText(final ModifyEvent event) {
196 fMethodNameStatus= fProcessor.setMethodName(fMethodNameField.getText());
197 handleStatusChanged();
200 TextFieldNavigationHandler.install(fMethodNameField);
202 fTargetNameLabel= new Label(control, SWT.SINGLE);
203 fTargetNameLabel.setText(RefactoringMessages.MoveInstanceMethodPage_Target_name);
204 fTargetNameLabel.setLayoutData(new GridData());
206 fTargetNameField= new Text(control, SWT.SINGLE | SWT.BORDER);
207 final String name= fProcessor.getTargetName();
208 if (name != null && name.length() > 0)
209 fTargetNameField.setText(fProcessor.getTargetName());
211 setPageComplete(RefactoringStatus.createInfoStatus(RefactoringCoreMessages.Checks_Choose_name));
212 setPageComplete(false);
214 fTargetNameField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
215 fTargetNameField.addModifyListener(new ModifyListener() {
217 public final void modifyText(final ModifyEvent event) {
218 fTargetNameStatus= fProcessor.setTargetName(fTargetNameField.getText());
219 handleStatusChanged();
222 TextFieldNavigationHandler.install(fTargetNameField);
224 label= new Label(control, SWT.NONE);
226 data= new GridData();
227 data.horizontalSpan= 2;
228 label.setLayoutData(data);
230 Composite delegateComposite= new Composite(control, SWT.NONE);
231 delegateComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
232 delegateComposite.setLayout(SWTUtil.newLayoutNoMargins(1));
234 fLeaveDelegateCheckBox= DelegateUIHelper.generateLeaveDelegateCheckbox(delegateComposite, getRefactoring(), false);
235 if (fLeaveDelegateCheckBox != null) {
236 fDeprecateDelegateCheckBox= new Button(delegateComposite, SWT.CHECK);
237 data= new GridData();
238 data.horizontalAlignment= GridData.FILL;
239 data.horizontalIndent= (layout.marginWidth + fDeprecateDelegateCheckBox.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
240 fDeprecateDelegateCheckBox.setLayoutData(data);
241 fDeprecateDelegateCheckBox.setText(DelegateUIHelper.getDeprecateDelegateCheckBoxTitle());
242 fDeprecateDelegateCheckBox.setSelection(DelegateUIHelper.loadDeprecateDelegateSetting(fProcessor));
243 fProcessor.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
244 fDeprecateDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
246 public void widgetSelected(SelectionEvent e) {
247 fProcessor.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
250 fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
251 fLeaveDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
253 public void widgetSelected(SelectionEvent e) {
254 fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
259 handleTargetChanged(possibleTargets[0]);
261 Dialog.applyDialogFont(control);
262 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.MOVE_MEMBERS_WIZARD_PAGE);
266 public void dispose() {
267 DelegateUIHelper.saveLeaveDelegateSetting(fLeaveDelegateCheckBox);
268 DelegateUIHelper.saveDeprecateDelegateSetting(fDeprecateDelegateCheckBox);
273 * Handles the status changed event.
275 protected final void handleStatusChanged() {
276 final RefactoringStatus status= new RefactoringStatus();
277 status.merge(fMethodNameStatus);
278 status.merge(fTargetNameStatus);
279 status.merge(fTargetTypeStatus);
280 if (!fTargetTypeStatus.isOK())
281 setPageComplete(false);
283 setPageComplete(status);
287 * Handles the target changed event.
289 * @param target the changed target
291 protected final void handleTargetChanged(final IVariableBinding target) {
292 Assert.isNotNull(target);
293 fProcessor.setTarget(target);
294 fTargetNameField.setEnabled(fProcessor.needsTargetNode());
295 fTargetNameLabel.setEnabled(fProcessor.needsTargetNode());
300 * Table label provider for the target selection table.
302 public static class TargetLabelProvider extends BindingLabelProvider implements ITableLabelProvider {
305 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
307 public Image getColumnImage(final Object element, final int column) {
308 final IVariableBinding binding= (IVariableBinding) element;
311 return getImage(binding.getType());
313 return getImage(binding);
320 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
322 public String getColumnText(final Object element, final int column) {
323 final IVariableBinding binding= (IVariableBinding) element;
326 return getText(binding.getType());
328 return getText(binding);
335 /** The default create delegator setting */
336 protected static boolean DEFAULT_CREATE_DELEGATOR_SETTING= false;
338 /** The default deprecate delegator setting */
339 protected static boolean DEFAULT_DEPRECATE_DELEGATOR_SETTING= false;
341 /** The associated move instance method processor */
342 protected final MoveInstanceMethodProcessor fProcessor;
345 * Creates a new move instance method wizard.
347 * @param processor the processor
348 * @param refactoring the refactoring to host
350 public MoveInstanceMethodWizard(MoveInstanceMethodProcessor processor, Refactoring refactoring) {
351 super(refactoring, DIALOG_BASED_USER_INTERFACE);
352 fProcessor= processor;
353 setDefaultPageTitle(RefactoringMessages.MoveInstanceMethodWizard_Move_Method);
357 * @see RefactoringWizard#addUserInputPages
360 protected void addUserInputPages() {
361 addPage(new MoveInstanceMethodPage());