KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.refactoring;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.ModifyEvent;
17 import org.eclipse.swt.events.ModifyListener;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Table;
27 import org.eclipse.swt.widgets.TableColumn;
28 import org.eclipse.swt.widgets.Text;
29
30 import org.eclipse.jface.dialogs.Dialog;
31 import org.eclipse.jface.viewers.ArrayContentProvider;
32 import org.eclipse.jface.viewers.ColumnWeightData;
33 import org.eclipse.jface.viewers.ISelectionChangedListener;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.ITableLabelProvider;
36 import org.eclipse.jface.viewers.SelectionChangedEvent;
37 import org.eclipse.jface.viewers.StructuredSelection;
38 import org.eclipse.jface.viewers.TableViewer;
39
40 import org.eclipse.ui.PlatformUI;
41
42 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
43 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
44 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
45
46 import org.eclipse.jdt.core.dom.IVariableBinding;
47
48 import org.eclipse.jdt.internal.corext.dom.Bindings;
49 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
50 import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor;
51 import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodRefactoring;
52 import org.eclipse.jdt.internal.corext.util.Messages;
53
54 import org.eclipse.jdt.ui.JavaElementLabels;
55
56 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
57 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
58 import org.eclipse.jdt.internal.ui.util.SWTUtil;
59 import org.eclipse.jdt.internal.ui.util.TableLayoutComposite;
60 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
61
62 /**
63  * Refactoring wizard for the 'move instance method' refactoring.
64  */

65 public final class MoveInstanceMethodWizard extends RefactoringWizard {
66
67     /**
68      * The input wizard page of the 'move instance method' refactoring.
69      */

70     public final class MoveInstanceMethodPage extends UserInputWizardPage {
71
72         /** The page name */
73         protected static final String JavaDoc PAGE_NAME= "MoveInstanceMethodPage"; //$NON-NLS-1$
74

75         /** The "leave delegate" checkbox */
76         protected Button fLeaveDelegateCheckBox= null;
77
78         /** The "deprecate delegate" checkbox */
79         protected Button fDeprecateDelegateCheckBox= null;
80
81         /** The method name text field */
82         protected Text fMethodNameField= null;
83
84         /** The current method name status */
85         protected RefactoringStatus fMethodNameStatus= new RefactoringStatus();
86
87         /** The target name text field */
88         protected Text fTargetNameField= null;
89
90         /** The target name label */
91         protected Label fTargetNameLabel= null;
92
93         /** The current target name status */
94         protected RefactoringStatus fTargetNameStatus= new RefactoringStatus();
95
96         /** The current target type status */
97         protected RefactoringStatus fTargetTypeStatus= new RefactoringStatus();
98
99         /**
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.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)));
120
121             GridData data= new GridData();
122             data.horizontalSpan= 2;
123             label.setLayoutData(data);
124
125             final TableLayoutComposite composite= new TableLayoutComposite(control, SWT.NULL);
126             composite.addColumnData(new ColumnWeightData(40, true));
127             composite.addColumnData(new ColumnWeightData(60, true));
128
129             final Table table= new Table(composite, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
130             table.setHeaderVisible(true);
131             table.setLinesVisible(false);
132
133             TableColumn column= new TableColumn(table, SWT.NONE);
134             column.setText(RefactoringMessages.MoveInstanceMethodPage_Name);
135             column.setResizable(true);
136
137             column= new TableColumn(table, SWT.NONE);
138             column.setText(RefactoringMessages.MoveInstanceMethodPage_Type);
139             column.setResizable(true);
140
141             final TableViewer viewer= new TableViewer(table);
142             viewer.setContentProvider(new ArrayContentProvider());
143             viewer.setLabelProvider(new TargetLabelProvider());
144
145             final IVariableBinding[] candidateTargets= fProcessor.getCandidateTargets();
146             viewer.setInput(candidateTargets);
147             final IVariableBinding[] possibleTargets= fProcessor.getPossibleTargets();
148             viewer.setSelection(new StructuredSelection(new Object JavaDoc[] { possibleTargets[0]}));
149
150             viewer.addSelectionChangedListener(new ISelectionChangedListener() {
151
152                 public final void selectionChanged(final SelectionChangedEvent event) {
153                     final Object JavaDoc element= ((IStructuredSelection) event.getSelection()).getFirstElement();
154                     if (element instanceof IVariableBinding) {
155                         final IVariableBinding target= (IVariableBinding) element;
156                         final IVariableBinding[] targets= fProcessor.getPossibleTargets();
157                         boolean success= false;
158                         for (int index= 0; index < targets.length; index++) {
159                             if (Bindings.equals(target, targets[index])) {
160                                 handleTargetChanged(target);
161                                 success= true;
162                                 break;
163                             }
164                         }
165                         if (!success)
166                             fTargetTypeStatus= RefactoringStatus.createWarningStatus(Messages.format(RefactoringMessages.MoveInstanceMethodPage_invalid_target, target.getName()));
167                         else
168                             fTargetTypeStatus= new RefactoringStatus();
169                         handleStatusChanged();
170                     }
171                 }
172             });
173
174             data= new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
175             data.heightHint= SWTUtil.getTableHeightHint(table, 7);
176             data.horizontalSpan= 2;
177             composite.setLayoutData(data);
178
179             label= new Label(control, SWT.SINGLE);
180             label.setText(RefactoringMessages.MoveInstanceMethodPage_Method_name);
181             label.setLayoutData(new GridData());
182
183             fMethodNameField= new Text(control, SWT.SINGLE | SWT.BORDER);
184             fMethodNameField.setText(fProcessor.getMethodName());
185             fMethodNameField.selectAll();
186             fMethodNameField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
187             fMethodNameField.setFocus();
188             fMethodNameField.addModifyListener(new ModifyListener() {
189
190                 public final void modifyText(final ModifyEvent event) {
191                     fMethodNameStatus= fProcessor.setMethodName(fMethodNameField.getText());
192                     handleStatusChanged();
193                 }
194             });
195             TextFieldNavigationHandler.install(fMethodNameField);
196
197             fTargetNameLabel= new Label(control, SWT.SINGLE);
198             fTargetNameLabel.setText(RefactoringMessages.MoveInstanceMethodPage_Target_name);
199             fTargetNameLabel.setLayoutData(new GridData());
200
201             fTargetNameField= new Text(control, SWT.SINGLE | SWT.BORDER);
202             final String JavaDoc name= fProcessor.getTargetName();
203             if (name != null && name.length() > 0)
204                 fTargetNameField.setText(fProcessor.getTargetName());
205             else {
206                 setPageComplete(RefactoringStatus.createInfoStatus(RefactoringCoreMessages.Checks_Choose_name));
207                 setPageComplete(false);
208             }
209             fTargetNameField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
210             fTargetNameField.addModifyListener(new ModifyListener() {
211
212                 public final void modifyText(final ModifyEvent event) {
213                     fTargetNameStatus= fProcessor.setTargetName(fTargetNameField.getText());
214                     handleStatusChanged();
215                 }
216             });
217             TextFieldNavigationHandler.install(fTargetNameField);
218
219             label= new Label(control, SWT.NONE);
220
221             data= new GridData();
222             data.horizontalSpan= 2;
223             label.setLayoutData(data);
224
225             fLeaveDelegateCheckBox= DelegateUIHelper.generateLeaveDelegateCheckbox(control, getRefactoring(), false);
226             if (fLeaveDelegateCheckBox != null) {
227                 fDeprecateDelegateCheckBox= new Button(control, SWT.CHECK);
228                 data= new GridData();
229                 data.horizontalAlignment= GridData.FILL;
230                 data.horizontalIndent= (layout.marginWidth + fDeprecateDelegateCheckBox.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
231                 data.horizontalSpan= 2;
232                 fDeprecateDelegateCheckBox.setLayoutData(data);
233                 fDeprecateDelegateCheckBox.setText(DelegateUIHelper.getDeprecateDelegateCheckBoxTitle());
234                 fDeprecateDelegateCheckBox.setSelection(DelegateUIHelper.loadDeprecateDelegateSetting(fProcessor));
235                 fProcessor.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
236                 fDeprecateDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
237                     public void widgetSelected(SelectionEvent e) {
238                         fProcessor.setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
239                     }
240                 });
241                 fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
242                 fLeaveDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
243                     public void widgetSelected(SelectionEvent e) {
244                         fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
245                     }
246                 });
247             }
248
249             fProcessor.setInlineDelegator(!fLeaveDelegateCheckBox.getSelection());
250             fProcessor.setRemoveDelegator(!fLeaveDelegateCheckBox.getSelection());
251
252             handleTargetChanged(possibleTargets[0]);
253
254             Dialog.applyDialogFont(control);
255             PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.MOVE_MEMBERS_WIZARD_PAGE);
256         }
257         
258         public void dispose() {
259             DelegateUIHelper.saveLeaveDelegateSetting(fLeaveDelegateCheckBox);
260             DelegateUIHelper.saveDeprecateDelegateSetting(fDeprecateDelegateCheckBox);
261             super.dispose();
262         }
263
264         /**
265          * Handles the status changed event.
266          */

267         protected final void handleStatusChanged() {
268             final RefactoringStatus status= new RefactoringStatus();
269             status.merge(fMethodNameStatus);
270             status.merge(fTargetNameStatus);
271             status.merge(fTargetTypeStatus);
272             if (!fTargetTypeStatus.isOK())
273                 setPageComplete(false);
274             else
275                 setPageComplete(status);
276         }
277
278         /**
279          * Handles the target changed event.
280          *
281          * @param target the changed target
282          */

283         protected final void handleTargetChanged(final IVariableBinding target) {
284             Assert.isNotNull(target);
285             fProcessor.setTarget(target);
286             fTargetNameField.setEnabled(fProcessor.needsTargetNode());
287             fTargetNameLabel.setEnabled(fProcessor.needsTargetNode());
288         }
289     }
290
291     /**
292      * Table label provider for the target selection table.
293      */

294     public static class TargetLabelProvider extends BindingLabelProvider implements ITableLabelProvider {
295
296         /*
297          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
298          */

299         public Image getColumnImage(final Object JavaDoc element, final int column) {
300             if (column == 0)
301                 return getImage(element);
302             return null;
303         }
304
305         /*
306          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
307          */

308         public String JavaDoc getColumnText(final Object JavaDoc element, final int column) {
309             final IVariableBinding binding= (IVariableBinding) element;
310             switch (column) {
311                 case 0:
312                     return getText(binding);
313                 case 1:
314                     return getText(binding.getType());
315                 default:
316                     return null;
317             }
318         }
319     }
320
321     /** The default create delegator setting */
322     protected static boolean DEFAULT_CREATE_DELEGATOR_SETTING= false;
323
324     /** The default deprecate delegator setting */
325     protected static boolean DEFAULT_DEPRECATE_DELEGATOR_SETTING= false;
326
327     /** The associated move instance method processor */
328     protected final MoveInstanceMethodProcessor fProcessor;
329
330     /**
331      * Creates a new move instance method wizard.
332      *
333      * @param refactoring the refactoring to host
334      */

335     public MoveInstanceMethodWizard(final MoveInstanceMethodRefactoring refactoring) {
336         super(refactoring, DIALOG_BASED_USER_INTERFACE);
337         fProcessor= refactoring.getMoveMethodProcessor();
338         setDefaultPageTitle(RefactoringMessages.MoveInstanceMethodWizard_Move_Method);
339     }
340
341     /*
342      * @see RefactoringWizard#addUserInputPages
343      */

344     protected void addUserInputPages() {
345         addPage(new MoveInstanceMethodPage());
346     }
347 }
348
Popular Tags