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.util.*;
7    import javax.swing.*;
8    import java.awt.*;
9    import java.awt.geom.*;
10   import java.awt.event.*;
11   import java.awt.Graphics2D;
12   
13   /**A box is a rectangle. They can be used for borders etc.
14    */
15   public class Box extends Sub implements ActionListener {
16   
17     private static final long serialVersionUID = Version.getSUID();
18   
19     /**
20      *The thickness to draw the border.
21      */
22     Size bordersize = null;
23   
24     /**
25      *The Style to draw the border.
26      */
27     LineType borderstyle = null;
28   
29     /**
30      *The Color to draw the border.
31      */
32     Color bordercolor = Color.black;
33   
34     /**
35      *The Fill to paint the interior.
36      */
37     Fill fill = null;
38   
39     /**
40      *The color to paint the interior.
41      */
42     Color color = null;
43   
44     /**This constructer may not be used.
45      *I should check and remove if possible. (it's hard to tell because
46      *symbols are made different ways).
47      */
48     public Box(){}
49   
50     /**Create a new box at a given position.
51      *If arg is a rectangle, it gives the box's shape.
52      */
53     public Box(Point2D where,View view,Object arg) {
54       super(where,view,(arg instanceof Rectangle2D)?arg:new Rectangle2D.Double(-0.5,-0.5,1,1));
55       editscale = true;
56       editrotation=false;
57       subaspect = 1.0;
58       init(where,view);
59     }
60   
61     transient JTextField aspfield = null;
62     transient JComboBox bordersizefield=null;
63     transient JComboBox borderstylefield=null;
64     transient ColorBox bordercolorbox=null;
65     transient JButton fillbutton=null;
66     transient ColorBox colorbox=null;
67   
68     transient Library filllib = null;
69   
70     public int getLevel() {return(super.getLevel()+1);}
71   
72     public static String NOFILL = "Transparent";
73   
74     public void getPropertyEdit(Object[] edits,int slot,Set sub, Symbol parent) {
75       JPanel res = new JPanel();
76       res.setLayout(new BoxLayout(res,BoxLayout.Y_AXIS));
77       edits[slot] = res;
78       res.setName("Box");
79    
80       aspfield = PrefEditor.addDouble(res,subaspect,"Aspect Ratio");
81   
82       JPanel row = new JPanel();
83       row.setLayout(new BoxLayout(row,BoxLayout.X_AXIS));
84   
85       row.add(new JLabel("Border thickness "));
86       bordersizefield = new JComboBox(Size.getAll(Size.class));
87       bordersizefield.setSelectedItem(bordersize);
88       row.add(bordersizefield);
89       res.add(row);
90   
91       row = new JPanel();
92       row.setLayout(new BoxLayout(row,BoxLayout.X_AXIS));
93   
94       row.add(new JLabel("Border Style "));
95       borderstylefield = new JComboBox(LineType.predefined.toArray());
96       borderstylefield.setSelectedItem(borderstyle);
97       row.add(borderstylefield);
98       res.add(row);
99   
100      bordercolorbox=new ColorBox(bordercolor,"Border Color");
101      res.add(bordercolorbox);
102  
103      row = new JPanel();
104      row.setLayout(new BoxLayout(row,BoxLayout.X_AXIS));
105  /*
106      row.add(new JLabel("Fill "));
107      filllib = new Library(new java.io.File(Carto.filebase,"fill"),
108          		      new ImageIcon(getClass().getResource("icons/fill.gif")),
109          		      null);
110      fillbutton = new JButton();
111      fillbutton.addActionListener(this);
112      fillbutton.setIcon(filllib);
113      fillbutton.setToolTipText("Set fill for box");
114      row.add(fillbutton);
115  
116  //    JButton button = null;
117  
118  //    row.add(button = new JButton(NOFILL));
119  //    button.addActionListener(this);
120  
121      res.add(row);
122  */
123  
124  //    row = new JPanel();
125  //    row.setLayout(new BoxLayout(row,BoxLayout.X_AXIS));
126  
127      colorbox=new ColorBox(color,"Fill Color");
128      row.add(colorbox);
129  
130      JButton butt = new JButton(NOFILL);
131      row.add(butt);
132      butt.addActionListener(this);
133  
134      res.add(row);
135  
136      super.getPropertyEdit(edits,slot-1,sub,parent);
137    }
138  
139    public void acceptPropertyEdit() {
140      if (aspfield!=null) {
141        double newasp = PrefEditor.readDouble(aspfield);
142        if (newasp!=subaspect) {
143          subaspect=newasp;
144          reshape();
145        }
146  //    System.out.print("bordersize = "+bordersize);
147        bordersize = (Size)bordersizefield.getSelectedItem();
148  //    System.out.println(" ----> "+bordersize);
149        bordercolor = bordercolorbox.color;
150        borderstyle = (LineType)borderstylefield.getSelectedItem();
151  //      fill = filllib.getSelected();
152        color = colorbox.color;
153  
154        aspfield = null;
155        bordersizefield=null;
156        borderstylefield=null;
157        bordercolorbox=null;
158        fillbutton=null;
159        colorbox=null;
160        filllib = null;
161      }
162      super.acceptPropertyEdit();
163    }
164  
165    public void abandonPropertyEdit() {
166      aspfield = null;
167      bordersizefield=null;
168      borderstylefield=null;
169      bordercolorbox=null;
170      fillbutton=null;
171      colorbox=null;
172      filllib = null;
173  
174      super.abandonPropertyEdit();
175    }
176  
177    public void actionPerformed(ActionEvent e) {    
178      fill = null;
179      colorbox.color = null;
180  //    filllib.modalSelect();
181    }
182  
183    /**
184     *Get the edit modes that are supported.
185     */
186    protected Vector getEditModes() {
187      Vector res = super.getEditModes();
188      res.add("Aspect");
189  //    if (size==null || size.equals(Size.none))
190      res.add("Scale & Aspect");
191      return(res);
192    }
193  
194    public Area getIn(View view) {
195      if (view.dontShowIn!=null && view.dontShowIn.contains(this)) return(new Area());
196      Area res = new Area(getShape(view.trans));
197  /*
198      if (view.passpriority!=null) {
199        Set mypri = (Set)view.passpriority.get(this);
200        if (mypri!=null)
201          for (Iterator it = mypri.iterator(); it.hasNext();) {
202            Passage mask = (Passage)it.next();
203            int relation = ((Integer)passages.get(mask)).intValue();
204            if (relation==Passage.OVER || relation==Passage.UNDER)
205              res.subtract(mask.getIn(view));
206          }
207      }
208  */
209      return(res);
210    }
211  
212    /** Paint the borderary of the box.
213     *Unless overriden, that is all there is to see.*/
214    public void paint(View view) {
215      if (!view.visible.isMember(this)) return;
216      super.paint(view);
217      Point2D tpos = new Point2D.Double();
218      Point2D tpos2 = new Point2D.Double();
219  //    located=false;
220      locate();
221  
222      GeneralPath path = new GeneralPath();
223  
224      view.trans.transform(corners[0],tpos);
225      path.moveTo((int)tpos.getX(),(int)tpos.getY());
226  
227      for (int i=1;i<4;i++) {
228        view.trans.transform(corners[i],tpos);
229        path.lineTo((int)tpos.getX(),(int)tpos.getY());
230      }
231      path.closePath();
232  
233      Graphics2D gr = (Graphics2D)view.draw;
234      Paint realpaint=null;
235      if (fill!=null) realpaint = fill.getPaint(view,path);
236      if (color!=null) realpaint = color;
237      if (realpaint!=null) {
238        Paint pa = gr.getPaint();
239        gr.setPaint(realpaint);
240        gr.fill(path);
241        gr.setPaint(pa);
242      }
243      
244  //    System.out.print("bordersize = "+bordersize);
245      if (bordersize==null) bordersize = (Size)Size.lookup(Size.class,"One Pixel");
246  //    System.out.println(" -> "+bordersize);
247      if (bordercolor==null) bordercolor = Color.black;
248      if (borderstyle==null) borderstyle = LineType.lookup("Generic");
249  
250      SavableStroke stroke = borderstyle.getStroke(bordersize.getSize(view.mapscale));
251      if (stroke!=null) {
252        Stroke st = view.draw.getStroke();
253        Color color = view.draw.getColor();
254        view.draw.setStroke(stroke);
255        view.draw.setColor(bordercolor);
256  //      stroke.setFlip(current.flip);y
257  //      Shape shape = view.trans.createTransformedShape((GeneralPath)subpath.elementAt(i));
258        view.draw.draw(path);
259        view.draw.setStroke(st);
260        view.draw.setColor(color);
261      }
262    }
263  
264    /** Paint the contents of the box.
265     *A box has no contents, but we must define this to
266     *avoid making box abstract.
267     */
268    public void paintContents(View view) {}
269  }
270