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.util;
011:
012: import org.eclipse.jface.action.ContributionItem;
013: import org.eclipse.jface.action.IContributionManager;
014: import org.eclipse.jface.action.StatusLineLayoutData;
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.custom.CLabel;
017: import org.eclipse.swt.graphics.FontMetrics;
018: import org.eclipse.swt.graphics.GC;
019: import org.eclipse.swt.graphics.Point;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Label;
022:
023: /**
024: * @issue needs Javadoc
025: */
026: public class StatusLineContributionItem extends ContributionItem {
027:
028: public final static int DEFAULT_CHAR_WIDTH = 40;
029:
030: private int charWidth;
031:
032: private CLabel label;
033:
034: /**
035: * The composite into which this contribution item has been placed. This
036: * will be <code>null</code> if this instance has not yet been
037: * initialized.
038: */
039: private Composite statusLine = null;
040:
041: private String text = Util.ZERO_LENGTH_STRING;
042:
043: private int widthHint = -1;
044:
045: private int heightHint = -1;
046:
047: public StatusLineContributionItem(String id) {
048: this (id, DEFAULT_CHAR_WIDTH);
049: }
050:
051: public StatusLineContributionItem(String id, int charWidth) {
052: super (id);
053: this .charWidth = charWidth;
054: setVisible(false); // no text to start with
055: }
056:
057: public void fill(Composite parent) {
058: statusLine = parent;
059:
060: Label sep = new Label(parent, SWT.SEPARATOR);
061: label = new CLabel(statusLine, SWT.SHADOW_NONE);
062:
063: if (widthHint < 0) {
064: GC gc = new GC(statusLine);
065: gc.setFont(statusLine.getFont());
066: FontMetrics fm = gc.getFontMetrics();
067: widthHint = fm.getAverageCharWidth() * charWidth;
068: heightHint = fm.getHeight();
069: gc.dispose();
070: }
071:
072: StatusLineLayoutData data = new StatusLineLayoutData();
073: data.widthHint = widthHint;
074: label.setLayoutData(data);
075: label.setText(text);
076:
077: data = new StatusLineLayoutData();
078: data.heightHint = heightHint;
079: sep.setLayoutData(data);
080:
081: }
082:
083: /**
084: * An accessor for the current location of this status line contribution
085: * item -- relative to the display.
086: *
087: * @return The current location of this status line; <code>null</code> if
088: * not yet initialized.
089: */
090: public Point getDisplayLocation() {
091: if ((label != null) && (statusLine != null)) {
092: return statusLine.toDisplay(label.getLocation());
093: }
094:
095: return null;
096: }
097:
098: public String getText() {
099: return text;
100: }
101:
102: public void setText(String text) {
103: if (text == null) {
104: throw new NullPointerException();
105: }
106:
107: this .text = text;
108:
109: if (label != null && !label.isDisposed()) {
110: label.setText(this .text);
111: }
112:
113: if (this .text.length() == 0) {
114: if (isVisible()) {
115: setVisible(false);
116: IContributionManager contributionManager = getParent();
117:
118: if (contributionManager != null) {
119: contributionManager.update(true);
120: }
121: }
122: } else {
123: if (!isVisible()) {
124: setVisible(true);
125: IContributionManager contributionManager = getParent();
126:
127: if (contributionManager != null) {
128: contributionManager.update(true);
129: }
130: }
131: }
132: }
133: }
|