KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > variables > StatusLineContributionItem


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.debug.internal.ui.views.variables;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.CLabel;
16 import org.eclipse.swt.graphics.GC;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.widgets.Composite;
19
20 import org.eclipse.jface.action.ContributionItem;
21 import org.eclipse.jface.action.IContributionManager;
22 import org.eclipse.jface.action.StatusLineLayoutData;
23
24 public class StatusLineContributionItem extends ContributionItem {
25
26     public final static int DEFAULT_CHAR_WIDTH = 40;
27
28     private int charWidth;
29     private CLabel label;
30     /**
31      * The composite into which this contribution item has been placed. This
32      * will be <code>null</code> if this instance has not yet been
33      * initialized.
34      */

35     private Composite statusLine = null;
36     private String JavaDoc text= ""; //$NON-NLS-1$
37
private int widthHint = -1;
38
39     public StatusLineContributionItem(String JavaDoc id) {
40         this(id, DEFAULT_CHAR_WIDTH);
41     }
42
43     public StatusLineContributionItem(String JavaDoc id, int charWidth) {
44         super(id);
45         this.charWidth = charWidth;
46         setVisible(false); // no text to start with
47
}
48
49     public void fill(Composite parent) {
50         statusLine = parent;
51         label = new CLabel(statusLine, SWT.NONE);
52         StatusLineLayoutData statusLineLayoutData = new StatusLineLayoutData();
53
54         if (widthHint < 0) {
55             GC gc = new GC(statusLine);
56             gc.setFont(statusLine.getFont());
57             widthHint = gc.getFontMetrics().getAverageCharWidth() * charWidth;
58             gc.dispose();
59         }
60
61         statusLineLayoutData.widthHint = widthHint;
62         label.setLayoutData(statusLineLayoutData);
63         label.setText(text);
64     }
65
66     /**
67      * An accessor for the current location of this status line contribution
68      * item -- relative to the display.
69      *
70      * @return The current location of this status line; <code>null</code> if
71      * not yet initialized.
72      */

73     public Point getDisplayLocation() {
74         if ((label != null) && (statusLine != null)) {
75             return statusLine.toDisplay(label.getLocation());
76         }
77
78         return null;
79     }
80
81     public String JavaDoc getText() {
82         return text;
83     }
84
85     public void setText(String JavaDoc text) {
86         if (text == null)
87             throw new NullPointerException JavaDoc();
88
89         this.text = text;
90
91         if (label != null && !label.isDisposed())
92             label.setText(this.text);
93
94         if (this.text.length() == 0) {
95             if (isVisible()) {
96                 setVisible(false);
97                 IContributionManager contributionManager = getParent();
98
99                 if (contributionManager != null)
100                     contributionManager.update(true);
101             }
102         } else {
103             if (!isVisible()) {
104                 setVisible(true);
105                 IContributionManager contributionManager = getParent();
106
107                 if (contributionManager != null)
108                     contributionManager.update(true);
109             }
110         }
111     }
112 }
113
Popular Tags