Eclipse 3M7
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / MembersOrderPreferenceCache.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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.ui.PreferenceConstants;
16
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20
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_ENTRIES= STATIC_METHODS_INDEX + 1;    
35         
36         private int[] fOffsets= null;
37
38         public void propertyChange(PropertyChangeEvent event) {
39                 if (PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER.equals(event.getProperty())) {
40                         fOffsets= null;
41                 }
42         }
43
44         public int getIndex(int kind) {
45                 if (fOffsets == null) {
46                         fOffsets= getOffsets();
47                 }
48                 return fOffsets[kind];
49         }
50         
51         private int[] getOffsets() {
52                 int[] offsets= new int[N_ENTRIES];
53                 IPreferenceStore store= PreferenceConstants.getPreferenceStore();
54                 String key= PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER;
55                 boolean success= fillOffsetsFromPreferenceString(store.getString(key), offsets);
56                 if (!success) {
57                         store.setToDefault(key);
58                         fillOffsetsFromPreferenceString(store.getDefaultString(key), offsets);  
59                 }
60                 return offsets;
61         }               
62         
63         private boolean fillOffsetsFromPreferenceString(String str, int[] offsets) {
64                 StringTokenizer tokenizer= new StringTokenizer(str, ","); //$NON-NLS-1$
65                 int i= 0;
66                 while (tokenizer.hasMoreTokens()) {
67                         String token= tokenizer.nextToken().trim();
68                         if ("T".equals(token)) { //$NON-NLS-1$
69                                 offsets[TYPE_INDEX]= i++;
70                         } else if ("M".equals(token)) { //$NON-NLS-1$
71                                 offsets[METHOD_INDEX]= i++;
72                         } else if ("F".equals(token)) { //$NON-NLS-1$
73                                 offsets[FIELDS_INDEX]= i++;
74                         } else if ("I".equals(token)) { //$NON-NLS-1$
75                                 offsets[INIT_INDEX]= i++;
76                         } else if ("SF".equals(token)) { //$NON-NLS-1$
77                                 offsets[STATIC_FIELDS_INDEX]= i++;
78                         } else if ("SI".equals(token)) { //$NON-NLS-1$
79                                 offsets[STATIC_INIT_INDEX]= i++;
80                         } else if ("SM".equals(token)) { //$NON-NLS-1$
81                                 offsets[STATIC_METHODS_INDEX]= i++;
82                         } else if ("C".equals(token)) { //$NON-NLS-1$
83                                 offsets[CONSTRUCTORS_INDEX]= i++;
84                         }
85                 }
86                 return i == N_ENTRIES;
87         }
88         
89
90 }