4e27db697dc6dbb92152aa6e0c5a381b7391b582
[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, MessageManager.getString("action.change_font"), 440, 115);
96
97     init = false;
98   }
99
100   public void actionPerformed(ActionEvent evt)
101   {
102     if (evt.getSource() == ok)
103     {
104       ok_actionPerformed();
105     }
106     else if (evt.getSource() == cancel)
107     {
108       cancel_actionPerformed();
109     }
110   }
111
112   public void itemStateChanged(ItemEvent evt)
113   {
114     if (evt.getSource() == fontName)
115     {
116       fontName_actionPerformed();
117     }
118     else if (evt.getSource() == fontSize)
119     {
120       fontSize_actionPerformed();
121     }
122     else if (evt.getSource() == fontStyle)
123     {
124       fontStyle_actionPerformed();
125     }
126   }
127
128   protected void ok_actionPerformed()
129   {
130     frame.setVisible(false);
131     if (ap != null)
132     {
133       if (ap.getOverviewPanel() != null)
134       {
135         ap.getOverviewPanel().updateOverviewImage();
136       }
137     }
138
139   }
140
141   protected void cancel_actionPerformed()
142   {
143     if (ap != null)
144     {
145       ap.av.setFont(oldFont);
146       ap.paintAlignment(true);
147     }
148     else if (tp != null)
149     {
150       tp.setTreeFont(oldFont);
151       tp.treeCanvas.repaint();
152     }
153
154     fontName.select(oldFont.getName());
155     fontSize.select(oldFont.getSize() + "");
156     fontStyle.select(oldFont.getStyle());
157
158     frame.setVisible(false);
159   }
160
161   private Font lastSelected = null;
162
163   private int lastSelStyle = 0;
164
165   private int lastSelSize = 0;
166
167   /**
168    * DOCUMENT ME!
169    */
170   void changeFont()
171   {
172     if (lastSelected == null)
173     {
174       // initialise with original font
175       lastSelected = oldFont;
176       lastSelSize = oldFont.getSize();
177       lastSelStyle = oldFont.getStyle();
178     }
179
180     Font newFont = new Font(fontName.getSelectedItem().toString(),
181             fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
182                     .getSelectedItem().toString()));
183     FontMetrics fm = getGraphics().getFontMetrics(newFont);
184     double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
185             .getStringBounds("I", getGraphics()).getWidth();
186     if (mw < 1 || iw < 1)
187     {
188       // TODO: JAL-1100
189       fontName.select(lastSelected.getName());
190       fontStyle.select(lastSelStyle);
191       fontSize.select("" + lastSelSize);
192       JVDialog d = new JVDialog(this.frame, MessageManager.getString("label.invalid_font"), true, 350, 200);
193       Panel mp = new Panel();
194       d.cancel.setVisible(false);
195       mp.setLayout(new FlowLayout());
196       mp.add(new Label(
197               "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
198       d.setMainPanel(mp);
199       d.setVisible(true);
200       return;
201     }
202     if (tp != null)
203     {
204       tp.setTreeFont(newFont);
205     }
206     else if (ap != null)
207     {
208       ap.av.setFont(newFont);
209       ap.fontChanged();
210     }
211     // remember last selected
212     lastSelected = newFont;
213   }
214
215   protected void fontName_actionPerformed()
216   {
217     if (init)
218     {
219       return;
220     }
221     changeFont();
222   }
223
224   protected void fontSize_actionPerformed()
225   {
226     if (init)
227     {
228       return;
229     }
230     changeFont();
231   }
232
233   protected void fontStyle_actionPerformed()
234   {
235     if (init)
236     {
237       return;
238     }
239     changeFont();
240   }
241
242   Label label1 = new Label();
243
244   protected Choice fontSize = new Choice();
245
246   protected Choice fontStyle = new Choice();
247
248   Label label2 = new Label();
249
250   Label label3 = new Label();
251
252   protected Choice fontName = new Choice();
253
254   Button ok = new Button();
255
256   Button cancel = new Button();
257
258   Panel panel1 = new Panel();
259
260   Panel panel2 = new Panel();
261
262   Panel panel3 = new Panel();
263
264   BorderLayout borderLayout1 = new BorderLayout();
265
266   BorderLayout borderLayout2 = new BorderLayout();
267
268   BorderLayout borderLayout3 = new BorderLayout();
269
270   Panel panel4 = new Panel();
271
272   Panel panel5 = new Panel();
273
274   BorderLayout borderLayout4 = new BorderLayout();
275
276   private void jbInit() throws Exception
277   {
278     label1.setFont(new java.awt.Font("Verdana", 0, 11));
279     label1.setAlignment(Label.RIGHT);
280     label1.setText(MessageManager.getString("label.font"));
281     this.setLayout(borderLayout4);
282     fontSize.setFont(new java.awt.Font("Verdana", 0, 11));
283     fontSize.addItemListener(this);
284     fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));
285     fontStyle.addItemListener(this);
286     label2.setAlignment(Label.RIGHT);
287     label2.setFont(new java.awt.Font("Verdana", 0, 11));
288     label2.setText(MessageManager.getString("label.size"));
289     label3.setAlignment(Label.RIGHT);
290     label3.setFont(new java.awt.Font("Verdana", 0, 11));
291     label3.setText(MessageManager.getString("label.style"));
292     fontName.setFont(new java.awt.Font("Verdana", 0, 11));
293     fontName.addItemListener(this);
294     ok.setFont(new java.awt.Font("Verdana", 0, 11));
295     ok.setLabel(MessageManager.getString("action.ok"));
296     ok.addActionListener(this);
297     cancel.setFont(new java.awt.Font("Verdana", 0, 11));
298     cancel.setLabel(MessageManager.getString("action.cancel"));
299     cancel.addActionListener(this);
300     this.setBackground(Color.white);
301     panel1.setLayout(borderLayout1);
302     panel2.setLayout(borderLayout3);
303     panel3.setLayout(borderLayout2);
304     panel5.setBackground(Color.white);
305     panel4.setBackground(Color.white);
306     panel1.setBackground(Color.white);
307     panel2.setBackground(Color.white);
308     panel3.setBackground(Color.white);
309     panel1.add(label1, BorderLayout.WEST);
310     panel1.add(fontName, BorderLayout.CENTER);
311     panel5.add(panel1, null);
312     panel5.add(panel3, null);
313     panel5.add(panel2, null);
314     panel2.add(label3, BorderLayout.WEST);
315     panel2.add(fontStyle, BorderLayout.CENTER);
316     panel3.add(label2, BorderLayout.WEST);
317     panel3.add(fontSize, BorderLayout.CENTER);
318     this.add(panel4, BorderLayout.SOUTH);
319     panel4.add(ok, null);
320     panel4.add(cancel, null);
321     this.add(panel5, BorderLayout.CENTER);
322   }
323
324 }