KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > util > StatusLineContributionItem


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
12 package org.eclipse.ui.internal.util;
13
14 import org.eclipse.jface.action.ContributionItem;
15 import org.eclipse.jface.action.IContributionManager;
16 import org.eclipse.jface.action.StatusLineLayoutData;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.CLabel;
19 import org.eclipse.swt.graphics.FontMetrics;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Label;
24
25 /**
26  * @issue needs Javadoc
27  */

28 public class StatusLineContributionItem extends ContributionItem {
29
30     public final static int DEFAULT_CHAR_WIDTH = 40;
31
32     private int charWidth;
33
34     private CLabel label;
35
36     /**
37      * The composite into which this contribution item has been placed. This
38      * will be <code>null</code> if this instance has not yet been
39      * initialized.
40      */

41     private Composite statusLine = null;
42
43     private String JavaDoc text = Util.ZERO_LENGTH_STRING;
44
45     private int widthHint = -1;
46
47     private int heightHint = -1;
48
49     public StatusLineContributionItem(String JavaDoc id) {
50         this(id, DEFAULT_CHAR_WIDTH);
51     }
52
53     public StatusLineContributionItem(String JavaDoc id, int charWidth) {
54         super(id);
55         this.charWidth = charWidth;
56         setVisible(false); // no text to start with
57
}
58
59     public void fill(Composite parent) {
60         statusLine = parent;
61
62         Label sep = new Label(parent, SWT.SEPARATOR);
63         label = new CLabel(statusLine, SWT.SHADOW_NONE);
64
65         if (widthHint < 0) {
66             GC gc = new GC(statusLine);
67             gc.setFont(statusLine.getFont());
68             FontMetrics fm = gc.getFontMetrics();
69             widthHint = fm.getAverageCharWidth() * charWidth;
70             heightHint = fm.getHeight();
71             gc.dispose();
72         }
73
74         StatusLineLayoutData data = new StatusLineLayoutData();
75         data.widthHint = widthHint;
76         label.setLayoutData(data);
77         label.setText(text);
78
79         data = new StatusLineLayoutData();
80         data.heightHint = heightHint;
81         sep.setLayoutData(data);
82
83     }
84
85     /**
86      * An accessor for the current location of this status line contribution
87      * item -- relative to the display.
88      *
89      * @return The current location of this status line; <code>null</code> if
90      * not yet initialized.
91      */

92     public Point getDisplayLocation() {
93         if ((label != null) && (statusLine != null)) {
94             return statusLine.toDisplay(label.getLocation());
95         }
96
97         return null;
98     }
99
100     public String JavaDoc getText() {
101         return text;
102     }
103
104     public void setText(String JavaDoc text) {
105         if (text == null) {
106             throw new NullPointerException JavaDoc();
107         }
108
109         this.text = text;
110
111         if (label != null && !label.isDisposed()) {
112             label.setText(this.text);
113         }
114
115         if (this.text.length() == 0) {
116             if (isVisible()) {
117                 setVisible(false);
118                 IContributionManager contributionManager = getParent();
119
120                 if (contributionManager != null) {
121                     contributionManager.update(true);
122                 }
123             }
124         } else {
125             if (!isVisible()) {
126                 setVisible(true);
127                 IContributionManager contributionManager = getParent();
128
129                 if (contributionManager != null) {
130                     contributionManager.update(true);
131                 }
132             }
133         }
134     }
135 }
136
Popular Tags