Bug #1248155: avoid NPE in parser
[phpeclipse.git] / net.sourceforge.phpeclipse / 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         public static final int CONSTRUCTORS_INDEX= 1;
28         public static final int METHOD_INDEX= 2;
29         public static final int FIELDS_INDEX= 3;
30         public static final int INIT_INDEX= 4;
31         public static final int STATIC_FIELDS_INDEX= 5;
32         public static final int STATIC_INIT_INDEX= 6;
33         public static final int STATIC_METHODS_INDEX= 7;
34         public static final int N_CATEGORIES= STATIC_METHODS_INDEX + 1;
35         
36         private static final int PUBLIC_INDEX= 0;
37         private static final int PRIVATE_INDEX= 1;
38         private static final int PROTECTED_INDEX= 2;
39         private static final int DEFAULT_INDEX= 3;
40         private static final int N_VISIBILITIES= DEFAULT_INDEX + 1;     
41         
42         private int[] fCategoryOffsets= null;
43         
44         private boolean fSortByVisibility;
45         private int[] fVisibilityOffsets= null;
46         
47         public MembersOrderPreferenceCache() {
48                 fCategoryOffsets= null;
49                 fSortByVisibility= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER);
50                 fVisibilityOffsets= null;
51         }
52         
53         public static boolean isMemberOrderProperty(String property) {
54                 return PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER.equals(property)
55                         || PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER.equals(property)
56                         || PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER.equals(property);
57         }
58
59         public void propertyChange(PropertyChangeEvent event) {
60                 String property= event.getProperty();
61                 
62                 if (PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER.equals(property)) {
63                         fCategoryOffsets= null;
64                 } else if (PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER.equals(property)) {
65                         fVisibilityOffsets= null;
66                 } else if (PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER.equals(property)) {
67                         fSortByVisibility= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER);
68                 }
69         }
70
71         public int getCategoryIndex(int kind) {
72                 if (fCategoryOffsets == null) {
73                         fCategoryOffsets= getCategoryOffsets();
74                 }
75                 return fCategoryOffsets[kind];
76         }
77         
78         private int[] getCategoryOffsets() {
79                 int[] offsets= new int[N_CATEGORIES];
80                 IPreferenceStore store= PreferenceConstants.getPreferenceStore();
81                 String key= PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER;
82                 boolean success= fillCategoryOffsetsFromPreferenceString(store.getString(key), offsets);
83                 if (!success) {
84                         store.setToDefault(key);
85                         fillCategoryOffsetsFromPreferenceString(store.getDefaultString(key), offsets);  
86                 }
87                 return offsets;
88         }
89         
90         private boolean fillCategoryOffsetsFromPreferenceString(String str, int[] offsets) {
91                 StringTokenizer tokenizer= new StringTokenizer(str, ","); //$NON-NLS-1$
92                 int i= 0;
93                 while (tokenizer.hasMoreTokens()) {
94                         String token= tokenizer.nextToken().trim();
95                         if ("T".equals(token)) { //$NON-NLS-1$
96                                 offsets[TYPE_INDEX]= i++;
97                         } else if ("M".equals(token)) { //$NON-NLS-1$
98                                 offsets[METHOD_INDEX]= i++;
99                         } else if ("F".equals(token)) { //$NON-NLS-1$
100                                 offsets[FIELDS_INDEX]= i++;
101                         } else if ("I".equals(token)) { //$NON-NLS-1$
102                                 offsets[INIT_INDEX]= i++;
103                         } else if ("SF".equals(token)) { //$NON-NLS-1$
104                                 offsets[STATIC_FIELDS_INDEX]= i++;
105                         } else if ("SI".equals(token)) { //$NON-NLS-1$
106                                 offsets[STATIC_INIT_INDEX]= i++;
107                         } else if ("SM".equals(token)) { //$NON-NLS-1$
108                                 offsets[STATIC_METHODS_INDEX]= i++;
109                         } else if ("C".equals(token)) { //$NON-NLS-1$
110                                 offsets[CONSTRUCTORS_INDEX]= i++;
111                         }
112                 }
113                 return i == N_CATEGORIES;
114         }
115         
116         public boolean isSortByVisibility() {
117                 return fSortByVisibility;
118         }
119         
120         
121         public int getVisibilityIndex(int modifierFlags) {
122                 if (fVisibilityOffsets == null) {
123                         fVisibilityOffsets= getVisibilityOffsets();
124                 }
125                 int kind= DEFAULT_INDEX;
126                 if (Flags.isPublic(modifierFlags)) {
127                         kind= PUBLIC_INDEX;
128                 } else if (Flags.isProtected(modifierFlags)) {
129                         kind= PROTECTED_INDEX;
130                 } else if (Flags.isPrivate(modifierFlags)) {
131                         kind= PRIVATE_INDEX;
132                 }
133                 
134                 return fVisibilityOffsets[kind];
135         }
136         
137         private int[] getVisibilityOffsets() {
138                 int[] offsets= new int[N_VISIBILITIES];
139                 IPreferenceStore store= PreferenceConstants.getPreferenceStore();
140                 String key= PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER;
141                 boolean success= fillVisibilityOffsetsFromPreferenceString(store.getString(key), offsets);
142                 if (!success) {
143                         store.setToDefault(key);
144                         fillVisibilityOffsetsFromPreferenceString(store.getDefaultString(key), offsets);        
145                 }
146                 return offsets;
147         }       
148                 
149         private boolean fillVisibilityOffsetsFromPreferenceString(String str, int[] offsets) {
150                 StringTokenizer tokenizer= new StringTokenizer(str, ","); //$NON-NLS-1$
151                 int i= 0;
152                 while (tokenizer.hasMoreTokens()) {
153                         String token= tokenizer.nextToken().trim();
154                         if ("B".equals(token)) { //$NON-NLS-1$
155                                 offsets[PUBLIC_INDEX]= i++;
156                         } else if ("V".equals(token)) { //$NON-NLS-1$
157                                 offsets[PRIVATE_INDEX]= i++;
158                         } else if ("R".equals(token)) { //$NON-NLS-1$
159                                 offsets[PROTECTED_INDEX]= i++;
160                         } else if ("D".equals(token)) { //$NON-NLS-1$
161                                 offsets[DEFAULT_INDEX]= i++;
162                         }
163                 }
164                 return i == N_VISIBILITIES;
165         }
166         
167
168 }