KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > dck > items > FontInput


1 package rero.dck.items;
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6 import javax.swing.*;
7 import javax.swing.event.*;
8
9 import java.io.*;
10
11 import rero.dck.*;
12 import rero.config.*;
13
14 public class FontInput extends SuperInput implements ItemListener
15 {
16    protected JComboBox name;
17    protected JComboBox style;
18    protected JComboBox size;
19
20    protected JLabel preview;
21
22    protected Font value;
23
24    protected boolean listing = true;
25
26    public static String JavaDoc funny()
27    {
28       String JavaDoc taglines[] = {
29          "The quick brown fox jumped over the lazy coder",
30          "Fat butane, grubbin' on French fries",
31          "Sun sucks!@",
32      };
33      int r = ((int)System.currentTimeMillis() / 1000) % taglines.length;
34      return taglines[Math.abs(r)];
35    }
36
37    public FontInput(String JavaDoc _variable, Font _value)
38    {
39       variable = _variable;
40
41       value = _value;
42
43       setLayout(new BorderLayout());
44
45       JPanel top = new JPanel();
46       top.setLayout(new BorderLayout());
47       
48       JPanel fonts = new JPanel();
49       fonts.setLayout(new FlowLayout(FlowLayout.CENTER));
50       
51       name = new JComboBox();
52       name.setPrototypeDisplayValue("Times New Roman.");
53       name.addItem("Loading fonts...");
54
55       style = new JComboBox(new String JavaDoc[] { "Plain", "Italic", "Bold" });
56       size = new JComboBox(new String JavaDoc[] { "5", "8", "9","10", "11", "12", "13", "14", "15", "16", "17", "18", "20", "22", "26", "32" });
57
58       name.addItemListener(this);
59       style.addItemListener(this);
60       size.addItemListener(this);
61  
62       fonts.add(name);
63       fonts.add(style);
64       fonts.add(size);
65
66       top.add(fonts, BorderLayout.CENTER);
67
68       preview = new JLabel(funny());
69 // preview.setEditable(false);
70
preview.setOpaque(false);
71 // preview.setText(funny());
72

73       JPanel bottom = new JPanel();
74       bottom.setLayout(new FlowLayout(FlowLayout.CENTER));
75       bottom.add(preview);
76
77       add(top, BorderLayout.NORTH);
78       add(bottom, BorderLayout.CENTER);
79    }
80
81    public void save()
82    {
83       ClientState.getClientState().setFont(getVariable(), preview.getFont());
84    }
85
86    public int getEstimatedWidth()
87    {
88       return 0;
89    }
90
91    public void setAlignWidth(int width)
92    {
93    }
94
95    public void itemStateChanged(ItemEvent ev)
96    {
97       Font f = Font.decode(name.getSelectedItem()+"-"+style.getSelectedItem().toString().toUpperCase()+"-"+size.getSelectedItem());
98
99       preview.setFont(f);
100       preview.revalidate();
101
102       notifyParent();
103    }
104
105    public JComponent getComponent()
106    {
107       return this;
108    }
109
110    public void refresh()
111    {
112       if (!listing)
113       {
114          Font f = ClientState.getClientState().getFont(getVariable(), value);
115
116          name.setSelectedItem(f.getFamily());
117          size.setSelectedItem(f.getSize()+"");
118
119          if (f.isBold())
120          {
121             style.setSelectedItem("Bold");
122          }
123          else if (f.isItalic())
124          {
125             style.setSelectedItem("Italic");
126          }
127          else
128          {
129             style.setSelectedItem("Plain");
130          }
131
132          preview.setFont(f);
133          preview.validate();
134       }
135       else
136       {
137       //
138
// obtaining all of the fonts from the system is pretty damned slow so we're going to do it in a thread.
139
//
140
SwingUtilities.invokeLater(new Runnable JavaDoc()
141          {
142              public void run()
143              {
144                 String JavaDoc fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
145                 for (int x = 0; x < fonts.length; x++)
146                 {
147                    name.addItem(fonts[x]);
148                 }
149
150                 name.removeItemAt(0);
151                 listing = false;
152                 refresh();
153                 revalidate();
154              }
155          });
156       }
157    }
158 }
159
160
161
Popular Tags