KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > StatusBar


1 /*
2  * StatusBar.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves
5  * $Id: StatusBar.java,v 1.5 2004/09/16 18:32:37 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.FontMetrics JavaDoc;
28 import java.awt.Graphics JavaDoc;
29 import java.awt.Insets JavaDoc;
30 import java.awt.Toolkit JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.UIManager JavaDoc;
33 import javax.swing.border.Border JavaDoc;
34 import javax.swing.border.CompoundBorder JavaDoc;
35 import javax.swing.border.EmptyBorder JavaDoc;
36 import javax.swing.border.MatteBorder JavaDoc;
37
38 public final class StatusBar extends JComponent JavaDoc implements PreferencesChangeListener
39 {
40     private static final Font JavaDoc font = new Font JavaDoc("SansSerif", Font.PLAIN, 12);
41     private static final int LEFT_MARGIN = 2;
42     private static final int RIGHT_MARGIN = 2;
43
44     private static FontMetrics JavaDoc fm;
45     private static int displayContext = 1;
46
47     private final Frame frame;
48     private final Border JavaDoc border;
49     private final int charAscent;
50     private String JavaDoc messageText;
51
52     public StatusBar(Frame frame)
53     {
54         this.frame = frame;
55         Editor.preferences().addPreferencesChangeListener(this);
56         preferencesChanged();
57         if (fm == null)
58             fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
59         charAscent = fm.getAscent();
60         int charDescent = fm.getDescent();
61         Dimension JavaDoc dim = frame.getSize();
62         Insets JavaDoc insets = frame.getInsets();
63         dim.width -= (insets.left + insets.right);
64         border = new CompoundBorder JavaDoc(new MatteBorder JavaDoc(1, 0, 0, 0, Color.gray),
65                                     new EmptyBorder JavaDoc(2, 0, 2, 0));
66         setBorder(border);
67         insets = border.getBorderInsets(this);
68         dim.height = charAscent + charDescent + insets.top + insets.bottom;
69         setPreferredSize(dim);
70     }
71
72     public final void setText(String JavaDoc s)
73     {
74         messageText = s;
75     }
76
77     public final String JavaDoc getText()
78     {
79         return messageText;
80     }
81
82     private FastStringBuffer sb = new FastStringBuffer(48);
83
84     private String JavaDoc getStatusText(Editor editor)
85     {
86         final Buffer buffer = editor.getBuffer();
87         if (buffer == null)
88              return "";
89         sb.setLength(0);
90         String JavaDoc modeName = buffer.getMode().getDisplayName();
91         if (modeName != null)
92             sb.append(modeName);
93         String JavaDoc s = buffer.getStatusText(editor);
94         if (s != null && s.length() > 0) {
95             sb.append(" ");
96             sb.append(s);
97         }
98         return sb.toString();
99     }
100
101     public void paint(Graphics JavaDoc g)
102     {
103         Editor editor = frame.getCurrentEditor();
104         Buffer buffer = editor.getBuffer();
105         border.paintBorder(this, g, 0, 0, getWidth(), getHeight());
106         Insets JavaDoc insets = border.getBorderInsets(this);
107         int textAreaWidth = getWidth() - insets.left - insets.right;
108         int textAreaHeight = getHeight() - insets.top - insets.bottom;
109         g.setColor(UIManager.getColor("control"));
110         g.fillRect(insets.left, insets.top, textAreaWidth, textAreaHeight);
111         g.setColor(UIManager.getColor("controlText"));
112         g.setFont(font);
113         Display.setRenderingHints(g);
114         int x1 = insets.left + LEFT_MARGIN;
115         int y = insets.top + charAscent;
116         if (messageText == null && displayContext > 0) {
117             // We want the long context string if displayContext > 1.
118
messageText =
119                 buffer.getMode().getContextString(editor, displayContext > 1);
120         }
121         int x2 = textAreaWidth - RIGHT_MARGIN;
122         String JavaDoc statusText = getStatusText(editor);
123         if (statusText != null)
124             x2 -= fm.stringWidth(statusText);
125         if (messageText != null) {
126             g.setClip(x1, 0, x2 - x1 - 20, getHeight());
127             g.drawString(messageText, x1, y);
128         }
129         if (statusText != null) {
130             g.setClip(x2, 0, textAreaWidth - x2, getHeight());
131             g.drawString(statusText, x2, y);
132         }
133     }
134
135     public void repaintNow()
136     {
137         paintImmediately(0, 0, getWidth(), getHeight());
138     }
139
140     public void preferencesChanged()
141     {
142         displayContext =
143             Editor.preferences().getIntegerProperty(Property.STATUS_BAR_DISPLAY_CONTEXT);
144     }
145 }
146
Popular Tags