01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.mlm.debug.ui;
28:
29: import java.awt.BorderLayout;
30: import java.awt.Dimension;
31: import java.awt.event.ComponentAdapter;
32: import java.awt.event.ComponentEvent;
33:
34: import javax.swing.BoundedRangeModel;
35: import javax.swing.JPanel;
36: import javax.swing.JScrollBar;
37: import javax.swing.JTextField;
38: import javax.swing.SwingConstants;
39:
40: /** A single line of text with a horizontal scroll bar.
41: From Ray Tomlinson.
42: */
43:
44: public class ScrollingTextLine extends JPanel {
45: MyTextField tf = new MyTextField();
46: JScrollBar sb = new JScrollBar(SwingConstants.HORIZONTAL);
47:
48: public ScrollingTextLine(int ncolumns) {
49: super (new BorderLayout());
50: tf.setColumns(ncolumns);
51: final BoundedRangeModel rm = tf.getHorizontalVisibility();
52: // make text field's visibility model be the scrollbar's model
53: sb.setModel(rm);
54: sb.updateUI(); // JScrollBar fails to update its UI when its model changes
55: sb.setUnitIncrement(16);// Move 16 pixels at a time.
56: add(tf, BorderLayout.CENTER);
57: add(sb, BorderLayout.SOUTH);
58: tf.setEditable(false);
59: tf.addComponentListener(new ComponentAdapter() {
60: public void componentResized(ComponentEvent e) {
61: sb.setBlockIncrement(tf.getSize().width - 16);
62: }
63: });
64: }
65:
66: public void setText(String s) {
67: tf.setText(s);
68: tf.setScrollOffset(0);
69: }
70:
71: }
72:
73: /** A text field that reports its preferred size as the width
74: of its contents, or if it's empty, the width of the number of columns
75: with which it was constructed.
76: */
77:
78: class MyTextField extends JTextField {
79:
80: public MyTextField() {
81: super ();
82: }
83:
84: public MyTextField(int ncolumns) {
85: super (ncolumns);
86: }
87:
88: public Dimension getPreferredSize() {
89: Dimension d = super .getPreferredSize();
90: if (getText().length() == 0)
91: return new Dimension(getColumns() * getColumnWidth(),
92: d.height);
93: else
94: return new Dimension(getFontMetrics(getFont()).stringWidth(
95: getText()), d.height);
96: }
97:
98: }
|