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 java.io.*;
8    import java.awt.*;
9    import java.awt.event.*;
10   import javax.swing.*;
11   
12   public class TextForm extends FileDefaultable implements Serializable,ActionListener{
13   
14     private static final long serialVersionUID = Version.getSUID();
15   
16     public static String TESTSTRING1 = "the quick brown fox jumped over the lazy dog";
17     public static String TESTSTRING2 = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG";
18     public static String TESTSTRING3 = "0123456789`~!@#$%^&*()_+|-=\\[]{};:'\",.<>/?";
19   
20     public static String PLAIN = "plain";
21     public static String STATION = "station label";
22   
23     public static TreeSet defaults;
24     public static TreeSet mindefaults = new TreeSet();
25     public static TreeSet file = new TreeSet();
26     public static TreeSet fixed = new TreeSet();
27   
28     static {
29       mindefaults.add(new TextForm(PLAIN));
30       TextForm station = new TextForm(STATION);
31       mindefaults.add(station);
32       station.family = Vertex.DEFFAMILY;
33       station.size = (Size)FileDefaultable.lookup(Size.class,Size.STALAB);
34   
35       defaults = (TreeSet)mindefaults.clone();
36   
37       fixedmap.put(TextForm.class,fixed);
38       defmap.put(TextForm.class,defaults);
39       minmap.put(TextForm.class,mindefaults);
40       filemap.put(TextForm.class,file);
41     }
42   
43     public Size size = (Size)FileDefaultable.lookup(Size.class,Size.TEXT);
44     public Color color = Color.black;
45     public String family = "Utopia";
46     public boolean italic = false;
47     public boolean bold = false;
48     public String name;
49   
50     transient Font font = null;
51     public int height = 0;
52   
53     public TextForm() {name="noname";}
54   
55     public TextForm(String name) {
56       this.name = name;
57     }
58   
59     public String getName() {return(name);}
60   
61     transient JCheckBox itfield = null;
62     transient JCheckBox bofield = null;
63     transient ColorBox colorfield = null;
64     transient JComboBox sizefield = null;
65     transient JList famlist = null;
66     transient JLabel testlabel1 = null;
67     transient JLabel testlabel2 = null;
68     transient JLabel testlabel3 = null;
69     transient javax.swing.Box panel = null;
70   
71     public boolean edit(JFrame owner) {
72       String[] fams = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
73   
74       JComponent top = new JPanel();
75       top.setLayout(new BoxLayout(top,BoxLayout.X_AXIS)); 
76       top.setAlignmentX(0);
77       famlist = new JList(fams);
78       famlist.setAlignmentX(0);
79       famlist.setSelectedValue(family,true);
80       top.add(famlist);
81   
82       javax.swing.Box right = new javax.swing.Box(BoxLayout.Y_AXIS); 
83       
84       itfield = PrefEditor.addCheck(right,italic,"Italic");
85       bofield = PrefEditor.addCheck(right,bold,"Bold");
86       colorfield = PrefEditor.addColor(right,color,"Color");
87       
88       javax.swing.Box combo = new javax.swing.Box(BoxLayout.X_AXIS);
89       combo.add(new JLabel("Size "));
90       sizefield = new JComboBox(Size.getAll(Size.class));
91       sizefield.setSelectedItem(size);
92       combo.add(sizefield);
93       right.add(combo);
94   
95       JButton but = new JButton("Preview");
96       but.addActionListener(this);
97       right.add(but);
98   
99       top.add(right);
100  
101      panel = new javax.swing.Box(BoxLayout.Y_AXIS); 
102      panel.add(top);
103  
104      testlabel1 = new JLabel(TESTSTRING1);
105      testlabel1.setAlignmentX(0);
106      panel.add(testlabel1);
107      testlabel2 = new JLabel(TESTSTRING2);
108      panel.add(testlabel2);
109      testlabel3 = new JLabel(TESTSTRING3);
110      panel.add(testlabel3);
111  
112      setView();
113      
114      if (JOptionPane.showConfirmDialog(owner,panel,"Edit size"+name,JOptionPane.OK_CANCEL_OPTION) ==
115          JOptionPane.OK_OPTION) {
116        String fontname = (String)famlist.getSelectedValue();
117        if (fontname!=null) family = fontname;
118        color = colorfield.color;
119        bold = bofield.isSelected();
120        italic = itfield.isSelected();
121        size = (Size)sizefield.getSelectedItem();
122        font = null;
123        clearEdit();
124        return(true);
125      }
126  
127      clearEdit();
128      return(false);
129    }
130  
131    private void clearEdit() {
132      sizefield = null;
133      itfield = null;
134      bofield = null;
135      colorfield = null;
136      famlist = null;
137      testlabel1=testlabel2=testlabel3=null;
138      panel = null;
139    }
140  
141    public void actionPerformed(ActionEvent e) {
142      setView();
143    }
144  
145    public Font getFont() {
146      if (font==null) {
147  //      if (fontname==null) return(null);
148        font =new Font(family,
149          	     (bold?Font.BOLD:0) | (italic?Font.ITALIC:0),
150          	     16);
151      }
152      return(font);
153    }
154  
155    private Font readFont() {
156      String fontname = (String)famlist.getSelectedValue();
157      if (fontname==null) return(null);
158      return(new Font(fontname,
159          	    (bofield.isSelected()?Font.BOLD:0) |(itfield.isSelected()?Font.ITALIC:0),
160          	    16));
161      
162    }
163  
164    private void setView() {
165      Font testfont = readFont();
166      if (testfont!=null) {
167        testlabel1.setFont(testfont);
168        testlabel2.setFont(testfont);
169        testlabel3.setFont(testfont);
170      }
171      testlabel1.setForeground(colorfield.color);
172      testlabel2.setForeground(colorfield.color);
173      testlabel3.setForeground(colorfield.color);
174    }
175  
176    public Object clone() {
177      TextForm res = new TextForm(name);
178      res.copy(this);
179      return((Object)res);
180    }
181  
182    public void copy(FileDefaultable thatitem) {
183      TextForm that = (TextForm)thatitem;
184      name = that.name;
185      family = that.family;
186      size = that.size;
187      italic = that.italic;
188      bold = that.bold;
189      color = that.color;
190      font = null;
191    }
192  
193    public Object readResolve() throws ObjectStreamException {
194      return((Object)getGlobal(TextForm.class));
195    }
196  }
197  
198  
199