KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.Table;
15 import org.eclipse.swt.widgets.TableItem;
16 import org.eclipse.ui.IPerspectiveDescriptor;
17 import org.eclipse.ui.IPerspectiveListener;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.model.PerspectiveLabelProvider;
21
22 /**
23  * Implements a action to enable the user switch between perspectives
24  * using keyboard.
25  */

26 public class CyclePerspectiveAction extends CyclePartAction implements
27         IPerspectiveListener {
28
29     private PerspectiveLabelProvider labelProvider = new PerspectiveLabelProvider(
30             false);
31
32     /**
33      * Creates a CyclePerspectiveAction.
34      *
35      * @param window the window
36      * @param forward if you are cycling forward
37      */

38     public CyclePerspectiveAction(IWorkbenchWindow window, boolean forward) {
39         super(window, forward);
40         window.addPerspectiveListener(this);
41         updateState();
42     }
43
44     protected void setText() {
45         // TBD: Remove text and tooltip when this becomes an invisible action.
46
if (forward) {
47             setText(WorkbenchMessages.CyclePerspectiveAction_next_text);
48             setToolTipText(WorkbenchMessages.CyclePerspectiveAction_next_toolTip);
49             // @issue missing action ids
50
getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp(this,
51                     IWorkbenchHelpContextIds.CYCLE_PERSPECTIVE_FORWARD_ACTION);
52             setActionDefinitionId("org.eclipse.ui.window.nextPerspective"); //$NON-NLS-1$
53
} else {
54             setText(WorkbenchMessages.CyclePerspectiveAction_prev_text);
55             setToolTipText(WorkbenchMessages.CyclePerspectiveAction_prev_toolTip);
56             // @issue missing action ids
57
getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp(this,
58                     IWorkbenchHelpContextIds.CYCLE_PERSPECTIVE_BACKWARD_ACTION);
59             setActionDefinitionId("org.eclipse.ui.window.previousPerspective"); //$NON-NLS-1$
60
}
61     }
62
63     /* (non-Javadoc)
64      * Method declared on IAction.
65      */

66     public void dispose() {
67         if (getWorkbenchWindow() == null) {
68             // already disposed
69
return;
70         }
71         getWorkbenchWindow().removePerspectiveListener(this);
72         labelProvider.dispose();
73         super.dispose();
74     }
75
76     /**
77      * Activate the selected item.
78      */

79     public void activate(IWorkbenchPage page, Object JavaDoc selection) {
80         if (selection != null) {
81             IPerspectiveDescriptor persp = (IPerspectiveDescriptor) selection;
82             page.setPerspective(persp);
83         }
84     }
85
86     /**
87      * Updates the enabled state.
88      */

89     public void updateState() {
90         WorkbenchPage page = (WorkbenchPage) getActivePage();
91         if (page == null) {
92             setEnabled(false);
93             return;
94         }
95         // enable iff there is at least one other editor to switch to
96
setEnabled(page.getSortedPerspectives().length >= 1);
97     }
98
99     /**
100      * Add all views to the dialog in the activation order
101      */

102     protected void addItems(Table table, WorkbenchPage page) {
103         IPerspectiveDescriptor perspectives[] = page.getSortedPerspectives();
104         for (int i = perspectives.length - 1; i >= 0; i--) {
105             TableItem item = new TableItem(table, SWT.NONE);
106             IPerspectiveDescriptor desc = perspectives[i];
107             String JavaDoc text = labelProvider.getText(desc);
108             if(text == null) {
109                 text = "";//$NON-NLS-1$
110
}
111             item.setText(text);
112             item.setImage(labelProvider.getImage(desc));
113             item.setData(desc);
114         }
115     }
116
117     /**
118      * Returns the string which will be shown in the table header.
119      */

120     protected String JavaDoc getTableHeader() {
121         return WorkbenchMessages.CyclePerspectiveAction_header;
122     }
123
124     /* (non-Javadoc)
125      * Method declared on IPerspectiveListener
126      */

127     public void perspectiveActivated(IWorkbenchPage page,
128             IPerspectiveDescriptor perspective) {
129         updateState();
130     }
131
132     /* (non-Javadoc)
133      * Method declared on IPerspectiveListener
134      */

135     public void perspectiveChanged(IWorkbenchPage page,
136             IPerspectiveDescriptor perspective, String JavaDoc changeId) {
137         // do nothing
138
}
139
140 }
141
Popular Tags