KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 package org.eclipse.ui.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
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 /**
30  * The <code>NavigationHistoryAction</code> moves navigation history
31  * back and forward.
32  */

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 JavaDoc 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 JavaDoc(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     /**
103      * Create a new instance of <code>NavigationHistoryAction</code>
104      *
105      * @param window the workbench window this action applies to
106      * @param forward if this action should move history forward of backward
107      */

108     public NavigationHistoryAction(IWorkbenchWindow window, boolean forward) {
109         super("", window); //$NON-NLS-1$
110
ISharedImages sharedImages = window.getWorkbench().getSharedImages();
111         if (forward) {
112             setText(WorkbenchMessages.NavigationHistoryAction_forward_text);
113             setToolTipText(WorkbenchMessages.NavigationHistoryAction_forward_toolTip);
114             // @issue missing action id
115
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"); //$NON-NLS-1$
122
} else {
123             setText(WorkbenchMessages.NavigationHistoryAction_backward_text);
124             setToolTipText(WorkbenchMessages.NavigationHistoryAction_backward_toolTip);
125             // @issue missing action id
126
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"); //$NON-NLS-1$
133
}
134         // WorkbenchHelp.setHelp(this, IHelpContextIds.CLOSE_ALL_PAGES_ACTION);
135
setEnabled(false);
136         this.forward = forward;
137         setMenuCreator(new MenuCreator());
138     }
139
140     /* (non-Javadoc)
141      * Method declared on PageEventAction.
142      */

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 JavaDoc allEntries = new ArrayList JavaDoc(Arrays.asList(entries));
151         NavigationHistoryEntry previousEntry = null;
152         int i = -1;
153         for (Iterator JavaDoc iter = allEntries.iterator(); iter.hasNext();) {
154             NavigationHistoryEntry entry = (NavigationHistoryEntry) iter.next();
155             if (previousEntry != null) {
156                 String JavaDoc 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     /* (non-Javadoc)
174      * Method declared on PageEventAction.
175      */

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     /* (non-Javadoc)
187      * Method declared on IAction.
188      */

189     public void run() {
190         if (getWorkbenchWindow() == null) {
191             // action has been disposed
192
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         // Set the enabled state of the action and set the tool tip text. The tool tip
208
// text is set to reflect the item that one will move back/forward to.
209
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 JavaDoc 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 JavaDoc 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