KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.core.Flags;
14 import org.eclipse.jdt.core.JavaModelException;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
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.Text;
25
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.IMessageProvider;
29 import org.eclipse.jface.resource.ImageDescriptor;
30
31 import org.eclipse.ui.PlatformUI;
32
33 import org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring;
34 import org.eclipse.jdt.internal.corext.util.JdtFlags;
35
36 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
37
38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
39 import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
41 import org.eclipse.jdt.internal.ui.refactoring.contentassist.VariableNamesProcessor;
42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
43 import org.eclipse.jdt.internal.ui.util.RowLayouter;
44 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
45
46 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
47 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
48
49 public class ExtractConstantWizard extends RefactoringWizard {
50
51     private static final String JavaDoc MESSAGE = RefactoringMessages.ExtractConstantInputPage_enter_name;
52
53     public ExtractConstantWizard(ExtractConstantRefactoring ref) {
54         super(ref, DIALOG_BASED_USER_INTERFACE | PREVIEW_EXPAND_FIRST_NODE);
55         setDefaultPageTitle(RefactoringMessages.ExtractConstantWizard_defaultPageTitle);
56     }
57
58     /* non java-doc
59      * @see RefactoringWizard#addUserInputPages
60      */

61     protected void addUserInputPages() {
62
63         String JavaDoc message= null;
64         int messageType= IMessageProvider.NONE;
65         if(!getExtractConstantRefactoring().selectionAllStaticFinal()) {
66             message= RefactoringMessages.ExtractConstantInputPage_selection_refers_to_nonfinal_fields;
67             messageType= IMessageProvider.INFORMATION;
68         } else {
69             message= MESSAGE;
70             messageType= IMessageProvider.NONE;
71         }
72         
73         String JavaDoc[] guessedNames= getExtractConstantRefactoring().guessConstantNames();
74         String JavaDoc initialValue= guessedNames.length == 0 ? "" : guessedNames[0]; //$NON-NLS-1$
75
addPage(new ExtractConstantInputPage(message, messageType, initialValue, guessedNames));
76     }
77
78     private ExtractConstantRefactoring getExtractConstantRefactoring(){
79         return (ExtractConstantRefactoring) getRefactoring();
80     }
81
82     private static class ExtractConstantInputPage extends TextInputWizardPage {
83         private static final String JavaDoc QUALIFY_REFERENCES= "qualifyReferences"; //$NON-NLS-1$
84

85         private Label fLabel;
86         private final boolean fInitialValid;
87         private final int fOriginalMessageType;
88         private final String JavaDoc fOriginalMessage;
89         
90         private Button fQualifyReferences;
91         private String JavaDoc[] fConstNameProposals;
92
93         private VariableNamesProcessor fContentAssistProcessor;
94         private String JavaDoc fAccessModifier;
95     
96         public ExtractConstantInputPage(String JavaDoc description, int messageType, String JavaDoc initialValue, String JavaDoc[] guessedNames) {
97             super(description, true, initialValue);
98             fOriginalMessage= description;
99             fOriginalMessageType= messageType;
100             fInitialValid= ! ("".equals(initialValue)); //$NON-NLS-1$
101
fConstNameProposals= guessedNames;
102         }
103
104         public void createControl(Composite parent) {
105             Composite result= new Composite(parent, SWT.NONE);
106             setControl(result);
107             GridLayout layout= new GridLayout();
108             layout.numColumns= 2;
109             layout.verticalSpacing= 8;
110             result.setLayout(layout);
111             RowLayouter layouter= new RowLayouter(2);
112         
113             Label label= new Label(result, SWT.NONE);
114             label.setText(RefactoringMessages.ExtractConstantInputPage_constant_name);
115         
116             Text text= createTextInputField(result);
117             text.selectAll();
118             text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
119             if (fConstNameProposals.length > 0) {
120                 fContentAssistProcessor= new VariableNamesProcessor(fConstNameProposals);
121                 ControlContentAssistHelper.createTextContentAssistant(text, fContentAssistProcessor);
122             }
123                 
124             layouter.perform(label, text, 1);
125         
126             addAccessModifierGroup(result, layouter);
127             addReplaceAllCheckbox(result, layouter);
128             addQualifyReferencesCheckbox(result, layouter);
129             addSeparator(result, layouter);
130             addLabel(result, layouter);
131         
132             validateTextField(text.getText());
133         
134             Dialog.applyDialogFont(result);
135             PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.EXTRACT_CONSTANT_WIZARD_PAGE);
136         }
137     
138         private void addAccessModifierGroup(Composite result, RowLayouter layouter) {
139             fAccessModifier= getExtractConstantRefactoring().getVisibility();
140             if (getExtractConstantRefactoring().getTargetIsInterface())
141                 return;
142             
143             Label label= new Label(result, SWT.NONE);
144             label.setText(RefactoringMessages.ExtractConstantInputPage_access_modifiers);
145         
146             Composite group= new Composite(result, SWT.NONE);
147             group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
148             GridLayout layout= new GridLayout();
149             layout.numColumns= 4; layout.marginWidth= 0;
150             group.setLayout(layout);
151         
152             String JavaDoc[] labels= new String JavaDoc[] {
153                 RefactoringMessages.ExtractMethodInputPage_public,
154                 RefactoringMessages.ExtractMethodInputPage_protected,
155                 RefactoringMessages.ExtractMethodInputPage_default,
156                 RefactoringMessages.ExtractMethodInputPage_private
157             };
158             String JavaDoc[] data= new String JavaDoc[] { JdtFlags.VISIBILITY_STRING_PUBLIC,
159                                           JdtFlags.VISIBILITY_STRING_PROTECTED,
160                                           JdtFlags.VISIBILITY_STRING_PACKAGE,
161                                           JdtFlags.VISIBILITY_STRING_PRIVATE }; //
162

163             updateContentAssistImage();
164             for (int i= 0; i < labels.length; i++) {
165                 Button radio= new Button(group, SWT.RADIO);
166                 radio.setText(labels[i]);
167                 radio.setData(data[i]);
168                 if (data[i] == fAccessModifier)
169                     radio.setSelection(true);
170                 radio.addSelectionListener(new SelectionAdapter() {
171                     public void widgetSelected(SelectionEvent event) {
172                         setAccessModifier((String JavaDoc)event.widget.getData());
173                     }
174                 });
175             }
176             layouter.perform(label, group, 1);
177         }
178     
179         private void updateContentAssistImage() {
180             if (fContentAssistProcessor == null)
181                 return;
182             
183             int flags;
184             if (fAccessModifier == JdtFlags.VISIBILITY_STRING_PRIVATE) {
185                 flags= Flags.AccPrivate;
186             } else if (fAccessModifier == JdtFlags.VISIBILITY_STRING_PROTECTED) {
187                 flags= Flags.AccProtected;
188             } else if (fAccessModifier == JdtFlags.VISIBILITY_STRING_PUBLIC) {
189                 flags= Flags.AccPublic;
190             } else {
191                 flags= Flags.AccDefault;
192             }
193             ImageDescriptor imageDesc= JavaElementImageProvider.getFieldImageDescriptor(false, flags);
194             imageDesc= new JavaElementImageDescriptor(imageDesc, JavaElementImageDescriptor.STATIC | JavaElementImageDescriptor.FINAL, JavaElementImageProvider.BIG_SIZE);
195             fContentAssistProcessor.setProposalImageDescriptor(imageDesc);
196         }
197
198         private void addReplaceAllCheckbox(Composite result, RowLayouter layouter) {
199             String JavaDoc title= RefactoringMessages.ExtractConstantInputPage_replace_all;
200             boolean defaultValue= getExtractConstantRefactoring().replaceAllOccurrences();
201             final Button checkBox= createCheckbox(result, title, defaultValue, layouter);
202             getExtractConstantRefactoring().setReplaceAllOccurrences(checkBox.getSelection());
203             checkBox.addSelectionListener(new SelectionAdapter(){
204                 public void widgetSelected(SelectionEvent e) {
205                     getExtractConstantRefactoring().setReplaceAllOccurrences(checkBox.getSelection());
206                 }
207             });
208         }
209
210         private void addQualifyReferencesCheckbox(Composite result, RowLayouter layouter) {
211             String JavaDoc title= RefactoringMessages.ExtractConstantInputPage_qualify_constant_references_with_class_name;
212             boolean defaultValue= getBooleanSetting(QUALIFY_REFERENCES, getExtractConstantRefactoring().qualifyReferencesWithDeclaringClassName());
213             fQualifyReferences= createCheckbox(result, title, defaultValue, layouter);
214             getExtractConstantRefactoring().setQualifyReferencesWithDeclaringClassName(fQualifyReferences.getSelection());
215             fQualifyReferences.addSelectionListener(new SelectionAdapter(){
216                 public void widgetSelected(SelectionEvent e) {
217                     getExtractConstantRefactoring().setQualifyReferencesWithDeclaringClassName(fQualifyReferences.getSelection());
218                 }
219             });
220         }
221
222         private void addLabel(Composite result, RowLayouter layouter) {
223             fLabel= new Label(result, SWT.WRAP);
224             GridData gd= new GridData(GridData.FILL_BOTH);
225             gd.widthHint= convertWidthInCharsToPixels(50);
226             fLabel.setLayoutData(gd);
227             updatePreviewLabel();
228             layouter.perform(fLabel);
229         }
230
231         private void addSeparator(Composite result, RowLayouter layouter) {
232             Label separator= new Label(result, SWT.SEPARATOR | SWT.HORIZONTAL);
233             separator.setLayoutData((new GridData(GridData.FILL_HORIZONTAL)));
234             layouter.perform(separator);
235         }
236     
237         private void updatePreviewLabel(){
238             try {
239                 if (fLabel != null)
240                     fLabel.setText(RefactoringMessages.ExtractConstantInputPage_signature_preview + getExtractConstantRefactoring().getConstantSignaturePreview());
241             } catch(JavaModelException e) {
242                 ExceptionHandler.handle(e, RefactoringMessages.ExtractTempInputPage_extract_local, RefactoringMessages.ExtractConstantInputPage_exception);
243             }
244         }
245     
246         /*
247          * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#validateTextField(String)
248          */

249         protected RefactoringStatus validateTextField(String JavaDoc text) {
250             try {
251                 getExtractConstantRefactoring().setConstantName(text);
252                 updatePreviewLabel();
253                 RefactoringStatus result= getExtractConstantRefactoring().checkConstantNameOnChange();
254                 if (fOriginalMessageType == IMessageProvider.INFORMATION && result.getSeverity() == RefactoringStatus.OK)
255                     return RefactoringStatus.createInfoStatus(fOriginalMessage);
256                 else
257                     return result;
258             } catch (JavaModelException e) {
259                 JavaPlugin.log(e);
260                 return RefactoringStatus.createFatalErrorStatus(RefactoringMessages.ExtractConstantInputPage_Internal_Error);
261             }
262         }
263
264         private void setAccessModifier(String JavaDoc accessModifier) {
265             getExtractConstantRefactoring().setVisibility(accessModifier);
266             fAccessModifier= accessModifier;
267             updateContentAssistImage();
268             updatePreviewLabel();
269         }
270     
271         private ExtractConstantRefactoring getExtractConstantRefactoring(){
272             return (ExtractConstantRefactoring)getRefactoring();
273         }
274     
275         private static Button createCheckbox(Composite parent, String JavaDoc title, boolean value, RowLayouter layouter){
276             Button checkBox= new Button(parent, SWT.CHECK);
277             checkBox.setText(title);
278             checkBox.setSelection(value);
279             layouter.perform(checkBox);
280             return checkBox;
281         }
282
283         /*
284          * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#isInitialInputValid()
285          */

286         protected boolean isInitialInputValid() {
287             return fInitialValid;
288         }
289
290         /*
291          * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#restoreMessage()
292          */

293         protected void restoreMessage() {
294             setMessage(fOriginalMessage, fOriginalMessageType);
295         }
296         
297         private boolean getBooleanSetting(String JavaDoc key, boolean defaultValue) {
298             String JavaDoc update= getRefactoringSettings().get(key);
299             if (update != null)
300                 return Boolean.valueOf(update).booleanValue();
301             else
302                 return defaultValue;
303         }
304         
305         private void saveBooleanSetting(String JavaDoc key, Button checkBox) {
306             if (checkBox != null)
307                 getRefactoringSettings().put(key, checkBox.getSelection());
308         }
309         
310         private boolean saveSettings() {
311             if (getContainer() instanceof Dialog)
312                 return ((Dialog)getContainer()).getReturnCode() == IDialogConstants.OK_ID;
313             return true;
314         }
315         
316         public void dispose() {
317             if (saveSettings()) {
318                 saveBooleanSetting(QUALIFY_REFERENCES, fQualifyReferences);
319             }
320             super.dispose();
321         }
322
323     }
324 }
325
Popular Tags