1    import javax.swing.*;
2    import java.awt.event.*;
3    import java.awt.*;
4    import java.util.*;
5    
6    public abstract class PrefDef extends PrefEditor implements java.io.Serializable,ActionListener {
7    
8      private static final long serialVersionUID = Version.getSUID();
9    
10     public TreeSet mine = new TreeSet();
11   
12     static String EDIT = "Edit";
13     static String NEW = "New";
14     static String DELETE = "Delete";
15   
16     transient TreeSet curdefs = null;
17     transient JList deflist = null;
18     transient JPanel defpanel = null;
19   
20     Carto deftarget;
21   
22     String defname;
23   
24     public PrefDef() {
25       name = "Default ";
26       mine = (TreeSet)FileDefaultable.getDefs(getDefClass()).clone();
27       deftarget=null;
28     }
29   
30     public PrefDef(Carto deftarget) {
31       this.deftarget = deftarget;
32   
33       if (!deftarget.defaults.containsKey(getDefClass())) {
34         deftarget.defaults.put(getDefClass(),new TreeSet());
35       }
36   
37       mine = (TreeSet)deftarget.getDefs(getDefClass()).clone();
38     }
39   
40     public abstract Class getDefClass();
41   
42     public void newPane() {
43       pane.setLayout(new GridLayout(8,1));
44       
45       if (deftarget==null) mine = FileDefaultable.getDefs(getDefClass());
46       else mine = deftarget.getDefs(getDefClass());
47       curdefs = FileDefaultable.cloneSet(mine);
48       defpanel = new JPanel();
49       deflist = new JList(curdefs.toArray());
50       defpanel.add(deflist);
51       pane.add(defpanel);
52   
53       JPanel actionpane = new JPanel();
54   //    actionpane.setLayout(new GridLayout(2,2));
55   
56       JButton displaybutton = new JButton(NEW);
57       displaybutton.addActionListener(this);
58       actionpane.add(displaybutton);
59   
60       displaybutton = new JButton(EDIT);
61       displaybutton.addActionListener(this);
62       actionpane.add(displaybutton);
63   
64       displaybutton = new JButton(DELETE);
65       displaybutton.addActionListener(this);
66       actionpane.add(displaybutton);
67   
68       pane.add(actionpane);
69     }
70   
71     public void save() {
72       if (pane==null) return;
73       mine = curdefs;
74       apply();
75       curdefs=null;//Size.cloneSet(sizes);
76       pane = null;
77       if (owner!=null) owner.repaint();
78     }
79   
80     public void apply() {
81       if (deftarget==null) {
82         FileDefaultable.add(mine,FileDefaultable.getMin(getDefClass()));  //Don't delete minimal set
83         FileDefaultable.mergeDelete(FileDefaultable.getDefs(getDefClass()),
84           			  mine);  //Override or add new ones
85         mine = FileDefaultable.getDefs(getDefClass());                              //Copy the set for future
86       } else {
87         FileDefaultable.mergeFile(FileDefaultable.getDefs(getDefClass()),
88           			deftarget.getDefs(getDefClass()),
89           			mine);
90         mine = deftarget.getDefs(getDefClass());
91       }
92       FileDefaultable.checkAll();
93     }
94   
95     public void actionPerformed(ActionEvent e) {
96       if (e.getActionCommand()==EDIT) {
97         FileDefaultable ed = (FileDefaultable)deflist.getSelectedValue();
98         if (ed!=null && ed.edit(owner)){        
99           updateList();
100        }
101      }
102      else if (e.getActionCommand()==NEW) {
103        FileDefaultable newdef = null;
104  
105        JList list = new JList(deftarget==null ?
106          		     owner.carto.getDefs(getDefClass()).toArray() :
107          		     FileDefaultable.getDefs(getDefClass()).toArray());
108        JPanel pane = new JPanel();
109        String verb = (deftarget==null?"make a default":"override");
110  
111        boolean haslist = list.getModel().getSize()>0;
112        if (haslist) {
113          pane.add(new JLabel("Choose a "+defname+" to "+verb));
114          pane.add(list);
115        }
116        pane.add(new JLabel((haslist?" or e":"E")+"nter name of new "+defname));
117  
118        JTextField newnamefield = new JTextField("");
119        newnamefield.setColumns(10);
120        pane.add(newnamefield);
121        if (JOptionPane.showConfirmDialog(owner,pane,"new "+defname,JOptionPane.OK_CANCEL_OPTION) ==
122            JOptionPane.OK_OPTION) {
123          if (newnamefield.getText().compareTo("")!=0)
124            newdef = makeDef(owner,newnamefield.getText());
125          else {
126            if (haslist) newdef = (FileDefaultable)((FileDefaultable)list.getSelectedValue()).clone();  
127          }
128        }
129        if (newdef!=null) {
130          curdefs.add(newdef);
131          newdef.edit(owner);
132          updateList();
133        }
134      }
135      else if (e.getActionCommand()==DELETE) {
136        FileDefaultable olddef = (FileDefaultable)deflist.getSelectedValue();
137        if (olddef!=null&&
138            JOptionPane.YES_OPTION ==
139            JOptionPane.showConfirmDialog(owner,"Really Delete "+defname+" "+olddef+"?",
140          				"Delete "+defname,
141          				JOptionPane.YES_NO_OPTION)) {
142          curdefs.remove(olddef);
143          updateList();
144        }
145      }
146    }
147  
148    public FileDefaultable makeDef(JFrame owner,String newname) {
149      try {
150        Class[] arglist = {String.class};
151        Object[] arg = {newname};
152        return((FileDefaultable)getDefClass().getConstructor(arglist).newInstance(arg));
153      } catch (Exception ex) {ErrorLog.exception(ex,"Carto internal error\ncreating default");}
154      return(null);
155    }
156  
157    void updateList() {
158      defpanel.remove(deflist);
159      if (curdefs!=null) {
160        deflist = new JList(curdefs.toArray());
161        defpanel.add(deflist);
162      }
163      if (dialog!=null) dialog.validate();
164    }
165  
166  
167  }
168  
169