updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / gui / 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 package jalview.gui;
20
21 import jalview.bin.*;
22
23 import jalview.jbgui.*;
24
25 import java.awt.*;
26 import java.awt.event.*;
27
28 import javax.swing.*;
29
30
31 /**
32  * DOCUMENT ME!
33  *
34  * @author $author$
35  * @version $Revision$
36  */
37 public class FontChooser extends GFontChooser
38 {
39     AlignmentPanel ap;
40     TreePanel tp;
41     Font oldFont;
42     boolean init = true;
43     JInternalFrame frame;
44
45     /**
46      * Creates a new FontChooser object.
47      *
48      * @param ap DOCUMENT ME!
49      */
50     public FontChooser(TreePanel tp)
51     {
52       this.tp = tp;
53       oldFont = tp.getTreeFont();
54       defaultButton.setVisible(false);
55       init();
56     }
57     /**
58      * Creates a new FontChooser object.
59      *
60      * @param ap DOCUMENT ME!
61      */
62     public FontChooser(AlignmentPanel ap)
63     {
64       oldFont = ap.av.getFont();
65       this.ap = ap;
66       init();
67     }
68
69     void init()
70     {
71         frame = new JInternalFrame();
72         frame.setContentPane(this);
73         if(tp!=null)
74            Desktop.addInternalFrame(frame, "Change Font (Tree Panel)", 540, 100, false);
75         else
76            Desktop.addInternalFrame(frame, "Change Font", 540, 100, false);
77
78         frame.setLayer(JLayeredPane.PALETTE_LAYER);
79
80         String[] fonts = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment()
81                                                      .getAvailableFontFamilyNames();
82
83         for (int i = 0; i < fonts.length; i++)
84         {
85             fontName.addItem(fonts[i]);
86         }
87
88         for (int i = 1; i < 51; i++)
89         {
90             fontSize.addItem(i + "");
91         }
92
93         fontStyle.addItem("plain");
94         fontStyle.addItem("bold");
95         fontStyle.addItem("italic");
96
97         fontName.setSelectedItem(oldFont.getName());
98         fontSize.setSelectedItem(oldFont.getSize() + "");
99         fontStyle.setSelectedIndex(oldFont.getStyle());
100
101         FontMetrics fm = getGraphics().getFontMetrics(oldFont);
102         monospaced.setSelected( fm.getStringBounds("M",getGraphics()).getWidth()
103                               ==fm.getStringBounds("|",getGraphics()).getWidth());
104
105
106         init = false;
107     }
108
109     /**
110      * DOCUMENT ME!
111      *
112      * @param e DOCUMENT ME!
113      */
114     protected void ok_actionPerformed(ActionEvent e)
115     {
116         try
117         {
118             frame.setClosed(true);
119         }
120         catch (Exception ex)
121         {
122         }
123
124         if(ap!=null)
125         {
126           if (ap.getOverviewPanel() != null)
127           {
128             ap.getOverviewPanel().updateOverviewImage();
129           }
130         }
131     }
132
133     /**
134      * DOCUMENT ME!
135      *
136      * @param e DOCUMENT ME!
137      */
138     protected void cancel_actionPerformed(ActionEvent e)
139     {
140         if(ap!=null)
141         {
142           ap.av.setFont(oldFont);
143           ap.repaint();
144         }
145         else if(tp!=null)
146         {
147           tp.setTreeFont(oldFont);
148         }
149         fontName.setSelectedItem(oldFont.getName());
150         fontSize.setSelectedItem(oldFont.getSize() + "");
151         fontStyle.setSelectedIndex(oldFont.getStyle());
152
153         try
154         {
155             frame.setClosed(true);
156         }
157         catch (Exception ex)
158         {
159         }
160     }
161
162     /**
163      * DOCUMENT ME!
164      */
165     void changeFont()
166     {
167       Font newFont = new Font(fontName.getSelectedItem().toString(),
168                               fontStyle.getSelectedIndex(),
169                               Integer.parseInt(fontSize.getSelectedItem().toString()));
170       if (ap != null)
171       {
172         ap.av.setFont(newFont);
173         ap.fontChanged();
174       }
175       else if(tp != null)
176       {
177         tp.setTreeFont(newFont);
178       }
179
180       FontMetrics fm = getGraphics().getFontMetrics(newFont);
181
182       monospaced.setSelected( fm.getStringBounds("M",getGraphics()).getWidth()
183                                 ==fm.getStringBounds("|",getGraphics()).getWidth());
184
185     }
186
187     /**
188      * DOCUMENT ME!
189      *
190      * @param e DOCUMENT ME!
191      */
192     protected void fontName_actionPerformed(ActionEvent e)
193     {
194         if (init)
195         {
196             return;
197         }
198
199         changeFont();
200     }
201
202     /**
203      * DOCUMENT ME!
204      *
205      * @param e DOCUMENT ME!
206      */
207     protected void fontSize_actionPerformed(ActionEvent e)
208     {
209         if (init)
210         {
211             return;
212         }
213
214         changeFont();
215     }
216
217     /**
218      * DOCUMENT ME!
219      *
220      * @param e DOCUMENT ME!
221      */
222     protected void fontStyle_actionPerformed(ActionEvent e)
223     {
224         if (init)
225         {
226             return;
227         }
228
229         changeFont();
230     }
231
232     /**
233      * DOCUMENT ME!
234      *
235      * @param e DOCUMENT ME!
236      */
237     public void defaultButton_actionPerformed(ActionEvent e)
238     {
239         Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
240         Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
241         Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
242     }
243 }