KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > sef > SelfEncapsulateFieldInputPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.jdt.internal.ui.refactoring.sef;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Text;
31
32 import org.eclipse.jface.dialogs.Dialog;
33 import org.eclipse.jface.dialogs.IDialogSettings;
34
35 import org.eclipse.ui.PlatformUI;
36
37 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
38 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
39
40 import org.eclipse.jdt.core.Flags;
41 import org.eclipse.jdt.core.IField;
42 import org.eclipse.jdt.core.IMethod;
43 import org.eclipse.jdt.core.JavaModelException;
44
45 import org.eclipse.jdt.internal.corext.refactoring.sef.SelfEncapsulateFieldRefactoring;
46 import org.eclipse.jdt.internal.corext.util.JdtFlags;
47
48 import org.eclipse.jdt.ui.JavaElementLabels;
49
50 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
51 import org.eclipse.jdt.internal.ui.JavaPluginImages;
52 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
53 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
54 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
55
56 public class SelfEncapsulateFieldInputPage extends UserInputWizardPage {
57
58     private SelfEncapsulateFieldRefactoring fRefactoring;
59     private IDialogSettings fSettings;
60     private List JavaDoc fEnablements;
61     
62     private static final String JavaDoc GENERATE_JAVADOC= "GenerateJavadoc"; //$NON-NLS-1$
63

64
65     public SelfEncapsulateFieldInputPage() {
66         super("InputPage"); //$NON-NLS-1$
67
setDescription(RefactoringMessages.SelfEncapsulateFieldInputPage_description);
68         setImageDescriptor(JavaPluginImages.DESC_WIZBAN_REFACTOR_CU);
69     }
70     
71     public void createControl(Composite parent) {
72         fRefactoring= (SelfEncapsulateFieldRefactoring)getRefactoring();
73         fEnablements= new ArrayList JavaDoc();
74         loadSettings();
75         
76         Composite result= new Composite(parent, SWT.NONE);
77         setControl(result);
78         initializeDialogUnits(result);
79         
80         GridLayout layout= new GridLayout();
81         layout.numColumns= 3;
82         layout.verticalSpacing= 8;
83         result.setLayout(layout);
84         GridData gd= new GridData(GridData.FILL_HORIZONTAL);
85         gd.widthHint= convertWidthInCharsToPixels(25);
86         
87         Label label= new Label(result, SWT.LEAD);
88         label.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_getter_name);
89         
90         Text getter= new Text(result, SWT.BORDER);
91         getter.setText(fRefactoring.getGetterName());
92         getter.setLayoutData(gd);
93         TextFieldNavigationHandler.install(getter);
94         
95         final Label reUseGetter= new Label(result,SWT.LEAD);
96         GridData getterGD= new GridData();
97         getterGD.widthHint=convertWidthInCharsToPixels(23);
98         reUseGetter.setLayoutData(getterGD);
99         updateUseGetter(reUseGetter);
100         getter.addModifyListener(new ModifyListener() {
101             public void modifyText(ModifyEvent e) {
102                 String JavaDoc getterName= ((Text)e.widget).getText();
103                 fRefactoring.setGetterName(getterName);
104                 updateUseGetter(reUseGetter);
105                 processValidation();
106             }
107         });
108         
109         if (needsSetter()) {
110             label= new Label(result, SWT.LEAD);
111             label.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_setter_name);
112             
113             Text setter= new Text(result, SWT.BORDER);
114             setter.setText(fRefactoring.getSetterName());
115             setter.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
116
117             final Label reUseSetter= new Label(result, SWT.LEAD);
118             GridData setterGD= new GridData();
119             setterGD.widthHint=convertWidthInCharsToPixels(23);
120             reUseSetter.setLayoutData(setterGD);
121             updateUseSetter(reUseSetter);
122             setter.addModifyListener(new ModifyListener() {
123                 public void modifyText(ModifyEvent e) {
124                     String JavaDoc setterName= ((Text)e.widget).getText();
125                     fRefactoring.setSetterName(setterName);
126                     updateUseSetter(reUseSetter);
127                     processValidation();
128                 }
129
130             });
131             TextFieldNavigationHandler.install(setter);
132         }
133         
134         // createSeparator(result, layouter);
135
createFieldAccessBlock(result);
136         
137         label= new Label(result, SWT.LEFT);
138         label.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_insert_after);
139         fEnablements.add(label);
140         final Combo combo= new Combo(result, SWT.READ_ONLY);
141         fillWithPossibleInsertPositions(combo, fRefactoring.getField());
142         combo.addSelectionListener(new SelectionAdapter() {
143             public void widgetSelected(SelectionEvent event) {
144                 fRefactoring.setInsertionIndex(combo.getSelectionIndex() - 1);
145             }
146         });
147         GridData gridData= new GridData(GridData.FILL_HORIZONTAL);
148         gridData.horizontalSpan=2;
149         combo.setLayoutData(gridData);
150         fEnablements.add(combo);
151         
152         
153         createAccessModifier(result);
154             
155         Button checkBox= new Button(result, SWT.CHECK);
156         checkBox.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_generateJavadocComment);
157         checkBox.setSelection(fRefactoring.getGenerateJavadoc());
158         checkBox.addSelectionListener(new SelectionAdapter() {
159             public void widgetSelected(SelectionEvent e) {
160                 setGenerateJavadoc(((Button)e.widget).getSelection());
161             }
162         });
163         GridData checkGD= new GridData(GridData.FILL_HORIZONTAL);
164         checkGD.horizontalSpan=3;
165         checkBox.setLayoutData(checkGD);
166         fEnablements.add(checkBox);
167         
168         updateEnablements();
169         
170         processValidation();
171         
172         getter.setFocus();
173         
174         Dialog.applyDialogFont(result);
175         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.SEF_WIZARD_PAGE);
176     }
177
178     private void updateUseSetter(Label reUseSetter) {
179         if (fRefactoring.isUsingLocalSetter())
180             reUseSetter.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_useexistingsetter_label);
181         else
182             reUseSetter.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_usenewgetter_label);
183         updateEnablements();
184     }
185
186     private void updateEnablements() {
187         boolean enable=!(fRefactoring.isUsingLocalSetter()&&fRefactoring.isUsingLocalGetter());
188         for (Iterator JavaDoc iter= fEnablements.iterator(); iter.hasNext();) {
189             Control control= (Control) iter.next();
190             control.setEnabled(enable);
191         }
192     }
193
194     private void updateUseGetter(Label reUseGetter) {
195         if (fRefactoring.isUsingLocalGetter())
196             reUseGetter.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_useexistinggetter_label);
197         else
198             reUseGetter.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_usenewsetter_label);
199         updateEnablements();
200     }
201
202     private void loadSettings() {
203         fSettings= getDialogSettings().getSection(SelfEncapsulateFieldWizard.DIALOG_SETTING_SECTION);
204         if (fSettings == null) {
205             fSettings= getDialogSettings().addNewSection(SelfEncapsulateFieldWizard.DIALOG_SETTING_SECTION);
206             fSettings.put(GENERATE_JAVADOC, JavaPreferencesSettings.getCodeGenerationSettings(fRefactoring.getField().getJavaProject()).createComments);
207         }
208         fRefactoring.setGenerateJavadoc(fSettings.getBoolean(GENERATE_JAVADOC));
209     }
210
211     private void createAccessModifier(Composite result) {
212         int visibility= fRefactoring.getVisibility();
213         if (Flags.isPublic(visibility))
214             return;
215         GridLayout layout;
216         Label label;
217         label= new Label(result, SWT.NONE);
218         label.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_access_Modifiers);
219         fEnablements.add(label);
220         Composite group= new Composite(result, SWT.NONE);
221         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
222         layout= new GridLayout();
223         layout.numColumns= 4; layout.marginWidth= 0; layout.marginHeight= 0;
224         group.setLayout(layout);
225         GridData gridData= new GridData(GridData.FILL_HORIZONTAL);
226         gridData.horizontalSpan=2;
227         group.setLayoutData(gridData);
228         
229         Object JavaDoc[] info= createData(visibility);
230         String JavaDoc[] labels= (String JavaDoc[])info[0];
231         Integer JavaDoc[] data= (Integer JavaDoc[])info[1];
232         for (int i= 0; i < labels.length; i++) {
233             Button radio= new Button(group, SWT.RADIO);
234             radio.setText(labels[i]);
235             radio.setData(data[i]);
236             int iData= data[i].intValue();
237             if (iData == visibility)
238                 radio.setSelection(true);
239             radio.addSelectionListener(new SelectionAdapter() {
240                 public void widgetSelected(SelectionEvent event) {
241                     fRefactoring.setVisibility(((Integer JavaDoc)event.widget.getData()).intValue());
242                 }
243             });
244             fEnablements.add(radio);
245         }
246     }
247     
248     private void createFieldAccessBlock(Composite result) {
249         Label label= new Label(result, SWT.LEFT);
250         label.setText(RefactoringMessages.SelfEncapsulateField_field_access);
251         
252         Composite group= new Composite(result, SWT.NONE);
253         GridLayout layout= new GridLayout();
254         layout.marginWidth= 0; layout.marginHeight= 0; layout.numColumns= 2;
255         group.setLayout(layout);
256         GridData gridData= new GridData(GridData.FILL_HORIZONTAL);
257         gridData.horizontalSpan=2;
258         group.setLayoutData(gridData);
259         Button radio= new Button(group, SWT.RADIO);
260         radio.setText(RefactoringMessages.SelfEncapsulateField_use_setter_getter);
261         radio.setSelection(true);
262         radio.addSelectionListener(new SelectionAdapter() {
263             public void widgetSelected(SelectionEvent e) {
264                 fRefactoring.setEncapsulateDeclaringClass(true);
265             }
266         });
267         radio.setLayoutData(new GridData());
268         
269         radio= new Button(group, SWT.RADIO);
270         radio.setText(RefactoringMessages.SelfEncapsulateField_keep_references);
271         radio.addSelectionListener(new SelectionAdapter() {
272             public void widgetSelected(SelectionEvent e) {
273                 fRefactoring.setEncapsulateDeclaringClass(false);
274             }
275         });
276         radio.setLayoutData(new GridData());
277     }
278
279     private Object JavaDoc[] createData(int visibility) {
280         String JavaDoc pub= RefactoringMessages.SelfEncapsulateFieldInputPage_public;
281         String JavaDoc pro= RefactoringMessages.SelfEncapsulateFieldInputPage_protected;
282         String JavaDoc def= RefactoringMessages.SelfEncapsulateFieldInputPage_default;
283         String JavaDoc priv= RefactoringMessages.SelfEncapsulateFieldInputPage_private;
284         
285         String JavaDoc[] labels= null;
286         Integer JavaDoc[] data= null;
287         if (Flags.isPrivate(visibility)) {
288             labels= new String JavaDoc[] { pub, pro, def, priv };
289             data= new Integer JavaDoc[] {new Integer JavaDoc(Flags.AccPublic), new Integer JavaDoc(Flags.AccProtected), new Integer JavaDoc(0), new Integer JavaDoc(Flags.AccPrivate) };
290         } else if (Flags.isProtected(visibility)) {
291             labels= new String JavaDoc[] { pub, pro };
292             data= new Integer JavaDoc[] {new Integer JavaDoc(Flags.AccPublic), new Integer JavaDoc(Flags.AccProtected)};
293         } else {
294             labels= new String JavaDoc[] { pub, def };
295             data= new Integer JavaDoc[] {new Integer JavaDoc(Flags.AccPublic), new Integer JavaDoc(0)};
296         }
297         return new Object JavaDoc[] {labels, data};
298     }
299     
300     private void fillWithPossibleInsertPositions(Combo combo, IField field) {
301         int select= 0;
302         combo.add(RefactoringMessages.SelfEncapsulateFieldInputPage_first_method);
303         try {
304             IMethod[] methods= field.getDeclaringType().getMethods();
305             for (int i= 0; i < methods.length; i++) {
306                 combo.add(JavaElementLabels.getElementLabel(methods[i], JavaElementLabels.M_PARAMETER_TYPES));
307             }
308             if (methods.length > 0)
309                 select= methods.length;
310         } catch (JavaModelException e) {
311             // Fall through
312
}
313         combo.select(select);
314         fRefactoring.setInsertionIndex(select - 1);
315     }
316     
317     private void setGenerateJavadoc(boolean value) {
318         fSettings.put(GENERATE_JAVADOC, value);
319         fRefactoring.setGenerateJavadoc(value);
320     }
321     
322     private void processValidation() {
323         RefactoringStatus status= fRefactoring.checkMethodNames();
324         String JavaDoc message= null;
325         boolean valid= true;
326         if (status.hasFatalError()) {
327             message= status.getMessageMatchingSeverity(RefactoringStatus.FATAL);
328             valid= false;
329         }
330         setErrorMessage(message);
331         setPageComplete(valid);
332     }
333     
334     private boolean needsSetter() {
335         try {
336             return !JdtFlags.isFinal(fRefactoring.getField());
337         } catch(JavaModelException e) {
338             return true;
339         }
340     }
341 }
342
Popular Tags