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