1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 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 29 public class EditorHistory { 30 33 public static final int MAX_SIZE = 15; 34 35 38 private ArrayList fifoList = new ArrayList (MAX_SIZE); 39 40 43 public EditorHistory() { 44 super(); 45 } 46 47 50 public void add(IEditorInput input, IEditorDescriptor desc) { 51 add(new EditorHistoryItem(input, desc), 0); 52 } 53 54 57 private void add(EditorHistoryItem newItem, int index) { 58 if (newItem.isRestored()) { 61 remove(newItem.getInput()); 62 } 63 64 if (fifoList.size() == MAX_SIZE) { 66 fifoList.remove(MAX_SIZE - 1); 67 } 68 69 fifoList.add(index < MAX_SIZE ? index : MAX_SIZE - 1, newItem); 71 } 72 73 77 public EditorHistoryItem[] getItems() { 78 refresh(); 79 EditorHistoryItem[] array = new EditorHistoryItem[fifoList.size()]; 80 fifoList.toArray(array); 81 return array; 82 } 83 84 88 public void refresh() { 89 Iterator 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 104 public void remove(EditorHistoryItem item) { 105 fifoList.remove(item); 106 } 107 108 111 public void remove(IEditorInput input) { 112 if (input == null) { 113 return; 114 } 115 Iterator 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 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())) { add(item, fifoList.size()); 135 } 136 } 137 return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); } 139 140 145 public IStatus saveState(IMemento memento) { 146 Iterator 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); } 157 } 158 | Popular Tags |