1 11 package org.eclipse.jdt.internal.ui.preferences; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.IStatus; 17 18 import org.eclipse.core.resources.IProject; 19 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Shell; 27 import org.eclipse.swt.widgets.Table; 28 29 import org.eclipse.jface.dialogs.StatusDialog; 30 import org.eclipse.jface.viewers.ColumnLayoutData; 31 import org.eclipse.jface.viewers.ColumnWeightData; 32 import org.eclipse.jface.viewers.ITableLabelProvider; 33 import org.eclipse.jface.viewers.LabelProvider; 34 import org.eclipse.jface.window.Window; 35 36 import org.eclipse.ui.PlatformUI; 37 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; 38 39 import org.eclipse.jdt.core.JavaConventions; 40 import org.eclipse.jdt.core.JavaCore; 41 42 import org.eclipse.jdt.internal.corext.util.Messages; 43 import org.eclipse.jdt.internal.corext.util.Strings; 44 45 import org.eclipse.jdt.ui.JavaElementImageDescriptor; 46 import org.eclipse.jdt.ui.PreferenceConstants; 47 48 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 49 import org.eclipse.jdt.internal.ui.JavaPlugin; 50 import org.eclipse.jdt.internal.ui.JavaPluginImages; 51 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 52 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil; 53 import org.eclipse.jdt.internal.ui.util.SWTUtil; 54 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry; 55 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; 56 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener; 57 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 58 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 59 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter; 60 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 61 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; 62 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 63 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 64 65 68 public class NameConventionConfigurationBlock extends OptionsConfigurationBlock { 69 70 private final static int FIELD= 1; 71 private final static int STATIC= 2; 72 private final static int ARGUMENT= 3; 73 private final static int LOCAL= 4; 74 75 private static final Key PREF_FIELD_PREFIXES= getJDTCoreKey(JavaCore.CODEASSIST_FIELD_PREFIXES); 77 private static final Key PREF_FIELD_SUFFIXES= getJDTCoreKey(JavaCore.CODEASSIST_FIELD_SUFFIXES); 78 private static final Key PREF_STATIC_FIELD_PREFIXES= getJDTCoreKey(JavaCore.CODEASSIST_STATIC_FIELD_PREFIXES); 79 private static final Key PREF_STATIC_FIELD_SUFFIXES= getJDTCoreKey(JavaCore.CODEASSIST_STATIC_FIELD_SUFFIXES); 80 private static final Key PREF_ARGUMENT_PREFIXES= getJDTCoreKey(JavaCore.CODEASSIST_ARGUMENT_PREFIXES); 81 private static final Key PREF_ARGUMENT_SUFFIXES= getJDTCoreKey(JavaCore.CODEASSIST_ARGUMENT_SUFFIXES); 82 private static final Key PREF_LOCAL_PREFIXES= getJDTCoreKey(JavaCore.CODEASSIST_LOCAL_PREFIXES); 83 private static final Key PREF_LOCAL_SUFFIXES= getJDTCoreKey(JavaCore.CODEASSIST_LOCAL_SUFFIXES); 84 85 private static final Key PREF_KEYWORD_THIS= getJDTUIKey(PreferenceConstants.CODEGEN_KEYWORD_THIS); 86 private static final Key PREF_IS_FOR_GETTERS= getJDTUIKey(PreferenceConstants.CODEGEN_IS_FOR_GETTERS); 87 private static final Key PREF_EXCEPTION_NAME= getJDTUIKey(PreferenceConstants.CODEGEN_EXCEPTION_VAR_NAME); 88 89 private static final Key PREF_USE_OVERRIDE_ANNOT= getJDTUIKey(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION); 90 private static final Key PREF_GENERATE_COMMENTS= getJDTUIKey(PreferenceConstants.CODEGEN_ADD_COMMENTS); 91 92 private static class NameConventionEntry { 93 public int kind; 94 public String prefix; 95 public String suffix; 96 public Key prefixkey; 97 public Key suffixkey; 98 } 99 100 private class NameConventionInputDialog extends StatusDialog implements IDialogFieldListener { 101 102 private StringDialogField fPrefixField; 103 private StringDialogField fSuffixField; 104 private NameConventionEntry fEntry; 105 private DialogField fMessageField; 106 107 public NameConventionInputDialog(Shell parent, String title, String message, NameConventionEntry entry) { 108 super(parent); 109 fEntry= entry; 110 111 setTitle(title); 112 113 fMessageField= new DialogField(); 114 fMessageField.setLabelText(message); 115 116 fPrefixField= new StringDialogField(); 117 fPrefixField.setLabelText(PreferencesMessages.NameConventionConfigurationBlock_dialog_prefix); 118 fPrefixField.setDialogFieldListener(this); 119 120 fSuffixField= new StringDialogField(); 121 fSuffixField.setLabelText(PreferencesMessages.NameConventionConfigurationBlock_dialog_suffix); 122 fSuffixField.setDialogFieldListener(this); 123 124 fPrefixField.setText(entry.prefix); 125 fSuffixField.setText(entry.suffix); 126 } 127 128 public NameConventionEntry getResult() { 129 NameConventionEntry res= new NameConventionEntry(); 130 res.prefix= Strings.removeTrailingCharacters(fPrefixField.getText(), ','); 131 res.suffix= Strings.removeTrailingCharacters(fSuffixField.getText(), ','); 132 res.prefixkey= fEntry.prefixkey; 133 res.suffixkey= fEntry.suffixkey; 134 res.kind= fEntry.kind; 135 return res; 136 } 137 138 protected Control createDialogArea(Composite parent) { 139 Composite composite= (Composite) super.createDialogArea(parent); 140 Composite inner= new Composite(composite, SWT.NONE); 141 inner.setFont(composite.getFont()); 142 GridLayout layout= new GridLayout(); 143 layout.marginHeight= 0; 144 layout.marginWidth= 0; 145 layout.numColumns= 2; 146 inner.setLayout(layout); 147 148 fMessageField.doFillIntoGrid(inner, 2); 149 fPrefixField.doFillIntoGrid(inner, 2); 150 fSuffixField.doFillIntoGrid(inner, 2); 151 152 LayoutUtil.setHorizontalGrabbing(fPrefixField.getTextControl(null)); 153 LayoutUtil.setWidthHint(fPrefixField.getTextControl(null), convertWidthInCharsToPixels(45)); 154 LayoutUtil.setWidthHint(fSuffixField.getTextControl(null), convertWidthInCharsToPixels(45)); 155 156 fPrefixField.postSetFocusOnDialogField(parent.getDisplay()); 157 158 applyDialogFont(composite); 159 160 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.CODE_STYLE_EDIT_PREFIX_SUFFIX); 161 162 return composite; 163 } 164 165 168 public void dialogFieldChanged(DialogField field) { 169 IStatus prefixStatus= validateIdentifiers(getTokens(fPrefixField.getText(), ","), true); IStatus suffixStatus= validateIdentifiers(getTokens(fSuffixField.getText(), ","), false); 173 updateStatus(StatusUtil.getMoreSevere(suffixStatus, prefixStatus)); 174 } 175 176 private IStatus validateIdentifiers(String [] values, boolean prefix) { 177 for (int i= 0; i < values.length; i++) { 178 String val= values[i]; 179 if (val.length() == 0) { 180 if (prefix) { 181 return new StatusInfo(IStatus.ERROR, PreferencesMessages.NameConventionConfigurationBlock_error_emptyprefix); 182 } else { 183 return new StatusInfo(IStatus.ERROR, PreferencesMessages.NameConventionConfigurationBlock_error_emptysuffix); 184 } 185 } 186 String name= prefix ? val + "x" : "x" + val; IStatus status= JavaConventions.validateFieldName(name); 188 if (status.matches(IStatus.ERROR)) { 189 if (prefix) { 190 return new StatusInfo(IStatus.ERROR, Messages.format(PreferencesMessages.NameConventionConfigurationBlock_error_invalidprefix, val)); 191 } else { 192 return new StatusInfo(IStatus.ERROR, Messages.format(PreferencesMessages.NameConventionConfigurationBlock_error_invalidsuffix, val)); 193 } 194 } 195 } 196 return new StatusInfo(); 197 } 198 199 202 protected void configureShell(Shell newShell) { 203 super.configureShell(newShell); 204 } 206 } 207 208 private static class NameConventionLabelProvider extends LabelProvider implements ITableLabelProvider { 209 210 213 public Image getImage(Object element) { 214 return getColumnImage(element, 0); 215 } 216 217 220 public String getText(Object element) { 221 return getColumnText(element, 0); 222 } 223 224 227 public Image getColumnImage(Object element, int columnIndex) { 228 if (columnIndex != 0) { 229 return null; 230 } 231 232 NameConventionEntry entry= (NameConventionEntry) element; 233 ImageDescriptorRegistry registry= JavaPlugin.getImageDescriptorRegistry(); 234 switch (entry.kind) { 235 case FIELD: 236 return registry.get(JavaPluginImages.DESC_FIELD_PUBLIC); 237 case STATIC: 238 return registry.get(new JavaElementImageDescriptor(JavaPluginImages.DESC_FIELD_PUBLIC, JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE)); 239 case ARGUMENT: 240 return registry.get(JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE); 241 default: 242 return registry.get(JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE); 243 } 244 } 245 248 public String getColumnText(Object element, int columnIndex) { 249 NameConventionEntry entry= (NameConventionEntry) element; 250 if (columnIndex == 0) { 251 switch (entry.kind) { 252 case FIELD: 253 return PreferencesMessages.NameConventionConfigurationBlock_field_label; 254 case STATIC: 255 return PreferencesMessages.NameConventionConfigurationBlock_static_label; 256 case ARGUMENT: 257 return PreferencesMessages.NameConventionConfigurationBlock_arg_label; 258 default: 259 return PreferencesMessages.NameConventionConfigurationBlock_local_label; 260 } 261 } else if (columnIndex == 1) { 262 return entry.prefix; 263 } else { 264 return entry.suffix; 265 } 266 } 267 } 268 269 private class NameConventionAdapter implements IListAdapter, IDialogFieldListener { 270 271 private boolean canEdit(ListDialogField field) { 272 return field.getSelectedElements().size() == 1; 273 } 274 275 public void customButtonPressed(ListDialogField field, int index) { 276 doEditButtonPressed(); 277 } 278 279 public void selectionChanged(ListDialogField field) { 280 field.enableButton(0, canEdit(field)); 281 } 282 283 public void doubleClicked(ListDialogField field) { 284 if (canEdit(field)) { 285 doEditButtonPressed(); 286 } 287 } 288 289 public void dialogFieldChanged(DialogField field) { 290 updateModel(field); 291 } 292 } 293 294 private ListDialogField fNameConventionList; 295 private SelectionButtonDialogField fUseKeywordThisBox; 296 private SelectionButtonDialogField fUseIsForBooleanGettersBox; 297 298 private StringDialogField fExceptionName; 299 private SelectionButtonDialogField fUseOverrideAnnotation; 300 private SelectionButtonDialogField fGenerateComments; 301 302 303 public NameConventionConfigurationBlock(IStatusChangeListener context, IProject project, IWorkbenchPreferenceContainer container) { 304 super(context, project, getAllKeys(), container); 305 306 NameConventionAdapter adapter= new NameConventionAdapter(); 307 String [] buttons= new String [] { 308 PreferencesMessages.NameConventionConfigurationBlock_list_edit_button 309 }; 310 fNameConventionList= new ListDialogField(adapter, buttons, new NameConventionLabelProvider()); 311 fNameConventionList.setDialogFieldListener(adapter); 312 fNameConventionList.setLabelText(PreferencesMessages.NameConventionConfigurationBlock_list_label); 313 314 String [] columnsHeaders= new String [] { 315 PreferencesMessages.NameConventionConfigurationBlock_list_name_column, 316 PreferencesMessages.NameConventionConfigurationBlock_list_prefix_column, 317 PreferencesMessages.NameConventionConfigurationBlock_list_suffix_column, 318 }; 319 ColumnLayoutData[] data= new ColumnLayoutData[] { 320 new ColumnWeightData(3), 321 new ColumnWeightData(2), 322 new ColumnWeightData(2) 323 }; 324 325 fNameConventionList.setTableColumns(new ListDialogField.ColumnsDescription(data, columnsHeaders, true)); 326 327 if (fNameConventionList.getSize() > 0) { 328 fNameConventionList.selectFirstElement(); 329 } else { 330 fNameConventionList.enableButton(0, false); 331 } 332 333 fExceptionName= new StringDialogField(); 334 fExceptionName.setDialogFieldListener(adapter); 335 fExceptionName.setLabelText(PreferencesMessages.NameConventionConfigurationBlock_exceptionname_label); 336 337 fUseKeywordThisBox= new SelectionButtonDialogField(SWT.CHECK | SWT.WRAP); 338 fUseKeywordThisBox.setDialogFieldListener(adapter); 339 fUseKeywordThisBox.setLabelText(PreferencesMessages.NameConventionConfigurationBlock_keywordthis_label); 340 341 fUseIsForBooleanGettersBox= new SelectionButtonDialogField(SWT.CHECK | SWT.WRAP); 342 fUseIsForBooleanGettersBox.setDialogFieldListener(adapter); 343 fUseIsForBooleanGettersBox.setLabelText(PreferencesMessages.NameConventionConfigurationBlock_isforbooleangetters_label); 344 345 fUseOverrideAnnotation= new SelectionButtonDialogField(SWT.CHECK | SWT.WRAP); 346 fUseOverrideAnnotation.setDialogFieldListener(adapter); 347 fUseOverrideAnnotation.setLabelText(PreferencesMessages.NameConventionConfigurationBlock_use_override_annotation_label); 348 349 fGenerateComments= new SelectionButtonDialogField(SWT.CHECK | SWT.WRAP); 350 fGenerateComments.setDialogFieldListener(adapter); 351 fGenerateComments.setLabelText(PreferencesMessages.CodeTemplateBlock_createcomment_label); 352 353 updateControls(); 354 } 355 356 private static Key[] getAllKeys() { 357 return new Key[] { 358 PREF_FIELD_PREFIXES, PREF_FIELD_SUFFIXES, PREF_STATIC_FIELD_PREFIXES, PREF_STATIC_FIELD_SUFFIXES, 359 PREF_ARGUMENT_PREFIXES, PREF_ARGUMENT_SUFFIXES, PREF_LOCAL_PREFIXES, PREF_LOCAL_SUFFIXES, 360 PREF_EXCEPTION_NAME, PREF_KEYWORD_THIS, PREF_IS_FOR_GETTERS, PREF_USE_OVERRIDE_ANNOT, PREF_GENERATE_COMMENTS 361 }; 362 } 363 364 protected Control createContents(Composite parent) { 365 setShell(parent.getShell()); 366 367 GridLayout layout= new GridLayout(); 368 layout.numColumns= 3; 369 layout.marginHeight= 0; 370 layout.marginWidth= 0; 371 372 Composite composite= new Composite(parent, SWT.NONE); 373 composite.setFont(parent.getFont()); 374 composite.setLayout(layout); 375 376 fNameConventionList.doFillIntoGrid(composite, 4); 377 LayoutUtil.setHorizontalSpan(fNameConventionList.getLabelControl(null), 2); 378 Table table= fNameConventionList.getTableViewer().getTable(); 379 GridData data= (GridData)fNameConventionList.getListControl(null).getLayoutData(); 380 data.heightHint= SWTUtil.getTableHeightHint(table, 5); 381 data.grabExcessHorizontalSpace= true; 382 data.verticalAlignment= GridData.BEGINNING; 383 data.grabExcessVerticalSpace= false; 384 385 data= (GridData)fNameConventionList.getButtonBox(null).getLayoutData(); 386 data.grabExcessVerticalSpace= false; 387 data.verticalAlignment= GridData.BEGINNING; 388 389 fUseKeywordThisBox.doFillIntoGrid(composite, 3); 390 fUseIsForBooleanGettersBox.doFillIntoGrid(composite, 3); 391 DialogField.createEmptySpace(composite, 3); 392 393 fGenerateComments.doFillIntoGrid(composite, 3); 394 fUseOverrideAnnotation.doFillIntoGrid(composite, 3); 395 DialogField.createEmptySpace(composite, 3); 396 397 fExceptionName.doFillIntoGrid(composite, 2); 398 399 return composite; 400 } 401 402 405 protected void validateSettings(Key changedKey, String oldValue, String newValue) { 406 } 408 409 protected final void updateModel(DialogField field) { 410 if (field == fNameConventionList) { 411 for (int i= 0; i < fNameConventionList.getSize(); i++) { 412 NameConventionEntry entry= (NameConventionEntry) fNameConventionList.getElement(i); 413 setValue(entry.suffixkey, entry.suffix); 414 setValue(entry.prefixkey, entry.prefix); 415 } 416 } else if (field == fExceptionName) { 417 String name= fExceptionName.getText(); 418 419 setValue(PREF_EXCEPTION_NAME, name); 420 421 IStatus status = JavaConventions.validateIdentifier(name); 423 if (!status.isOK()) { 424 fContext.statusChanged(status); 425 } else { 426 fContext.statusChanged(new StatusInfo()); 427 } 428 } else if (field == fUseKeywordThisBox) { 429 setValue(PREF_KEYWORD_THIS, fUseKeywordThisBox.isSelected()); 430 } else if (field == fUseIsForBooleanGettersBox) { 431 setValue(PREF_IS_FOR_GETTERS, fUseIsForBooleanGettersBox.isSelected()); 432 } else if (field == fUseOverrideAnnotation) { 433 setValue(PREF_USE_OVERRIDE_ANNOT, fUseOverrideAnnotation.isSelected()); 434 } else if (field == fGenerateComments) { 435 setValue(PREF_GENERATE_COMMENTS, fGenerateComments.isSelected()); 436 } 437 } 438 439 440 443 protected void updateControls() { 444 ArrayList list= new ArrayList (4); 445 createEntry(list, PREF_FIELD_PREFIXES, PREF_FIELD_SUFFIXES, FIELD); 446 createEntry(list, PREF_STATIC_FIELD_PREFIXES, PREF_STATIC_FIELD_SUFFIXES, STATIC); 447 createEntry(list, PREF_ARGUMENT_PREFIXES, PREF_ARGUMENT_SUFFIXES, ARGUMENT); 448 createEntry(list, PREF_LOCAL_PREFIXES, PREF_LOCAL_SUFFIXES, LOCAL); 449 fNameConventionList.setElements(list); 450 451 fExceptionName.setText(getValue(PREF_EXCEPTION_NAME)); 452 fUseKeywordThisBox.setSelection(getBooleanValue(PREF_KEYWORD_THIS)); 453 fUseIsForBooleanGettersBox.setSelection(getBooleanValue(PREF_IS_FOR_GETTERS)); 454 fUseOverrideAnnotation.setSelection(getBooleanValue(PREF_USE_OVERRIDE_ANNOT)); 455 fGenerateComments.setSelection(getBooleanValue(PREF_GENERATE_COMMENTS)); 456 } 457 458 461 protected String [] getFullBuildDialogStrings(boolean workspaceSettings) { 462 return null; } 464 465 private void createEntry(List list, Key prefixKey, Key suffixKey, int kind) { 466 NameConventionEntry entry= new NameConventionEntry(); 467 entry.kind= kind; 468 entry.suffixkey= suffixKey; 469 entry.prefixkey= prefixKey; 470 entry.suffix= getPreferenceValue(suffixKey); 471 entry.prefix= getPreferenceValue(prefixKey); 472 list.add(entry); 473 } 474 475 private String getPreferenceValue(Key key) { 476 String value= getValue(key); 477 if (value == null) { 478 value= ""; JavaPlugin.logErrorMessage("JavaCore preference is null. Key:" + key); } 481 return value; 482 } 483 484 private void doEditButtonPressed() { 485 NameConventionEntry entry= (NameConventionEntry) fNameConventionList.getSelectedElements().get(0); 486 487 String title; 488 String message; 489 switch (entry.kind) { 490 case FIELD: 491 title= PreferencesMessages.NameConventionConfigurationBlock_field_dialog_title; 492 message= PreferencesMessages.NameConventionConfigurationBlock_field_dialog_message; 493 break; 494 case STATIC: 495 title= PreferencesMessages.NameConventionConfigurationBlock_static_dialog_title; 496 message= PreferencesMessages.NameConventionConfigurationBlock_static_dialog_message; 497 break; 498 case ARGUMENT: 499 title= PreferencesMessages.NameConventionConfigurationBlock_arg_dialog_title; 500 message= PreferencesMessages.NameConventionConfigurationBlock_arg_dialog_message; 501 break; 502 default: 503 title= PreferencesMessages.NameConventionConfigurationBlock_local_dialog_title; 504 message= PreferencesMessages.NameConventionConfigurationBlock_local_dialog_message; 505 } 506 507 NameConventionInputDialog dialog= new NameConventionInputDialog(getShell(), title, message, entry); 508 if (dialog.open() == Window.OK) { 509 fNameConventionList.replaceElement(entry, dialog.getResult()); 510 updateModel(fNameConventionList); 511 } 512 } 513 } 514 | Popular Tags |