001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.Status;
017: import org.eclipse.ui.IEditorDescriptor;
018: import org.eclipse.ui.IEditorInput;
019: import org.eclipse.ui.IMemento;
020: import org.eclipse.ui.PlatformUI;
021:
022: /**
023: * This class is used to record "open editor" actions as they
024: * happen. The input and type of each editor are recorded so that
025: * the user can reopen an item from the recently used files list.
026: */
027: public class EditorHistory {
028: /**
029: * The maximum of entries in the history.
030: */
031: public static final int MAX_SIZE = 15;
032:
033: /**
034: * The list of editor entries, in FIFO order.
035: */
036: private ArrayList fifoList = new ArrayList(MAX_SIZE);
037:
038: /**
039: * Constructs a new history.
040: */
041: public EditorHistory() {
042: super ();
043: }
044:
045: /**
046: * Adds an item to the history. Added in fifo fashion.
047: */
048: public void add(IEditorInput input, IEditorDescriptor desc) {
049: add(new EditorHistoryItem(input, desc), 0);
050: }
051:
052: /**
053: * Adds an item to the history.
054: */
055: private void add(EditorHistoryItem newItem, int index) {
056: // Remove the item if it already exists so that it will be put
057: // at the top of the list.
058: if (newItem.isRestored()) {
059: remove(newItem.getInput());
060: }
061:
062: // Remove the oldest one
063: if (fifoList.size() == MAX_SIZE) {
064: fifoList.remove(MAX_SIZE - 1);
065: }
066:
067: // Add the new item.
068: fifoList.add(index < MAX_SIZE ? index : MAX_SIZE - 1, newItem);
069: }
070:
071: /**
072: * Returns an array of editor history items. The items are returned in order
073: * of most recent first.
074: */
075: public EditorHistoryItem[] getItems() {
076: refresh();
077: EditorHistoryItem[] array = new EditorHistoryItem[fifoList
078: .size()];
079: fifoList.toArray(array);
080: return array;
081: }
082:
083: /**
084: * Refresh the editor list. Any stale items are removed.
085: * Only restored items are considered.
086: */
087: public void refresh() {
088: Iterator iter = fifoList.iterator();
089: while (iter.hasNext()) {
090: EditorHistoryItem item = (EditorHistoryItem) iter.next();
091: if (item.isRestored()) {
092: IEditorInput input = item.getInput();
093: if (input != null && !input.exists()) {
094: iter.remove();
095: }
096: }
097: }
098: }
099:
100: /**
101: * Removes the given history item.
102: */
103: public void remove(EditorHistoryItem item) {
104: fifoList.remove(item);
105: }
106:
107: /**
108: * Removes all traces of an editor input from the history.
109: */
110: public void remove(IEditorInput input) {
111: if (input == null) {
112: return;
113: }
114: Iterator iter = fifoList.iterator();
115: while (iter.hasNext()) {
116: EditorHistoryItem item = (EditorHistoryItem) iter.next();
117: if (item.matches(input)) {
118: iter.remove();
119: }
120: }
121: }
122:
123: /**
124: * Restore the most-recently-used history from the given memento.
125: *
126: * @param memento the memento to restore the mru history from
127: */
128: public IStatus restoreState(IMemento memento) {
129: IMemento[] mementos = memento
130: .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 iterator = fifoList.iterator();
147: while (iterator.hasNext()) {
148: EditorHistoryItem item = (EditorHistoryItem) iterator
149: .next();
150: if (item.canSave()) {
151: IMemento itemMemento = memento
152: .createChild(IWorkbenchConstants.TAG_FILE);
153: item.saveState(itemMemento);
154: }
155: }
156: return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
157: }
158: }
|