KickJava   Java API By Example, From Geeks To Geeks.

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


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.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Text;
24
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.jface.dialogs.IDialogSettings;
27
28
29 import org.eclipse.ui.PlatformUI;
30
31 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
32 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
33
34 import org.eclipse.jdt.internal.corext.refactoring.code.ExtractTempRefactoring;
35
36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
37 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
38 import org.eclipse.jdt.internal.ui.refactoring.contentassist.VariableNamesProcessor;
39 import org.eclipse.jdt.internal.ui.util.RowLayouter;
40
41 public class ExtractTempWizard extends RefactoringWizard {
42
43     /* package */ static final String JavaDoc DIALOG_SETTING_SECTION= "ExtractTempWizard"; //$NON-NLS-1$
44

45     public ExtractTempWizard(ExtractTempRefactoring ref) {
46         super(ref, DIALOG_BASED_USER_INTERFACE | PREVIEW_EXPAND_FIRST_NODE);
47         setDefaultPageTitle(RefactoringMessages.ExtractTempWizard_defaultPageTitle);
48     }
49
50     /* non java-doc
51      * @see RefactoringWizard#addUserInputPages
52      */

53     protected void addUserInputPages(){
54         addPage(new ExtractTempInputPage(getExtractTempRefactoring().guessTempNames()));
55     }
56     
57     private ExtractTempRefactoring getExtractTempRefactoring(){
58         return (ExtractTempRefactoring)getRefactoring();
59     }
60
61     private static class ExtractTempInputPage extends TextInputWizardPage {
62
63         private static final String JavaDoc DECLARE_FINAL= "declareFinal"; //$NON-NLS-1$
64
private static final String JavaDoc REPLACE_ALL= "replaceOccurrences"; //$NON-NLS-1$
65

66         private final boolean fInitialValid;
67         private static final String JavaDoc DESCRIPTION = RefactoringMessages.ExtractTempInputPage_enter_name;
68         private String JavaDoc[] fTempNameProposals;
69         private IDialogSettings fSettings;
70         
71         public ExtractTempInputPage(String JavaDoc[] tempNameProposals) {
72             super(DESCRIPTION, true, tempNameProposals.length == 0 ? "" : tempNameProposals[0]); //$NON-NLS-1$
73
Assert.isNotNull(tempNameProposals);
74             fTempNameProposals= tempNameProposals;
75             fInitialValid= tempNameProposals.length > 0;
76         }
77     
78         public void createControl(Composite parent) {
79             loadSettings();
80             Composite result= new Composite(parent, SWT.NONE);
81             setControl(result);
82             GridLayout layout= new GridLayout();
83             layout.numColumns= 2;
84             layout.verticalSpacing= 8;
85             result.setLayout(layout);
86             RowLayouter layouter= new RowLayouter(2);
87             
88             Label label= new Label(result, SWT.NONE);
89             label.setText(RefactoringMessages.ExtractTempInputPage_variable_name);
90             
91             Text text= createTextInputField(result);
92             text.selectAll();
93             text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
94             ControlContentAssistHelper.createTextContentAssistant(text, new VariableNamesProcessor(fTempNameProposals));
95                     
96             layouter.perform(label, text, 1);
97             
98             addReplaceAllCheckbox(result, layouter);
99             addDeclareFinalCheckbox(result, layouter);
100             
101             validateTextField(text.getText());
102             
103             Dialog.applyDialogFont(result);
104             PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.EXTRACT_TEMP_WIZARD_PAGE);
105         }
106
107         private void loadSettings() {
108             fSettings= getDialogSettings().getSection(ExtractTempWizard.DIALOG_SETTING_SECTION);
109             if (fSettings == null) {
110                 fSettings= getDialogSettings().addNewSection(ExtractTempWizard.DIALOG_SETTING_SECTION);
111                 fSettings.put(DECLARE_FINAL, false);
112                 fSettings.put(REPLACE_ALL, true);
113             }
114             getExtractTempRefactoring().setDeclareFinal(fSettings.getBoolean(DECLARE_FINAL));
115             getExtractTempRefactoring().setReplaceAllOccurrences(fSettings.getBoolean(REPLACE_ALL));
116         }
117
118         private void addReplaceAllCheckbox(Composite result, RowLayouter layouter) {
119             String JavaDoc title= RefactoringMessages.ExtractTempInputPage_replace_all;
120             boolean defaultValue= getExtractTempRefactoring().replaceAllOccurrences();
121             final Button checkBox= createCheckbox(result, title, defaultValue, layouter);
122             getExtractTempRefactoring().setReplaceAllOccurrences(checkBox.getSelection());
123             checkBox.addSelectionListener(new SelectionAdapter(){
124                 public void widgetSelected(SelectionEvent e) {
125                     fSettings.put(REPLACE_ALL, checkBox.getSelection());
126                     getExtractTempRefactoring().setReplaceAllOccurrences(checkBox.getSelection());
127                 }
128             });
129         }
130         
131         private void addDeclareFinalCheckbox(Composite result, RowLayouter layouter) {
132             String JavaDoc title= RefactoringMessages.ExtractTempInputPage_declare_final;
133             boolean defaultValue= getExtractTempRefactoring().declareFinal();
134             final Button checkBox= createCheckbox(result, title, defaultValue, layouter);
135             getExtractTempRefactoring().setDeclareFinal(checkBox.getSelection());
136             checkBox.addSelectionListener(new SelectionAdapter(){
137                 public void widgetSelected(SelectionEvent e) {
138                     fSettings.put(DECLARE_FINAL, checkBox.getSelection());
139                     getExtractTempRefactoring().setDeclareFinal(checkBox.getSelection());
140                 }
141             });
142         }
143         
144         /*
145          * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#textModified(java.lang.String)
146          */

147         protected void textModified(String JavaDoc text) {
148             getExtractTempRefactoring().setTempName(text);
149             super.textModified(text);
150         }
151         
152         
153         /*
154          * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#validateTextField(String)
155          */

156         protected RefactoringStatus validateTextField(String JavaDoc text) {
157             return getExtractTempRefactoring().checkTempName(text);
158         }
159         
160         private ExtractTempRefactoring getExtractTempRefactoring(){
161             return (ExtractTempRefactoring)getRefactoring();
162         }
163         
164         private static Button createCheckbox(Composite parent, String JavaDoc title, boolean value, RowLayouter layouter){
165             Button checkBox= new Button(parent, SWT.CHECK);
166             checkBox.setText(title);
167             checkBox.setSelection(value);
168             layouter.perform(checkBox);
169             return checkBox;
170         }
171     
172         /*
173          * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#isInitialInputValid()
174          */

175         protected boolean isInitialInputValid() {
176             return fInitialValid;
177         }
178     }
179 }
180
Popular Tags