001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.jface.action.ContributionItem;
013: import org.eclipse.jface.preference.IPreferenceStore;
014: import org.eclipse.jface.resource.ImageDescriptor;
015: import org.eclipse.osgi.util.NLS;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.events.SelectionAdapter;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.graphics.GC;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.widgets.ToolBar;
022: import org.eclipse.swt.widgets.ToolItem;
023: import org.eclipse.ui.IPerspectiveDescriptor;
024: import org.eclipse.ui.IWorkbenchPage;
025: import org.eclipse.ui.IWorkbenchPreferenceConstants;
026: import org.eclipse.ui.internal.util.PrefUtil;
027:
028: /**
029: * A {@link ContributionItem} specifically for contributions to the perspective switcher.
030: *
031: */
032: public class PerspectiveBarContributionItem extends ContributionItem {
033:
034: private IPerspectiveDescriptor perspective;
035:
036: private IPreferenceStore apiPreferenceStore = PrefUtil
037: .getAPIPreferenceStore();
038:
039: private ToolItem toolItem = null;
040:
041: private Image image;
042:
043: private IWorkbenchPage workbenchPage;
044:
045: /**
046: * Create a new perspective contribution item
047: *
048: * @param perspective the descriptor for the perspective
049: * @param workbenchPage the page that this perspective is in
050: */
051: public PerspectiveBarContributionItem(
052: IPerspectiveDescriptor perspective,
053: IWorkbenchPage workbenchPage) {
054: super (perspective.getId());
055: this .perspective = perspective;
056: this .workbenchPage = workbenchPage;
057: }
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.jface.action.ContributionItem#dispose()
061: */
062: public void dispose() {
063: super .dispose();
064: if (image != null && !image.isDisposed()) {
065: image.dispose();
066: image = null;
067: }
068: apiPreferenceStore = null;
069: workbenchPage = null;
070: perspective = null;
071:
072: }
073:
074: public void fill(ToolBar parent, int index) {
075: if (toolItem == null && parent != null && !parent.isDisposed()) {
076:
077: if (index >= 0) {
078: toolItem = new ToolItem(parent, SWT.CHECK, index);
079: } else {
080: toolItem = new ToolItem(parent, SWT.CHECK);
081: }
082:
083: if (image == null || image.isDisposed()) {
084: createImage();
085: }
086: toolItem.setImage(image);
087:
088: toolItem
089: .setToolTipText(NLS
090: .bind(
091: WorkbenchMessages.PerspectiveBarContributionItem_toolTip,
092: perspective.getLabel()));
093: toolItem.addSelectionListener(new SelectionAdapter() {
094:
095: public void widgetSelected(SelectionEvent event) {
096: select();
097: }
098: });
099: toolItem.setData(this ); //TODO review need for this
100: update();
101: }
102: }
103:
104: private void createImage() {
105: ImageDescriptor imageDescriptor = perspective
106: .getImageDescriptor();
107: if (imageDescriptor != null) {
108: image = imageDescriptor.createImage();
109: } else {
110: image = WorkbenchImages
111: .getImageDescriptor(
112: IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE)
113: .createImage();
114: }
115: }
116:
117: Image getImage() {
118: if (image == null) {
119: createImage();
120: }
121: return image;
122: }
123:
124: /**
125: * Select this perspective
126: */
127: public void select() {
128: if (workbenchPage.getPerspective() != perspective) {
129: workbenchPage.setPerspective(perspective);
130: } else {
131: toolItem.setSelection(true);
132: }
133: }
134:
135: public void update() {
136: if (toolItem != null && !toolItem.isDisposed()) {
137: toolItem
138: .setSelection(workbenchPage.getPerspective() == perspective);
139: if (apiPreferenceStore
140: .getBoolean(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR)) {
141: if (apiPreferenceStore
142: .getString(
143: IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR)
144: .equals(IWorkbenchPreferenceConstants.TOP_LEFT)) {
145: toolItem.setText(perspective.getLabel());
146: } else {
147: toolItem.setText(shortenText(
148: perspective.getLabel(), toolItem));
149: }
150: } else {
151: toolItem.setText(""); //$NON-NLS-1$
152: }
153: }
154: }
155:
156: /**
157: * Update this item with a new perspective descriptor
158: * @param newDesc
159: */
160: public void update(IPerspectiveDescriptor newDesc) {
161: perspective = newDesc;
162: if (toolItem != null && !toolItem.isDisposed()) {
163: ImageDescriptor imageDescriptor = perspective
164: .getImageDescriptor();
165: if (imageDescriptor != null) {
166: toolItem.setImage(imageDescriptor.createImage());
167: } else {
168: toolItem
169: .setImage(WorkbenchImages
170: .getImageDescriptor(
171: IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE)
172: .createImage());
173: }
174: toolItem
175: .setToolTipText(NLS
176: .bind(
177: WorkbenchMessages.PerspectiveBarContributionItem_toolTip,
178: perspective.getLabel()));
179: }
180: update();
181: }
182:
183: IWorkbenchPage getPage() {
184: return workbenchPage;
185: }
186:
187: IPerspectiveDescriptor getPerspective() {
188: return perspective;
189: }
190:
191: ToolItem getToolItem() {
192: return toolItem;
193: }
194:
195: /**
196: * Answer whether the receiver is a match for the provided
197: * perspective descriptor
198: *
199: * @param perspective the perspective descriptor
200: * @param workbenchPage the page
201: * @return <code>true</code> if it is a match
202: */
203: public boolean handles(IPerspectiveDescriptor perspective,
204: IWorkbenchPage workbenchPage) {
205: return this .perspective == perspective
206: && this .workbenchPage == workbenchPage;
207: }
208:
209: /**
210: * Set the current perspective
211: * @param newPerspective
212: */
213: public void setPerspective(IPerspectiveDescriptor newPerspective) {
214: this .perspective = newPerspective;
215: }
216:
217: // TODO review need for this method
218: void setSelection(boolean b) {
219: if (toolItem != null && !toolItem.isDisposed()) {
220: toolItem.setSelection(b);
221: }
222: }
223:
224: static int getMaxWidth(Image image) {
225: return image.getBounds().width * 5;
226: }
227:
228: private static final String ellipsis = "..."; //$NON-NLS-1$
229:
230: protected String shortenText(String textValue, ToolItem item) {
231: if (textValue == null || toolItem == null
232: || toolItem.isDisposed()) {
233: return null;
234: }
235: String returnText = textValue;
236: GC gc = new GC(item.getParent());
237: int maxWidth = getMaxWidth(item.getImage());
238: if (gc.textExtent(textValue).x >= maxWidth) {
239: for (int i = textValue.length(); i > 0; i--) {
240: String test = textValue.substring(0, i);
241: test = test + ellipsis;
242: if (gc.textExtent(test).x < maxWidth) {
243: returnText = test;
244: break;
245: }
246: }
247: }
248: gc.dispose();
249: return returnText;
250: }
251: }
|