1    /*
2    Copyright 2001 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.event.*;
9    import java.awt.*;
10   import java.util.*;
11   
12   public class Prefs extends JDialog implements ActionListener,ListSelectionListener {
13   
14     public static String OK = "Ok";
15     public static String DISCARD = "Cancel";
16   
17     public static PrefEditor[] prefs = null;
18     JList list = null;
19     JScrollPane curpref = null;
20     Container pane;
21     public PrefEditor[] myprefs = null;
22   
23     static Class[] fileprefs = {PrefUnits.class,PrefSize.class,PrefLayer.class,PrefFont.class,PrefArrow.class};
24     JLabel lab = new JLabel();
25   
26     public Prefs(CartoFrame owner) {
27       super(owner,"Preferences and defaults",false);
28       myprefs = prefs;
29       init();
30     }
31   
32     public Prefs(CartoFrame owner,Carto target) {
33       super(owner,"Preferences for this file",false);
34   
35       if (target.prefs==null) target.prefs = new PrefEditor[0];
36       if (target.prefs.length<fileprefs.length) {
37         myprefs = new PrefEditor[fileprefs.length];
38         for (int i=0;i<target.prefs.length;i++)
39           myprefs[i] = target.prefs[i];
40         for (int i=target.prefs.length;i<fileprefs.length;i++) {
41           try {
42             Class[] argtypes = {Carto.class};
43             Object[] args = {target};
44             myprefs[i] = (PrefEditor)fileprefs[i].getConstructor(argtypes).newInstance(args);
45           } catch (Exception e) {
46             ErrorLog.exception(e,"Trying to generate a new file Preference of class "+fileprefs[i]);
47           }
48         }
49         target.prefs = myprefs;
50       } else
51         myprefs = target.prefs;
52   
53       init();
54     }
55   
56     void init() {
57       pane = getContentPane();
58       pane.setLayout(new BorderLayout());
59       list = new JList(myprefs);
60       list.addListSelectionListener(this);
61       pane.add(list,BorderLayout.WEST);
62       JPanel actions = new JPanel();
63       JButton button = new JButton(OK);
64       button.addActionListener(this);
65       actions.add(button);
66       button = new JButton(DISCARD);
67       button.addActionListener(this);
68       actions.add(button);
69       pane.add(actions,BorderLayout.SOUTH);
70       pane.add(lab,BorderLayout.NORTH);
71       list.setSelectedIndex(0);
72       Dimension size = null;
73       size = Toolkit.getDefaultToolkit().getScreenSize();
74       if (size==null) size = new Dimension(600,630);
75       else {
76         size = new Dimension(size.width*3/4,size.height*3/4);
77         if (size.width > 600) size = new Dimension(600,size.height);
78         if (size.height > 630) size = new Dimension(size.width,630);
79       }
80       setSize(size);
81     }
82   
83     public void valueChanged(ListSelectionEvent e) {
84       if (curpref!=null) pane.remove(curpref);
85       int index = list.getSelectedIndex();
86       curpref = myprefs[index].getPane((CartoFrame)getOwner());
87       myprefs[index].dialog = this;
88       pane.add(curpref,BorderLayout.CENTER);
89       lab.setText(myprefs[index].name);
90       repaint();
91     }
92   
93     public void actionPerformed(ActionEvent e) {
94       if (e.getSource() instanceof AbstractButton) {
95         String command = ((AbstractButton)e.getSource()).getActionCommand();
96         if (command==DISCARD) {
97           for (int i=0;i<myprefs.length;i++) {
98             myprefs[i].abandon();
99             myprefs[i].pane = null;
100          }
101                 dispose();
102        }
103        else if (command==OK) {
104          for (int i=0;i<myprefs.length;i++) {
105            myprefs[i].save();
106            myprefs[i].pane = null;
107          }
108          Persist.save();
109          dispose();
110          ((CartoFrame)getOwner()).dirty = true;
111        }
112      }
113    }
114  }
115