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