1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import javax.swing.*;
7    import java.awt.*;
8    
9    class LineCellRenderer extends JComponent implements ListCellRenderer {
10   
11     public double samplesize = 2;
12   
13     JLabel namelabel = new JLabel("         ");
14     JComponent sample = null;
15     
16     LineType type = null;
17   
18     public LineCellRenderer() {
19       setLayout(new GridLayout(1,2));
20       add(namelabel);
21       sample = new JComponent(){
22           public void paint(Graphics g) {
23             SavableStroke samplestroke = null;
24             if (type!=null&&(samplestroke=type.getStroke(samplesize))!=null) {
25               Dimension size = getSize();
26               ((Graphics2D)g).setStroke(samplestroke);
27               g.drawLine(0,size.height/2,size.width,size.height/2);
28             }
29           }
30         };
31       add(sample);
32     }
33   
34     public void paintComponent(Graphics g) {
35       Dimension size = sample.getSize();
36       g.setColor(getBackground());
37       g.fillRect(0,0,this.getSize().width,size.height);
38       g.setColor(getForeground());
39     }
40   
41     public Component getListCellRendererComponent(
42       JList list,
43       Object value,            // value to display
44       int index,               // cell index
45       boolean isSelected,      // is the cell selected
46       boolean cellHasFocus)    // the list and the cell have the focus 
47       {
48         type = (LineType)value;
49   
50         namelabel.setText(type.name);
51   
52         if (isSelected) {        
53           setBackground(Color.black); 
54           sample.setForeground(Color.white); 
55           namelabel.setForeground(Color.white); 
56         } else {
57           sample.setForeground(Color.black);
58           setBackground(Color.white);
59           namelabel.setForeground(Color.black);
60         }
61         setEnabled(list.isEnabled()); 
62         setFont(list.getFont());
63   
64         setSize(200,20);
65         return this;
66       }
67   }
68