KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > logicalstructures > JavaLogicalStructures


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.jdt.internal.debug.core.logicalstructures;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.IExtensionPoint;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
28 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
29 import org.eclipse.debug.core.DebugException;
30 import org.eclipse.debug.core.ILogicalStructureProvider;
31 import org.eclipse.debug.core.ILogicalStructureType;
32 import org.eclipse.debug.core.model.IValue;
33 import org.eclipse.jdt.debug.core.IJavaClassType;
34 import org.eclipse.jdt.debug.core.IJavaInterfaceType;
35 import org.eclipse.jdt.debug.core.IJavaObject;
36 import org.eclipse.jdt.debug.core.IJavaType;
37 import org.eclipse.jdt.debug.core.JDIDebugModel;
38 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
39
40 public class JavaLogicalStructures implements ILogicalStructureProvider {
41     
42     // preference values
43
static final char IS_SUBTYPE_TRUE= 'T';
44     static final char IS_SUBTYPE_FALSE= 'F';
45     
46     /**
47      * The list of java logical structures.
48      */

49     private static Map JavaDoc fJavaLogicalStructureMap;
50
51     /**
52      * The list of java logical structures in this Eclipse install.
53      */

54     private static List JavaDoc fPluginContributedJavaLogicalStructures;
55     
56     /**
57      * The list of java logical structures defined by the user.
58      */

59     private static List JavaDoc fUserDefinedJavaLogicalStructures;
60     
61     /**
62      * The list of java logical structures listeners.
63      */

64     private static Set JavaDoc fListeners= new HashSet JavaDoc();
65     
66     /**
67      * Preference key for the list of user defined Java logical structures
68      *
69      * @since 3.1
70      */

71     private static final String JavaDoc PREF_JAVA_LOGICAL_STRUCTURES= JDIDebugModel.getPluginIdentifier() + ".PREF_JAVA_LOGICAL_STRUCTURES"; //$NON-NLS-1$
72

73     /**
74      * Updates user defined logical structures if the preference changes
75      */

76     static class PreferenceListener implements IPropertyChangeListener {
77
78         /* (non-Javadoc)
79          * @see org.eclipse.core.runtime.Preferences.IPropertyChangeListener#propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
80          */

81         public void propertyChange(PropertyChangeEvent event) {
82             if (PREF_JAVA_LOGICAL_STRUCTURES.equals(event.getProperty())) {
83                 initUserDefinedJavaLogicalStructures();
84                 initJavaLogicalStructureMap();
85                 Iterator JavaDoc iter = fListeners.iterator();
86                 while (iter.hasNext()) {
87                     ((IJavaStructuresListener) iter.next()).logicalStructuresChanged();
88                 }
89             }
90         }
91         
92     }
93     
94     /**
95      * Get the logical structure from the extension point and the preference store,
96      * and initialize the map.
97      */

98     static {
99         initPluginContributedJavaLogicalStructure();
100         initUserDefinedJavaLogicalStructures();
101         initJavaLogicalStructureMap();
102         JDIDebugPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(new PreferenceListener());
103     }
104     
105     private static void initJavaLogicalStructureMap() {
106         fJavaLogicalStructureMap= new HashMap JavaDoc();
107         addAllLogicalStructures(fPluginContributedJavaLogicalStructures);
108         addAllLogicalStructures(fUserDefinedJavaLogicalStructures);
109     }
110     
111     /**
112      * @param pluginContributedJavaLogicalStructures
113      */

114     private static void addAllLogicalStructures(List JavaDoc pluginContributedJavaLogicalStructures) {
115         for (Iterator JavaDoc iter= pluginContributedJavaLogicalStructures.iterator(); iter.hasNext();) {
116             addLogicalStructure((JavaLogicalStructure) iter.next());
117         }
118     }
119
120     /**
121      * @param structure
122      */

123     private static void addLogicalStructure(JavaLogicalStructure structure) {
124         String JavaDoc typeName= structure.getQualifiedTypeName();
125         List JavaDoc logicalStructure= (List JavaDoc)fJavaLogicalStructureMap.get(typeName);
126         if (logicalStructure == null) {
127             logicalStructure= new ArrayList JavaDoc();
128             fJavaLogicalStructureMap.put(typeName, logicalStructure);
129         }
130         logicalStructure.add(structure);
131     }
132
133     /**
134      * Get the configuration elements for the extension point.
135      */

136     private static void initPluginContributedJavaLogicalStructure() {
137         fPluginContributedJavaLogicalStructures= new ArrayList JavaDoc();
138         IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.EXTENSION_POINT_JAVA_LOGICAL_STRUCTURES);
139         IConfigurationElement[] javaLogicalStructureElements= extensionPoint.getConfigurationElements();
140         for (int i= 0; i < javaLogicalStructureElements.length; i++) {
141             try {
142                 fPluginContributedJavaLogicalStructures.add(new JavaLogicalStructure(javaLogicalStructureElements[i]));
143             } catch (CoreException e) {
144                 JDIDebugPlugin.log(e);
145             }
146         }
147     }
148     
149     /**
150      * Get the user defined logical structures (from the preference store).
151      */

152      private static void initUserDefinedJavaLogicalStructures() {
153         fUserDefinedJavaLogicalStructures= new ArrayList JavaDoc();
154         String JavaDoc logicalStructuresString= JDIDebugModel.getPreferences().getString(PREF_JAVA_LOGICAL_STRUCTURES);
155         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(logicalStructuresString, "\0", true); //$NON-NLS-1$
156
while (tokenizer.hasMoreTokens()) {
157             String JavaDoc type= tokenizer.nextToken();
158             tokenizer.nextToken();
159             String JavaDoc description= tokenizer.nextToken();
160             tokenizer.nextToken();
161             String JavaDoc isSubtypeValue= tokenizer.nextToken();
162             boolean isSubtype= isSubtypeValue.charAt(0) == IS_SUBTYPE_TRUE;
163             tokenizer.nextToken();
164             String JavaDoc value= tokenizer.nextToken();
165             if (value.charAt(0) == '\0') {
166                 value= null;
167             } else {
168                 tokenizer.nextToken();
169             }
170             String JavaDoc variablesCounterValue= tokenizer.nextToken();
171             int variablesCounter= Integer.parseInt(variablesCounterValue);
172             tokenizer.nextToken();
173             String JavaDoc[][] variables= new String JavaDoc[variablesCounter][2];
174             for (int i= 0; i < variablesCounter; i++) {
175                 variables[i][0]= tokenizer.nextToken();
176                 tokenizer.nextToken();
177                 variables[i][1]= tokenizer.nextToken();
178                 tokenizer.nextToken();
179             }
180             fUserDefinedJavaLogicalStructures.add(new JavaLogicalStructure(type, isSubtype, value, description, variables));
181         }
182     }
183     
184     /**
185      * Save the user defined logical structures in the preference store.
186      */

187     public static void saveUserDefinedJavaLogicalStructures() {
188         StringBuffer JavaDoc logicalStructuresString= new StringBuffer JavaDoc();
189         for (Iterator JavaDoc iter= fUserDefinedJavaLogicalStructures.iterator(); iter.hasNext();) {
190             JavaLogicalStructure logicalStructure= (JavaLogicalStructure) iter.next();
191             logicalStructuresString.append(logicalStructure.getQualifiedTypeName()).append('\0');
192             logicalStructuresString.append(logicalStructure.getDescription()).append('\0');
193             logicalStructuresString.append(logicalStructure.isSubtypes() ? IS_SUBTYPE_TRUE : IS_SUBTYPE_FALSE).append('\0');
194             String JavaDoc value= logicalStructure.getValue();
195             if (value != null) {
196                 logicalStructuresString.append(value);
197             }
198             logicalStructuresString.append('\0');
199             String JavaDoc[][] variables= logicalStructure.getVariables();
200             logicalStructuresString.append(variables.length).append('\0');
201             for (int i= 0; i < variables.length; i++) {
202                 String JavaDoc[] strings= variables[i];
203                 logicalStructuresString.append(strings[0]).append('\0');
204                 logicalStructuresString.append(strings[1]).append('\0');
205             }
206         }
207         JDIDebugModel.getPreferences().setValue(PREF_JAVA_LOGICAL_STRUCTURES, logicalStructuresString.toString());
208     }
209
210     /**
211      * Return all the defined logical structures.
212      */

213     public static JavaLogicalStructure[] getJavaLogicalStructures() {
214         JavaLogicalStructure[] logicalStructures= new JavaLogicalStructure[fPluginContributedJavaLogicalStructures.size() + fUserDefinedJavaLogicalStructures.size()];
215         int i= 0;
216         for (Iterator JavaDoc iter= fPluginContributedJavaLogicalStructures.iterator(); iter.hasNext();) {
217             logicalStructures[i++]= (JavaLogicalStructure) iter.next();
218         }
219         for (Iterator JavaDoc iter= fUserDefinedJavaLogicalStructures.iterator(); iter.hasNext();) {
220             logicalStructures[i++]= (JavaLogicalStructure) iter.next();
221         }
222         return logicalStructures;
223     }
224
225     /**
226      * Set the user defined logical structures.
227      */

228     public static void setUserDefinedJavaLogicalStructures(JavaLogicalStructure[] logicalStructures) {
229         fUserDefinedJavaLogicalStructures= Arrays.asList(logicalStructures);
230         saveUserDefinedJavaLogicalStructures();
231     }
232     
233     public static void addStructuresListener(IJavaStructuresListener listener) {
234         fListeners.add(listener);
235     }
236     
237     public static void removeStructuresListener(IJavaStructuresListener listener) {
238         fListeners.remove(listener);
239     }
240
241     public ILogicalStructureType[] getLogicalStructureTypes(IValue value) {
242         if (!(value instanceof IJavaObject)) {
243             return new ILogicalStructureType[0];
244         }
245         IJavaObject javaValue= (IJavaObject) value;
246         List JavaDoc logicalStructures= new ArrayList JavaDoc();
247         try {
248             IJavaType type= javaValue.getJavaType();
249             if (!(type instanceof IJavaClassType)) {
250                 return new ILogicalStructureType[0];
251             }
252             IJavaClassType classType= (IJavaClassType) type;
253             List JavaDoc list= (List JavaDoc)fJavaLogicalStructureMap.get(classType.getName());
254             if (list != null) {
255                 logicalStructures.addAll(list);
256             }
257             IJavaClassType superClass= classType.getSuperclass();
258             while (superClass != null) {
259                 addIfIsSubtype(logicalStructures, (List JavaDoc)fJavaLogicalStructureMap.get(superClass.getName()));
260                 superClass= superClass.getSuperclass();
261             }
262             IJavaInterfaceType[] superInterfaces= classType.getAllInterfaces();
263             for (int i= 0; i < superInterfaces.length; i++) {
264                 addIfIsSubtype(logicalStructures, (List JavaDoc)fJavaLogicalStructureMap.get(superInterfaces[i].getName()));
265             }
266         } catch (DebugException e) {
267             JDIDebugPlugin.log(e);
268             return new ILogicalStructureType[0];
269         }
270         return (ILogicalStructureType[]) logicalStructures.toArray(new ILogicalStructureType[logicalStructures.size()]);
271     }
272
273     private void addIfIsSubtype(List JavaDoc logicalStructures, List JavaDoc list) {
274         if (list == null) {
275             return;
276         }
277         for (Iterator JavaDoc iter= list.iterator(); iter.hasNext();) {
278             JavaLogicalStructure logicalStructure= (JavaLogicalStructure) iter.next();
279             if (logicalStructure.isSubtypes()) {
280                 logicalStructures.add(logicalStructure);
281             }
282         }
283     }
284
285 }
286
Popular Tags