KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > list > ListDisplayComponent


1 package text.list;
2
3 import text.*;
4
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.util.*;
8 import javax.swing.*;
9 import javax.swing.event.*;
10 import java.awt.font.*;
11
12 import rero.config.*;
13
14 public class ListDisplayComponent extends JComponent implements ChangeListener, ClientStateListener
15 {
16    protected ListData data;
17
18    public ListDisplayComponent()
19    {
20       setOpaque(false);
21       setDoubleBuffered(false);
22
23       ClientState.getClientState().addClientStateListener("listbox.width", this);
24
25       propertyChanged(null, null);
26    }
27
28    public void installDataSource(ListData _data)
29    {
30        data = _data;
31    }
32
33    protected String JavaDoc test_string = "";
34    private static final String JavaDoc widthstring = "1234567890123456789012345678901234567890123456789012345678901234567890";
35      
36    public void propertyChanged(String JavaDoc a, String JavaDoc b)
37    {
38       test_string = widthstring.substring(0, ClientState.getClientState().getInteger("listbox.width", ClientDefaults.listbox_width));
39       revalidate();
40    }
41
42    public void stateChanged(ChangeEvent e)
43    {
44        repaint();
45    }
46
47    public Dimension getPreferredSize()
48    {
49        FontMetrics fm = TextSource.fontMetrics;
50        return new Dimension(fm.stringWidth(test_string) + TextSource.UNIVERSAL_TWEAK, 0);
51    }
52
53
54    public void paint (Graphics g)
55    {
56       TextSource.initGraphics(g);
57
58       data.setExtent(getHeight() / (TextSource.fontMetrics.getHeight() + 2));
59
60       int checkY = (g.getClipBounds()).y - 10; // reverse these if
61
int checkH = checkY+(g.getClipBounds()).height + 20; // painting fucks up
62

63       int width = super.getWidth();
64       int height = super.getHeight();
65
66       g.setFont(TextSource.clientFont);
67
68       int baseline = TextSource.fontMetrics.getHeight() - 2; // starting at the top for this one...
69

70       synchronized (data.getSynchronizationKeyOuter())
71       {
72       synchronized (data.getSynchronizationKeyInner())
73       {
74       ListElement head = data.head(); // iteration is built into the ListData class, not my favorite pattern
75

76       while (head != null && baseline < height)
77       {
78          if (baseline <= checkH && baseline >= checkY)
79          {
80             if (head.isSelected())
81             {
82                g.setColor(UIManager.getColor("TextField.selectionBackground")); // setup the appropriate color...
83

84                int txtheight = TextSource.fontMetrics.getHeight();
85                g.fillRect(0, baseline - txtheight + 2, width, txtheight + 2); // the +2 may become a +4
86
}
87             TextSource.drawText(g, head.getAttributedText(), TextSource.UNIVERSAL_TWEAK, baseline);
88          }
89
90          baseline += (TextSource.fontMetrics.getHeight() + 2);
91
92          head = data.next();
93       } // end while loop... I do need to reformat this...
94

95       } // end key inner
96
} // end key outer
97
}
98 }
99
Popular Tags