KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > RenameFieldWizard


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.reorg;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.jdt.core.IMethod;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.BusyIndicator;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
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
27 import org.eclipse.jface.dialogs.Dialog;
28
29 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameFieldProcessor;
30 import org.eclipse.jdt.internal.corext.util.Messages;
31
32 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34 import org.eclipse.jdt.internal.ui.JavaPluginImages;
35 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
36
37 import org.eclipse.ltk.core.refactoring.Refactoring;
38 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
39 import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
40
41 public class RenameFieldWizard extends RenameRefactoringWizard {
42
43     public RenameFieldWizard(Refactoring refactoring) {
44         super(refactoring, RefactoringMessages.RenameFieldWizard_defaultPageTitle,
45             RefactoringMessages.RenameFieldWizard_inputPage_description,
46             JavaPluginImages.DESC_WIZBAN_REFACTOR_FIELD,
47             IJavaHelpContextIds.RENAME_FIELD_WIZARD_PAGE);
48     }
49
50     protected RenameInputWizardPage createInputPage(String JavaDoc message, String JavaDoc initialSetting) {
51         return new RenameFieldInputWizardPage(message, IJavaHelpContextIds.RENAME_FIELD_WIZARD_PAGE, initialSetting) {
52             protected RefactoringStatus validateTextField(String JavaDoc text) {
53                 RefactoringStatus result= validateNewName(text);
54                 updateGetterSetterLabels();
55                 return result;
56             }
57         };
58     }
59
60     private static class RenameFieldInputWizardPage extends RenameInputWizardPage {
61
62         private Button fRenameGetter;
63         private Button fRenameSetter;
64         private String JavaDoc fGetterRenamingErrorMessage;
65         private String JavaDoc fSetterRenamingErrorMessage;
66         
67         public RenameFieldInputWizardPage(String JavaDoc message, String JavaDoc contextHelpId, String JavaDoc initialValue) {
68             super(message, contextHelpId, true, initialValue);
69         }
70
71         public void createControl(Composite parent) {
72             super.createControl(parent);
73             Composite parentComposite= (Composite)getControl();
74                 
75             Composite composite= new Composite(parentComposite, SWT.NONE);
76             final GridLayout layout= new GridLayout();
77             layout.marginHeight= 0;
78             layout.marginWidth= 0;
79             composite.setLayout(layout);
80             composite.setLayoutData(new GridData(GridData.FILL_BOTH));
81         
82             Label separator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
83             separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
84
85             getGetterSetterRenamingEnablement();
86                 
87             fRenameGetter= new Button(composite, SWT.CHECK);
88             boolean getterEnablement= fGetterRenamingErrorMessage == null;
89             fRenameGetter.setEnabled(getterEnablement);
90             boolean getterSelection= getterEnablement && getBooleanSetting(RenameRefactoringWizard.FIELD_RENAME_GETTER, getRenameFieldProcessor().getRenameGetter());
91             fRenameGetter.setSelection(getterSelection);
92             getRenameFieldProcessor().setRenameGetter(getterSelection);
93             fRenameGetter.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
94             fRenameGetter.addSelectionListener(new SelectionAdapter(){
95                 public void widgetSelected(SelectionEvent e) {
96                     getRenameFieldProcessor().setRenameGetter(fRenameGetter.getSelection());
97                     updateLeaveDelegateCheckbox(getRenameFieldProcessor().getDelegateCount());
98                 }
99             });
100         
101             fRenameSetter= new Button(composite, SWT.CHECK);
102             boolean setterEnablement= fSetterRenamingErrorMessage == null;
103             fRenameSetter.setEnabled(setterEnablement);
104             boolean setterSelection= setterEnablement && getBooleanSetting(RenameRefactoringWizard.FIELD_RENAME_SETTER, getRenameFieldProcessor().getRenameSetter());
105             fRenameSetter.setSelection(setterSelection);
106             getRenameFieldProcessor().setRenameSetter(setterSelection);
107             fRenameSetter.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
108             fRenameSetter.addSelectionListener(new SelectionAdapter(){
109                 public void widgetSelected(SelectionEvent e) {
110                     getRenameFieldProcessor().setRenameSetter(fRenameSetter.getSelection());
111                     updateLeaveDelegateCheckbox(getRenameFieldProcessor().getDelegateCount());
112                 }
113             });
114         
115             updateGetterSetterLabels();
116             updateLeaveDelegateCheckbox(getRenameFieldProcessor().getDelegateCount());
117             Dialog.applyDialogFont(composite);
118         }
119         
120         public void dispose() {
121             if (saveSettings()) {
122                 if (fRenameGetter.isEnabled())
123                     saveBooleanSetting(RenameRefactoringWizard.FIELD_RENAME_GETTER, fRenameGetter);
124                 if (fRenameSetter.isEnabled())
125                     saveBooleanSetting(RenameRefactoringWizard.FIELD_RENAME_SETTER, fRenameSetter);
126             }
127             super.dispose();
128         }
129         
130         private void getGetterSetterRenamingEnablement() {
131             BusyIndicator.showWhile(getShell().getDisplay(), new Runnable JavaDoc(){
132                 public void run() {
133                     checkGetterRenamingEnablement();
134                     checkSetterRenamingEnablement();
135                 }
136             });
137         }
138     
139         protected void updateGetterSetterLabels(){
140             fRenameGetter.setText(getRenameGetterLabel());
141             fRenameSetter.setText(getRenameSetterLabel());
142         }
143     
144         private String JavaDoc getRenameGetterLabel(){
145             String JavaDoc defaultLabel= RefactoringMessages.RenameFieldInputWizardPage_rename_getter;
146             if (fGetterRenamingErrorMessage != null)
147                 return constructDisabledGetterRenamingLabel(defaultLabel);
148             try {
149                 IMethod getter= getRenameFieldProcessor().getGetter();
150                 if (getter == null || ! getter.exists())
151                     return defaultLabel;
152                 String JavaDoc oldGetterName= getter.getElementName();
153                 String JavaDoc newGetterName= createNewGetterName();
154                 return Messages.format(RefactoringMessages.RenameFieldInputWizardPage_rename_getter_to, new String JavaDoc[]{oldGetterName, newGetterName});
155             } catch(CoreException e) {
156                 JavaPlugin.log(e) ;
157                 return defaultLabel;
158             }
159         }
160         
161         private String JavaDoc getRenameSetterLabel(){
162             String JavaDoc defaultLabel= RefactoringMessages.RenameFieldInputWizardPage_rename_setter;
163             if (fSetterRenamingErrorMessage != null)
164                 return constructDisabledSetterRenamingLabel(defaultLabel);
165             try {
166                 IMethod setter= getRenameFieldProcessor().getSetter();
167                 if (setter == null || ! setter.exists())
168                     return defaultLabel;
169                 String JavaDoc oldSetterName= setter.getElementName();
170                 String JavaDoc newSetterName= createNewSetterName();
171                 return Messages.format(RefactoringMessages.RenameFieldInputWizardPage_rename_setter_to, new String JavaDoc[]{oldSetterName, newSetterName});
172             } catch(CoreException e) {
173                 JavaPlugin.log(e);
174                 return defaultLabel;
175             }
176         }
177         private String JavaDoc constructDisabledSetterRenamingLabel(String JavaDoc defaultLabel) {
178             if (fSetterRenamingErrorMessage.equals("")) //$NON-NLS-1$
179
return defaultLabel;
180             String JavaDoc[] keys= {defaultLabel, fSetterRenamingErrorMessage};
181             return Messages.format(RefactoringMessages.RenameFieldInputWizardPage_setter_label, keys);
182         }
183     
184         private String JavaDoc constructDisabledGetterRenamingLabel(String JavaDoc defaultLabel) {
185             if (fGetterRenamingErrorMessage.equals("")) //$NON-NLS-1$
186
return defaultLabel;
187             String JavaDoc[] keys= {defaultLabel, fGetterRenamingErrorMessage};
188             return Messages.format(RefactoringMessages.RenameFieldInputWizardPage_getter_label, keys);
189         }
190     
191         private String JavaDoc createNewGetterName() throws CoreException {
192             return getRenameFieldProcessor().getNewGetterName();
193         }
194     
195         private String JavaDoc createNewSetterName() throws CoreException {
196             return getRenameFieldProcessor().getNewSetterName();
197         }
198     
199         private String JavaDoc checkGetterRenamingEnablement() {
200             if (fGetterRenamingErrorMessage != null)
201                 return fGetterRenamingErrorMessage;
202             try {
203                 fGetterRenamingErrorMessage= getRenameFieldProcessor().canEnableGetterRenaming();
204                 return fGetterRenamingErrorMessage;
205             } catch (CoreException e) {
206                 JavaPlugin.log(e);
207                 return ""; //$NON-NLS-1$
208
}
209         }
210
211         private String JavaDoc checkSetterRenamingEnablement() {
212             if (fSetterRenamingErrorMessage != null)
213                 return fSetterRenamingErrorMessage;
214             try {
215                 fSetterRenamingErrorMessage= getRenameFieldProcessor().canEnableSetterRenaming();
216                 return fSetterRenamingErrorMessage;
217             } catch (CoreException e) {
218                 JavaPlugin.log(e);
219                 return ""; //$NON-NLS-1$
220
}
221         }
222     
223         private RenameFieldProcessor getRenameFieldProcessor() {
224             return (RenameFieldProcessor)((RenameRefactoring)getRefactoring()).getProcessor();
225         }
226     }
227 }
228
Popular Tags