Skip to main content
summaryrefslogtreecommitdiffstats
blob: 06b43fbc7a7f48e5468fdd2a9705f96b33343e6c (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.workingset;

import java.io.*;
import java.util.*;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

import org.eclipse.core.runtime.*;
import org.eclipse.help.internal.*;
import org.eclipse.help.internal.base.*;
import org.eclipse.help.internal.workingset.PropertyChange.*;
import org.w3c.dom.*;
import org.xml.sax.*;

/**
 * The working set manager stores help working sets. Working sets are persisted
 * whenever one is added or removed.
 * 
 * @since 2.1
 */
public class WorkingSetManager implements IHelpWorkingSetManager,
		ITocsChangedListener {

	// Note: keep the following constants in sych with the values defined in
	// IWorkingSetManager.
	//       They are needed to synch the ui and the help working sets, as help should
	// run w/o ui plugins.

	/**
	 * Change event id when a working set is added newValue of the
	 * PropertyChangeEvent will be the added working set. oldValue will be null.
	 * 
	 * @see IPropertyChangeListener
	 */
	public static final String CHANGE_WORKING_SET_ADD = "workingSetAdd"; //$NON-NLS-1$

	/**
	 * Change event id when a working set is removed newValue of the
	 * PropertyChangeEvent will be null. oldValue will be the removed working
	 * set.
	 * 
	 * @see IPropertyChangeListener
	 */
	public static final String CHANGE_WORKING_SET_REMOVE = "workingSetRemove"; //$NON-NLS-1$

	/**
	 * Change event id when the working set contents changed newValue of the
	 * PropertyChangeEvent will be the changed working set. oldValue will be
	 * null.
	 * 
	 * @see IPropertyChangeListener
	 */
	public static final String CHANGE_WORKING_SET_CONTENT_CHANGE = "workingSetContentChange"; //$NON-NLS-1$

	/**
	 * Change event id when the working set name changed. newValue of the
	 * PropertyChangeEvent will be the changed working set. oldValue will be
	 * null.
	 * 
	 * @see IPropertyChangeListener
	 */
	public static final String CHANGE_WORKING_SET_NAME_CHANGE = "workingSetNameChange"; //$NON-NLS-1$	

	/**
	 * Synchronize event id. When other working sets repositories are used, one
	 * may want to keep things in synch.
	 * 
	 * @see IPropertyChangeListener
	 */
	public static final String CHANGE_WORKING_SETS_SYNCH = "workingSetsSynch"; //$NON-NLS-1$

	// Working set persistence
	private static final String WORKING_SET_STATE_FILENAME = "workingsets.xml"; //$NON-NLS-1$

	private SortedSet workingSets = new TreeSet(new WorkingSetComparator());

	private PropertyChange.ListenerList propertyChangeListeners = new PropertyChange.ListenerList();

	private AdaptableTocsArray root;

	private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
			.newInstance();

	private static final TransformerFactory transformerFactory = TransformerFactory
			.newInstance();

	/**
	 * Constructor
	 */
	public WorkingSetManager() {
		restoreState();
		HelpPlugin.getDefault().addTocsChangedListener(this);
	}

	public AdaptableTocsArray getRoot() {
		if (root == null)
			root = new AdaptableTocsArray(HelpPlugin.getTocManager().getTocs(
					Platform.getNL()));
		return root;
	}

	/**
	 * Adds a new working set and saves it
	 */
	public void addWorkingSet(WorkingSet workingSet) {
		if (workingSet == null || workingSets.contains(workingSet))
			return;
		workingSets.add(workingSet);
		saveState();
		firePropertyChange(CHANGE_WORKING_SET_ADD, null, workingSet);
	}

	/**
	 */
	public void addPropertyChangeListener(
			PropertyChange.IPropertyChangeListener listener) {
		propertyChangeListeners.add(listener);
	}

	/**
	 * Creates a new working set
	 */
	public WorkingSet createWorkingSet(String name,
			AdaptableHelpResource[] elements) {
		return new WorkingSet(name, elements);
	}

	/**
	 * Tests the receiver and the object for equality
	 * 
	 * @param object
	 *            object to compare the receiver to
	 * @return true=the object equals the receiver, it has the same working
	 *         sets. false otherwise
	 */
	public boolean equals(Object object) {
		if (this == object) {
			return true;
		}
		if (object instanceof WorkingSetManager) {
			WorkingSetManager workingSetManager = (WorkingSetManager) object;
			return workingSetManager.workingSets.equals(workingSets);
		}
		return false;
	}

	/**
	 * Notify property change listeners about a change to the list of working
	 * sets.
	 * 
	 * @param changeId
	 *            one of CHANGE_WORKING_SET_ADD CHANGE_WORKING_SET_REMOVE
	 *            CHANGE_WORKING_SET_CONTENT_CHANGE
	 *            CHANGE_WORKING_SET_NAME_CHANGE
	 * @param oldValue
	 *            the removed working set or null if a working set was added or
	 *            changed.
	 * @param newValue
	 *            the new or changed working set or null if a working set was
	 *            removed.
	 */
	private void firePropertyChange(String changeId, Object oldValue,
			Object newValue) {
		final PropertyChange.PropertyChangeEvent event = new PropertyChange.PropertyChangeEvent(
				this, changeId, oldValue, newValue);

		Object[] listeners = propertyChangeListeners.getListeners();
		for (int i = 0; i < listeners.length; i++) {
			((PropertyChange.IPropertyChangeListener) listeners[i])
					.propertyChange(event);
		}
	}

	/**
	 * Returns a working set by name
	 *  
	 */
	public WorkingSet getWorkingSet(String name) {
		if (name == null || workingSets == null)
			return null;

		Iterator iter = workingSets.iterator();
		while (iter.hasNext()) {
			WorkingSet workingSet = (WorkingSet) iter.next();
			if (name.equals(workingSet.getName()))
				return workingSet;
		}
		return null;
	}

	/**
	 * Returns the hash code.
	 * 
	 * @return the hash code.
	 */
	public int hashCode() {
		return workingSets.hashCode();
	}

	/**
	 * Implements IWorkingSetManager.
	 * 
	 * @see org.eclipse.ui.IWorkingSetManager#getWorkingSets()
	 */
	public WorkingSet[] getWorkingSets() {
		return (WorkingSet[]) workingSets.toArray(new WorkingSet[workingSets
				.size()]);
	}

	/**
	 * Returns the file used as the persistence store
	 * 
	 * @return the file used as the persistence store
	 */
	private File getWorkingSetStateFile() {
		IPath path = HelpBasePlugin.getDefault().getStateLocation();
		path = path.append(WORKING_SET_STATE_FILENAME);
		return path.toFile();
	}

	/**
	 * Removes specified working set
	 */
	public void removeWorkingSet(WorkingSet workingSet) {
		workingSets.remove(workingSet);
		saveState();
		firePropertyChange(CHANGE_WORKING_SET_REMOVE, workingSet, null);
	}

	/**
	 * Reads the persistence store and creates the working sets stored in it.
	 */
	public boolean restoreState() {
		File stateFile = getWorkingSetStateFile();

		if (stateFile.exists()) {
			try {
				FileInputStream input = new FileInputStream(stateFile);
				InputStreamReader reader = new InputStreamReader(input, "utf-8"); //$NON-NLS-1$

				InputSource inputSource = new InputSource(reader);
				inputSource.setSystemId(stateFile.toString());

				DocumentBuilder parser = documentBuilderFactory
						.newDocumentBuilder();
				Document d = parser.parse(inputSource);

				Element rootElement = d.getDocumentElement();
				restoreWorkingSetState(rootElement);
				input.close();

				return true;
			} catch (ParserConfigurationException pce) {
				HelpPlugin
						.logError(
								"DocumentBuilder implementation could not be loaded, to restore working set state.", pce); //$NON-NLS-1$
				return false;
			} catch (SAXException se) {
				HelpBasePlugin
						.logError(
								"Error occurred parsing file " + stateFile.toString() + ", while restoring working set state.", se); //$NON-NLS-1$ //$NON-NLS-2$
				return false;
			} catch (IOException ioe) {
				HelpBasePlugin
						.logError(
								"Error occurred parsing file " + stateFile.toString() + ", while restoring working set state.", ioe); //$NON-NLS-1$ //$NON-NLS-2$
				return false;
			}
		}
		return false;
	}

	/**
	 * Recreates all working sets from the persistence store and adds them to
	 * the receiver.
	 * 
	 * @param parent
	 *            the xml element containing serialized working sets
	 */
	private void restoreWorkingSetState(Element parent) {
		NodeList workingSets = parent.getChildNodes();

		for (int i = 0; i < workingSets.getLength(); i++) {
			if (workingSets.item(i).getNodeType() != Node.ELEMENT_NODE)
				continue;

			WorkingSet workingSet = restoreWorkingSet((Element) workingSets
					.item(i));
			if (workingSet != null) {
				this.workingSets.add(workingSet);
			}
		}
	}

	/**
	 * Recreates a working set from the persistence store.
	 * 
	 * @return the working set created from the memento or null if creation
	 *         failed.
	 */
	private WorkingSet restoreWorkingSet(Element workingSetNode) {

		String name = workingSetNode.getAttribute("name"); //$NON-NLS-1$
		NodeList items = workingSetNode.getElementsByTagName("item"); //$NON-NLS-1$
		List helpResources = new ArrayList(items.getLength());
		for (int i = 0; i < items.getLength(); i++) {
			Element item = (Element) items.item(i);
			String href = item.getAttribute("toc"); //$NON-NLS-1$
			if (href == null || href.length() == 0)
				continue;

			String child_pos = item.getAttribute("topic"); //$NON-NLS-1$
			int pos = -1;
			if (child_pos != null) {
				try {
					pos = Integer.parseInt(child_pos);
				} catch (Exception e) {
				}
			}

			AdaptableHelpResource toc = getAdaptableToc(href);

			if (toc == null)
				return null;

			if (pos == -1) {
				// Create the adaptable toc.
				helpResources.add(toc);
			} else {
				// Create the adaptable topic
				AdaptableTopic[] topics = (AdaptableTopic[]) toc.getChildren();
				if (pos >= 0 && topics.length > pos)
					helpResources.add(topics[pos]);
			}
		}

		AdaptableHelpResource[] elements = new AdaptableHelpResource[helpResources
				.size()];
		helpResources.toArray(elements);

		WorkingSet ws = createWorkingSet(name, elements);

		return ws;
	}

	/**
	 */
	public void removePropertyChangeListener(
			PropertyChange.IPropertyChangeListener listener) {
		propertyChangeListeners.remove(listener);
	}

	/**
	 * Saves the working sets in the persistence store
	 */
	public synchronized boolean saveState() {
		File stateFile = null;
		try {
			DocumentBuilder docBuilder = documentBuilderFactory
					.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("workingSets"); //$NON-NLS-1$
			doc.appendChild(rootElement);

			saveWorkingSetState(rootElement);

			stateFile = getWorkingSetStateFile();
			stateFile.getParentFile().mkdir();
			FileOutputStream stream = new FileOutputStream(stateFile);

			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(stream);

			transformer.transform(source, result);
			stream.close();
			return true;
		} catch (ParserConfigurationException pce) {
			HelpPlugin.logError(
					"DocumentBuilder implementation could not be loaded.", pce); //$NON-NLS-1$
			return false;
		} catch (TransformerException e) {
			HelpPlugin.logError("Problems occurred while saving working sets.", //$NON-NLS-1$
					null);
			return false;
		} catch (IOException e) {
			stateFile.delete();
			HelpBasePlugin.logError(
					"Problems occurred while saving working set file.", null); //$NON-NLS-1$
			return false;
		}
	}

	/**
	 * Saves all persistable working sets in the persistence store.
	 * 
	 * @param parent:
	 *            the xml node to save to
	 */
	private void saveWorkingSetState(Element parent) {
		Iterator iterator = workingSets.iterator();

		while (iterator.hasNext()) {
			WorkingSet workingSet = (WorkingSet) iterator.next();
			workingSet.saveState(parent);
		}
	}

	/**
	 * Persists all working sets. Should only be called by the webapp working
	 * set dialog.
	 * 
	 * @param changedWorkingSet
	 *            the working set that has changed
	 */
	public void workingSetChanged(WorkingSet changedWorkingSet) {
		saveState();
		firePropertyChange(CHANGE_WORKING_SET_NAME_CHANGE, null,
				changedWorkingSet);
		firePropertyChange(CHANGE_WORKING_SET_CONTENT_CHANGE, null,
				changedWorkingSet);
	}

	/**
	 * Synchronizes the working sets. Should only be called by the webapp
	 * working set manager dialog.
	 */
	public void synchronizeWorkingSets() {
		firePropertyChange(CHANGE_WORKING_SETS_SYNCH, null, null);
	}

	public AdaptableToc getAdaptableToc(String href) {
		return getRoot().getAdaptableToc(href);
	}

	public AdaptableTopic getAdaptableTopic(String id) {

		if (id == null || id.length() == 0)
			return null;

		// toc id's are hrefs: /pluginId/path/to/toc.xml
		// topic id's are based on parent toc id and index of topic:
		// /pluginId/path/to/toc.xml_index_
		int len = id.length();
		if (id.charAt(len - 1) == '_') {
			// This is a first level topic
			String indexStr = id.substring(id.lastIndexOf('_', len - 2) + 1,
					len - 1);
			int index = 0;
			try {
				index = Integer.parseInt(indexStr);
			} catch (Exception e) {
			}

			String tocStr = id.substring(0, id.lastIndexOf('_', len - 2));
			AdaptableToc toc = getAdaptableToc(tocStr);
			if (toc == null)
				return null;
			IAdaptable[] topics = toc.getChildren();
			if (index < 0 || index >= topics.length)
				return null;
			else
				return (AdaptableTopic) topics[index];
		}

		return null;
	}

	public String getCurrentWorkingSet() {
		return HelpBasePlugin.getDefault().getPluginPreferences().getString(
				BaseHelpSystem.WORKING_SET);
	}

	public void setCurrentWorkingSet(String workingSet) {
		HelpBasePlugin.getDefault().getPluginPreferences().setValue(
				BaseHelpSystem.WORKING_SET, workingSet);
		HelpBasePlugin.getDefault().savePluginPreferences();
	}

	public void tocsChanged() {
		saveState();
		List oldWorkingSets = new ArrayList(workingSets);
		root = null;
		workingSets = new TreeSet(new WorkingSetComparator());
		restoreState();
		List newWorkingSets = new ArrayList(workingSets);
		for (Iterator it = oldWorkingSets.iterator(); it.hasNext();) {
			WorkingSet ws = (WorkingSet) it.next();
			firePropertyChange(CHANGE_WORKING_SET_REMOVE, ws, null);
		}
		for (Iterator it = newWorkingSets.iterator(); it.hasNext();) {
			WorkingSet ws = (WorkingSet) it.next();
			firePropertyChange(CHANGE_WORKING_SET_ADD, null, ws);
		}
	}

}

Back to the top