Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca9ab08170ced7daa0abdf246a6e9a9879e3af84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILogicalStructureType;
import org.eclipse.debug.core.model.IValue;

/**
 * Manages logical structure extensions
 *
 * @since 3.0
 */
public class LogicalStructureManager {

	private static LogicalStructureManager fgDefault;
	private List<LogicalStructureType> fTypes = null;
	private List<LogicalStructureProvider> fTypeProviders;

    /**
     * Map containing the user's selection for each combination of logical
     * structure types.
     * key: String - Comma-separated list of ints representing a combination of structure types.
     *               These integers are indeces into the fStructureTypeIds array.
     * value: Integer - One of the ints from the combo key (the one chosen by the user) or -1 if
     *                  the user has chosen not to display any structures for this combination
     */
	private Map<String, Integer> fStructureTypeSelections = null;
    /**
     * List of known type identifiers. An identifier's index in this list is used as
     * its ID number. This list is maintained as a space-saving measure so that the various
     * combinations of structure types can be persisted using indeces instead of storing the
     * full index strings.
     */
	private List<String> fStructureTypeIds = null;

    /**
     * Preference key used for storing the user's selected structure for each combination
     * or structures. The preference value is stored in the form:
     * int,int,...,int|int,int,...int|...
     * Where int is an integer index of a structure in the array of known structures.
     */
    public static final String PREF_STRUCTURE_SELECTIONS= "selectedStructures"; //$NON-NLS-1$
    /**
     * Preference key used for storing the array of known structures. The preference
     * value is in the form:
     * string,string,string,...,string,
     * Where string is an identifier of a logical structure.
     */
    public static final String PREF_STRUCTURE_IDS= "allStructures"; //$NON-NLS-1$

	public static LogicalStructureManager getDefault() {
		if (fgDefault == null) {
			fgDefault = new LogicalStructureManager();
		}
		return fgDefault;
	}

    /**
     * Returns the logical structure types that are applicable to the given value.
     * @param value the value
     * @return the logical structure types that are applicable to the given value
     */
	public ILogicalStructureType[] getLogicalStructureTypes(IValue value) {
		initialize();
		// looks in the logical structure types
		List<ILogicalStructureType> select = new ArrayList<>();
		for (ILogicalStructureType type : fTypes) {
			if (type.providesLogicalStructure(value)) {
				select.add(type);
			}
		}
		// asks the logical structure providers
		for (LogicalStructureProvider provider : fTypeProviders) {
			ILogicalStructureType[] types = provider.getLogicalStructures(value);
			for (int i = 0; i < types.length; i++) {
				select.add(types[i]);
			}
		}
		return select.toArray(new ILogicalStructureType[select.size()]);
	}

    /**
     * Loads the map of structure selections from the preference store.
     */
    private void loadStructureTypeSelections() {
		fStructureTypeSelections = new HashMap<>();
        String selections= Platform.getPreferencesService().getString(DebugPlugin.getUniqueIdentifier(), PREF_STRUCTURE_SELECTIONS, IInternalDebugCoreConstants.EMPTY_STRING, null);
    	// selections are stored in the form:
    	// selection|selection|...selection|
        StringTokenizer tokenizer= new StringTokenizer(selections, "|"); //$NON-NLS-1$
        while (tokenizer.hasMoreTokens()) {
            String selection = tokenizer.nextToken();
            // selection string is of the form:
            // id,id,...,selectedid
            int i = selection.lastIndexOf(',');
            if (i > 0 && i < selection.length() - 1) {
                String comboKey= selection.substring(0, i + 1);
                String selected= selection.substring(i + 1, selection.length());
                fStructureTypeSelections.put(comboKey, Integer.valueOf(Integer.parseInt(selected)));
            }
        }
    }

    /**
     * Stores the map of structure selections to the preference store
     */
    private void storeStructureTypeSelections() {
        StringBuffer buffer= new StringBuffer();
		for (Entry<String, Integer> entry : fStructureTypeSelections.entrySet()) {
			buffer.append(entry.getKey());
            buffer.append(entry.getValue());
            buffer.append('|');
        }
        Preferences.setString(DebugPlugin.getUniqueIdentifier(), PREF_STRUCTURE_SELECTIONS, buffer.toString(), null);
    }

    /**
     * Loads the collection of known structures identifiers from the preference store
     */
    private void loadStructureTypeIds() {
		fStructureTypeIds = new ArrayList<>();
    	// Types are stored as a comma-separated, ordered list.
        String types= Platform.getPreferencesService().getString(DebugPlugin.getUniqueIdentifier(), PREF_STRUCTURE_IDS, IInternalDebugCoreConstants.EMPTY_STRING, null);
        StringTokenizer tokenizer= new StringTokenizer(types, ","); //$NON-NLS-1$
        while (tokenizer.hasMoreTokens()) {
            String id= tokenizer.nextToken();
            if (id.length() > 0) {
                fStructureTypeIds.add(id);
            }
        }
    }

    /**
     * Stores the collection of known structure identifiers to the preference store
     */
    private void storeStructureTypeIds() {
        StringBuffer buffer= new StringBuffer();
		for (String id : fStructureTypeIds) {
			buffer.append(id).append(',');
        }
        Preferences.setString(DebugPlugin.getUniqueIdentifier(), PREF_STRUCTURE_IDS, buffer.toString(), null);
    }

    /**
     * Returns the structure that the user has chosen from among the given
     * collection of structures or <code>null</code> if the user has chosen
     * to display none.
     * @param structureTypes the collection of structures available
     * @return the structure that the user has chosen from among the given collection
     *  or <code>null</code> if the user has chosen to display none
     */
    public ILogicalStructureType getSelectedStructureType(ILogicalStructureType[] structureTypes) {
        if (structureTypes.length == 0) {
            return null;
        }
        String combo= getComboString(structureTypes);
        // Lookup the combo
        Integer index = fStructureTypeSelections.get(combo);
        if (index == null) {
            // If the user hasn't explicitly chosen anything for this
            // combo yet, just return the first type.
            return structureTypes[0];
        } else if (index.intValue() == -1) {
            // An index of -1 means the user has deselected all structures for this combo
            return null;
        }
        // If an index is stored for this combo, retrieve the id at the index
        String id= fStructureTypeIds.get(index.intValue());
        for (int i = 0; i < structureTypes.length; i++) {
            // Return the type with the retrieved id
            ILogicalStructureType type = structureTypes[i];
            if (type.getId().equals(id)) {
                return type;
            }
        }
        return structureTypes[0];
    }

    /**
     *
     * @param types the array of types
     * @param selected the type that is selected for the given combo or <code>null</code>
     *  if the user has de-selected any structure for the given combo
     */
    public void setEnabledType(ILogicalStructureType[] types, ILogicalStructureType selected) {
        String combo= getComboString(types);
        int index= -1; // Initialize to "none selected"
        if (selected != null) {
            index= fStructureTypeIds.indexOf(selected.getId());
        }
        Integer integer= Integer.valueOf(index);
        fStructureTypeSelections.put(combo, integer);
        storeStructureTypeSelections();
        storeStructureTypeIds();
    }

    /**
     * Returns the string representing the given combination of logical
     * structure types. This string will be a series of comma-separated
     * indices representing the various types. If any of the given types
     * don't have indices associated with them, this method will create
     * the appropriate index.
     * @param types the logical structure types
     * @return the string representing the given combination of logical
     *  structure types
     */
    protected String getComboString(ILogicalStructureType[] types) {
        StringBuffer comboKey= new StringBuffer();
        for (int i = 0; i < types.length; i++) {
            ILogicalStructureType type = types[i];
            int typeIndex = fStructureTypeIds.indexOf(type.getId());
            if (typeIndex == -1) {
                typeIndex= fStructureTypeIds.size();
                fStructureTypeIds.add(type.getId());
            }
            comboKey.append(typeIndex).append(',');
        }
        return comboKey.toString();
    }

	private synchronized void initialize() {
		if (fTypes == null) {
			//get the logical structure types from the extension points
			IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LOGICAL_STRUCTURE_TYPES);
			IConfigurationElement[] extensions = point.getConfigurationElements();
			fTypes = new ArrayList<>(extensions.length);
			for (int i = 0; i < extensions.length; i++) {
				IConfigurationElement extension = extensions[i];
				LogicalStructureType type;
				try {
					type = new LogicalStructureType(extension);
					fTypes.add(type);
				} catch (CoreException e) {
					DebugPlugin.log(e);
				}
			}
			// get the logical structure providers from the extension point
			point= Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LOGICAL_STRUCTURE_PROVIDERS);
			extensions= point.getConfigurationElements();
			fTypeProviders = new ArrayList<>(extensions.length);
			for (int i= 0; i < extensions.length; i++) {
				try {
					fTypeProviders.add(new LogicalStructureProvider(extensions[i]));
				} catch (CoreException e) {
					DebugPlugin.log(e);
				}
			}
		}
        if (fStructureTypeSelections == null) {
            loadStructureTypeSelections();
        }
        if (fStructureTypeIds == null) {
            loadStructureTypeIds();
        }
	}
}

Back to the top