KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal;
13
14 import org.eclipse.core.commands.Command;
15 import org.eclipse.core.commands.ParameterizedCommand;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Table;
18 import org.eclipse.swt.widgets.TableItem;
19 import org.eclipse.ui.IPerspectiveDescriptor;
20 import org.eclipse.ui.commands.ICommandService;
21 import org.eclipse.ui.model.PerspectiveLabelProvider;
22
23 /**
24  * This handler is used to switch between perspectives using the keyboard.
25  * <p>
26  * Replacement for CyclePerspectiveAction
27  * </p>
28  *
29  * @since 3.3
30  */

31 public class CyclePerspectiveHandler extends CycleBaseHandler {
32     private PerspectiveLabelProvider labelProvider = new PerspectiveLabelProvider(
33             false);
34     
35     /* (non-Javadoc)
36      * @see org.eclipse.ui.internal.CycleBaseHandler#addItems(org.eclipse.swt.widgets.Table, org.eclipse.ui.internal.WorkbenchPage)
37      */

38     protected void addItems(Table table, WorkbenchPage page) {
39         IPerspectiveDescriptor perspectives[] = page.getSortedPerspectives();
40         for (int i = perspectives.length - 1; i >= 0; i--) {
41             TableItem item = new TableItem(table, SWT.NONE);
42             IPerspectiveDescriptor desc = perspectives[i];
43             String JavaDoc text = labelProvider.getText(desc);
44             if(text == null) {
45                 text = "";//$NON-NLS-1$
46
}
47             item.setText(text);
48             item.setImage(labelProvider.getImage(desc));
49             item.setData(desc);
50         }
51
52     }
53
54     /* (non-Javadoc)
55      * @see org.eclipse.ui.internal.CycleBaseHandler#getBackwardCommand()
56      */

57     protected ParameterizedCommand getBackwardCommand() {
58         final ICommandService commandService = (ICommandService) window.getWorkbench().getService(ICommandService.class);
59         final Command command = commandService.getCommand("org.eclipse.ui.window.previousPerspective"); //$NON-NLS-1$
60
ParameterizedCommand commandBack = new ParameterizedCommand(command, null);
61         return commandBack;
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.ui.internal.CycleBaseHandler#getForwardCommand()
66      */

67     protected ParameterizedCommand getForwardCommand() {
68         final ICommandService commandService = (ICommandService) window.getWorkbench().getService(ICommandService.class);
69         final Command command = commandService.getCommand("org.eclipse.ui.window.nextPerspective"); //$NON-NLS-1$
70
ParameterizedCommand commandF = new ParameterizedCommand(command, null);
71         return commandF;
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.ui.internal.CycleBaseHandler#getTableHeader()
76      */

77     protected String JavaDoc getTableHeader() {
78         return WorkbenchMessages.CyclePerspectiveAction_header;
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.core.commands.AbstractHandler#dispose()
83      */

84     public void dispose() {
85         if (labelProvider!=null) {
86             labelProvider.dispose();
87             labelProvider = null;
88         }
89         super.dispose();
90     }
91 }
92
Popular Tags