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.util.*;
8    import java.awt.font.*;
9    import java.awt.geom.*;
10   import javax.swing.*;
11   import java.io.*;
12   import java.awt.Graphics2D;
13   
14   public class Text extends Sub /*implements SubSelectable*/{
15   
16     private static final long serialVersionUID = Version.getSUID();
17   
18     public String contents="";
19     boolean valid = false;
20     public TextForm form;
21   
22     transient FontRenderContext rendercontext = null;
23   
24     public Text(Point2D where,View view,Object arg) {
25       super(where,view,arg);
26       editscale=false;
27       editrotation=false;
28   
29       form  = (TextForm)FileDefaultable.lookup(TextForm.class,TextForm.PLAIN);
30       contents = JOptionPane.showInputDialog("Enter text");
31       if (contents==null) contents = "";
32       showcorners = true;
33       rendercontext= view.draw.getFontRenderContext();
34       init(where,view);
35       view.parent.repaint();
36     }
37   
38   //  void getCenter() {
39   //    locate=false;
40   //    box = new SavableRectangle2D(form.getFont().getStringBounds(contents+"  ",rendercontext),1,0);
41   //    xorig = box.getX()+box.getWidth()/2;
42   //    yorig = box.getX()+box.getHeight()/2;
43   //  }
44   
45     public void reshape() {
46       located = false;
47       box = new SavableRectangle2D(form.getFont().getStringBounds(contents+"  ",rendercontext),1,0);
48     }
49   
50     public void paint(View view) {
51       if (form==null) form = (TextForm)FileDefaultable.lookup(TextForm.class,TextForm.PLAIN);
52       if (form.size!=null && form.size!=Size.none) {
53         size = form.size;
54         sizewidth=false;
55       }
56       super.paint(view);
57     }
58   
59     public void paintContents(View view){
60       if (rendercontext==null) rendercontext = view.draw.getFontRenderContext();
61       if (contents.length() == 0) return;
62       AffineTransform oldtrans = view.draw.getTransform();
63       AffineTransform newtrans = new AffineTransform(oldtrans);
64       view.draw.setFont(form.getFont());
65       Color c = view.draw.getColor();
66       view.draw.setColor(form.color);
67       newtrans.concatenate(view.trans);
68       newtrans.concatenate(new AffineTransform(1,0,0,-1,0,0));
69       view.draw.setTransform(newtrans);
70       view.draw.drawString(contents,0,0);
71       view.draw.setColor(c);
72       view.draw.setTransform(oldtrans);
73     }
74   
75     public boolean valid() {
76       return(contents.length()>0);
77     }
78   
79     public int getLevel() {return(super.getLevel()+1);}
80   
81     transient JTextField contfield=null;
82     transient JComboBox formfield=null;
83   
84     public void getPropertyEdit(Object[] edits,int slot,Set sub,Symbol parent) {
85       javax.swing.Box box = new javax.swing.Box(BoxLayout.Y_AXIS);
86   
87       contfield = new JTextField(contents);
88       contfield.setAlignmentX(0);
89       box.add(contfield);
90   
91       formfield= PrefEditor.addUnit(box,form,TextForm.getAll(TextForm.class),"Font");
92   
93       edits[slot] = box;
94       ((Component)edits[slot]).setName("Text");
95       contfield.setColumns(30);
96       super.getPropertyEdit(edits,slot-1,sub,parent);
97     }
98   
99     public void acceptPropertyEdit() {
100      if (contfield!=null) {
101        contents = contfield.getText();
102        contfield = null;
103        form = (TextForm)formfield.getSelectedItem();
104        formfield = null;
105      }
106      super.acceptPropertyEdit();
107  //    getCenter();
108      reshape();
109    }
110  
111    public void abandonPropertyEdit() {
112      contfield = null;
113      super.abandonPropertyEdit();
114    }
115  
116  }
117