001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.texteditor;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.custom.CLabel;
014: import org.eclipse.swt.events.DisposeEvent;
015: import org.eclipse.swt.events.DisposeListener;
016: import org.eclipse.swt.events.MouseAdapter;
017: import org.eclipse.swt.events.MouseEvent;
018: import org.eclipse.swt.events.MouseListener;
019: import org.eclipse.swt.graphics.GC;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Display;
023: import org.eclipse.swt.widgets.Label;
024:
025: import org.eclipse.jface.action.ContributionItem;
026: import org.eclipse.jface.action.IAction;
027: import org.eclipse.jface.action.StatusLineLayoutData;
028: import org.eclipse.jface.resource.JFaceColors;
029:
030: /**
031: * Contribution item for the status line.
032: * @since 2.0
033: */
034: public class StatusLineContributionItem extends ContributionItem
035: implements IStatusField, IStatusFieldExtension {
036:
037: /**
038: * Internal mouse listener to track double clicking the status line item.
039: * @since 3.0
040: */
041: private class Listener extends MouseAdapter {
042: /*
043: * @see org.eclipse.swt.events.MouseAdapter#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
044: */
045: public void mouseDoubleClick(MouseEvent e) {
046: if (fActionHandler != null && fActionHandler.isEnabled())
047: fActionHandler.run();
048: }
049: }
050:
051: /**
052: * Left and right margin used in CLabel.
053: * @since 2.1
054: */
055: private static final int INDENT = 3;
056: /**
057: * Default number of characters that should fit into the item.
058: * @since 3.0
059: */
060: static final int DEFAULT_WIDTH_IN_CHARS = 14;
061: /**
062: * Pre-computed label width hint.
063: * @since 2.1
064: */
065: private int fFixedWidth = -1;
066: /**
067: * Pre-computed label height hint.
068: * @since 3.0
069: */
070: private int fFixedHeight = -1;
071: /** The text */
072: private String fText;
073: /** The image */
074: private Image fImage;
075: /**
076: * The error text.
077: * @since 3.0
078: */
079: private String fErrorText;
080: /**
081: * The error image.
082: * @since 3.0
083: */
084: private Image fErrorImage;
085: /**
086: * The tool tip text.
087: * @since 3.0
088: */
089: private String fToolTipText;
090: /**
091: * Number of characters that should fit into the item.
092: * @since 3.0
093: */
094: private int fWidthInChars;
095: /** The status line label widget */
096: private CLabel fLabel;
097: /**
098: * The action handler.
099: * @since 3.0
100: */
101: private IAction fActionHandler;
102: /**
103: * The mouse listener
104: * @since 3.0
105: */
106: private MouseListener fMouseListener;
107:
108: /**
109: * Creates a new item with the given id.
110: *
111: * @param id the item's id
112: */
113: public StatusLineContributionItem(String id) {
114: this (id, true, DEFAULT_WIDTH_IN_CHARS);
115: }
116:
117: /**
118: * Creates a new item with the given attributes.
119: *
120: * @param id the item's id
121: * @param visible the visibility of this item
122: * @param widthInChars the width in characters
123: * @since 3.0
124: */
125: public StatusLineContributionItem(String id, boolean visible,
126: int widthInChars) {
127: super (id);
128: setVisible(visible);
129: fWidthInChars = widthInChars;
130: }
131:
132: /*
133: * @see IStatusField#setText(String)
134: */
135: public void setText(String text) {
136: fText = text;
137: updateMessageLabel();
138: }
139:
140: /*
141: * @see IStatusField#setImage(Image)
142: */
143: public void setImage(Image image) {
144: fImage = image;
145: updateMessageLabel();
146: }
147:
148: /*
149: * @see org.eclipse.ui.texteditor.IStatusFieldExtension#setErrorText(java.lang.String)
150: * @since 3.0
151: */
152: public void setErrorText(String text) {
153: fErrorText = text;
154: updateMessageLabel();
155: }
156:
157: /*
158: * @see org.eclipse.ui.texteditor.IStatusFieldExtension#setErrorImage(org.eclipse.swt.graphics.Image)
159: * @since 3.0
160: */
161: public void setErrorImage(Image image) {
162: fErrorImage = image;
163: updateMessageLabel();
164: }
165:
166: /*
167: * @see org.eclipse.ui.texteditor.IStatusFieldExtension#setToolTipText(java.lang.String)
168: * @since 3.0
169: */
170: public void setToolTipText(String string) {
171: fToolTipText = string;
172: updateMessageLabel();
173: }
174:
175: /*
176: * @see IContributionItem#fill(Composite)
177: */
178: public void fill(Composite parent) {
179:
180: Label sep = new Label(parent, SWT.SEPARATOR);
181: fLabel = new CLabel(parent, SWT.SHADOW_NONE);
182:
183: fLabel.addDisposeListener(new DisposeListener() {
184: public void widgetDisposed(DisposeEvent e) {
185: fMouseListener = null;
186: }
187: });
188: if (fActionHandler != null) {
189: fMouseListener = new Listener();
190: fLabel.addMouseListener(fMouseListener);
191: }
192:
193: StatusLineLayoutData data = new StatusLineLayoutData();
194: data.widthHint = getWidthHint(parent);
195: fLabel.setLayoutData(data);
196:
197: data = new StatusLineLayoutData();
198: data.heightHint = getHeightHint(parent);
199: sep.setLayoutData(data);
200:
201: updateMessageLabel();
202: }
203:
204: public void setActionHandler(IAction actionHandler) {
205: if (fActionHandler != null && actionHandler == null
206: && fMouseListener != null) {
207: if (!fLabel.isDisposed())
208: fLabel.removeMouseListener(fMouseListener);
209: fMouseListener = null;
210: }
211:
212: fActionHandler = actionHandler;
213:
214: if (fLabel != null && !fLabel.isDisposed()
215: && fMouseListener == null && fActionHandler != null) {
216: fMouseListener = new Listener();
217: fLabel.addMouseListener(fMouseListener);
218: }
219: }
220:
221: /**
222: * Returns the width hint for this label.
223: *
224: * @param control the root control of this label
225: * @return the width hint for this label
226: * @since 2.1
227: */
228: private int getWidthHint(Composite control) {
229: if (fFixedWidth < 0) {
230: GC gc = new GC(control);
231: gc.setFont(control.getFont());
232: fFixedWidth = gc.getFontMetrics().getAverageCharWidth()
233: * fWidthInChars;
234: fFixedWidth += INDENT * 2;
235: gc.dispose();
236: }
237: return fFixedWidth;
238: }
239:
240: /**
241: * Returns the height hint for this label.
242: *
243: * @param control the root control of this label
244: * @return the height hint for this label
245: * @since 3.0
246: */
247: private int getHeightHint(Composite control) {
248: if (fFixedHeight < 0) {
249: GC gc = new GC(control);
250: gc.setFont(control.getFont());
251: fFixedHeight = gc.getFontMetrics().getHeight();
252: gc.dispose();
253: }
254: return fFixedHeight;
255: }
256:
257: /**
258: * Updates the message label widget.
259: *
260: * @since 3.0
261: */
262: private void updateMessageLabel() {
263: if (fLabel != null && !fLabel.isDisposed()) {
264: Display display = fLabel.getDisplay();
265: if ((fErrorText != null && fErrorText.length() > 0)
266: || fErrorImage != null) {
267: fLabel.setForeground(JFaceColors.getErrorText(display));
268: fLabel.setText(fErrorText);
269: fLabel.setImage(fErrorImage);
270: if (fToolTipText != null)
271: fLabel.setToolTipText(fToolTipText);
272: else if (fErrorText.length() > fWidthInChars)
273: fLabel.setToolTipText(fErrorText);
274: else
275: fLabel.setToolTipText(null);
276: } else {
277: fLabel.setForeground(display
278: .getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
279: fLabel.setText(fText);
280: fLabel.setImage(fImage);
281: if (fToolTipText != null)
282: fLabel.setToolTipText(fToolTipText);
283: else if (fText != null
284: && fText.length() > fWidthInChars)
285: fLabel.setToolTipText(fText);
286: else
287: fLabel.setToolTipText(null);
288: }
289: }
290: }
291: }
|