1 11 package org.eclipse.ui.internal.cheatsheets; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 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 29 public class CheatSheetHistory { 30 31 private static final int DEFAULT_DEPTH = 5; 32 33 private ArrayList history; 34 private CheatSheetRegistryReader reg; 35 private ListenerList listeners = new ListenerList(); 36 37 public CheatSheetHistory(CheatSheetRegistryReader reg) { 38 this.history = new ArrayList (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 [] 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"); 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 iter = history.iterator(); 71 while (iter.hasNext()) { 72 CheatSheetElement element = (CheatSheetElement)iter.next(); 73 if(element != null) { 74 memento.createChild("element", element.getID()); } 76 } 77 return new Status(IStatus.OK,ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,0,ICheatSheetResource.EMPTY_STRING,null); 78 } 79 80 public void add(String id) { 81 CheatSheetElement element = reg.findCheatSheet(id); 82 if (element != null) 83 add(element); 84 } 85 86 public void add(CheatSheetElement element) { 87 if (history.contains(element)) 89 return; 90 91 int size = history.size(); 93 int preferredSize = DEFAULT_DEPTH; 94 while (size >= preferredSize) { 95 size--; 96 history.remove(size); 97 } 98 99 history.add(0, element); 101 fireChange(); 102 } 103 104 public void refreshFromRegistry() { 105 boolean change = false; 106 107 Iterator 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 129 public int copyItems(List 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 |