KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > CheatSheetHistory


1 /*******************************************************************************
2  * Copyright (c) 2002, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.cheatsheets;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.ListenerList;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.ui.IMemento;
21 import org.eclipse.ui.IPropertyListener;
22 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement;
23 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
24
25 /**
26  * This is used to store the most recently used (MRU) list
27  * of cheatsheet for the entire workbench.
28  */

29 public class CheatSheetHistory {
30
31     private static final int DEFAULT_DEPTH = 5;
32     
33     private ArrayList JavaDoc history;
34     private CheatSheetRegistryReader reg;
35     private ListenerList listeners = new ListenerList();
36
37     public CheatSheetHistory(CheatSheetRegistryReader reg) {
38         this.history = new ArrayList JavaDoc(DEFAULT_DEPTH);
39         this.reg = reg;
40     }
41
42     public void addListener(IPropertyListener l) {
43         listeners.add(l);
44     }
45     
46     public void removeListener(IPropertyListener l) {
47         listeners.remove(l);
48     }
49     
50     private void fireChange() {
51         Object JavaDoc[] array = listeners.getListeners();
52         for (int i = 0; i < array.length; i++) {
53             IPropertyListener element = (IPropertyListener)array[i];
54             element.propertyChanged(this, 0);
55         }
56     }
57     
58     public IStatus restoreState(IMemento memento) {
59         IMemento [] children = memento.getChildren("element"); //$NON-NLS-1$
60
for (int i = 0; i < children.length && i < DEFAULT_DEPTH; i++) {
61             CheatSheetElement element =
62                 reg.findCheatSheet(children[i].getID());
63             if (element != null)
64                 history.add(element);
65         }
66         return new Status(IStatus.OK,ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,0,ICheatSheetResource.EMPTY_STRING,null);
67     }
68     
69     public IStatus saveState(IMemento memento) {
70         Iterator JavaDoc iter = history.iterator();
71         while (iter.hasNext()) {
72             CheatSheetElement element = (CheatSheetElement)iter.next();
73             if(element != null) {
74                 memento.createChild("element", element.getID()); //$NON-NLS-1$
75
}
76         }
77         return new Status(IStatus.OK,ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,0,ICheatSheetResource.EMPTY_STRING,null);
78     }
79
80     public void add(String JavaDoc id) {
81         CheatSheetElement element = reg.findCheatSheet(id);
82         if (element != null)
83             add(element);
84     }
85     
86     public void add(CheatSheetElement element) {
87         // Avoid duplicates
88
if (history.contains(element))
89             return;
90
91         // If the shortcut list will be too long, remove oldest ones
92
int size = history.size();
93         int preferredSize = DEFAULT_DEPTH;
94         while (size >= preferredSize) {
95             size--;
96             history.remove(size);
97         }
98         
99         // Insert at top as most recent
100
history.add(0, element);
101         fireChange();
102     }
103     
104     public void refreshFromRegistry() {
105         boolean change = false;
106         
107         Iterator JavaDoc iter = history.iterator();
108         while (iter.hasNext()) {
109             CheatSheetElement element = (CheatSheetElement)iter.next();
110             if (reg.findCheatSheet(element.getID()) == null) {
111                 iter.remove();
112                 change = true;
113             }
114         }
115         
116         if (change)
117             fireChange();
118     }
119
120     /**
121      * Copy the requested number of items from the history into
122      * the destination list at the given index.
123      *
124      * @param dest destination list to contain the items
125      * @param destStart index in destination list to start copying items at
126      * @param count number of items to copy from history
127      * @return the number of items actually copied
128      */

129     public int copyItems(List JavaDoc dest, int destStart, int count) {
130         int itemCount = count;
131         if (itemCount > history.size())
132             itemCount = history.size();
133             
134         for (int i = 0; i < itemCount; i++)
135             dest.add(destStart + i, history.get(i));
136             
137         return itemCount;
138     }
139 }
140
141
Popular Tags