KickJava   Java API By Example, From Geeks To Geeks.

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


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.jface.action.ContributionItem;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.widgets.ToolBar;
23 import org.eclipse.swt.widgets.ToolItem;
24 import org.eclipse.ui.IPerspectiveDescriptor;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkbenchPreferenceConstants;
27 import org.eclipse.ui.internal.util.PrefUtil;
28
29 /**
30  * A {@link ContributionItem} specifically for contributions to the perspective switcher.
31  *
32  */

33 public class PerspectiveBarContributionItem extends ContributionItem {
34
35     private IPerspectiveDescriptor perspective;
36
37     private IPreferenceStore apiPreferenceStore = PrefUtil
38             .getAPIPreferenceStore();
39
40     private ToolItem toolItem = null;
41
42     private Image image;
43
44     private IWorkbenchPage workbenchPage;
45
46     /**
47      * Create a new perspective contribution item
48      *
49      * @param perspective the descriptor for the perspective
50      * @param workbenchPage the page that this perspective is in
51      */

52     public PerspectiveBarContributionItem(IPerspectiveDescriptor perspective,
53             IWorkbenchPage workbenchPage) {
54         super(perspective.getId());
55         this.perspective = perspective;
56         this.workbenchPage = workbenchPage;
57     }
58
59     /* (non-Javadoc)
60      * @see org.eclipse.jface.action.ContributionItem#dispose()
61      */

62     public void dispose() {
63         super.dispose();
64         if (image != null && !image.isDisposed()) {
65             image.dispose();
66             image = null;
67         }
68         apiPreferenceStore = null;
69         workbenchPage = null;
70         perspective = null;
71
72     }
73
74     public void fill(ToolBar parent, int index) {
75         if (toolItem == null && parent != null && !parent.isDisposed()) {
76
77             if (index >= 0) {
78                 toolItem = new ToolItem(parent, SWT.CHECK, index);
79             } else {
80                 toolItem = new ToolItem(parent, SWT.CHECK);
81             }
82
83             if (image == null || image.isDisposed()) {
84                 createImage();
85             }
86             toolItem.setImage(image);
87
88             toolItem.setToolTipText(NLS.bind(WorkbenchMessages.PerspectiveBarContributionItem_toolTip, perspective.getLabel()));
89             toolItem.addSelectionListener(new SelectionAdapter() {
90
91                 public void widgetSelected(SelectionEvent event) {
92                     select();
93                 }
94             });
95             toolItem.setData(this); //TODO review need for this
96
update();
97         }
98     }
99
100     private void createImage() {
101         ImageDescriptor imageDescriptor = perspective.getImageDescriptor();
102         if (imageDescriptor != null) {
103             image = imageDescriptor.createImage();
104         } else {
105             image = WorkbenchImages.getImageDescriptor(
106                     IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE)
107                     .createImage();
108         }
109     }
110
111     Image getImage() {
112         if (image == null) {
113             createImage();
114         }
115         return image;
116     }
117
118     /**
119      * Select this perspective
120      */

121     public void select() {
122         if (workbenchPage.getPerspective() != perspective) {
123             workbenchPage.setPerspective(perspective);
124         } else {
125             toolItem.setSelection(true);
126         }
127     }
128
129     public void update() {
130         if (toolItem != null && !toolItem.isDisposed()) {
131             toolItem
132                     .setSelection(workbenchPage.getPerspective() == perspective);
133             if (apiPreferenceStore
134                     .getBoolean(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR)) {
135                 if (apiPreferenceStore.getString(
136                         IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR)
137                         .equals(IWorkbenchPreferenceConstants.TOP_LEFT)) {
138                     toolItem.setText(perspective.getLabel());
139                 } else {
140                     toolItem.setText(shortenText(perspective.getLabel(),
141                             toolItem));
142                 }
143             } else {
144                 toolItem.setText(""); //$NON-NLS-1$
145
}
146         }
147     }
148
149     /**
150      * Update this item with a new perspective descriptor
151      * @param newDesc
152      */

153     public void update(IPerspectiveDescriptor newDesc) {
154         perspective = newDesc;
155         if (toolItem != null && !toolItem.isDisposed()) {
156             ImageDescriptor imageDescriptor = perspective.getImageDescriptor();
157             if (imageDescriptor != null) {
158                 toolItem.setImage(imageDescriptor.createImage());
159             } else {
160                 toolItem.setImage(WorkbenchImages.getImageDescriptor(
161                         IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE)
162                         .createImage());
163             }
164             toolItem.setToolTipText(NLS.bind(WorkbenchMessages.PerspectiveBarContributionItem_toolTip, perspective.getLabel() ));
165         }
166         update();
167     }
168
169     IWorkbenchPage getPage() {
170         return workbenchPage;
171     }
172
173     IPerspectiveDescriptor getPerspective() {
174         return perspective;
175     }
176
177     ToolItem getToolItem() {
178         return toolItem;
179     }
180
181     /**
182      * Answer whether the receiver is a match for the provided
183      * perspective descriptor
184      *
185      * @param perspective the perspective descriptor
186      * @param workbenchPage the page
187      * @return <code>true</code> if it is a match
188      */

189     public boolean handles(IPerspectiveDescriptor perspective,
190             IWorkbenchPage workbenchPage) {
191         return this.perspective == perspective
192                 && this.workbenchPage == workbenchPage;
193     }
194
195     /**
196      * Set the current perspective
197      * @param newPerspective
198      */

199     public void setPerspective(IPerspectiveDescriptor newPerspective) {
200         this.perspective = newPerspective;
201     }
202
203     // TODO review need for this method
204
void setSelection(boolean b) {
205         if (toolItem != null && !toolItem.isDisposed()) {
206             toolItem.setSelection(b);
207         }
208     }
209
210     static int getMaxWidth(Image image) {
211         return image.getBounds().width * 5;
212     }
213
214     private static final String JavaDoc ellipsis = "..."; //$NON-NLS-1$
215

216     protected String JavaDoc shortenText(String JavaDoc textValue, ToolItem item) {
217         if (textValue == null || toolItem == null || toolItem.isDisposed()) {
218             return null;
219         }
220         String JavaDoc returnText = textValue;
221         GC gc = new GC(item.getParent());
222         int maxWidth = getMaxWidth(item.getImage());
223         if (gc.textExtent(textValue).x >= maxWidth) {
224             for (int i = textValue.length(); i > 0; i--) {
225                 String JavaDoc test = textValue.substring(0, i);
226                 test = test + ellipsis;
227                 if (gc.textExtent(test).x < maxWidth) {
228                     returnText = test;
229                     break;
230                 }
231             }
232         }
233         gc.dispose();
234         return returnText;
235     }
236 }
237
Popular Tags