1) Reintroduced some out commented methods. (Did break some preference settings)
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / preferences / MembersOrderPreferenceCache.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
12
13 import java.util.StringTokenizer;
14
15 import net.sourceforge.phpdt.core.Flags;
16 import net.sourceforge.phpdt.ui.PreferenceConstants;
17
18 import org.eclipse.jface.preference.IPreferenceStore;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21
22 /**
23  */
24 public class MembersOrderPreferenceCache implements IPropertyChangeListener {
25
26         public static final int TYPE_INDEX = 0;
27
28         public static final int CONSTRUCTORS_INDEX = 1;
29
30         public static final int METHOD_INDEX = 2;
31
32         public static final int FIELDS_INDEX = 3;
33
34         public static final int INIT_INDEX = 4;
35
36         public static final int STATIC_FIELDS_INDEX = 5;
37
38         public static final int STATIC_INIT_INDEX = 6;
39
40         public static final int STATIC_METHODS_INDEX = 7;
41
42         public static final int N_CATEGORIES = STATIC_METHODS_INDEX + 1;
43
44         private static final int PUBLIC_INDEX = 0;
45
46         private static final int PRIVATE_INDEX = 1;
47
48         private static final int PROTECTED_INDEX = 2;
49
50         private static final int DEFAULT_INDEX = 3;
51
52         private static final int N_VISIBILITIES = DEFAULT_INDEX + 1;
53
54         private int[] fCategoryOffsets = null;
55
56         private boolean fSortByVisibility;
57
58         private int[] fVisibilityOffsets = null;
59
60         public MembersOrderPreferenceCache() {
61                 fCategoryOffsets = null;
62                 fSortByVisibility = PreferenceConstants
63                                 .getPreferenceStore()
64                                 .getBoolean(
65                                                 PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER);
66                 fVisibilityOffsets = null;
67         }
68
69         public static boolean isMemberOrderProperty(String property) {
70                 return PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER
71                                 .equals(property)
72                                 || PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER
73                                                 .equals(property)
74                                 || PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER
75                                                 .equals(property);
76         }
77
78         public void propertyChange(PropertyChangeEvent event) {
79                 String property = event.getProperty();
80
81                 if (PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER.equals(property)) {
82                         fCategoryOffsets = null;
83                 } else if (PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER
84                                 .equals(property)) {
85                         fVisibilityOffsets = null;
86                 } else if (PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER
87                                 .equals(property)) {
88                         fSortByVisibility = PreferenceConstants
89                                         .getPreferenceStore()
90                                         .getBoolean(
91                                                         PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER);
92                 }
93         }
94
95         public int getCategoryIndex(int kind) {
96                 if (fCategoryOffsets == null) {
97                         fCategoryOffsets = getCategoryOffsets();
98                 }
99                 return fCategoryOffsets[kind];
100         }
101
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);
108                 if (!success) {
109                         store.setToDefault(key);
110                         fillCategoryOffsetsFromPreferenceString(
111                                         store.getDefaultString(key), offsets);
112                 }
113                 return offsets;
114         }
115
116         private boolean fillCategoryOffsetsFromPreferenceString(String str,
117                         int[] offsets) {
118                 StringTokenizer tokenizer = new StringTokenizer(str, ","); //$NON-NLS-1$
119                 int i = 0;
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++;
138                         }
139                 }
140                 return i == N_CATEGORIES;
141         }
142
143         public boolean isSortByVisibility() {
144                 return fSortByVisibility;
145         }
146
147         public int getVisibilityIndex(int modifierFlags) {
148                 if (fVisibilityOffsets == null) {
149                         fVisibilityOffsets = getVisibilityOffsets();
150                 }
151                 int kind = DEFAULT_INDEX;
152                 if (Flags.isPublic(modifierFlags)) {
153                         kind = PUBLIC_INDEX;
154                 } else if (Flags.isProtected(modifierFlags)) {
155                         kind = PROTECTED_INDEX;
156                 } else if (Flags.isPrivate(modifierFlags)) {
157                         kind = PRIVATE_INDEX;
158                 }
159
160                 return fVisibilityOffsets[kind];
161         }
162
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);
169                 if (!success) {
170                         store.setToDefault(key);
171                         fillVisibilityOffsetsFromPreferenceString(store
172                                         .getDefaultString(key), offsets);
173                 }
174                 return offsets;
175         }
176
177         private boolean fillVisibilityOffsetsFromPreferenceString(String str,
178                         int[] offsets) {
179                 StringTokenizer tokenizer = new StringTokenizer(str, ","); //$NON-NLS-1$
180                 int i = 0;
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++;
191                         }
192                 }
193                 return i == N_VISIBILITIES;
194         }
195
196 }