01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.internal;
11:
12: import org.eclipse.core.commands.Command;
13: import org.eclipse.core.commands.ParameterizedCommand;
14: import org.eclipse.swt.SWT;
15: import org.eclipse.swt.widgets.Table;
16: import org.eclipse.swt.widgets.TableItem;
17: import org.eclipse.ui.IPerspectiveDescriptor;
18: import org.eclipse.ui.commands.ICommandService;
19: import org.eclipse.ui.model.PerspectiveLabelProvider;
20:
21: /**
22: * This handler is used to switch between perspectives using the keyboard.
23: * <p>
24: * Replacement for CyclePerspectiveAction
25: * </p>
26: *
27: * @since 3.3
28: */
29: public class CyclePerspectiveHandler extends CycleBaseHandler {
30: private PerspectiveLabelProvider labelProvider = new PerspectiveLabelProvider(
31: false);
32:
33: /* (non-Javadoc)
34: * @see org.eclipse.ui.internal.CycleBaseHandler#addItems(org.eclipse.swt.widgets.Table, org.eclipse.ui.internal.WorkbenchPage)
35: */
36: protected void addItems(Table table, WorkbenchPage page) {
37: IPerspectiveDescriptor perspectives[] = page
38: .getSortedPerspectives();
39: for (int i = perspectives.length - 1; i >= 0; i--) {
40: TableItem item = new TableItem(table, SWT.NONE);
41: IPerspectiveDescriptor desc = perspectives[i];
42: String text = labelProvider.getText(desc);
43: if (text == null) {
44: text = "";//$NON-NLS-1$
45: }
46: item.setText(text);
47: item.setImage(labelProvider.getImage(desc));
48: item.setData(desc);
49: }
50:
51: }
52:
53: /* (non-Javadoc)
54: * @see org.eclipse.ui.internal.CycleBaseHandler#getBackwardCommand()
55: */
56: protected ParameterizedCommand getBackwardCommand() {
57: final ICommandService commandService = (ICommandService) window
58: .getWorkbench().getService(ICommandService.class);
59: final Command command = commandService
60: .getCommand("org.eclipse.ui.window.previousPerspective"); //$NON-NLS-1$
61: ParameterizedCommand commandBack = new ParameterizedCommand(
62: command, null);
63: return commandBack;
64: }
65:
66: /* (non-Javadoc)
67: * @see org.eclipse.ui.internal.CycleBaseHandler#getForwardCommand()
68: */
69: protected ParameterizedCommand getForwardCommand() {
70: final ICommandService commandService = (ICommandService) window
71: .getWorkbench().getService(ICommandService.class);
72: final Command command = commandService
73: .getCommand("org.eclipse.ui.window.nextPerspective"); //$NON-NLS-1$
74: ParameterizedCommand commandF = new ParameterizedCommand(
75: command, null);
76: return commandF;
77: }
78:
79: /* (non-Javadoc)
80: * @see org.eclipse.ui.internal.CycleBaseHandler#getTableHeader()
81: */
82: protected String getTableHeader() {
83: return WorkbenchMessages.CyclePerspectiveAction_header;
84: }
85:
86: /* (non-Javadoc)
87: * @see org.eclipse.core.commands.AbstractHandler#dispose()
88: */
89: public void dispose() {
90: if (labelProvider != null) {
91: labelProvider.dispose();
92: labelProvider = null;
93: }
94: super.dispose();
95: }
96: }
|