KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > MembersOrderPreferenceCache


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.preferences;
12
13 import java.util.StringTokenizer JavaDoc;
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 /**
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 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc str, int[] offsets) {
107         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(str, ","); //$NON-NLS-1$
108
int i= 0;
109         offsets[ENUM_CONSTANTS_INDEX]= i++; // enum constants always on top
110

111         while (tokenizer.hasMoreTokens()) {
112             String JavaDoc token= tokenizer.nextToken().trim();
113             if ("T".equals(token)) { //$NON-NLS-1$
114
offsets[TYPE_INDEX]= i++;
115             } else if ("M".equals(token)) { //$NON-NLS-1$
116
offsets[METHOD_INDEX]= i++;
117             } else if ("F".equals(token)) { //$NON-NLS-1$
118
offsets[FIELDS_INDEX]= i++;
119             } else if ("I".equals(token)) { //$NON-NLS-1$
120
offsets[INIT_INDEX]= i++;
121             } else if ("SF".equals(token)) { //$NON-NLS-1$
122
offsets[STATIC_FIELDS_INDEX]= i++;
123             } else if ("SI".equals(token)) { //$NON-NLS-1$
124
offsets[STATIC_INIT_INDEX]= i++;
125             } else if ("SM".equals(token)) { //$NON-NLS-1$
126
offsets[STATIC_METHODS_INDEX]= i++;
127             } else if ("C".equals(token)) { //$NON-NLS-1$
128
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 JavaDoc 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 JavaDoc str, int[] offsets) {
167         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(str, ","); //$NON-NLS-1$
168
int i= 0;
169         while (tokenizer.hasMoreTokens()) {
170             String JavaDoc token= tokenizer.nextToken().trim();
171             if ("B".equals(token)) { //$NON-NLS-1$
172
offsets[PUBLIC_INDEX]= i++;
173             } else if ("V".equals(token)) { //$NON-NLS-1$
174
offsets[PRIVATE_INDEX]= i++;
175             } else if ("R".equals(token)) { //$NON-NLS-1$
176
offsets[PROTECTED_INDEX]= i++;
177             } else if ("D".equals(token)) { //$NON-NLS-1$
178
offsets[DEFAULT_INDEX]= i++;
179             }
180         }
181         return i == N_VISIBILITIES;
182     }
183     
184
185 }
186
Popular Tags