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