001: /*
002: * StatusBar.java
003: *
004: * Copyright (C) 1998-2004 Peter Graves
005: * $Id: StatusBar.java,v 1.5 2004/09/16 18:32:37 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.Font;
027: import java.awt.FontMetrics;
028: import java.awt.Graphics;
029: import java.awt.Insets;
030: import java.awt.Toolkit;
031: import javax.swing.JComponent;
032: import javax.swing.UIManager;
033: import javax.swing.border.Border;
034: import javax.swing.border.CompoundBorder;
035: import javax.swing.border.EmptyBorder;
036: import javax.swing.border.MatteBorder;
037:
038: public final class StatusBar extends JComponent implements
039: PreferencesChangeListener {
040: private static final Font font = new Font("SansSerif", Font.PLAIN,
041: 12);
042: private static final int LEFT_MARGIN = 2;
043: private static final int RIGHT_MARGIN = 2;
044:
045: private static FontMetrics fm;
046: private static int displayContext = 1;
047:
048: private final Frame frame;
049: private final Border border;
050: private final int charAscent;
051: private String messageText;
052:
053: public StatusBar(Frame frame) {
054: this .frame = frame;
055: Editor.preferences().addPreferencesChangeListener(this );
056: preferencesChanged();
057: if (fm == null)
058: fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
059: charAscent = fm.getAscent();
060: int charDescent = fm.getDescent();
061: Dimension dim = frame.getSize();
062: Insets insets = frame.getInsets();
063: dim.width -= (insets.left + insets.right);
064: border = new CompoundBorder(new MatteBorder(1, 0, 0, 0,
065: Color.gray), new EmptyBorder(2, 0, 2, 0));
066: setBorder(border);
067: insets = border.getBorderInsets(this );
068: dim.height = charAscent + charDescent + insets.top
069: + insets.bottom;
070: setPreferredSize(dim);
071: }
072:
073: public final void setText(String s) {
074: messageText = s;
075: }
076:
077: public final String getText() {
078: return messageText;
079: }
080:
081: private FastStringBuffer sb = new FastStringBuffer(48);
082:
083: private String getStatusText(Editor editor) {
084: final Buffer buffer = editor.getBuffer();
085: if (buffer == null)
086: return "";
087: sb.setLength(0);
088: String modeName = buffer.getMode().getDisplayName();
089: if (modeName != null)
090: sb.append(modeName);
091: String s = buffer.getStatusText(editor);
092: if (s != null && s.length() > 0) {
093: sb.append(" ");
094: sb.append(s);
095: }
096: return sb.toString();
097: }
098:
099: public void paint(Graphics g) {
100: Editor editor = frame.getCurrentEditor();
101: Buffer buffer = editor.getBuffer();
102: border.paintBorder(this , g, 0, 0, getWidth(), getHeight());
103: Insets insets = border.getBorderInsets(this );
104: int textAreaWidth = getWidth() - insets.left - insets.right;
105: int textAreaHeight = getHeight() - insets.top - insets.bottom;
106: g.setColor(UIManager.getColor("control"));
107: g.fillRect(insets.left, insets.top, textAreaWidth,
108: textAreaHeight);
109: g.setColor(UIManager.getColor("controlText"));
110: g.setFont(font);
111: Display.setRenderingHints(g);
112: int x1 = insets.left + LEFT_MARGIN;
113: int y = insets.top + charAscent;
114: if (messageText == null && displayContext > 0) {
115: // We want the long context string if displayContext > 1.
116: messageText = buffer.getMode().getContextString(editor,
117: displayContext > 1);
118: }
119: int x2 = textAreaWidth - RIGHT_MARGIN;
120: String statusText = getStatusText(editor);
121: if (statusText != null)
122: x2 -= fm.stringWidth(statusText);
123: if (messageText != null) {
124: g.setClip(x1, 0, x2 - x1 - 20, getHeight());
125: g.drawString(messageText, x1, y);
126: }
127: if (statusText != null) {
128: g.setClip(x2, 0, textAreaWidth - x2, getHeight());
129: g.drawString(statusText, x2, y);
130: }
131: }
132:
133: public void repaintNow() {
134: paintImmediately(0, 0, getWidth(), getHeight());
135: }
136:
137: public void preferencesChanged() {
138: displayContext = Editor.preferences().getIntegerProperty(
139: Property.STATUS_BAR_DISPLAY_CONTEXT);
140: }
141: }
|