Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 23e6fcedd1aff363bdc87b013d335e1b536ed01a (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*******************************************************************************
 * Copyright (c) 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.views.internal.categories;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.tcf.te.runtime.events.ChangeEvent;
import org.eclipse.tcf.te.runtime.events.EventManager;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.ui.views.activator.UIPlugin;
import org.eclipse.tcf.te.ui.views.extensions.CategoriesExtensionPointManager;
import org.eclipse.tcf.te.ui.views.interfaces.categories.ICategoryManager;


/**
 * Category manager implementation.
 */
public class CategoryManager implements ICategoryManager {
	// The map maintaining the id list per category id
	private Map<String, List<String>> cat2id = new HashMap<String, List<String>>();
	// The map maintaining the category id list per id
	private Map<String, List<String>> id2cat = new HashMap<String, List<String>>();

	// The map maintaining the transient id list per category id
	private final Map<String, List<String>> _t_cat2id = new HashMap<String, List<String>>();
	// The map maintaining the transient category id list per id
	private final Map<String, List<String>> _t_id2cat = new HashMap<String, List<String>>();

	/**
	 * Constructor.
	 */
	public CategoryManager() {
		super();
		initialize();
	}

	/**
	 * Returns the root path where to persist the maps between the sessions.
	 *
	 * @return The root path or <code>null</code>.
	 */
	private IPath getRoot() {
		try {
			File file = UIPlugin.getDefault().getStateLocation().toFile();
			boolean exists = file.exists();
			if (!exists) {
				exists = file.mkdirs();
			}
			if (exists && file.canRead() && file.isDirectory()) {
				return new Path(file.toString());
			}
		} catch (IllegalStateException e) {
			/* ignored on purpose */
		}

		File file = new Path(System.getProperty("user.home")).append(".tcf").toFile(); //$NON-NLS-1$ //$NON-NLS-2$
		if (file.canRead() && file.isDirectory()) {
			return new Path(file.toString());
		}

		return null;
	}

	/**
	 * Initialize the category manager.
	 */
	private void initialize() {
		IPath root = getRoot();
		if (root == null) {
			return;
		}

		// Clear out the transient maps
		_t_cat2id.clear();
		_t_id2cat.clear();

		cat2id.clear();
		id2cat.clear();

		try {
			// Get the persistence service
			IURIPersistenceService uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
			if (uRIPersistenceService == null) {
				throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
			}
			// Save the history to the persistence storage
			cat2id = (Map<String,List<String>>)uRIPersistenceService.read(cat2id, root.append("cat2id.json").toFile().toURI()); //$NON-NLS-1$
		} catch (IOException e) {
		}

		try {
			// Get the persistence service
			IURIPersistenceService uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
			if (uRIPersistenceService == null) {
				throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
			}
			// Save the history to the persistence storage
			id2cat = (Map<String,List<String>>)uRIPersistenceService.read(id2cat, root.append("id2cat.json").toFile().toURI()); //$NON-NLS-1$
		} catch (IOException e) {
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.views.categories.ICategoryManager#flush()
	 */
	@Override
	public void flush() {
		IPath root = getRoot();
		if (root == null) {
			return;
		}

		try {
			// Get the persistence service
			IURIPersistenceService uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
			if (uRIPersistenceService == null) {
				throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
			}
			// Save the history to the persistence storage
			uRIPersistenceService.write(cat2id, root.append("cat2id.json").toFile().toURI()); //$NON-NLS-1$
		} catch (IOException e) {
		}

		try {
			// Get the persistence service
			IURIPersistenceService uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
			if (uRIPersistenceService == null) {
				throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
			}
			// Save the history to the persistence storage
			uRIPersistenceService.write(id2cat, root.append("id2cat.json").toFile().toURI()); //$NON-NLS-1$
		} catch (IOException e) {
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.views.categories.ICategoryManager#add(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean add(String categoryId, String id) {
		Assert.isNotNull(categoryId);
		Assert.isNotNull(id);

		boolean added = false;

		List<String> ids = cat2id.get(categoryId);
		if (ids == null) {
			ids = new ArrayList<String>();
			cat2id.put(categoryId, ids);
		}
		if (!ids.contains(id)) {
			added |= ids.add(id);
		}

		List<String> cats = id2cat.get(id);
		if (cats == null) {
			cats = new ArrayList<String>();
			id2cat.put(id, cats);
		}
		if (!cats.contains(categoryId)) {
			added |= cats.add(categoryId);
		}

		if (added) {
			flush();
			EventManager.getInstance().fireEvent(
							new ChangeEvent(CategoriesExtensionPointManager.getInstance().getCategory(categoryId, false), ChangeEvent.ID_ADDED, null, id));
		}

		return added;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.views.interfaces.categories.ICategoryManager#addTransient(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean addTransient(String categoryId, String id) {
		Assert.isNotNull(categoryId);
		Assert.isNotNull(id);

		boolean added = false;

		List<String> ids = _t_cat2id.get(categoryId);
		if (ids == null) {
			ids = new ArrayList<String>();
			_t_cat2id.put(categoryId, ids);
		}
		if (!ids.contains(id)) {
			added |= ids.add(id);
		}

		List<String> cats = _t_id2cat.get(id);
		if (cats == null) {
			cats = new ArrayList<String>();
			_t_id2cat.put(id, cats);
		}
		if (!cats.contains(categoryId)) {
			added |= cats.add(categoryId);
		}

		if (added) {
			EventManager.getInstance().fireEvent(
							new ChangeEvent(CategoriesExtensionPointManager.getInstance().getCategory(categoryId, false), ChangeEvent.ID_ADDED, null, id));
		}

		return added;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.views.categories.ICategoryManager#remove(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean remove(String categoryId, String id) {
		Assert.isNotNull(categoryId);
		Assert.isNotNull(id);

		boolean removed = false;

		List<String> ids = cat2id.get(categoryId);
		if (ids != null) {
			removed |= ids.remove(id);
			if (ids.isEmpty()) {
				cat2id.remove(categoryId);
			}
		}
		ids = _t_cat2id.get(categoryId);
		if (ids != null) {
			removed |= ids.remove(id);
			if (ids.isEmpty()) {
				_t_cat2id.remove(categoryId);
			}
		}

		List<String> cats = id2cat.get(id);
		if (cats != null) {
			removed |= cats.remove(categoryId);
			if (cats.isEmpty()) {
				id2cat.remove(id);
			}
		}
		cats = _t_id2cat.get(id);
		if (cats != null) {
			removed |= cats.remove(categoryId);
			if (cats.isEmpty()) {
				_t_id2cat.remove(id);
			}
		}

		if (removed) {
			flush();
			EventManager.getInstance().fireEvent(
							new ChangeEvent(CategoriesExtensionPointManager.getInstance().getCategory(categoryId, false), ChangeEvent.ID_REMOVED, id, null));
		}

		return removed;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.views.categories.ICategoryManager#belongsTo(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean belongsTo(String categoryId, String id) {
		Assert.isNotNull(categoryId);
		Assert.isNotNull(id);

		boolean belongsTo = false;

		List<String> ids = cat2id.get(categoryId);
		if (ids != null && ids.contains(id)) {
			belongsTo = true;
		} else {
			ids = _t_cat2id.get(categoryId);
			if (ids != null && ids.contains(id)) {
				belongsTo = true;
			}
		}

		return belongsTo;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.views.categories.ICategoryManager#getCategoryIds(java.lang.String)
	 */
	@Override
	public String[] getCategoryIds(String id) {
		Assert.isNotNull(id);

		List<String> allCategories = new ArrayList<String>();

		List<String> cats = id2cat.get(id);
		if (cats != null) {
			allCategories.addAll(cats);
		}

		cats = _t_id2cat.get(id);
		if (cats != null) {
			for (String cat : cats) {
				if (!allCategories.contains(cat)) {
					allCategories.add(cat);
				}
			}
		}

		return allCategories.toArray(new String[allCategories.size()]);
	}
}

Back to the top