1 11 package org.eclipse.ui.internal; 12 13 import java.util.*; 14 15 import org.eclipse.core.runtime.*; 16 import org.eclipse.core.runtime.Status; 17 import org.eclipse.jface.util.ListenerList; 18 import org.eclipse.ui.*; 19 20 24 public class PerspectiveHistory { 25 26 private static final int DEFAULT_DEPTH = 50; 27 28 private ArrayList shortcuts; 29 private IPerspectiveRegistry reg; 30 private ListenerList listeners = new ListenerList(); 31 32 public PerspectiveHistory(IPerspectiveRegistry reg) { 33 this.shortcuts = new ArrayList(DEFAULT_DEPTH); 34 this.reg = reg; 35 } 36 37 public void addListener(IPropertyListener l) { 38 listeners.add(l); 39 } 40 41 public void removeListener(IPropertyListener l) { 42 listeners.remove(l); 43 } 44 45 private void fireChange() { 46 Object [] array = listeners.getListeners(); 47 for (int i = 0; i < array.length; i++) { 48 IPropertyListener element = (IPropertyListener)array[i]; 49 element.propertyChanged(this, 0); 50 } 51 } 52 53 public IStatus restoreState(IMemento memento) { 54 IMemento [] children = memento.getChildren("desc"); for (int i = 0; i < children.length && i < DEFAULT_DEPTH; i++) { 56 IPerspectiveDescriptor desc = 57 reg.findPerspectiveWithId(children[i].getID()); 58 if (desc != null) 59 shortcuts.add(desc); 60 } 61 return new Status(IStatus.OK,PlatformUI.PLUGIN_ID,0,"",null); } 63 64 public IStatus saveState(IMemento memento) { 65 Iterator iter = shortcuts.iterator(); 66 while (iter.hasNext()) { 67 IPerspectiveDescriptor desc = (IPerspectiveDescriptor)iter.next(); 68 memento.createChild("desc", desc.getId()); } 70 return new Status(IStatus.OK,PlatformUI.PLUGIN_ID,0,"",null); } 72 73 public void add(String id) { 74 IPerspectiveDescriptor desc = reg.findPerspectiveWithId(id); 75 if (desc != null) 76 add(desc); 77 } 78 79 public void add(IPerspectiveDescriptor desc) { 80 if (shortcuts.contains(desc)) 82 return; 83 84 int size = shortcuts.size(); 86 int preferredSize = DEFAULT_DEPTH; 87 while (size >= preferredSize) { 88 size--; 89 shortcuts.remove(size); 90 } 91 92 shortcuts.add(0, desc); 94 fireChange(); 95 } 96 97 public void refreshFromRegistry() { 98 boolean change = false; 99 100 Iterator iter = shortcuts.iterator(); 101 while (iter.hasNext()) { 102 IPerspectiveDescriptor desc = (IPerspectiveDescriptor)iter.next(); 103 if (reg.findPerspectiveWithId(desc.getId()) == null) { 104 iter.remove(); 105 change = true; 106 } 107 } 108 109 if (change) 110 fireChange(); 111 } 112 113 122 public int copyItems(List dest, int destStart, int count) { 123 int itemCount = count; 124 if (itemCount > shortcuts.size()) 125 itemCount = shortcuts.size(); 126 127 for (int i = 0; i < itemCount; i++) 128 dest.add(destStart + i, shortcuts.get(i)); 129 130 return itemCount; 131 } 132 133 public void removeItem(Object item) { 135 for (int i = 0; i < shortcuts.size(); i++) 136 if (shortcuts.get(i) == item) { 137 shortcuts.remove(i); 138 break; 139 } 140 } 141 } 142 143 | Popular Tags |