KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.ui.IEditorDescriptor;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.IMemento;
22 import org.eclipse.ui.PlatformUI;
23
24 /**
25  * This class is used to record "open editor" actions as they
26  * happen. The input and type of each editor are recorded so that
27  * the user can reopen an item from the recently used files list.
28  */

29 public class EditorHistory {
30     /**
31      * The maximum of entries in the history.
32      */

33     public static final int MAX_SIZE = 15;
34
35     /**
36      * The list of editor entries, in FIFO order.
37      */

38     private ArrayList JavaDoc fifoList = new ArrayList JavaDoc(MAX_SIZE);
39
40     /**
41      * Constructs a new history.
42      */

43     public EditorHistory() {
44         super();
45     }
46
47     /**
48      * Adds an item to the history. Added in fifo fashion.
49      */

50     public void add(IEditorInput input, IEditorDescriptor desc) {
51         add(new EditorHistoryItem(input, desc), 0);
52     }
53
54     /**
55      * Adds an item to the history.
56      */

57     private void add(EditorHistoryItem newItem, int index) {
58         // Remove the item if it already exists so that it will be put
59
// at the top of the list.
60
if (newItem.isRestored()) {
61             remove(newItem.getInput());
62         }
63
64         // Remove the oldest one
65
if (fifoList.size() == MAX_SIZE) {
66             fifoList.remove(MAX_SIZE - 1);
67         }
68
69         // Add the new item.
70
fifoList.add(index < MAX_SIZE ? index : MAX_SIZE - 1, newItem);
71     }
72
73     /**
74      * Returns an array of editor history items. The items are returned in order
75      * of most recent first.
76      */

77     public EditorHistoryItem[] getItems() {
78         refresh();
79         EditorHistoryItem[] array = new EditorHistoryItem[fifoList.size()];
80         fifoList.toArray(array);
81         return array;
82     }
83
84     /**
85      * Refresh the editor list. Any stale items are removed.
86      * Only restored items are considered.
87      */

88     public void refresh() {
89         Iterator JavaDoc iter = fifoList.iterator();
90         while (iter.hasNext()) {
91             EditorHistoryItem item = (EditorHistoryItem) iter.next();
92             if (item.isRestored()) {
93                 IEditorInput input = item.getInput();
94                 if (input != null && !input.exists()) {
95                     iter.remove();
96                 }
97             }
98         }
99     }
100
101     /**
102      * Removes the given history item.
103      */

104     public void remove(EditorHistoryItem item) {
105         fifoList.remove(item);
106     }
107
108     /**
109      * Removes all traces of an editor input from the history.
110      */

111     public void remove(IEditorInput input) {
112         if (input == null) {
113             return;
114         }
115         Iterator JavaDoc iter = fifoList.iterator();
116         while (iter.hasNext()) {
117             EditorHistoryItem item = (EditorHistoryItem) iter.next();
118             if (item.matches(input)) {
119                 iter.remove();
120             }
121         }
122     }
123
124     /**
125      * Restore the most-recently-used history from the given memento.
126      *
127      * @param memento the memento to restore the mru history from
128      */

129     public IStatus restoreState(IMemento memento) {
130         IMemento[] mementos = memento.getChildren(IWorkbenchConstants.TAG_FILE);
131         for (int i = 0; i < mementos.length; i++) {
132             EditorHistoryItem item = new EditorHistoryItem(mementos[i]);
133             if (!"".equals(item.getName()) || !"".equals(item.getToolTipText())) { //$NON-NLS-1$ //$NON-NLS-2$
134
add(item, fifoList.size());
135             }
136         }
137         return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
138
}
139
140     /**
141      * Save the most-recently-used history in the given memento.
142      *
143      * @param memento the memento to save the mru history in
144      */

145     public IStatus saveState(IMemento memento) {
146         Iterator JavaDoc iterator = fifoList.iterator();
147         while (iterator.hasNext()) {
148             EditorHistoryItem item = (EditorHistoryItem) iterator.next();
149             if (item.canSave()) {
150                 IMemento itemMemento = memento
151                         .createChild(IWorkbenchConstants.TAG_FILE);
152                 item.saveState(itemMemento);
153             }
154         }
155         return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
156
}
157 }
158
Popular Tags