514b0594625e76db41052833fd8ec48f04d7458d
[jalview.git] / src / jalview / appletgui / FontChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.appletgui;
20
21 import java.awt.*;
22 import java.awt.event.*;
23
24 public class FontChooser extends Panel implements ActionListener,
25         ItemListener
26 {
27   AlignmentPanel ap;
28
29   TreePanel tp;
30
31   Font oldFont;
32
33   boolean init = true;
34
35   Frame frame;
36
37   public FontChooser(TreePanel tp)
38   {
39     try
40     {
41       jbInit();
42     } catch (Exception e)
43     {
44       e.printStackTrace();
45     }
46
47     this.tp = tp;
48     oldFont = tp.getTreeFont();
49     init();
50   }
51
52   public FontChooser(AlignmentPanel ap)
53   {
54     try
55     {
56       jbInit();
57     } catch (Exception e)
58     {
59       e.printStackTrace();
60     }
61
62     this.ap = ap;
63     oldFont = ap.av.getFont();
64     init();
65   }
66
67   void init()
68   {
69     String fonts[] = Toolkit.getDefaultToolkit().getFontList();
70     for (int i = 0; i < fonts.length; i++)
71     {
72       fontName.addItem(fonts[i]);
73     }
74
75     for (int i = 1; i < 31; i++)
76     {
77       fontSize.addItem(i + "");
78     }
79
80     fontStyle.addItem("plain");
81     fontStyle.addItem("bold");
82     fontStyle.addItem("italic");
83
84     fontName.select(oldFont.getName());
85     fontSize.select(oldFont.getSize() + "");
86     fontStyle.select(oldFont.getStyle());
87
88     Frame frame = new Frame();
89     this.frame = frame;
90     frame.add(this);
91     jalview.bin.JalviewLite.addFrame(frame, "Change Font", 440, 115);
92
93     init = false;
94   }
95
96   public void actionPerformed(ActionEvent evt)
97   {
98     if (evt.getSource() == ok)
99     {
100       ok_actionPerformed();
101     }
102     else if (evt.getSource() == cancel)
103     {
104       cancel_actionPerformed();
105     }
106   }
107
108   public void itemStateChanged(ItemEvent evt)
109   {
110     if (evt.getSource() == fontName)
111     {
112       fontName_actionPerformed();
113     }
114     else if (evt.getSource() == fontSize)
115     {
116       fontSize_actionPerformed();
117     }
118     else if (evt.getSource() == fontStyle)
119     {
120       fontStyle_actionPerformed();
121     }
122   }
123
124   protected void ok_actionPerformed()
125   {
126     frame.setVisible(false);
127     if (ap != null)
128     {
129       if (ap.getOverviewPanel() != null)
130       {
131         ap.getOverviewPanel().updateOverviewImage();
132       }
133     }
134
135   }
136
137   protected void cancel_actionPerformed()
138   {
139     if (ap != null)
140     {
141       ap.av.setFont(oldFont);
142       ap.paintAlignment(true);
143     }
144     else if (tp != null)
145     {
146       tp.setTreeFont(oldFont);
147       tp.treeCanvas.repaint();
148     }
149
150     fontName.select(oldFont.getName());
151     fontSize.select(oldFont.getSize() + "");
152     fontStyle.select(oldFont.getStyle());
153
154     frame.setVisible(false);
155   }
156
157   void changeFont()
158   {
159     Font newFont = new Font(fontName.getSelectedItem().toString(),
160             fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
161                     .getSelectedItem().toString()));
162     if (ap != null)
163     {
164       ap.av.setFont(newFont);
165       ap.fontChanged();
166     }
167     else if (tp != null)
168     {
169       tp.setTreeFont(newFont);
170     }
171   }
172
173   protected void fontName_actionPerformed()
174   {
175     if (init)
176     {
177       return;
178     }
179     changeFont();
180   }
181
182   protected void fontSize_actionPerformed()
183   {
184     if (init)
185     {
186       return;
187     }
188     changeFont();
189   }
190
191   protected void fontStyle_actionPerformed()
192   {
193     if (init)
194     {
195       return;
196     }
197     changeFont();
198   }
199
200   Label label1 = new Label();
201
202   protected Choice fontSize = new Choice();
203
204   protected Choice fontStyle = new Choice();
205
206   Label label2 = new Label();
207
208   Label label3 = new Label();
209
210   protected Choice fontName = new Choice();
211
212   Button ok = new Button();
213
214   Button cancel = new Button();
215
216   Panel panel1 = new Panel();
217
218   Panel panel2 = new Panel();
219
220   Panel panel3 = new Panel();
221
222   BorderLayout borderLayout1 = new BorderLayout();
223
224   BorderLayout borderLayout2 = new BorderLayout();
225
226   BorderLayout borderLayout3 = new BorderLayout();
227
228   Panel panel4 = new Panel();
229
230   Panel panel5 = new Panel();
231
232   BorderLayout borderLayout4 = new BorderLayout();
233
234   private void jbInit() throws Exception
235   {
236     label1.setFont(new java.awt.Font("Verdana", 0, 11));
237     label1.setAlignment(Label.RIGHT);
238     label1.setText("Font: ");
239     this.setLayout(borderLayout4);
240     fontSize.setFont(new java.awt.Font("Verdana", 0, 11));
241     fontSize.addItemListener(this);
242     fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));
243     fontStyle.addItemListener(this);
244     label2.setAlignment(Label.RIGHT);
245     label2.setFont(new java.awt.Font("Verdana", 0, 11));
246     label2.setText("Size: ");
247     label3.setAlignment(Label.RIGHT);
248     label3.setFont(new java.awt.Font("Verdana", 0, 11));
249     label3.setText("Style: ");
250     fontName.setFont(new java.awt.Font("Verdana", 0, 11));
251     fontName.addItemListener(this);
252     ok.setFont(new java.awt.Font("Verdana", 0, 11));
253     ok.setLabel("OK");
254     ok.addActionListener(this);
255     cancel.setFont(new java.awt.Font("Verdana", 0, 11));
256     cancel.setLabel("Cancel");
257     cancel.addActionListener(this);
258     this.setBackground(Color.white);
259     panel1.setLayout(borderLayout1);
260     panel2.setLayout(borderLayout3);
261     panel3.setLayout(borderLayout2);
262     panel5.setBackground(Color.white);
263     panel4.setBackground(Color.white);
264     panel1.setBackground(Color.white);
265     panel2.setBackground(Color.white);
266     panel3.setBackground(Color.white);
267     panel1.add(label1, BorderLayout.WEST);
268     panel1.add(fontName, BorderLayout.CENTER);
269     panel5.add(panel1, null);
270     panel5.add(panel3, null);
271     panel5.add(panel2, null);
272     panel2.add(label3, BorderLayout.WEST);
273     panel2.add(fontStyle, BorderLayout.CENTER);
274     panel3.add(label2, BorderLayout.WEST);
275     panel3.add(fontSize, BorderLayout.CENTER);
276     this.add(panel4, BorderLayout.SOUTH);
277     panel4.add(ok, null);
278     panel4.add(cancel, null);
279     this.add(panel5, BorderLayout.CENTER);
280   }
281
282 }