KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > PerspectiveHistory


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

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 /**
21  * This is used to store the most recently used (MRU) list
22  * of perspectives for the entire workbench.
23  */

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 JavaDoc[] 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"); //$NON-NLS-1$
55
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); //$NON-NLS-1$
62
}
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()); //$NON-NLS-1$
69
}
70         return new Status(IStatus.OK,PlatformUI.PLUGIN_ID,0,"",null); //$NON-NLS-1$
71
}
72
73     public void add(String JavaDoc id) {
74         IPerspectiveDescriptor desc = reg.findPerspectiveWithId(id);
75         if (desc != null)
76             add(desc);
77     }
78     
79     public void add(IPerspectiveDescriptor desc) {
80         // Avoid duplicates
81
if (shortcuts.contains(desc))
82             return;
83
84         // If the shortcut list will be too long, remove oldest ones
85
int size = shortcuts.size();
86         int preferredSize = DEFAULT_DEPTH;
87         while (size >= preferredSize) {
88             size--;
89             shortcuts.remove(size);
90         }
91         
92         // Insert at top as most recent
93
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     /**
114      * Copy the requested number of items from the history into
115      * the destination list at the given index.
116      *
117      * @param dest destination list to contain the items
118      * @param destStart index in destination list to start copying items at
119      * @param count number of items to copy from history
120      * @return the number of items actually copied
121      */

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     //for dynamic UI
134
public void removeItem(Object JavaDoc 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