JAL-1355 (basic i18n support)
[jalview.git] / src / jalview / appletgui / FontChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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 jalview.util.MessageManager;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 public class FontChooser extends Panel implements ActionListener,
26         ItemListener
27 {
28   AlignmentPanel ap;
29
30   TreePanel tp;
31
32   Font oldFont;
33
34   boolean init = true;
35
36   Frame frame;
37
38   public FontChooser(TreePanel tp)
39   {
40     try
41     {
42       jbInit();
43     } catch (Exception e)
44     {
45       e.printStackTrace();
46     }
47
48     this.tp = tp;
49     oldFont = tp.getTreeFont();
50     init();
51   }
52
53   public FontChooser(AlignmentPanel ap)
54   {
55     try
56     {
57       jbInit();
58     } catch (Exception e)
59     {
60       e.printStackTrace();
61     }
62
63     this.ap = ap;
64     oldFont = ap.av.getFont();
65     init();
66   }
67
68   void init()
69   {
70     String fonts[] = Toolkit.getDefaultToolkit().getFontList();
71     for (int i = 0; i < fonts.length; i++)
72     {
73       fontName.addItem(fonts[i]);
74     }
75
76     for (int i = 1; i < 31; i++)
77     {
78       fontSize.addItem(i + "");
79     }
80
81     fontStyle.addItem("plain");
82     fontStyle.addItem("bold");
83     fontStyle.addItem("italic");
84
85     fontName.select(oldFont.getName());
86     fontSize.select(oldFont.getSize() + "");
87     fontStyle.select(oldFont.getStyle());
88
89     Frame frame = new Frame();
90     this.frame = frame;
91     frame.add(this);
92     jalview.bin.JalviewLite.addFrame(frame, MessageManager.getString("action.change_font"), 440, 115);
93
94     init = false;
95   }
96
97   public void actionPerformed(ActionEvent evt)
98   {
99     if (evt.getSource() == ok)
100     {
101       ok_actionPerformed();
102     }
103     else if (evt.getSource() == cancel)
104     {
105       cancel_actionPerformed();
106     }
107   }
108
109   public void itemStateChanged(ItemEvent evt)
110   {
111     if (evt.getSource() == fontName)
112     {
113       fontName_actionPerformed();
114     }
115     else if (evt.getSource() == fontSize)
116     {
117       fontSize_actionPerformed();
118     }
119     else if (evt.getSource() == fontStyle)
120     {
121       fontStyle_actionPerformed();
122     }
123   }
124
125   protected void ok_actionPerformed()
126   {
127     frame.setVisible(false);
128     if (ap != null)
129     {
130       if (ap.getOverviewPanel() != null)
131       {
132         ap.getOverviewPanel().updateOverviewImage();
133       }
134     }
135
136   }
137
138   protected void cancel_actionPerformed()
139   {
140     if (ap != null)
141     {
142       ap.av.setFont(oldFont);
143       ap.paintAlignment(true);
144     }
145     else if (tp != null)
146     {
147       tp.setTreeFont(oldFont);
148       tp.treeCanvas.repaint();
149     }
150
151     fontName.select(oldFont.getName());
152     fontSize.select(oldFont.getSize() + "");
153     fontStyle.select(oldFont.getStyle());
154
155     frame.setVisible(false);
156   }
157
158   private Font lastSelected = null;
159
160   private int lastSelStyle = 0;
161
162   private int lastSelSize = 0;
163
164   /**
165    * DOCUMENT ME!
166    */
167   void changeFont()
168   {
169     if (lastSelected == null)
170     {
171       // initialise with original font
172       lastSelected = oldFont;
173       lastSelSize = oldFont.getSize();
174       lastSelStyle = oldFont.getStyle();
175     }
176
177     Font newFont = new Font(fontName.getSelectedItem().toString(),
178             fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
179                     .getSelectedItem().toString()));
180     FontMetrics fm = getGraphics().getFontMetrics(newFont);
181     double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
182             .getStringBounds("I", getGraphics()).getWidth();
183     if (mw < 1 || iw < 1)
184     {
185       // TODO: JAL-1100
186       fontName.select(lastSelected.getName());
187       fontStyle.select(lastSelStyle);
188       fontSize.select("" + lastSelSize);
189       JVDialog d = new JVDialog(this.frame, MessageManager.getString("label.invalid_font"), true, 350, 200);
190       Panel mp = new Panel();
191       d.cancel.setVisible(false);
192       mp.setLayout(new FlowLayout());
193       mp.add(new Label(
194               "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
195       d.setMainPanel(mp);
196       d.setVisible(true);
197       return;
198     }
199     if (tp != null)
200     {
201       tp.setTreeFont(newFont);
202     }
203     else if (ap != null)
204     {
205       ap.av.setFont(newFont);
206       ap.fontChanged();
207     }
208     // remember last selected
209     lastSelected = newFont;
210   }
211
212   protected void fontName_actionPerformed()
213   {
214     if (init)
215     {
216       return;
217     }
218     changeFont();
219   }
220
221   protected void fontSize_actionPerformed()
222   {
223     if (init)
224     {
225       return;
226     }
227     changeFont();
228   }
229
230   protected void fontStyle_actionPerformed()
231   {
232     if (init)
233     {
234       return;
235     }
236     changeFont();
237   }
238
239   Label label1 = new Label();
240
241   protected Choice fontSize = new Choice();
242
243   protected Choice fontStyle = new Choice();
244
245   Label label2 = new Label();
246
247   Label label3 = new Label();
248
249   protected Choice fontName = new Choice();
250
251   Button ok = new Button();
252
253   Button cancel = new Button();
254
255   Panel panel1 = new Panel();
256
257   Panel panel2 = new Panel();
258
259   Panel panel3 = new Panel();
260
261   BorderLayout borderLayout1 = new BorderLayout();
262
263   BorderLayout borderLayout2 = new BorderLayout();
264
265   BorderLayout borderLayout3 = new BorderLayout();
266
267   Panel panel4 = new Panel();
268
269   Panel panel5 = new Panel();
270
271   BorderLayout borderLayout4 = new BorderLayout();
272
273   private void jbInit() throws Exception
274   {
275     label1.setFont(new java.awt.Font("Verdana", 0, 11));
276     label1.setAlignment(Label.RIGHT);
277     label1.setText(MessageManager.getString("label.font"));
278     this.setLayout(borderLayout4);
279     fontSize.setFont(new java.awt.Font("Verdana", 0, 11));
280     fontSize.addItemListener(this);
281     fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));
282     fontStyle.addItemListener(this);
283     label2.setAlignment(Label.RIGHT);
284     label2.setFont(new java.awt.Font("Verdana", 0, 11));
285     label2.setText(MessageManager.getString("label.size"));
286     label3.setAlignment(Label.RIGHT);
287     label3.setFont(new java.awt.Font("Verdana", 0, 11));
288     label3.setText(MessageManager.getString("label.style"));
289     fontName.setFont(new java.awt.Font("Verdana", 0, 11));
290     fontName.addItemListener(this);
291     ok.setFont(new java.awt.Font("Verdana", 0, 11));
292     ok.setLabel(MessageManager.getString("action.ok"));
293     ok.addActionListener(this);
294     cancel.setFont(new java.awt.Font("Verdana", 0, 11));
295     cancel.setLabel(MessageManager.getString("action.cancel"));
296     cancel.addActionListener(this);
297     this.setBackground(Color.white);
298     panel1.setLayout(borderLayout1);
299     panel2.setLayout(borderLayout3);
300     panel3.setLayout(borderLayout2);
301     panel5.setBackground(Color.white);
302     panel4.setBackground(Color.white);
303     panel1.setBackground(Color.white);
304     panel2.setBackground(Color.white);
305     panel3.setBackground(Color.white);
306     panel1.add(label1, BorderLayout.WEST);
307     panel1.add(fontName, BorderLayout.CENTER);
308     panel5.add(panel1, null);
309     panel5.add(panel3, null);
310     panel5.add(panel2, null);
311     panel2.add(label3, BorderLayout.WEST);
312     panel2.add(fontStyle, BorderLayout.CENTER);
313     panel3.add(label2, BorderLayout.WEST);
314     panel3.add(fontSize, BorderLayout.CENTER);
315     this.add(panel4, BorderLayout.SOUTH);
316     panel4.add(ok, null);
317     panel4.add(cancel, null);
318     this.add(panel5, BorderLayout.CENTER);
319   }
320
321 }