1 11 12 package org.eclipse.jdt.internal.ui.preferences; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.StringTokenizer ; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Control; 25 26 import org.eclipse.jface.dialogs.Dialog; 27 import org.eclipse.jface.preference.IPreferenceStore; 28 import org.eclipse.jface.preference.PreferencePage; 29 import org.eclipse.jface.resource.ImageDescriptor; 30 import org.eclipse.jface.viewers.LabelProvider; 31 32 import org.eclipse.ui.IWorkbench; 33 import org.eclipse.ui.IWorkbenchPreferencePage; 34 import org.eclipse.ui.PlatformUI; 35 36 import org.eclipse.jdt.core.Flags; 37 38 import org.eclipse.jdt.ui.JavaElementImageDescriptor; 39 import org.eclipse.jdt.ui.PreferenceConstants; 40 41 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 42 import org.eclipse.jdt.internal.ui.JavaPlugin; 43 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry; 44 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; 45 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 46 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 47 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; 48 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 49 50 public class MembersOrderPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { 51 52 public static final String PREF_ID= "org.eclipse.jdt.ui.preferences.MembersOrderPreferencePage"; 54 private static final String ALL_SORTMEMBER_ENTRIES= "T,SI,SF,SM,I,F,C,M"; private static final String ALL_VISIBILITY_ENTRIES= "B,V,R,D"; private static final String PREF_OUTLINE_SORT_OPTION= PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER; 57 private static final String PREF_VISIBILITY_SORT_OPTION= PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER; 58 private static final String PREF_USE_VISIBILITY_SORT_OPTION= PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER; 59 60 public static final String CONSTRUCTORS= "C"; public static final String FIELDS= "F"; public static final String METHODS= "M"; public static final String STATIC_METHODS= "SM"; public static final String STATIC_FIELDS= "SF"; public static final String INIT= "I"; public static final String STATIC_INIT= "SI"; public static final String TYPES= "T"; 69 public static final String PUBLIC= "B"; public static final String PRIVATE= "V"; public static final String PROTECTED= "R"; public static final String DEFAULT= "D"; 74 private boolean fUseVisibilitySort; 75 private ListDialogField fSortOrderList; 76 private ListDialogField fVisibilityOrderList; 77 private SelectionButtonDialogField fUseVisibilitySortField; 78 79 private static boolean isValidEntries(List entries, String entryString) { 80 StringTokenizer tokenizer= new StringTokenizer (entryString, ","); int i= 0; 82 for (; tokenizer.hasMoreTokens(); i++) { 83 String token= tokenizer.nextToken(); 84 if (!entries.contains(token)) 85 return false; 86 } 87 return i == entries.size(); 88 } 89 90 public MembersOrderPreferencePage() { 91 setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore()); 93 94 setDescription(PreferencesMessages.MembersOrderPreferencePage_label_description); 95 96 String memberSortString= getPreferenceStore().getString(PREF_OUTLINE_SORT_OPTION); 97 98 String upLabel= PreferencesMessages.MembersOrderPreferencePage_category_button_up; 99 String downLabel= PreferencesMessages.MembersOrderPreferencePage_category_button_down; 100 101 103 fSortOrderList= new ListDialogField(null, new String [] { upLabel, downLabel }, new MemberSortLabelProvider()); 104 fSortOrderList.setDownButtonIndex(1); 105 fSortOrderList.setUpButtonIndex(0); 106 107 List entries= parseList(memberSortString); 109 if (!isValidEntries(entries, ALL_SORTMEMBER_ENTRIES)) { 110 memberSortString= getPreferenceStore().getDefaultString(PREF_OUTLINE_SORT_OPTION); 111 entries= parseList(memberSortString); 112 } 113 114 fSortOrderList.setElements(entries); 115 116 118 fUseVisibilitySort= getPreferenceStore().getBoolean(PREF_USE_VISIBILITY_SORT_OPTION); 119 120 String visibilitySortString= getPreferenceStore().getString(PREF_VISIBILITY_SORT_OPTION); 121 122 upLabel= PreferencesMessages.MembersOrderPreferencePage_visibility_button_up; 123 downLabel= PreferencesMessages.MembersOrderPreferencePage_visibility_button_down; 124 125 fVisibilityOrderList= new ListDialogField(null, new String [] { upLabel, downLabel }, new VisibilitySortLabelProvider()); 126 fVisibilityOrderList.setDownButtonIndex(1); 127 fVisibilityOrderList.setUpButtonIndex(0); 128 129 entries= parseList(visibilitySortString); 131 if (!isValidEntries(entries, ALL_VISIBILITY_ENTRIES)) { 132 visibilitySortString= getPreferenceStore().getDefaultString(PREF_VISIBILITY_SORT_OPTION); 133 entries= parseList(visibilitySortString); 134 } 135 fVisibilityOrderList.setElements(entries); 136 } 137 138 private static List parseList(String string) { 139 StringTokenizer tokenizer= new StringTokenizer (string, ","); List entries= new ArrayList (); 141 for (int i= 0; tokenizer.hasMoreTokens(); i++) { 142 String token= tokenizer.nextToken(); 143 entries.add(token); 144 } 145 return entries; 146 } 147 148 151 public void createControl(Composite parent) { 152 super.createControl(parent); 153 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.SORT_ORDER_PREFERENCE_PAGE); 154 } 155 156 159 protected Control createContents(Composite parent) { 160 Composite sortComposite= new Composite(parent, SWT.NONE); 162 sortComposite.setFont(parent.getFont()); 163 164 GridLayout layout= new GridLayout(); 165 layout.numColumns= 2; 166 layout.marginWidth= 0; 167 layout.marginHeight= 0; 168 sortComposite.setLayout(layout); 169 170 GridData gd= new GridData(); 171 gd.verticalAlignment= GridData.FILL; 172 gd.horizontalAlignment= GridData.FILL_HORIZONTAL; 173 sortComposite.setLayoutData(gd); 174 175 createListDialogField(sortComposite, fSortOrderList); 176 177 fUseVisibilitySortField= new SelectionButtonDialogField(SWT.CHECK); 178 fUseVisibilitySortField.setDialogFieldListener(new IDialogFieldListener() { 179 public void dialogFieldChanged(DialogField field) { 180 fVisibilityOrderList.setEnabled(fUseVisibilitySortField.isSelected()); 181 } 182 }); 183 fUseVisibilitySortField.setLabelText(PreferencesMessages.MembersOrderPreferencePage_usevisibilitysort_label); 184 fUseVisibilitySortField.doFillIntoGrid(sortComposite, 2); 185 fUseVisibilitySortField.setSelection(fUseVisibilitySort); 186 187 createListDialogField(sortComposite, fVisibilityOrderList); 188 fVisibilityOrderList.setEnabled(fUseVisibilitySortField.isSelected()); 189 190 Dialog.applyDialogFont(sortComposite); 191 192 return sortComposite; 193 } 194 195 196 private void createListDialogField(Composite composite, ListDialogField dialogField) { 197 Control list= dialogField.getListControl(composite); 198 GridData gd= new GridData(); 199 gd.horizontalAlignment= GridData.FILL; 200 gd.grabExcessHorizontalSpace= true; 201 gd.verticalAlignment= GridData.FILL; 202 gd.grabExcessVerticalSpace= true; 203 gd.widthHint= convertWidthInCharsToPixels(50); 204 205 list.setLayoutData(gd); 206 207 Composite buttons= dialogField.getButtonBox(composite); 208 gd= new GridData(); 209 gd.horizontalAlignment= GridData.FILL; 210 gd.grabExcessHorizontalSpace= false; 211 gd.verticalAlignment= GridData.FILL; 212 gd.grabExcessVerticalSpace= true; 213 214 buttons.setLayoutData(gd); 215 } 216 217 220 public void init(IWorkbench workbench) { 221 } 222 223 226 protected void performDefaults() { 227 IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore(); 228 String str= prefs.getDefaultString(PREF_OUTLINE_SORT_OPTION); 229 if (str != null) 230 fSortOrderList.setElements(parseList(str)); 231 else 232 fSortOrderList.setElements(parseList(ALL_SORTMEMBER_ENTRIES)); 233 234 str= prefs.getDefaultString(PREF_VISIBILITY_SORT_OPTION); 235 if (str != null) 236 fVisibilityOrderList.setElements(parseList(str)); 237 else 238 fVisibilityOrderList.setElements(parseList(ALL_VISIBILITY_ENTRIES)); 239 240 fUseVisibilitySortField.setSelection(prefs.getDefaultBoolean(PREF_USE_VISIBILITY_SORT_OPTION)); 241 242 super.performDefaults(); 243 } 244 245 248 public boolean performOk() { 250 251 IPreferenceStore store= getPreferenceStore(); 253 updateList(store, fSortOrderList, PREF_OUTLINE_SORT_OPTION); 254 updateList(store, fVisibilityOrderList, PREF_VISIBILITY_SORT_OPTION); 255 256 store.setValue(PREF_USE_VISIBILITY_SORT_OPTION, fUseVisibilitySortField.isSelected()); 258 JavaPlugin.getDefault().savePluginPreferences(); 259 260 return true; 261 } 262 263 private void updateList(IPreferenceStore store, ListDialogField list, String str) { 264 StringBuffer buf= new StringBuffer (); 265 List curr= list.getElements(); 266 for (Iterator iter= curr.iterator(); iter.hasNext();) { 267 String s= (String ) iter.next(); 268 buf.append(s); 269 buf.append(','); 270 } 271 store.setValue(str, buf.toString()); 272 } 273 274 private class MemberSortLabelProvider extends LabelProvider { 275 276 public MemberSortLabelProvider() { 277 } 278 279 282 public Image getImage(Object element) { 283 ImageDescriptorRegistry registry= JavaPlugin.getImageDescriptorRegistry(); 285 ImageDescriptor descriptor= null; 286 287 if (element instanceof String ) { 288 int visibility= Flags.AccPublic; 289 String s= (String ) element; 290 if (s.equals(FIELDS)) { 291 descriptor= JavaElementImageProvider.getFieldImageDescriptor(false, visibility); 293 } else if (s.equals(CONSTRUCTORS)) { 294 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility); 295 descriptor= new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE); 297 } else if (s.equals(METHODS)) { 298 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility); 299 } else if (s.equals(STATIC_FIELDS)) { 300 descriptor= JavaElementImageProvider.getFieldImageDescriptor(false, visibility); 301 descriptor= new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE); 303 } else if (s.equals(STATIC_METHODS)) { 304 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility); 305 descriptor= new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE); 307 } else if (s.equals(INIT)) { 308 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility); 309 } else if (s.equals(STATIC_INIT)) { 310 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility); 311 descriptor= new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE); 312 } else if (s.equals(TYPES)) { 313 descriptor= JavaElementImageProvider.getTypeImageDescriptor(true, false, Flags.AccPublic, false); 314 } else { 315 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccPublic); 316 } 317 return registry.get(descriptor); 318 } 319 return null; 320 } 321 322 325 public String getText(Object element) { 326 327 if (element instanceof String ) { 328 String s= (String ) element; 329 if (s.equals(FIELDS)) { 330 return PreferencesMessages.MembersOrderPreferencePage_fields_label; 331 } else if (s.equals(METHODS)) { 332 return PreferencesMessages.MembersOrderPreferencePage_methods_label; 333 } else if (s.equals(STATIC_FIELDS)) { 334 return PreferencesMessages.MembersOrderPreferencePage_staticfields_label; 335 } else if (s.equals(STATIC_METHODS)) { 336 return PreferencesMessages.MembersOrderPreferencePage_staticmethods_label; 337 } else if (s.equals(CONSTRUCTORS)) { 338 return PreferencesMessages.MembersOrderPreferencePage_constructors_label; 339 } else if (s.equals(INIT)) { 340 return PreferencesMessages.MembersOrderPreferencePage_initialisers_label; 341 } else if (s.equals(STATIC_INIT)) { 342 return PreferencesMessages.MembersOrderPreferencePage_staticinitialisers_label; 343 } else if (s.equals(TYPES)) { 344 return PreferencesMessages.MembersOrderPreferencePage_types_label; 345 } 346 } 347 return ""; } 349 } 350 351 352 private class VisibilitySortLabelProvider extends LabelProvider { 353 354 public VisibilitySortLabelProvider() { 355 } 356 357 360 public Image getImage(Object element) { 361 ImageDescriptorRegistry registry= JavaPlugin.getImageDescriptorRegistry(); 363 ImageDescriptor descriptor= null; 364 365 if (element instanceof String ) { 366 String s= (String ) element; 367 if (s.equals(PUBLIC)) { 368 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccPublic); 369 } else if (s.equals(PRIVATE)) { 370 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccPrivate); 371 } else if (s.equals(PROTECTED)) { 372 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccProtected); 373 } else if (s.equals(DEFAULT)) { 374 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccDefault); 375 } 376 return registry.get(descriptor); 377 } 378 return null; 379 } 380 381 384 public String getText(Object element) { 385 if (element instanceof String ) { 386 String s= (String ) element; 387 388 if (s.equals(PUBLIC)) { 389 return PreferencesMessages.MembersOrderPreferencePage_public_label; 390 } else if (s.equals(PRIVATE)) { 391 return PreferencesMessages.MembersOrderPreferencePage_private_label; 392 } else if (s.equals(PROTECTED)) { 393 return PreferencesMessages.MembersOrderPreferencePage_protected_label; 394 } else if (s.equals(DEFAULT)) { 395 return PreferencesMessages.MembersOrderPreferencePage_default_label; 396 } 397 } 398 return ""; } 400 } 401 402 403 404 } 405 | Popular Tags |