1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
13 import java.util.StringTokenizer;
15 import net.sourceforge.phpdt.core.Flags;
16 import net.sourceforge.phpdt.ui.PreferenceConstants;
18 import org.eclipse.jface.preference.IPreferenceStore;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
24 public class MembersOrderPreferenceCache implements IPropertyChangeListener {
26 public static final int TYPE_INDEX = 0;
28 public static final int CONSTRUCTORS_INDEX = 1;
30 public static final int METHOD_INDEX = 2;
32 public static final int FIELDS_INDEX = 3;
34 public static final int INIT_INDEX = 4;
36 public static final int STATIC_FIELDS_INDEX = 5;
38 public static final int STATIC_INIT_INDEX = 6;
40 public static final int STATIC_METHODS_INDEX = 7;
42 public static final int N_CATEGORIES = STATIC_METHODS_INDEX + 1;
44 private static final int PUBLIC_INDEX = 0;
46 private static final int PRIVATE_INDEX = 1;
48 private static final int PROTECTED_INDEX = 2;
50 private static final int DEFAULT_INDEX = 3;
52 private static final int N_VISIBILITIES = DEFAULT_INDEX + 1;
54 private int[] fCategoryOffsets = null;
56 private boolean fSortByVisibility;
58 private int[] fVisibilityOffsets = null;
60 public MembersOrderPreferenceCache() {
61 fCategoryOffsets = null;
62 fSortByVisibility = PreferenceConstants
65 PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER);
66 fVisibilityOffsets = null;
69 public static boolean isMemberOrderProperty(String property) {
70 return PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER
72 || PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER
74 || PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER
78 public void propertyChange(PropertyChangeEvent event) {
79 String property = event.getProperty();
81 if (PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER.equals(property)) {
82 fCategoryOffsets = null;
83 } else if (PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER
85 fVisibilityOffsets = null;
86 } else if (PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER
88 fSortByVisibility = PreferenceConstants
91 PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER);
95 public int getCategoryIndex(int kind) {
96 if (fCategoryOffsets == null) {
97 fCategoryOffsets = getCategoryOffsets();
99 return fCategoryOffsets[kind];
102 private int[] getCategoryOffsets() {
103 int[] offsets = new int[N_CATEGORIES];
104 IPreferenceStore store = PreferenceConstants.getPreferenceStore();
105 String key = PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER;
106 boolean success = fillCategoryOffsetsFromPreferenceString(store
107 .getString(key), offsets);
109 store.setToDefault(key);
110 fillCategoryOffsetsFromPreferenceString(
111 store.getDefaultString(key), offsets);
116 private boolean fillCategoryOffsetsFromPreferenceString(String str,
118 StringTokenizer tokenizer = new StringTokenizer(str, ","); //$NON-NLS-1$
120 while (tokenizer.hasMoreTokens()) {
121 String token = tokenizer.nextToken().trim();
122 if ("T".equals(token)) { //$NON-NLS-1$
123 offsets[TYPE_INDEX] = i++;
124 } else if ("M".equals(token)) { //$NON-NLS-1$
125 offsets[METHOD_INDEX] = i++;
126 } else if ("F".equals(token)) { //$NON-NLS-1$
127 offsets[FIELDS_INDEX] = i++;
128 } else if ("I".equals(token)) { //$NON-NLS-1$
129 offsets[INIT_INDEX] = i++;
130 } else if ("SF".equals(token)) { //$NON-NLS-1$
131 offsets[STATIC_FIELDS_INDEX] = i++;
132 } else if ("SI".equals(token)) { //$NON-NLS-1$
133 offsets[STATIC_INIT_INDEX] = i++;
134 } else if ("SM".equals(token)) { //$NON-NLS-1$
135 offsets[STATIC_METHODS_INDEX] = i++;
136 } else if ("C".equals(token)) { //$NON-NLS-1$
137 offsets[CONSTRUCTORS_INDEX] = i++;
140 return i == N_CATEGORIES;
143 public boolean isSortByVisibility() {
144 return fSortByVisibility;
147 public int getVisibilityIndex(int modifierFlags) {
148 if (fVisibilityOffsets == null) {
149 fVisibilityOffsets = getVisibilityOffsets();
151 int kind = DEFAULT_INDEX;
152 if (Flags.isPublic(modifierFlags)) {
154 } else if (Flags.isProtected(modifierFlags)) {
155 kind = PROTECTED_INDEX;
156 } else if (Flags.isPrivate(modifierFlags)) {
157 kind = PRIVATE_INDEX;
160 return fVisibilityOffsets[kind];
163 private int[] getVisibilityOffsets() {
164 int[] offsets = new int[N_VISIBILITIES];
165 IPreferenceStore store = PreferenceConstants.getPreferenceStore();
166 String key = PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER;
167 boolean success = fillVisibilityOffsetsFromPreferenceString(store
168 .getString(key), offsets);
170 store.setToDefault(key);
171 fillVisibilityOffsetsFromPreferenceString(store
172 .getDefaultString(key), offsets);
177 private boolean fillVisibilityOffsetsFromPreferenceString(String str,
179 StringTokenizer tokenizer = new StringTokenizer(str, ","); //$NON-NLS-1$
181 while (tokenizer.hasMoreTokens()) {
182 String token = tokenizer.nextToken().trim();
183 if ("B".equals(token)) { //$NON-NLS-1$
184 offsets[PUBLIC_INDEX] = i++;
185 } else if ("V".equals(token)) { //$NON-NLS-1$
186 offsets[PRIVATE_INDEX] = i++;
187 } else if ("R".equals(token)) { //$NON-NLS-1$
188 offsets[PROTECTED_INDEX] = i++;
189 } else if ("D".equals(token)) { //$NON-NLS-1$
190 offsets[DEFAULT_INDEX] = i++;
193 return i == N_VISIBILITIES;