1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import java.awt.*;
7    import java.awt.geom.*;
8    import java.util.*;
9    import java.awt.event.*;
10   import javax.swing.*;
11   import java.text.*;
12   
13   public class Symbol implements java.io.Serializable,Element {
14   
15     private static final long serialVersionUID = Version.getSUID();
16   
17     public static boolean makenames = false;
18   
19   //  static int BOXSIZE = 5;
20   
21     /**The name of the Symbol<br>
22      *All symbols have a name, which may be assignesd automatically, or
23      *set by the user.
24      */
25     String name = null;
26   
27   //  public ViewTransform position = new ViewTransform(ViewTransform.PREMULTIPLY);
28   
29   //  public ViewTransform defaultPosition() {
30   //    return(new ViewTransform(0,0));
31   //  }
32    
33     public static Symbol getNew(Class type,Point2D pos,View view,Object arg) {
34       Class[] newargtypes = {Point2D.class,View.class,Object.class};
35       Object[] newargs = {pos,view,arg};
36       Symbol newsym;
37       try {
38   //      System.out.println("args = "+newargs+" pos = "+pos+" view = "+view+" arg = "+arg+" type = "+type);
39         newsym = (Symbol)type.getConstructor(newargtypes).newInstance(newargs);
40         if (newsym==null) throw new NullPointerException("New symbol is null");
41       } catch (Throwable exp) {
42         ErrorLog.exception(exp);
43         if (exp instanceof java.lang.reflect.InvocationTargetException )
44             ErrorLog.exception(((java.lang.reflect.InvocationTargetException)exp).getTargetException());
45         newsym = null;
46       }
47       return newsym;
48     }
49   
50   //  private void readObject(java.io.ObjectInputStream stream)
51   //    throws java.io.IOException,java.lang.ClassNotFoundException {
52   //    stream.defaultReadObject();
53   //    if (position == null)
54   //      position = defaultPosition();
55   //  }
56   
57     public String toString() {return(getName());}
58   
59     public Symbol() {this(null);}
60   
61     public Symbol(String name) {
62       this.name = name;
63       checkName();
64     }
65   
66     void checkName() {
67       if (name==null || name.length()==0) {
68         name = this.getClass().toString()+" ";
69         name = name.substring(6);
70         name += DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT).format(new Date());
71       }
72     }
73   
74     public boolean stripSurveys() {return(true);}
75   
76     public Symbol(Point2D pos,View view,Object arg) {}
77   
78     public String getName(){return(name);}
79   
80     public void setName(String name){this.name=name;}
81   
82     public Rectangle2D getBounds(AffineTransform trans) {return(new Rectangle2D.Double());}
83     public Rectangle2D getApproxBounds(AffineTransform trans) {return(getBounds(trans));}
84     public void viewChanged() {}
85   
86     public boolean valid() {return(true);}
87   
88     public boolean selectProbe(Point2D pos,View view){return(false);}
89   
90     public void showSelected(View view) {}
91   
92   //  public void unSelected() {}
93   
94     public void paint(View view) {}
95   
96     public void endDrag(int comand,Point2D pos,View view){
97     }
98   
99     public void editProperties(Set sub,Symbol parent) {
100      int level = getLevel();
101      Object[] edits = new Object[level+1];
102  
103  //    System.out.println("props parent = "+parent);
104  
105      getPropertyEdit(edits,level,sub,parent);
106  
107      JTabbedPane  propbox = new JTabbedPane(); //new JPanel();
108  //    propbox.setLayout(new BoxLayout(propbox,BoxLayout.Y_AXIS));
109      for (int i=0;i<level+1;i++)
110        if (edits[i] instanceof Component) {
111          Component comp = null;
112          if (edits[i] instanceof JScrollPane)
113            comp = (Component)((JScrollPane)edits[i]).getViewport().getView();
114          else comp = (Component)edits[i];
115          if (comp instanceof JComponent) ((JComponent)comp).setAlignmentX(0);
116          propbox.add(comp);
117        }
118      JScrollPane pane = new JScrollPane(propbox);
119  
120      Object[] options= {"Ok","Cancel"};
121      if (0==JOptionPane.showOptionDialog(null,pane,"Properties",
122          				JOptionPane.DEFAULT_OPTION,
123          				JOptionPane.PLAIN_MESSAGE,
124          				null,options,options[0])) {
125        acceptPropertyEdit();
126      } else {
127        abandonPropertyEdit();
128      }
129    }
130  
131    public int getLevel() {return(0);}
132  
133    transient JTextField namefield = null;
134  //  transient PrefTrans trans = null;
135  
136    public void getPropertyEdit(Object[] edits,int slot,Set sub,Symbol parent) {
137      JPanel res = new JPanel();
138      edits[slot] = res;
139      res.setName("Name");
140      res.add(new JLabel("Name"));
141      res.add(namefield = new JTextField(name,20));
142  
143  //    trans = new PrefTrans(position);
144  //    res.add(trans.getBarePane(null));
145    }
146  
147    public void acceptPropertyEdit() {
148      if (namefield!=null) {
149        setName(namefield.getText());
150        namefield = null;
151  //      trans.save();
152      }
153    }
154  
155    public void abandonPropertyEdit() {
156      namefield = null;
157    }
158  
159    /**
160     *Called when this Symbol is moved to a new Carto file.<br>
161     *Some Symbols hold references to parts of the old file. Get
162     *rid of them, and replace them with what they should be int
163     *the new context.
164     */
165    public Symbol importSym(Carto dest,Carto source) { return(this); }
166  
167    /**
168     *Delete all references to the given Symbol held by this one.
169     */
170    public boolean recursiveDelete(Symbol old){return(false);}
171  
172    /**
173     *Called just before a survey update.<br>
174     *Vector symbols that are tied (directly or indirectly through segments)
175     *to the survey need to know, so they
176     *can move with the survey.
177     */
178    public void prepareForSurveyUpdate(java.util.List segments) {}
179  
180    /**
181     *Called when the survey has changed.<br>
182     *Symbols that move with the survey should do that now.
183     */
184    public void useSurveyUpdate() {}
185  
186    /**Reads Symbol from a stream.
187     *We define this method so it will appear in the stacktrace
188     *if something goes wrong. It does the default. */
189    private void readObject(java.io.ObjectInputStream stream)
190        throws java.io.IOException,java.lang.ClassNotFoundException {
191      try {
192        stream.defaultReadObject();
193      } catch (Throwable ex) {
194        ErrorLog.log("Error reading "+getClass());
195        throw new Error(ex);
196      }
197    }
198  }
199