Merge branch 'Jalview-JS/jim/JAL-3253-JAL-3418' into Jalview-JS/JAL-3253-applet
[jalview.git] / srcjar / fr / orsay / lri / varna / views / VueFont.java
1 /*
2  VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3  Copyright (C) 2008  Kevin Darty, Alain Denise and Yann Ponty.
4  electronic mail : Yann.Ponty@lri.fr
5  paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
6
7  This file is part of VARNA version 3.1.
8  VARNA version 3.1 is free software: you can redistribute it and/or 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  VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16  If not, see http://www.gnu.org/licenses.
17  */
18 package fr.orsay.lri.varna.views;
19
20 import java.awt.Font;
21 import java.awt.GraphicsEnvironment;
22
23 import javax.swing.JComboBox;
24 import javax.swing.JPanel;
25 import javax.swing.JSlider;
26
27 import fr.orsay.lri.varna.VARNAPanel;
28
29 public class VueFont {
30
31         private VARNAPanel _vp;
32         private Font font;
33         private JComboBox stylesBox;
34         private JComboBox boxPolice;
35         private JPanel panel;
36         private JSlider sizeSlider;
37
38         public VueFont(VARNAPanel vp) {
39                 _vp = vp;
40                 init();
41                 buildViewVPTitle();
42         }
43
44         public VueFont(Font f) {
45                 font = f;
46                 init();
47                 buildViewFont();
48         }
49
50         private void init() {
51                 GraphicsEnvironment ge = GraphicsEnvironment
52                                 .getLocalGraphicsEnvironment();
53                 String[] polices = ge.getAvailableFontFamilyNames();
54                 boxPolice = new JComboBox(polices);
55
56                 sizeSlider = new JSlider(JSlider.HORIZONTAL, 4, 88, 14);
57                 // Turn on labels at major tick marks.
58                 sizeSlider.setMajorTickSpacing(10);
59                 sizeSlider.setMinorTickSpacing(5);
60                 sizeSlider.setPaintTicks(true);
61                 sizeSlider.setPaintLabels(true);
62
63                 String[] styles = { "Plain", "Italic", "Bold" };
64                 stylesBox = new JComboBox(styles);
65
66                 panel = new JPanel();
67                 panel.add(boxPolice);
68                 panel.add(sizeSlider);
69                 panel.add(stylesBox);
70         }
71
72         private void buildViewFont() {
73                 boxPolice.setSelectedItem(font.getFamily());
74                 sizeSlider.setValue(font.getSize());
75                 stylesBox.setSelectedItem(styleIntToString(font.getStyle()));
76         }
77
78         private void buildViewVPTitle() {
79                 boxPolice.setSelectedItem(_vp.getTitleFont().getFamily());
80                 sizeSlider.setValue(_vp.getTitleFont().getSize());
81                 stylesBox.setSelectedItem(styleIntToString(_vp.getTitleFont()
82                                 .getStyle()));
83         }
84
85         public String styleIntToString(int styleInt) {
86                 switch (styleInt) {
87                 case Font.PLAIN:// Plain
88                         return "Plain";
89                 case Font.ITALIC:// Italic
90                         return "Italic";
91                 case Font.BOLD:// Bold
92                         return "Bold";
93                 default:// Plain
94                         return "Plain";
95                 }
96         }
97
98         public JComboBox getStylesBox() {
99                 return stylesBox;
100         }
101
102         public JComboBox getBoxPolice() {
103                 return boxPolice;
104         }
105
106         public JPanel getPanel() {
107                 return panel;
108         }
109
110         public JSlider getSizeSlider() {
111                 return sizeSlider;
112         }
113
114         public Font getFont() {
115                 int style;
116                 switch (getStylesBox().getSelectedIndex()) {
117                 case 0:// Plain
118                         style = Font.PLAIN;
119                         break;
120                 case 1:// Italic
121                         style = Font.ITALIC;
122                         break;
123                 case 2:// Bold
124                         style = Font.BOLD;
125                         break;
126                 default:// Plain
127                         style = Font.PLAIN;
128                         break;
129                 }
130                 return new Font((String) getBoxPolice().getSelectedItem(), style,
131                                 getSizeSlider().getValue());
132         }
133 }