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