KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > LogicalStructureManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.debug.internal.core;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19 import java.util.Map.Entry;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtensionPoint;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.debug.core.DebugPlugin;
26 import org.eclipse.debug.core.ILogicalStructureType;
27 import org.eclipse.debug.core.model.IValue;
28
29 /**
30  * Manages logical structure extensions
31  *
32  * @since 3.0
33  */

34 public class LogicalStructureManager {
35
36     private static LogicalStructureManager fgDefault;
37     private List JavaDoc fTypes = null;
38     private List JavaDoc fTypeProviders;
39
40     /**
41      * Map containing the user's selection for each combination of logical
42      * structure types.
43      * key: String - Comma-separated list of ints representing a combination of structure types.
44      * These integers are indeces into the fStructureTypeIds array.
45      * value: Integer - One of the ints from the combo key (the one chosen by the user) or -1 if
46      * the user has chosen not to display any structures for this combination
47      */

48     private Map JavaDoc fStructureTypeSelections= null;
49     /**
50      * List of known type identifiers. An identifier's index in this list is used as
51      * its ID number. This list is maintained as a space-saving measure so that the various
52      * combinations of structure types can be persisted using indeces instead of storing the
53      * full index strings.
54      */

55     private List JavaDoc fStructureTypeIds= null;
56     
57     /**
58      * Preference key used for storing the user's selected structure for each combination
59      * or structures. The preference value is stored in the form:
60      * int,int,...,int|int,int,...int|...
61      * Where int is an integer index of a structure in the array of known structures.
62      */

63     public static final String JavaDoc PREF_STRUCTURE_SELECTIONS= "selectedStructures"; //$NON-NLS-1$
64
/**
65      * Preference key used for storing the array of known structures. The preference
66      * value is in the form:
67      * string,string,string,...,string,
68      * Where string is an identifier of a logical structure.
69      */

70     public static final String JavaDoc PREF_STRUCTURE_IDS= "allStructures"; //$NON-NLS-1$
71

72     public static LogicalStructureManager getDefault() {
73         if (fgDefault == null) {
74             fgDefault = new LogicalStructureManager();
75         }
76         return fgDefault;
77     }
78     
79     /**
80      * Returns the logical structure types that are applicable to the given value.
81      * @param value the value
82      * @return the logical structure types that are applicable to the given value
83      */

84     public ILogicalStructureType[] getLogicalStructureTypes(IValue value) {
85         initialize();
86         // looks in the logical structure types
87
Iterator JavaDoc iterator = fTypes.iterator();
88         List JavaDoc select = new ArrayList JavaDoc();
89         while (iterator.hasNext()) {
90             ILogicalStructureType type = (ILogicalStructureType)iterator.next();
91             if (type.providesLogicalStructure(value)) {
92                 select.add(type);
93             }
94         }
95         // asks the logical structure providers
96
for (Iterator JavaDoc iter= fTypeProviders.iterator(); iter.hasNext();) {
97             ILogicalStructureType[] logicalStructures= ((LogicalStructureProvider) iter.next()).getLogicalStructures(value);
98             for (int i= 0; i < logicalStructures.length; i++) {
99                 select.add(logicalStructures[i]);
100             }
101         }
102         return (ILogicalStructureType[]) select.toArray(new ILogicalStructureType[select.size()]);
103     }
104     
105     /**
106      * Loads the map of structure selections from the preference store.
107      */

108     private void loadStructureTypeSelections() {
109         fStructureTypeSelections= new HashMap JavaDoc();
110         String JavaDoc selections= DebugPlugin.getDefault().getPluginPreferences().getString(PREF_STRUCTURE_SELECTIONS);
111         // selections are stored in the form:
112
// selection|selection|...selection|
113
StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(selections, "|"); //$NON-NLS-1$
114
while (tokenizer.hasMoreTokens()) {
115             String JavaDoc selection = tokenizer.nextToken();
116             // selection string is of the form:
117
// id,id,...,selectedid
118
int i = selection.lastIndexOf(',');
119             if (i > 0 && i < selection.length() - 1) {
120                 String JavaDoc comboKey= selection.substring(0, i + 1);
121                 String JavaDoc selected= selection.substring(i + 1, selection.length());
122                 fStructureTypeSelections.put(comboKey, new Integer JavaDoc(Integer.parseInt(selected)));
123             }
124         }
125     }
126     
127     /**
128      * Stores the map of structure selections to the preference store
129      */

130     private void storeStructureTypeSelections() {
131         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
132         Iterator JavaDoc iter = fStructureTypeSelections.entrySet().iterator();
133         while (iter.hasNext()) {
134             Map.Entry JavaDoc entry = (Entry) iter.next();
135             buffer.append(entry.getKey());
136             buffer.append(entry.getValue());
137             buffer.append('|');
138         }
139         DebugPlugin.getDefault().getPluginPreferences().setValue(PREF_STRUCTURE_SELECTIONS, buffer.toString());
140     }
141     
142     /**
143      * Loads the collection of known structures identifiers from the preference store
144      */

145     private void loadStructureTypeIds() {
146         fStructureTypeIds= new ArrayList JavaDoc();
147         // Types are stored as a comma-separated, ordered list.
148
String JavaDoc types= DebugPlugin.getDefault().getPluginPreferences().getString(PREF_STRUCTURE_IDS);
149         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(types, ","); //$NON-NLS-1$
150
while (tokenizer.hasMoreTokens()) {
151             String JavaDoc id= tokenizer.nextToken();
152             if (id.length() > 0) {
153                 fStructureTypeIds.add(id);
154             }
155         }
156     }
157     
158     /**
159      * Stores the collection of known structure identifiers to the preference store
160      */

161     private void storeStructureTypeIds() {
162         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
163         Iterator JavaDoc iter = fStructureTypeIds.iterator();
164         while (iter.hasNext()) {
165             buffer.append(iter.next()).append(',');
166         }
167         DebugPlugin.getDefault().getPluginPreferences().setValue(PREF_STRUCTURE_IDS, buffer.toString());
168     }
169     
170     /**
171      * Returns the structure that the user has chosen from among the given
172      * collection of structures or <code>null</code> if the user has chosen
173      * to display none.
174      * @param structureTypes the collection of structures available
175      * @return the structure that the user has chosen from among the given collection
176      * or <code>null</code> if the user has chosen to display none
177      */

178     public ILogicalStructureType getSelectedStructureType(ILogicalStructureType[] structureTypes) {
179         if (structureTypes.length == 0) {
180             return null;
181         }
182         String JavaDoc combo= getComboString(structureTypes);
183         // Lookup the combo
184
Integer JavaDoc index = (Integer JavaDoc) fStructureTypeSelections.get(combo);
185         if (index == null) {
186             // If the user hasn't explicitly chosen anything for this
187
// combo yet, just return the first type.
188
return structureTypes[0];
189         } else if (index.intValue() == -1) {
190             // An index of -1 means the user has deselected all structures for this combo
191
return null;
192         }
193         // If an index is stored for this combo, retrieve the id at the index
194
String JavaDoc id= (String JavaDoc) fStructureTypeIds.get(index.intValue());
195         for (int i = 0; i < structureTypes.length; i++) {
196             // Return the type with the retrieved id
197
ILogicalStructureType type = structureTypes[i];
198             if (type.getId().equals(id)) {
199                 return type;
200             }
201         }
202         return structureTypes[0];
203     }
204     
205     /**
206      *
207      * @param types
208      * @param selected the type that is selected for the given combo or <code>null</code>
209      * if the user has deselected any structure for the given combo
210      */

211     public void setEnabledType(ILogicalStructureType[] types, ILogicalStructureType selected) {
212         String JavaDoc combo= getComboString(types);
213         int index= -1; // Initialize to "none selected"
214
if (selected != null) {
215             index= fStructureTypeIds.indexOf(selected.getId());
216         }
217         Integer JavaDoc integer= new Integer JavaDoc(index);
218         fStructureTypeSelections.put(combo, integer);
219         storeStructureTypeSelections();
220         storeStructureTypeIds();
221     }
222     
223     /**
224      * Returns the string representing the given combination of logical
225      * structure types. This string will be a series of comma-separated
226      * indeces representing the various types. If any of the given types
227      * don't have indeces associated with them, this method will create
228      * the appropriate index.
229      * @param types the logical structure types
230      * @return the string representing the given combination of logical
231      * structure types
232      */

233     protected String JavaDoc getComboString(ILogicalStructureType[] types) {
234         StringBuffer JavaDoc comboKey= new StringBuffer JavaDoc();
235         for (int i = 0; i < types.length; i++) {
236             ILogicalStructureType type = types[i];
237             int typeIndex = fStructureTypeIds.indexOf(type.getId());
238             if (typeIndex == -1) {
239                 typeIndex= fStructureTypeIds.size();
240                 fStructureTypeIds.add(type.getId());
241             }
242             comboKey.append(typeIndex).append(',');
243         }
244         return comboKey.toString();
245     }
246     
247     private synchronized void initialize() {
248         if (fTypes == null) {
249             //get the logical structure types from the extension points
250
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LOGICAL_STRUCTURE_TYPES);
251             IConfigurationElement[] extensions = point.getConfigurationElements();
252             fTypes = new ArrayList JavaDoc(extensions.length);
253             for (int i = 0; i < extensions.length; i++) {
254                 IConfigurationElement extension = extensions[i];
255                 LogicalStructureType type;
256                 try {
257                     type = new LogicalStructureType(extension);
258                     fTypes.add(type);
259                 } catch (CoreException e) {
260                     DebugPlugin.log(e);
261                 }
262             }
263             // get the logical structure providers from the extension point
264
point= Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LOGICAL_STRUCTURE_PROVIDERS);
265             extensions= point.getConfigurationElements();
266             fTypeProviders= new ArrayList JavaDoc(extensions.length);
267             for (int i= 0; i < extensions.length; i++) {
268                 try {
269                     fTypeProviders.add(new LogicalStructureProvider(extensions[i]));
270                 } catch (CoreException e) {
271                     DebugPlugin.log(e);
272                 }
273             }
274         }
275         if (fStructureTypeSelections == null) {
276             loadStructureTypeSelections();
277         }
278         if (fStructureTypeIds == null) {
279             loadStructureTypeIds();
280         }
281     }
282 }
283
Popular Tags