001: package net.refractions.udig.style.sld.editor.internal;
002:
003: /*******************************************************************************
004: * Copyright (c) 2004, 2005 IBM Corporation and others.
005: * All rights reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * IBM Corporation - initial API and implementation
012: *******************************************************************************/
013:
014: import java.util.ArrayList;
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Set;
019:
020: import org.eclipse.core.commands.IHandler;
021: import org.eclipse.jface.action.Action;
022: import org.eclipse.jface.action.ActionContributionItem;
023: import org.eclipse.jface.action.IAction;
024: import org.eclipse.jface.action.IContributionItem;
025: import org.eclipse.jface.action.IMenuCreator;
026: import org.eclipse.jface.action.ToolBarManager;
027: import org.eclipse.jface.commands.ActionHandler;
028: import org.eclipse.osgi.util.NLS;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Menu;
033: import org.eclipse.swt.widgets.ToolBar;
034: import org.eclipse.ui.ISharedImages;
035: import org.eclipse.ui.PlatformUI;
036: import org.eclipse.ui.handlers.IHandlerActivation;
037: import org.eclipse.ui.handlers.IHandlerService;
038: import org.eclipse.ui.internal.WorkbenchMessages;
039: import org.eclipse.ui.internal.WorkbenchPlugin;
040:
041: /**
042: * History for navigating preference pages.
043: *
044: * @since 3.1
045: */
046: class PageHistoryHolder {
047:
048: /**
049: * The history toolbar.
050: */
051: private ToolBarManager historyToolbar;
052:
053: /**
054: * A list of preference history domain elements that stores the history of
055: * the visited preference pages.
056: */
057: private List<PageHistoryEntry> history = new ArrayList<PageHistoryEntry>();
058:
059: /**
060: * Stores the current entry into <code>history</code> and
061: * <code>historyLabels</code>.
062: */
063: private int historyIndex = -1;
064:
065: /**
066: * The preference dialog we implement the history for.
067: */
068: private final FilteredEditorDialog dialog;
069:
070: /**
071: * The handler submission for these controls.
072: */
073: private Set<IHandlerActivation> activations = new HashSet<IHandlerActivation>();
074:
075: /**
076: * Creates a new history for the given dialog.
077: *
078: * @param dialog
079: * the preference dialog to create a history for
080: */
081: public PageHistoryHolder(FilteredEditorDialog dialog) {
082: this .dialog = dialog;
083: }
084:
085: /**
086: * Returns the preference page path (for now: its id) for the history at
087: * <code>index</code>.
088: *
089: * @param index
090: * the index into the history
091: * @return the preference page path at <code>index</code> or
092: * <code>null</code> if <code>index</code> is not a valid
093: * history index
094: */
095: private PageHistoryEntry getHistoryEntry(int index) {
096: if (index >= 0 && index < history.size())
097: return (PageHistoryEntry) history.get(index);
098: return null;
099: }
100:
101: /**
102: * Adds the preference page path and its label to the page history.
103: *
104: * @param entry
105: * the preference page history entry
106: */
107: public void addHistoryEntry(PageHistoryEntry entry) {
108: if (historyIndex == -1
109: || !history.get(historyIndex).equals(entry)) {
110: history.subList(historyIndex + 1, history.size()).clear();
111: history.add(entry);
112: historyIndex++;
113: updateHistoryControls();
114: }
115: }
116:
117: /**
118: * Sets the current page to be the one corresponding to the given index in
119: * the page history.
120: *
121: * @param index
122: * the index into the page history
123: */
124: private void jumpToHistory(int index) {
125: if (index >= 0 && index < history.size()) {
126: historyIndex = index;
127: dialog.setCurrentPageId(getHistoryEntry(index).getId());
128: }
129: updateHistoryControls();
130: }
131:
132: /**
133: * Updates the history controls.
134: *
135: */
136: private void updateHistoryControls() {
137: historyToolbar.update(false);
138: IContributionItem[] items = historyToolbar.getItems();
139: for (int i = 0; i < items.length; i++) {
140: items[i].update(IAction.ENABLED);
141: items[i].update(IAction.TOOL_TIP_TEXT);
142: }
143: }
144:
145: /**
146: * Creates the history toolbar and initializes <code>historyToolbar</code>.
147: *
148: * @param historyBar
149: * @param manager
150: * @return the control of the history toolbar
151: */
152: public ToolBar createHistoryControls(ToolBar historyBar,
153: ToolBarManager manager) {
154:
155: historyToolbar = manager;
156: /**
157: * Superclass of the two for-/backward actions for the history.
158: */
159: abstract class HistoryNavigationAction extends Action implements
160: IMenuCreator {
161: private Menu lastMenu;
162:
163: protected final static int MAX_ENTRIES = 5;
164:
165: HistoryNavigationAction() {
166: super ("", IAction.AS_DROP_DOWN_MENU); //$NON-NLS-1$
167: }
168:
169: @Override
170: public IMenuCreator getMenuCreator() {
171: return this ;
172: }
173:
174: public void dispose() {
175: if (lastMenu != null) {
176: lastMenu.dispose();
177: lastMenu = null;
178: }
179: }
180:
181: public Menu getMenu(Control parent) {
182: if (lastMenu != null) {
183: lastMenu.dispose();
184: }
185: lastMenu = new Menu(parent);
186: createEntries(lastMenu);
187: return lastMenu;
188:
189: }
190:
191: public Menu getMenu(Menu parent) {
192: return null;
193: }
194:
195: protected void addActionToMenu(Menu parent, IAction action) {
196: ActionContributionItem item = new ActionContributionItem(
197: action);
198: item.fill(parent, -1);
199: }
200:
201: protected abstract void createEntries(Menu menu);
202: }
203:
204: /**
205: * Menu entry for the toolbar dropdowns. Instances are direct-jump
206: * entries in the navigation history.
207: */
208: class HistoryItemAction extends Action {
209:
210: private final int index;
211:
212: HistoryItemAction(int index, String label) {
213: super (label, IAction.AS_PUSH_BUTTON);
214: this .index = index;
215: }
216:
217: @Override
218: public void run() {
219: jumpToHistory(index);
220: }
221: }
222:
223: HistoryNavigationAction backward = new HistoryNavigationAction() {
224: @Override
225: public void run() {
226: jumpToHistory(historyIndex - 1);
227: }
228:
229: @Override
230: public boolean isEnabled() {
231: boolean enabled = historyIndex > 0;
232: if (enabled)
233: setToolTipText(NLS
234: .bind(
235: WorkbenchMessages.NavigationHistoryAction_backward_toolTipName,
236: getHistoryEntry(historyIndex - 1)
237: .getLabel()));
238: return enabled;
239: }
240:
241: @Override
242: protected void createEntries(Menu menu) {
243: int limit = Math.max(0, historyIndex - MAX_ENTRIES);
244: for (int i = historyIndex - 1; i >= limit; i--) {
245: IAction action = new HistoryItemAction(i,
246: getHistoryEntry(i).getLabel());
247: addActionToMenu(menu, action);
248: }
249: }
250: };
251: backward
252: .setText(WorkbenchMessages.NavigationHistoryAction_backward_text);
253: backward
254: .setActionDefinitionId("org.eclipse.ui.navigate.backwardHistory"); //$NON-NLS-1$
255: backward.setImageDescriptor(WorkbenchPlugin.getDefault()
256: .getSharedImages().getImageDescriptor(
257: ISharedImages.IMG_TOOL_BACK));
258: registerKeybindings(backward);
259: historyToolbar.add(backward);
260:
261: HistoryNavigationAction forward = new HistoryNavigationAction() {
262: @Override
263: public void run() {
264: jumpToHistory(historyIndex + 1);
265: }
266:
267: @Override
268: public boolean isEnabled() {
269: boolean enabled = historyIndex < history.size() - 1;
270: if (enabled)
271: setToolTipText(NLS
272: .bind(
273: WorkbenchMessages.NavigationHistoryAction_forward_toolTipName,
274: getHistoryEntry(historyIndex + 1)
275: .getLabel()));
276: return enabled;
277: }
278:
279: @Override
280: protected void createEntries(Menu menu) {
281: int limit = Math.min(history.size(), historyIndex
282: + MAX_ENTRIES + 1);
283: for (int i = historyIndex + 1; i < limit; i++) {
284: IAction action = new HistoryItemAction(i,
285: getHistoryEntry(i).getLabel());
286: addActionToMenu(menu, action);
287: }
288: }
289: };
290: forward
291: .setText(WorkbenchMessages.NavigationHistoryAction_forward_text);
292: forward
293: .setActionDefinitionId("org.eclipse.ui.navigate.forwardHistory"); //$NON-NLS-1$
294: forward.setImageDescriptor(WorkbenchPlugin.getDefault()
295: .getSharedImages().getImageDescriptor(
296: ISharedImages.IMG_TOOL_FORWARD));
297: registerKeybindings(forward);
298: historyToolbar.add(forward);
299:
300: return historyBar;
301: }
302:
303: /**
304: * Registers the given action with the workbench command support.
305: *
306: * @param action
307: * the action to register.
308: */
309: private void registerKeybindings(IAction action) {
310: final IHandler handler = new ActionHandler(action);
311: final IHandlerService handlerService = (IHandlerService) PlatformUI
312: .getWorkbench().getAdapter(IHandlerService.class);
313: final IHandlerActivation activation = handlerService
314: .activateHandler(action.getActionDefinitionId(),
315: handler);
316: // new ActiveShellExpression(dialog.getShell()),
317: // ISources.ACTIVE_SHELL);
318: activations.add(activation);
319: }
320:
321: /**
322: * Dispose the receiver and clear out the references.
323: *
324: */
325: public void dispose() {
326: final IHandlerService handlerService = (IHandlerService) PlatformUI
327: .getWorkbench().getAdapter(IHandlerService.class);
328: final Iterator iterator = activations.iterator();
329: while (iterator.hasNext()) {
330: handlerService
331: .deactivateHandler((IHandlerActivation) iterator
332: .next());
333: }
334: activations.clear();
335:
336: }
337:
338: /**
339: * Create the history control in the parent
340: *
341: * @param parent
342: * @return Control
343: */
344: public Control createHistoryControls(Composite parent) {
345: ToolBar historyBar = new ToolBar(parent, SWT.FLAT
346: | SWT.HORIZONTAL);
347:
348: ToolBarManager historyManager = new ToolBarManager(historyBar);
349:
350: createHistoryControls(historyBar, historyManager);
351: return historyBar;
352: }
353: }
|