1 11 12 package org.eclipse.jdt.internal.ui.refactoring.sef; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 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 fEnablements; 61 62 private static final String GENERATE_JAVADOC= "GenerateJavadoc"; 64 65 public SelfEncapsulateFieldInputPage() { 66 super("InputPage"); 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 (); 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 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 setterName= ((Text)e.widget).getText(); 125 fRefactoring.setSetterName(setterName); 126 updateUseSetter(reUseSetter); 127 processValidation(); 128 } 129 130 }); 131 TextFieldNavigationHandler.install(setter); 132 } 133 134 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 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 [] info= createData(visibility); 230 String [] labels= (String [])info[0]; 231 Integer [] data= (Integer [])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 )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 [] createData(int visibility) { 280 String pub= RefactoringMessages.SelfEncapsulateFieldInputPage_public; 281 String pro= RefactoringMessages.SelfEncapsulateFieldInputPage_protected; 282 String def= RefactoringMessages.SelfEncapsulateFieldInputPage_default; 283 String priv= RefactoringMessages.SelfEncapsulateFieldInputPage_private; 284 285 String [] labels= null; 286 Integer [] data= null; 287 if (Flags.isPrivate(visibility)) { 288 labels= new String [] { pub, pro, def, priv }; 289 data= new Integer [] {new Integer (Flags.AccPublic), new Integer (Flags.AccProtected), new Integer (0), new Integer (Flags.AccPrivate) }; 290 } else if (Flags.isProtected(visibility)) { 291 labels= new String [] { pub, pro }; 292 data= new Integer [] {new Integer (Flags.AccPublic), new Integer (Flags.AccProtected)}; 293 } else { 294 labels= new String [] { pub, def }; 295 data= new Integer [] {new Integer (Flags.AccPublic), new Integer (0)}; 296 } 297 return new Object [] {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 } 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 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 |