001: /*******************************************************************************
002: * Copyright (c) 2002, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.core.runtime.ListenerList;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.ui.IMemento;
020: import org.eclipse.ui.IPropertyListener;
021: import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement;
022: import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
023:
024: /**
025: * This is used to store the most recently used (MRU) list
026: * of cheatsheet for the entire workbench.
027: */
028: public class CheatSheetHistory {
029:
030: private static final int DEFAULT_DEPTH = 5;
031:
032: private ArrayList history;
033: private CheatSheetRegistryReader reg;
034: private ListenerList listeners = new ListenerList();
035:
036: public CheatSheetHistory(CheatSheetRegistryReader reg) {
037: this .history = new ArrayList(DEFAULT_DEPTH);
038: this .reg = reg;
039: }
040:
041: public void addListener(IPropertyListener l) {
042: listeners.add(l);
043: }
044:
045: public void removeListener(IPropertyListener l) {
046: listeners.remove(l);
047: }
048:
049: private void fireChange() {
050: Object[] array = listeners.getListeners();
051: for (int i = 0; i < array.length; i++) {
052: IPropertyListener element = (IPropertyListener) array[i];
053: element.propertyChanged(this , 0);
054: }
055: }
056:
057: public IStatus restoreState(IMemento memento) {
058: IMemento[] children = memento.getChildren("element"); //$NON-NLS-1$
059: for (int i = 0; i < children.length && i < DEFAULT_DEPTH; i++) {
060: CheatSheetElement element = reg.findCheatSheet(children[i]
061: .getID());
062: if (element != null)
063: history.add(element);
064: }
065: return new Status(IStatus.OK,
066: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, 0,
067: ICheatSheetResource.EMPTY_STRING, null);
068: }
069:
070: public IStatus saveState(IMemento memento) {
071: Iterator iter = history.iterator();
072: while (iter.hasNext()) {
073: CheatSheetElement element = (CheatSheetElement) iter.next();
074: if (element != null) {
075: memento.createChild("element", element.getID()); //$NON-NLS-1$
076: }
077: }
078: return new Status(IStatus.OK,
079: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, 0,
080: ICheatSheetResource.EMPTY_STRING, null);
081: }
082:
083: public void add(String id) {
084: CheatSheetElement element = reg.findCheatSheet(id);
085: if (element != null)
086: add(element);
087: }
088:
089: public void add(CheatSheetElement element) {
090: // Avoid duplicates
091: if (history.contains(element))
092: return;
093:
094: // If the shortcut list will be too long, remove oldest ones
095: int size = history.size();
096: int preferredSize = DEFAULT_DEPTH;
097: while (size >= preferredSize) {
098: size--;
099: history.remove(size);
100: }
101:
102: // Insert at top as most recent
103: history.add(0, element);
104: fireChange();
105: }
106:
107: public void refreshFromRegistry() {
108: boolean change = false;
109:
110: Iterator iter = history.iterator();
111: while (iter.hasNext()) {
112: CheatSheetElement element = (CheatSheetElement) iter.next();
113: if (reg.findCheatSheet(element.getID()) == null) {
114: iter.remove();
115: change = true;
116: }
117: }
118:
119: if (change)
120: fireChange();
121: }
122:
123: /**
124: * Copy the requested number of items from the history into
125: * the destination list at the given index.
126: *
127: * @param dest destination list to contain the items
128: * @param destStart index in destination list to start copying items at
129: * @param count number of items to copy from history
130: * @return the number of items actually copied
131: */
132: public int copyItems(List dest, int destStart, int count) {
133: int itemCount = count;
134: if (itemCount > history.size())
135: itemCount = history.size();
136:
137: for (int i = 0; i < itemCount; i++)
138: dest.add(destStart + i, history.get(i));
139:
140: return itemCount;
141: }
142: }
|