001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.Arrays;
014: import java.util.Iterator;
015:
016: import org.eclipse.jface.action.IMenuCreator;
017: import org.eclipse.osgi.util.NLS;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.SelectionAdapter;
020: import org.eclipse.swt.events.SelectionEvent;
021: import org.eclipse.swt.widgets.Control;
022: import org.eclipse.swt.widgets.Menu;
023: import org.eclipse.swt.widgets.MenuItem;
024: import org.eclipse.ui.ISharedImages;
025: import org.eclipse.ui.IWorkbenchPage;
026: import org.eclipse.ui.IWorkbenchWindow;
027:
028: /**
029: * The <code>NavigationHistoryAction</code> moves navigation history
030: * back and forward.
031: */
032: public class NavigationHistoryAction extends PageEventAction {
033: private boolean forward;
034:
035: private Menu historyMenu;
036:
037: private int MAX_HISTORY_LENGTH = 9;
038:
039: private class MenuCreator implements IMenuCreator {
040: public void dispose() {
041: if (historyMenu != null) {
042: for (int i = 0; i < historyMenu.getItemCount(); i++) {
043: MenuItem menuItem = historyMenu.getItem(i);
044: menuItem.setData(null);
045: }
046: historyMenu.dispose();
047: historyMenu = null;
048: }
049: }
050:
051: public Menu getMenu(Menu parent) {
052: return null;
053: }
054:
055: public Menu getMenu(Control parent) {
056: dispose();
057: historyMenu = new Menu(parent);
058: IWorkbenchPage page = getWorkbenchWindow().getActivePage();
059: if (page == null) {
060: return historyMenu;
061: }
062:
063: final NavigationHistory history = (NavigationHistory) getWorkbenchWindow()
064: .getActivePage().getNavigationHistory();
065: NavigationHistoryEntry[] entries;
066: if (forward) {
067: entries = history.getForwardEntries();
068: } else {
069: entries = history.getBackwardEntries();
070: }
071: int entriesCount[] = new int[entries.length];
072: for (int i = 0; i < entriesCount.length; i++) {
073: entriesCount[i] = 1;
074: }
075: entries = colapseEntries(entries, entriesCount);
076: for (int i = 0; i < entries.length; i++) {
077: if (i > MAX_HISTORY_LENGTH) {
078: break;
079: }
080: String text = entries[i].getHistoryText();
081: if (text != null) {
082: MenuItem item = new MenuItem(historyMenu, SWT.NONE);
083: item.setData(entries[i]);
084: if (entriesCount[i] > 1) {
085: text = NLS
086: .bind(
087: WorkbenchMessages.NavigationHistoryAction_locations,
088: text, new Integer(
089: entriesCount[i]));
090: }
091: item.setText(text);
092: item.addSelectionListener(new SelectionAdapter() {
093: public void widgetSelected(SelectionEvent e) {
094: history.shiftCurrentEntry(
095: (NavigationHistoryEntry) e.widget
096: .getData(), forward);
097: }
098: });
099: }
100: }
101: return historyMenu;
102: }
103: }
104:
105: /**
106: * Create a new instance of <code>NavigationHistoryAction</code>
107: *
108: * @param window the workbench window this action applies to
109: * @param forward if this action should move history forward of backward
110: */
111: public NavigationHistoryAction(IWorkbenchWindow window,
112: boolean forward) {
113: super ("", window); //$NON-NLS-1$
114: ISharedImages sharedImages = window.getWorkbench()
115: .getSharedImages();
116: if (forward) {
117: setText(WorkbenchMessages.NavigationHistoryAction_forward_text);
118: setToolTipText(WorkbenchMessages.NavigationHistoryAction_forward_toolTip);
119: // @issue missing action id
120: window
121: .getWorkbench()
122: .getHelpSystem()
123: .setHelp(
124: this ,
125: IWorkbenchHelpContextIds.NAVIGATION_HISTORY_FORWARD);
126: setImageDescriptor(sharedImages
127: .getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));
128: setDisabledImageDescriptor(sharedImages
129: .getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD_DISABLED));
130: setActionDefinitionId("org.eclipse.ui.navigate.forwardHistory"); //$NON-NLS-1$
131: } else {
132: setText(WorkbenchMessages.NavigationHistoryAction_backward_text);
133: setToolTipText(WorkbenchMessages.NavigationHistoryAction_backward_toolTip);
134: // @issue missing action id
135: window
136: .getWorkbench()
137: .getHelpSystem()
138: .setHelp(
139: this ,
140: IWorkbenchHelpContextIds.NAVIGATION_HISTORY_BACKWARD);
141: setImageDescriptor(sharedImages
142: .getImageDescriptor(ISharedImages.IMG_TOOL_BACK));
143: setDisabledImageDescriptor(sharedImages
144: .getImageDescriptor(ISharedImages.IMG_TOOL_BACK_DISABLED));
145: setActionDefinitionId("org.eclipse.ui.navigate.backwardHistory"); //$NON-NLS-1$
146: }
147: // WorkbenchHelp.setHelp(this, IHelpContextIds.CLOSE_ALL_PAGES_ACTION);
148: setEnabled(false);
149: this .forward = forward;
150: setMenuCreator(new MenuCreator());
151: }
152:
153: /* (non-Javadoc)
154: * Method declared on PageEventAction.
155: */
156: public void pageClosed(IWorkbenchPage page) {
157: super .pageClosed(page);
158: setEnabled(false);
159: }
160:
161: private NavigationHistoryEntry[] colapseEntries(
162: NavigationHistoryEntry[] entries, int entriesCount[]) {
163: ArrayList allEntries = new ArrayList(Arrays.asList(entries));
164: NavigationHistoryEntry previousEntry = null;
165: int i = -1;
166: for (Iterator iter = allEntries.iterator(); iter.hasNext();) {
167: NavigationHistoryEntry entry = (NavigationHistoryEntry) iter
168: .next();
169: if (previousEntry != null) {
170: String text = previousEntry.getHistoryText();
171: if (text != null) {
172: if (text.equals(entry.getHistoryText())
173: && previousEntry.editorInfo == entry.editorInfo) {
174: iter.remove();
175: entriesCount[i]++;
176: continue;
177: }
178: }
179: }
180: previousEntry = entry;
181: i++;
182: }
183: entries = new NavigationHistoryEntry[allEntries.size()];
184: return (NavigationHistoryEntry[]) allEntries.toArray(entries);
185: }
186:
187: /* (non-Javadoc)
188: * Method declared on PageEventAction.
189: */
190: public void pageActivated(IWorkbenchPage page) {
191: super .pageActivated(page);
192: NavigationHistory nh = (NavigationHistory) page
193: .getNavigationHistory();
194: if (forward) {
195: nh.setForwardAction(this );
196: } else {
197: nh.setBackwardAction(this );
198: }
199: }
200:
201: /* (non-Javadoc)
202: * Method declared on IAction.
203: */
204: public void run() {
205: if (getWorkbenchWindow() == null) {
206: // action has been disposed
207: return;
208: }
209: IWorkbenchPage page = getActivePage();
210: if (page != null) {
211: NavigationHistory nh = (NavigationHistory) page
212: .getNavigationHistory();
213: if (forward) {
214: nh.forward();
215: } else {
216: nh.backward();
217: }
218: }
219: }
220:
221: public void update() {
222: // Set the enabled state of the action and set the tool tip text. The tool tip
223: // text is set to reflect the item that one will move back/forward to.
224: WorkbenchPage page = (WorkbenchPage) getActivePage();
225: if (page == null) {
226: return;
227: }
228: NavigationHistory history = (NavigationHistory) page
229: .getNavigationHistory();
230: NavigationHistoryEntry[] entries;
231: if (forward) {
232: setEnabled(history.canForward());
233: entries = history.getForwardEntries();
234: if (entries.length > 0) {
235: NavigationHistoryEntry entry = entries[0];
236: String text = NLS
237: .bind(
238: WorkbenchMessages.NavigationHistoryAction_forward_toolTipName,
239: entry.getHistoryText());
240: setToolTipText(text);
241: } else {
242: setToolTipText(WorkbenchMessages.NavigationHistoryAction_forward_toolTip);
243: }
244: } else {
245: setEnabled(history.canBackward());
246: entries = history.getBackwardEntries();
247: if (entries.length > 0) {
248: NavigationHistoryEntry entry = entries[0];
249: String text = NLS
250: .bind(
251: WorkbenchMessages.NavigationHistoryAction_backward_toolTipName,
252: entry.getHistoryText());
253: setToolTipText(text);
254: } else {
255: setToolTipText(WorkbenchMessages.NavigationHistoryAction_backward_toolTip);
256: }
257: }
258: }
259: }
|