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 javax.swing.event.*;
8    import java.awt.geom.*;
9    import java.awt.*;
10   import java.util.Set;
11   import java.awt.event.*;
12   
13   public class LinePicker extends javax.swing.Box implements ListSelectionListener,ActionListener {
14   
15     static String COLOR = "Color";
16     JButton sidebutton;
17     JButton colorbutton;
18   
19   //  GeneralPath line;
20   
21     LineType current;
22     Size csize;
23     Color color;
24   
25     JList list;
26     JList sizelist;
27   
28     public Styleable curve = null;
29     public Set sub = null;
30   
31     public LinePicker(Curve curve,Set sub) {
32       super(javax.swing.BoxLayout.Y_AXIS);
33       this.curve = curve;
34       this.sub = sub;
35       
36       int max = curve.points.size()-1;
37       if (curve.closed) max++;
38   
39       for (int i=0;i<max;i++)
40         if (sub.contains(curve.points.elementAt(i)) &&
41             sub.contains(curve.points.elementAt((i+1)%curve.points.size()))) {
42           CurveAtt att = (CurveAtt)curve.atribs.elementAt(i);
43           init(att.type,att.thickness,att.color);
44           return;
45         }
46   
47         init(curve.wholeatt.type,curve.wholeatt.thickness,curve.wholeatt.color);
48     }
49   
50     public LinePicker(LineType current,Size csize,Color color) {
51       super(javax.swing.BoxLayout.Y_AXIS);
52       init(current,csize,color);
53     }
54   
55   //  public CartoFrame owner = null;
56   
57   //  static public JDialog win = null;
58   //  static public LinePicker global = null;
59   
60     public void display(CartoFrame owner) {
61       curve = owner;
62       setSize(new Dimension(300,400));
63   //    if (win==null) {
64         JDialog win = new JDialog(owner);
65         win.getContentPane().add(this);
66         win.setSize(new Dimension(300,400));
67         win.show();
68   /*
69       }
70       else {
71         win.getContentPane().remove(0);
72         win.getContentPane().add(this);
73         invalidate();
74       }
75       global = this;
76   */
77     }
78   
79     public void init(LineType current,Size csize,Color color) {
80   
81       this.current = current;
82       this.color = color;
83       this.csize = csize;
84   
85       list = new JList(LineType.predefined.toArray());
86       list.setCellRenderer(new LineCellRenderer());
87       list.setSelectedValue(current,true);
88       list.addListSelectionListener(this);
89   
90       JScrollPane listpane = new JScrollPane();
91       listpane.setViewportView(list);
92       add(listpane);
93   
94       sizelist = new JList(FileDefaultable.getAll(Size.class));
95       sizelist.setSelectedValue(csize,true);
96       sizelist.addListSelectionListener(this);
97   
98       listpane = new JScrollPane();
99       listpane.setViewportView(sizelist);
100      add(listpane);
101  
102      JButton button = new JButton(COLOR);
103      button.addActionListener(this);
104      add(button);
105  
106      setPreferredSize(new Dimension(300,400));
107    }
108  
109    public void actionPerformed(ActionEvent e) {
110      if (e.getActionCommand()==COLOR) {
111        color = JColorChooser.showDialog(this,"Line color",color);
112        ((JButton)e.getSource()).setBackground(color);
113        change();
114        repaint();
115      }
116    }
117  
118    public void valueChanged(ListSelectionEvent e) {
119      change();
120    }
121  
122    void change() {
123      if (curve instanceof CartoFrame)
124        curve.setLineType((LineType)list.getSelectedValue(),
125          		(Size)sizelist.getSelectedValue(),
126          		color,sub);
127    }
128  
129    public void apply() {
130      if (curve!=null) {
131        curve.setLineType((LineType)list.getSelectedValue(),
132          		(Size)sizelist.getSelectedValue(),
133          		color,
134          		sub);
135      }
136    }
137  }
138