1 11 package org.eclipse.jdt.internal.ui.preferences; 12 13 import java.util.StringTokenizer ; 14 15 import org.eclipse.jface.preference.IPreferenceStore; 16 import org.eclipse.jface.util.IPropertyChangeListener; 17 import org.eclipse.jface.util.PropertyChangeEvent; 18 19 import org.eclipse.jdt.core.Flags; 20 21 import org.eclipse.jdt.ui.PreferenceConstants; 22 23 25 public class MembersOrderPreferenceCache implements IPropertyChangeListener { 26 27 public static final int TYPE_INDEX= 0; 28 public static final int CONSTRUCTORS_INDEX= 1; 29 public static final int METHOD_INDEX= 2; 30 public static final int FIELDS_INDEX= 3; 31 public static final int INIT_INDEX= 4; 32 public static final int STATIC_FIELDS_INDEX= 5; 33 public static final int STATIC_INIT_INDEX= 6; 34 public static final int STATIC_METHODS_INDEX= 7; 35 public static final int ENUM_CONSTANTS_INDEX= 8; 36 public static final int N_CATEGORIES= ENUM_CONSTANTS_INDEX + 1; 37 38 private static final int PUBLIC_INDEX= 0; 39 private static final int PRIVATE_INDEX= 1; 40 private static final int PROTECTED_INDEX= 2; 41 private static final int DEFAULT_INDEX= 3; 42 private static final int N_VISIBILITIES= DEFAULT_INDEX + 1; 43 44 private int[] fCategoryOffsets= null; 45 46 private boolean fSortByVisibility; 47 private int[] fVisibilityOffsets= null; 48 49 private IPreferenceStore fPreferenceStore; 50 51 public MembersOrderPreferenceCache() { 52 fPreferenceStore= null; 53 fCategoryOffsets= null; 54 fSortByVisibility= false; 55 fVisibilityOffsets= null; 56 } 57 58 public void install(IPreferenceStore store) { 59 fPreferenceStore= store; 60 store.addPropertyChangeListener(this); 61 fSortByVisibility= store.getBoolean(PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER); 62 } 63 64 public void dispose() { 65 fPreferenceStore.removePropertyChangeListener(this); 66 fPreferenceStore= null; 67 } 68 69 public static boolean isMemberOrderProperty(String property) { 70 return PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER.equals(property) 71 || PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER.equals(property) 72 || PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER.equals(property); 73 } 74 75 public void propertyChange(PropertyChangeEvent event) { 76 String property= event.getProperty(); 77 78 if (PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER.equals(property)) { 79 fCategoryOffsets= null; 80 } else if (PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER.equals(property)) { 81 fVisibilityOffsets= null; 82 } else if (PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER.equals(property)) { 83 fSortByVisibility= fPreferenceStore.getBoolean(PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER); 84 } 85 } 86 87 public int getCategoryIndex(int kind) { 88 if (fCategoryOffsets == null) { 89 fCategoryOffsets= getCategoryOffsets(); 90 } 91 return fCategoryOffsets[kind]; 92 } 93 94 private int[] getCategoryOffsets() { 95 int[] offsets= new int[N_CATEGORIES]; 96 IPreferenceStore store= fPreferenceStore; 97 String key= PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER; 98 boolean success= fillCategoryOffsetsFromPreferenceString(store.getString(key), offsets); 99 if (!success) { 100 store.setToDefault(key); 101 fillCategoryOffsetsFromPreferenceString(store.getDefaultString(key), offsets); 102 } 103 return offsets; 104 } 105 106 private boolean fillCategoryOffsetsFromPreferenceString(String str, int[] offsets) { 107 StringTokenizer tokenizer= new StringTokenizer (str, ","); int i= 0; 109 offsets[ENUM_CONSTANTS_INDEX]= i++; 111 while (tokenizer.hasMoreTokens()) { 112 String token= tokenizer.nextToken().trim(); 113 if ("T".equals(token)) { offsets[TYPE_INDEX]= i++; 115 } else if ("M".equals(token)) { offsets[METHOD_INDEX]= i++; 117 } else if ("F".equals(token)) { offsets[FIELDS_INDEX]= i++; 119 } else if ("I".equals(token)) { offsets[INIT_INDEX]= i++; 121 } else if ("SF".equals(token)) { offsets[STATIC_FIELDS_INDEX]= i++; 123 } else if ("SI".equals(token)) { offsets[STATIC_INIT_INDEX]= i++; 125 } else if ("SM".equals(token)) { offsets[STATIC_METHODS_INDEX]= i++; 127 } else if ("C".equals(token)) { offsets[CONSTRUCTORS_INDEX]= i++; 129 } 130 } 131 return i == N_CATEGORIES; 132 } 133 134 public boolean isSortByVisibility() { 135 return fSortByVisibility; 136 } 137 138 public int getVisibilityIndex(int modifierFlags) { 139 if (fVisibilityOffsets == null) { 140 fVisibilityOffsets= getVisibilityOffsets(); 141 } 142 int kind= DEFAULT_INDEX; 143 if (Flags.isPublic(modifierFlags)) { 144 kind= PUBLIC_INDEX; 145 } else if (Flags.isProtected(modifierFlags)) { 146 kind= PROTECTED_INDEX; 147 } else if (Flags.isPrivate(modifierFlags)) { 148 kind= PRIVATE_INDEX; 149 } 150 151 return fVisibilityOffsets[kind]; 152 } 153 154 private int[] getVisibilityOffsets() { 155 int[] offsets= new int[N_VISIBILITIES]; 156 IPreferenceStore store= fPreferenceStore; 157 String key= PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER; 158 boolean success= fillVisibilityOffsetsFromPreferenceString(store.getString(key), offsets); 159 if (!success) { 160 store.setToDefault(key); 161 fillVisibilityOffsetsFromPreferenceString(store.getDefaultString(key), offsets); 162 } 163 return offsets; 164 } 165 166 private boolean fillVisibilityOffsetsFromPreferenceString(String str, int[] offsets) { 167 StringTokenizer tokenizer= new StringTokenizer (str, ","); int i= 0; 169 while (tokenizer.hasMoreTokens()) { 170 String token= tokenizer.nextToken().trim(); 171 if ("B".equals(token)) { offsets[PUBLIC_INDEX]= i++; 173 } else if ("V".equals(token)) { offsets[PRIVATE_INDEX]= i++; 175 } else if ("R".equals(token)) { offsets[PROTECTED_INDEX]= i++; 177 } else if ("D".equals(token)) { offsets[DEFAULT_INDEX]= i++; 179 } 180 } 181 return i == N_VISIBILITIES; 182 } 183 184 185 } 186 | Popular Tags |