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