Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d2c07bed2b22568b1bdc688b9685264db41f5a8a (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
/*******************************************************************************
 * Copyright (c) 2002, 2015 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.ui.internal.cheatsheets;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement;
import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;

/**
 * This is used to store the most recently used (MRU) list
 * of cheatsheet for the entire workbench.
 */
public class CheatSheetHistory {

	private static final int DEFAULT_DEPTH = 5;

	private ArrayList history;
	private CheatSheetRegistryReader reg;
	private ListenerList listeners = new ListenerList();

	public CheatSheetHistory(CheatSheetRegistryReader reg) {
		this.history = new ArrayList(DEFAULT_DEPTH);
		this.reg = reg;
	}

	public void addListener(IPropertyListener l) {
		listeners.add(l);
	}

	public void removeListener(IPropertyListener l) {
		listeners.remove(l);
	}

	private void fireChange() {
		Object[] array = listeners.getListeners();
		for (int i = 0; i < array.length; i++) {
			IPropertyListener element = (IPropertyListener)array[i];
			element.propertyChanged(this, 0);
		}
	}

	public IStatus restoreState(IMemento memento) {
		IMemento [] children = memento.getChildren("element"); //$NON-NLS-1$
		for (int i = 0; i < children.length && i < DEFAULT_DEPTH; i++) {
			CheatSheetElement element =
				reg.findCheatSheet(children[i].getID());
			if (element != null)
				history.add(element);
		}
		return new Status(IStatus.OK,ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,0,ICheatSheetResource.EMPTY_STRING,null);
	}

	public IStatus saveState(IMemento memento) {
		Iterator iter = history.iterator();
		while (iter.hasNext()) {
			CheatSheetElement element = (CheatSheetElement)iter.next();
			if(element != null) {
				memento.createChild("element", element.getID()); //$NON-NLS-1$
			}
		}
		return new Status(IStatus.OK,ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,0,ICheatSheetResource.EMPTY_STRING,null);
	}

	public void add(String id) {
		CheatSheetElement element = reg.findCheatSheet(id);
		if (element != null)
			add(element);
	}

	public void add(CheatSheetElement element) {
		// Avoid duplicates
		if (history.contains(element))
			return;

		// If the shortcut list will be too long, remove oldest ones
		int size = history.size();
		int preferredSize = DEFAULT_DEPTH;
		while (size >= preferredSize) {
			size--;
			history.remove(size);
		}

		// Insert at top as most recent
		history.add(0, element);
		fireChange();
	}

	public void refreshFromRegistry() {
		boolean change = false;

		Iterator iter = history.iterator();
		while (iter.hasNext()) {
			CheatSheetElement element = (CheatSheetElement)iter.next();
			if (reg.findCheatSheet(element.getID()) == null) {
				iter.remove();
				change = true;
			}
		}

		if (change)
			fireChange();
	}

	/**
	 * Copy the requested number of items from the history into
	 * the destination list at the given index.
	 *
	 * @param dest destination list to contain the items
	 * @param destStart index in destination list to start copying items at
	 * @param count number of items to copy from history
	 * @return the number of items actually copied
	 */
	public int copyItems(List dest, int destStart, int count) {
		int itemCount = count;
		if (itemCount > history.size())
			itemCount = history.size();

		for (int i = 0; i < itemCount; i++)
			dest.add(destStart + i, history.get(i));

		return itemCount;
	}
}

Back to the top