1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 17 import org.eclipse.jface.action.IMenuCreator; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.SelectionAdapter; 21 import org.eclipse.swt.events.SelectionEvent; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Menu; 24 import org.eclipse.swt.widgets.MenuItem; 25 import org.eclipse.ui.ISharedImages; 26 import org.eclipse.ui.IWorkbenchPage; 27 import org.eclipse.ui.IWorkbenchWindow; 28 29 33 public class NavigationHistoryAction extends PageEventAction { 34 private boolean forward; 35 36 private Menu historyMenu; 37 38 private int MAX_HISTORY_LENGTH = 9; 39 40 private class MenuCreator implements IMenuCreator { 41 public void dispose() { 42 if (historyMenu != null) { 43 for (int i = 0; i < historyMenu.getItemCount(); i++) { 44 MenuItem menuItem = historyMenu.getItem(i); 45 menuItem.setData(null); 46 } 47 historyMenu.dispose(); 48 historyMenu = null; 49 } 50 } 51 52 public Menu getMenu(Menu parent) { 53 return null; 54 } 55 56 public Menu getMenu(Control parent) { 57 dispose(); 58 historyMenu = new Menu(parent); 59 IWorkbenchPage page = getWorkbenchWindow().getActivePage(); 60 if (page == null) { 61 return historyMenu; 62 } 63 64 final NavigationHistory history = (NavigationHistory) getWorkbenchWindow() 65 .getActivePage().getNavigationHistory(); 66 NavigationHistoryEntry[] entries; 67 if (forward) { 68 entries = history.getForwardEntries(); 69 } else { 70 entries = history.getBackwardEntries(); 71 } 72 int entriesCount[] = new int[entries.length]; 73 for (int i = 0; i < entriesCount.length; i++) { 74 entriesCount[i] = 1; 75 } 76 entries = colapseEntries(entries, entriesCount); 77 for (int i = 0; i < entries.length; i++) { 78 if (i > MAX_HISTORY_LENGTH) { 79 break; 80 } 81 String text = entries[i].getHistoryText(); 82 if (text != null) { 83 MenuItem item = new MenuItem(historyMenu, SWT.NONE); 84 item.setData(entries[i]); 85 if (entriesCount[i] > 1) { 86 text = NLS.bind(WorkbenchMessages.NavigationHistoryAction_locations,text, new Integer (entriesCount[i])); 87 } 88 item.setText(text); 89 item.addSelectionListener(new SelectionAdapter() { 90 public void widgetSelected(SelectionEvent e) { 91 history 92 .shiftCurrentEntry((NavigationHistoryEntry) e.widget 93 .getData(), forward); 94 } 95 }); 96 } 97 } 98 return historyMenu; 99 } 100 } 101 102 108 public NavigationHistoryAction(IWorkbenchWindow window, boolean forward) { 109 super("", window); ISharedImages sharedImages = window.getWorkbench().getSharedImages(); 111 if (forward) { 112 setText(WorkbenchMessages.NavigationHistoryAction_forward_text); 113 setToolTipText(WorkbenchMessages.NavigationHistoryAction_forward_toolTip); 114 window.getWorkbench().getHelpSystem().setHelp(this, 116 IWorkbenchHelpContextIds.NAVIGATION_HISTORY_FORWARD); 117 setImageDescriptor(sharedImages 118 .getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD)); 119 setDisabledImageDescriptor(sharedImages 120 .getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD_DISABLED)); 121 setActionDefinitionId("org.eclipse.ui.navigate.forwardHistory"); } else { 123 setText(WorkbenchMessages.NavigationHistoryAction_backward_text); 124 setToolTipText(WorkbenchMessages.NavigationHistoryAction_backward_toolTip); 125 window.getWorkbench().getHelpSystem().setHelp(this, 127 IWorkbenchHelpContextIds.NAVIGATION_HISTORY_BACKWARD); 128 setImageDescriptor(sharedImages 129 .getImageDescriptor(ISharedImages.IMG_TOOL_BACK)); 130 setDisabledImageDescriptor(sharedImages 131 .getImageDescriptor(ISharedImages.IMG_TOOL_BACK_DISABLED)); 132 setActionDefinitionId("org.eclipse.ui.navigate.backwardHistory"); } 134 setEnabled(false); 136 this.forward = forward; 137 setMenuCreator(new MenuCreator()); 138 } 139 140 143 public void pageClosed(IWorkbenchPage page) { 144 super.pageClosed(page); 145 setEnabled(false); 146 } 147 148 private NavigationHistoryEntry[] colapseEntries( 149 NavigationHistoryEntry[] entries, int entriesCount[]) { 150 ArrayList allEntries = new ArrayList (Arrays.asList(entries)); 151 NavigationHistoryEntry previousEntry = null; 152 int i = -1; 153 for (Iterator iter = allEntries.iterator(); iter.hasNext();) { 154 NavigationHistoryEntry entry = (NavigationHistoryEntry) iter.next(); 155 if (previousEntry != null) { 156 String text = previousEntry.getHistoryText(); 157 if (text != null) { 158 if (text.equals(entry.getHistoryText()) 159 && previousEntry.editorInfo == entry.editorInfo) { 160 iter.remove(); 161 entriesCount[i]++; 162 continue; 163 } 164 } 165 } 166 previousEntry = entry; 167 i++; 168 } 169 entries = new NavigationHistoryEntry[allEntries.size()]; 170 return (NavigationHistoryEntry[]) allEntries.toArray(entries); 171 } 172 173 176 public void pageActivated(IWorkbenchPage page) { 177 super.pageActivated(page); 178 NavigationHistory nh = (NavigationHistory) page.getNavigationHistory(); 179 if (forward) { 180 nh.setForwardAction(this); 181 } else { 182 nh.setBackwardAction(this); 183 } 184 } 185 186 189 public void run() { 190 if (getWorkbenchWindow() == null) { 191 return; 193 } 194 IWorkbenchPage page = getActivePage(); 195 if (page != null) { 196 NavigationHistory nh = (NavigationHistory) page 197 .getNavigationHistory(); 198 if (forward) { 199 nh.forward(); 200 } else { 201 nh.backward(); 202 } 203 } 204 } 205 206 public void update() { 207 WorkbenchPage page = (WorkbenchPage) getActivePage(); 210 if (page == null) { 211 return; 212 } 213 NavigationHistory history = (NavigationHistory) page 214 .getNavigationHistory(); 215 NavigationHistoryEntry[] entries; 216 if (forward) { 217 setEnabled(history.canForward()); 218 entries = history.getForwardEntries(); 219 if (entries.length > 0) { 220 NavigationHistoryEntry entry = entries[0]; 221 String text = NLS.bind(WorkbenchMessages.NavigationHistoryAction_forward_toolTipName, entry.getHistoryText() ); 222 setToolTipText(text); 223 } else { 224 setToolTipText(WorkbenchMessages.NavigationHistoryAction_forward_toolTip); 225 } 226 } else { 227 setEnabled(history.canBackward()); 228 entries = history.getBackwardEntries(); 229 if (entries.length > 0) { 230 NavigationHistoryEntry entry = entries[0]; 231 String text = NLS.bind(WorkbenchMessages.NavigationHistoryAction_backward_toolTipName, entry.getHistoryText() ); 232 setToolTipText(text); 233 } else { 234 setToolTipText(WorkbenchMessages.NavigationHistoryAction_backward_toolTip); 235 } 236 } 237 } 238 } 239 | Popular Tags |