1 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.layout.GridData; 21 import org.eclipse.swt.layout.GridLayout; 22 import org.eclipse.swt.widgets.Button; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Group; 25 import org.eclipse.swt.widgets.Label; 26 import org.eclipse.swt.widgets.Text; 27 28 import org.eclipse.jface.dialogs.Dialog; 29 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 30 31 import org.eclipse.ui.PlatformUI; 32 33 import org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring; 34 35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 36 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler; 37 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper; 38 import org.eclipse.jdt.internal.ui.refactoring.contentassist.FieldNameProcessor; 39 40 import org.eclipse.ltk.ui.refactoring.RefactoringWizard; 41 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage; 42 43 public class PromoteTempWizard extends RefactoringWizard { 44 45 public PromoteTempWizard(PromoteTempToFieldRefactoring ref) { 46 super(ref, DIALOG_BASED_USER_INTERFACE | PREVIEW_EXPAND_FIRST_NODE); 47 setDefaultPageTitle(RefactoringMessages.ConvertLocalToField_title); 48 } 49 50 53 protected void addUserInputPages(){ 54 addPage(new PromoteTempInputPage()); 55 } 56 57 private static class PromoteTempInputPage extends UserInputWizardPage { 58 59 private static final String DESCRIPTION = RefactoringMessages.PromoteTempInputPage_description; 60 public static final String PAGE_NAME= "PromoteTempInputPage"; private static final String [] RADIO_BUTTON_LABELS= { 62 RefactoringMessages.PromoteTempInputPage_Field_declaration, 63 RefactoringMessages.PromoteTempInputPage_Current_method, 64 RefactoringMessages.PromoteTempInputPage_constructors}; 65 private static final Integer [] RADIO_BUTTON_DATA= { 66 new Integer (PromoteTempToFieldRefactoring.INITIALIZE_IN_FIELD), 67 new Integer (PromoteTempToFieldRefactoring.INITIALIZE_IN_METHOD), 68 new Integer (PromoteTempToFieldRefactoring.INITIALIZE_IN_CONSTRUCTOR)}; 69 private Button fDeclareStaticCheckbox; 70 private Button fDeclareFinalCheckbox; 71 private Button[] fInitializeInRadioButtons; 72 private Text fNameField; 73 74 public PromoteTempInputPage() { 75 super(PAGE_NAME); 76 setDescription(DESCRIPTION); 77 } 78 79 public void createControl(Composite parent) { 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 87 addFieldNameField(result); 88 addVisibilityControl(result); 89 addInitizeInRadioButtonGroup(result); 90 addDeclareStaticCheckbox(result); 91 addDeclareFinalCheckbox(result); 92 93 Dialog.applyDialogFont(result); 94 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.PROMOTE_TEMP_TO_FIELD_WIZARD_PAGE); 95 } 96 97 private void addFieldNameField(Composite result) { 98 Label nameLabel= new Label(result, SWT.NONE); 99 nameLabel.setText(RefactoringMessages.PromoteTempInputPage_Field_name); 100 nameLabel.setLayoutData(new GridData()); 101 102 String [] guessedFieldNames= getPromoteTempRefactoring().guessFieldNames(); 103 104 fNameField = new Text(result, SWT.BORDER | SWT.SINGLE); 105 fNameField.setText(guessedFieldNames[0]); 106 fNameField.selectAll(); 107 fNameField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 108 fNameField.addModifyListener(new ModifyListener(){ 109 public void modifyText(ModifyEvent e) { 110 PromoteTempInputPage.this.getPromoteTempRefactoring().setFieldName(fNameField.getText()); 111 PromoteTempInputPage.this.updateStatus(); 112 } 113 }); 114 IContentAssistProcessor processor= new FieldNameProcessor(guessedFieldNames, getPromoteTempRefactoring()); 115 ControlContentAssistHelper.createTextContentAssistant(fNameField, processor); 116 TextFieldNavigationHandler.install(fNameField); 117 } 118 119 private void updateStatus() { 120 setPageComplete(getPromoteTempRefactoring().validateInput()); 121 } 122 123 private void addInitizeInRadioButtonGroup(Composite result) { 124 GridData gd; 125 Group initializeIn= new Group(result, SWT.NONE); 126 initializeIn.setText(RefactoringMessages.PromoteTempInputPage_Initialize); 127 initializeIn.setLayout(new GridLayout()); 128 gd= new GridData(GridData.FILL_HORIZONTAL); 129 gd.horizontalSpan= 2; 130 initializeIn.setLayoutData(gd); 131 132 Assert.isTrue(RADIO_BUTTON_LABELS.length == RADIO_BUTTON_DATA.length); 133 fInitializeInRadioButtons= new Button[RADIO_BUTTON_LABELS.length]; 134 for (int i= 0; i < RADIO_BUTTON_LABELS.length; i++) { 135 Integer dataItem= RADIO_BUTTON_DATA[i]; 136 fInitializeInRadioButtons[i]= new Button(initializeIn, SWT.RADIO); 137 fInitializeInRadioButtons[i].setEnabled(canEnable(dataItem.intValue())); 138 fInitializeInRadioButtons[i].setText(RADIO_BUTTON_LABELS[i]); 139 fInitializeInRadioButtons[i].setSelection(dataItem.intValue() == getPromoteTempRefactoring().getInitializeIn()); 140 fInitializeInRadioButtons[i].setLayoutData(new GridData()); 141 fInitializeInRadioButtons[i].setData(dataItem); 142 final int j= i; 143 fInitializeInRadioButtons[i].addSelectionListener(new SelectionAdapter(){ 144 public void widgetSelected(SelectionEvent e) { 145 getPromoteTempRefactoring().setInitializeIn(getDataAsInt(fInitializeInRadioButtons[j])); 146 updateButtonsEnablement(); 147 } 148 }); 149 } 150 } 151 152 private void updateButtonsEnablement() { 153 fDeclareFinalCheckbox.setEnabled(getPromoteTempRefactoring().canEnableSettingFinal()); 154 fDeclareStaticCheckbox.setEnabled(getPromoteTempRefactoring().canEnableSettingStatic()); 155 for (int i= 0; i < fInitializeInRadioButtons.length; i++) { 156 fInitializeInRadioButtons[i].setEnabled(canEnable(getDataAsInt(fInitializeInRadioButtons[i]))); 157 } 158 } 159 160 private static int getDataAsInt(Button button){ 161 return ((Integer )button.getData()).intValue(); 162 } 163 164 private boolean canEnable(int initializeIn){ 165 switch(initializeIn){ 166 case PromoteTempToFieldRefactoring.INITIALIZE_IN_CONSTRUCTOR: 167 return getPromoteTempRefactoring().canEnableSettingDeclareInConstructors(); 168 case PromoteTempToFieldRefactoring.INITIALIZE_IN_FIELD: 169 return getPromoteTempRefactoring().canEnableSettingDeclareInFieldDeclaration(); 170 case PromoteTempToFieldRefactoring.INITIALIZE_IN_METHOD: 171 return getPromoteTempRefactoring().canEnableSettingDeclareInMethod(); 172 default: Assert.isTrue(false); return false; 173 } 174 } 175 176 public void addDeclareStaticCheckbox(Composite result) { 177 GridData gd; 178 fDeclareStaticCheckbox= new Button(result, SWT.CHECK); 179 fDeclareStaticCheckbox.setEnabled(getPromoteTempRefactoring().canEnableSettingStatic()); 180 fDeclareStaticCheckbox.setSelection(getPromoteTempRefactoring().getDeclareStatic()); 181 fDeclareStaticCheckbox.setText(RefactoringMessages.PromoteTempInputPage_declare_static); 182 gd= new GridData(GridData.FILL_HORIZONTAL); 183 gd.horizontalSpan= 2; 184 fDeclareStaticCheckbox.setLayoutData(gd); 185 fDeclareStaticCheckbox.addSelectionListener(new SelectionAdapter(){ 186 public void widgetSelected(SelectionEvent e) { 187 getPromoteTempRefactoring().setDeclareStatic(fDeclareStaticCheckbox.getSelection()); 188 updateButtonsEnablement(); 189 } 190 }); 191 } 192 193 private void addDeclareFinalCheckbox(Composite result) { 194 GridData gd; 195 fDeclareFinalCheckbox= new Button(result, SWT.CHECK); 196 fDeclareFinalCheckbox.setEnabled(getPromoteTempRefactoring().canEnableSettingFinal()); 197 fDeclareFinalCheckbox.setSelection(getPromoteTempRefactoring().getDeclareFinal()); 198 fDeclareFinalCheckbox.setText(RefactoringMessages.PromoteTempInputPage_declare_final); 199 gd= new GridData(GridData.FILL_HORIZONTAL); 200 gd.horizontalSpan= 2; 201 fDeclareFinalCheckbox.setLayoutData(gd); 202 fDeclareFinalCheckbox.addSelectionListener(new SelectionAdapter(){ 203 public void widgetSelected(SelectionEvent e) { 204 getPromoteTempRefactoring().setDeclareFinal(fDeclareFinalCheckbox.getSelection()); 205 updateButtonsEnablement(); 206 } 207 }); 208 } 209 210 private void addVisibilityControl(Composite result) { 211 int[] availableVisibilities= getPromoteTempRefactoring().getAvailableVisibilities(); 212 int currectVisibility= getPromoteTempRefactoring().getVisibility(); 213 IVisibilityChangeListener visibilityChangeListener= new IVisibilityChangeListener(){ 214 public void visibilityChanged(int newVisibility) { 215 getPromoteTempRefactoring().setVisibility(newVisibility); 216 } 217 218 public void modifierChanged(int modifier, boolean isChecked) { 219 } 220 }; 221 Composite visibilityComposite= VisibilityControlUtil.createVisibilityControl(result, visibilityChangeListener, availableVisibilities, currectVisibility); 222 GridData gd= new GridData(GridData.FILL_HORIZONTAL); 223 gd.horizontalSpan= 2; 224 visibilityComposite.setLayoutData(gd); 225 } 226 227 private PromoteTempToFieldRefactoring getPromoteTempRefactoring(){ 228 return (PromoteTempToFieldRefactoring)getRefactoring(); 229 } 230 233 public void setVisible(boolean visible) { 234 super.setVisible(visible); 235 if (visible && fNameField != null) 236 fNameField.setFocus(); 237 } 238 239 } 240 } 241 | Popular Tags |