1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    
7    import java.util.*;
8    import java.io.*;
9    import javax.swing.*;
10   
11   public abstract class FileDefaultable implements Cloneable,Comparable{
12   
13   //  private static final long serialVersionUID = Version.getSUID();
14   
15     public abstract void copy(FileDefaultable that);
16     public abstract Object clone();
17     public abstract boolean edit(JFrame owner);
18     public abstract String getName();
19   
20     public static HashMap defmap = new HashMap();
21     public static HashMap minmap = new HashMap();
22     public static HashMap filemap = new HashMap();
23     public static HashMap fixedmap = new HashMap();
24   
25   //  String name;
26   
27     public String toString() {
28       if (getName()!=null) return(getName());
29       return(super.toString());
30     }
31   
32     String toSuperString() {return(super.toString());}
33   
34     public static TreeSet getMin(Class type) {
35       return((TreeSet)((TreeSet)minmap.get(type)));
36     }
37   
38     public static TreeSet getDefs(Class type) {
39       return((TreeSet)((TreeSet)defmap.get(type)));
40     }
41   
42     public static TreeSet getFile(Class type) {
43       return((TreeSet)((TreeSet)filemap.get(type)));
44     }
45   
46     public static TreeSet getFixed(Class type) {
47       return((TreeSet)((TreeSet)fixedmap.get(type)));
48     }
49   
50     public static Object[] getArray(Class type,HashMap file) {
51       TreeSet res = (TreeSet)((TreeSet)defmap.get(type)).clone();
52       if (file.containsKey(type))
53         absorb(res,(TreeSet)file.get(type));
54       return(res.toArray());
55     }
56   
57     public boolean check() {
58       return(true);
59     }
60   
61     static HashMap curfile;
62   
63     public static Object[] getAll(Class type) {
64       return(getAll(type,curfile));
65     }
66   
67     public static Object[] getAll(Class type,HashMap file) {
68       TreeSet res;
69       if (fixedmap.containsKey(type)) {
70         res = (TreeSet)((TreeSet)fixedmap.get(type)).clone();
71         res.addAll((TreeSet)defmap.get(type));
72       }
73       else res = (TreeSet)((TreeSet)defmap.get(type)).clone();
74       if (file.containsKey(type))
75         absorb(res,(TreeSet)file.get(type));
76       return(res.toArray());
77     }
78   
79     public static FileDefaultable getExisting (Class type,String name) {
80       TreeSet source = getFile(type);
81       for (Iterator it = source.iterator();it.hasNext();) {
82         FileDefaultable item = (FileDefaultable)it.next();
83         if (item.getName().equals(name)) return(item);
84       }
85       source = getDefs(type);
86       for (Iterator it = source.iterator();it.hasNext();) {
87         FileDefaultable item = (FileDefaultable)it.next();
88         if (item.getName().equals(name)) return(item);
89       }
90       source = getFixed(type);
91       if (source!=null)
92         for (Iterator it = source.iterator();it.hasNext();) {
93           FileDefaultable item = (FileDefaultable)it.next();
94           if (item.getName().equals(name)) return(item);
95         }
96       return(null);
97     }
98   
99     public static void checkAll() {
100      for (Iterator it = defmap.keySet().iterator();it.hasNext();) {
101        Class key = (Class)it.next();
102        for (Iterator it2 = ((TreeSet)defmap.get(key)).iterator();it2.hasNext();)
103          if (!((FileDefaultable)it2.next()).check())
104            it2.remove();
105      }
106      if (curfile!=null)
107        for (Iterator it = curfile.keySet().iterator();it.hasNext();) {
108          Class key = (Class)it.next();
109          for (Iterator it2 = ((TreeSet)curfile.get(key)).iterator();it2.hasNext();)
110            if (!((FileDefaultable)it2.next()).check())
111              it2.remove();
112        }
113    }
114  
115    public FileDefaultable getGlobal(Class myclass) {
116      if (fixedmap.containsKey(myclass)) {
117        TreeSet fixed = (TreeSet)fixedmap.get(myclass);
118        if (fixed.contains(this))
119          return((FileDefaultable)fixed.tailSet(this).first());
120      }
121      if (!filemap.containsKey(myclass)) filemap.put(myclass,new TreeMap());
122      TreeSet file = (TreeSet)filemap.get(myclass);
123      if (file.contains(this)) return((FileDefaultable)file.tailSet(this).first());
124      file.add(this);
125      return(this);
126    }
127  
128    public static FileDefaultable lookup(Class type,String str) {
129      if (fixedmap.containsKey(type)) {
130        FileDefaultable res =  lookup((TreeSet)fixedmap.get(type),str);
131        if (res!=null) return(res);
132      }
133      return((FileDefaultable)lookup(((TreeSet)defmap.get(type)),str));
134    }
135  
136    public boolean equals(Object that) {
137      if (that==null) return(false);
138      return((that.getClass() == getClass())&&
139             (getName().compareTo(((FileDefaultable)that).getName())==0));
140    }
141  
142    public int compareTo(Object that) {
143      return(getName().compareTo(((FileDefaultable)that).getName()));
144    }
145  
146    public static void sortOut(HashMap newmap) {
147      for (Iterator it = newmap.keySet().iterator();it.hasNext();) {
148        Class key = (Class)it.next();
149        seperate(getDefs(key),(TreeSet)newmap.get(key),getFile(key));
150      }
151      curfile = newmap;
152      checkAll();
153    }
154  
155    public static void seperate(TreeSet lowdest,TreeSet highdest,TreeSet source) {
156      for (Iterator it=source.iterator();it.hasNext();) {
157        FileDefaultable ref = (FileDefaultable)it.next();
158        it.remove();
159        if (highdest.contains(ref)) {
160          highdest.remove(ref);
161          highdest.add(ref);
162        }
163        else if (lowdest.contains(ref)) {
164          lowdest.remove(ref);
165          lowdest.add(ref);
166        }
167        else highdest.add(ref);
168      }
169    }
170  
171    public static TreeSet cloneSet(TreeSet source) {
172      TreeSet res = new TreeSet();
173      for (Iterator it=source.iterator();it.hasNext();)
174        res.add(((FileDefaultable)it.next()).clone());
175      return(res);
176    }
177  
178    public static void absorb(TreeSet target,TreeSet source) {
179      for (Iterator it=source.iterator();it.hasNext();) {
180        FileDefaultable specific = (FileDefaultable)it.next();
181        if (target.contains(specific))
182          target.remove(specific);
183        target.add(specific);
184      }
185    }
186  
187    public static void add(TreeSet target,TreeSet source) {
188      for (Iterator it=source.iterator();it.hasNext();) {
189        FileDefaultable specific = (FileDefaultable)it.next();
190        if (!target.contains(specific))
191          target.add(specific);
192      }
193    }
194  
195    public static void mergeDelete(TreeSet target,TreeSet source) {
196      for (Iterator it=target.iterator();it.hasNext();) {
197        FileDefaultable can = (FileDefaultable)it.next();
198        if (source.contains(can)) {
199          can.copy((FileDefaultable)source.tailSet(can).first());
200          source.remove(can);
201        }
202        else it.remove();
203      }
204      for (Iterator it=source.iterator();it.hasNext();)
205        target.add(it.next());
206    }
207  
208    public static void mergeFile(TreeSet def,TreeSet file,TreeSet source) {
209      for (Iterator it=file.iterator();it.hasNext();) {
210        FileDefaultable mine = (FileDefaultable)it.next();
211        if (source.contains(mine)) {
212          mine.copy((FileDefaultable)source.tailSet(mine).first());
213          source.remove(mine);
214        }
215        else {
216          it.remove();
217          if (def.contains(mine)) {
218            mine.copy((FileDefaultable)def.tailSet(mine).first());
219            def.remove(mine);
220            def.add(mine);
221          }
222        }
223      }
224      for (Iterator it=source.iterator();it.hasNext();) {
225        FileDefaultable newsize = (FileDefaultable)it.next();
226        if (def.contains(newsize)) {
227          FileDefaultable old = (FileDefaultable)def.tailSet(newsize).first();
228          def.remove(old);
229          def.add(old.clone());
230          old.copy(newsize);
231          file.add(old);
232        }
233        else file.add(newsize);
234      }
235    }
236  
237    public static FileDefaultable lookup(TreeSet defaults,String str) {
238      for (Iterator it=defaults.iterator();it.hasNext();) {
239        FileDefaultable possible = (FileDefaultable)it.next();
240        if (str.compareTo(possible.getName())==0) return(possible);
241      }
242      return(null);
243    }
244  
245    public abstract Object readResolve() throws ObjectStreamException;
246  }
247