Statistics
| Revision:

root / trunk / frameworks / _fwAndami / src / com / iver / andami / ui / fonts / FontUtils.java @ 9400

History | View | Annotate | Download (5.2 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.andami.ui.fonts;
42

    
43
import java.awt.Font;
44
import java.awt.GraphicsEnvironment;
45
import java.util.HashSet;
46
import java.util.Iterator;
47
import java.util.Locale;
48

    
49
import javax.swing.UIManager;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.andami.messages.Messages;
53

    
54
/**
55
 * Several methods to manage the user interface fonts.
56
 * 
57
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
58
 *
59
 */
60
public class FontUtils {
61
        
62
        /**
63
         * Changes the default UIManager font to the provided font.
64
         * 
65
         * @param fontName
66
         */
67
        public static void setFont(String fontName) {
68
                String[] fieldList = {
69
                                "Button.font",
70
                                "CheckBox.font",
71
                                "CheckBoxMenuItem.font",
72
                                "ColorChooser.font",
73
                                "ComboBox.font",
74
                                "DesktopIcon.font",
75
                                "EditorPane.font",
76
                                "FormattedTextField.font",
77
                                "Label.font",
78
                                "List.font",
79
                                "Menu.font",
80
                                "MenuBar.font",
81
                                "MenuItem.font",
82
                                "OptionPane.font",
83
                                "Panel.font",
84
                                "PasswordField.font",
85
                                "PopupMenu.font",
86
                                "ProgressBar.font",
87
                                "RadioButton.font",
88
                                "RadioButtonMenuItem.font",
89
                                "ScrollPane.font",
90
                                "Slider.font",
91
                                "Spinner.font",
92
                                "TabbedPane.font",
93
                                "Table.font",
94
                                "TableHeader.font",
95
                                "TextArea.font",
96
                                "TextField.font",
97
                                "TextPane.font",
98
                                "TitledBorder.font",
99
                                "ToggleButton.font",
100
                                "ToolBar.font",
101
                                "ToolTip.font",
102
                                "Tree.font",
103
                                "Viewport.font"
104
                } ;
105
                
106
                Font font;
107
                
108
                for (int i = fieldList.length-1; i>=0; i--) {
109
                        font = UIManager.getFont(fieldList[i]);
110
                        if (font!=null)
111
                                UIManager.put(fieldList[i], new Font(fontName, Font.PLAIN, font.getSize()));
112
                }
113
        }
114
        
115
        /**
116
         * Inits the UIManager fonts, so that it can display current's language
117
         * symbols. The font is only changed when necessary.
118
         */
119
        public static void initFonts() {
120
                // conservative behaviour: we just change the font, if the default one
121
                // can't correctly display the current language
122
        //        UIManager.getLookAndFeel().getDefaults()
123
                Font defaultFont = UIManager.getFont("Label.font");
124
                if (defaultFont==null) return;
125
                //PluginServices.getLogger().info("InitialDefaultFont: "+defaultFont.getName());
126
                if (defaultFont.canDisplayUpTo(Messages.getString("MDIFrame.quiere_salir"))==-1)
127
                                return;
128
                
129
                Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
130
                HashSet workingFonts = new HashSet();
131
                
132
                for (int i=allfonts.length-1; i>=0; i--) {
133
                        
134
                        if (allfonts[i].canDisplayUpTo(Messages.getString("MDIFrame.quiere_salir"))==-1) {
135
                                workingFonts.add(allfonts[i].getName());
136
                        }
137
                }
138
                
139
                // try to set the preferred font
140
                String[] preferredFonts = getPreferedFonts(Locale.getDefault().getLanguage());
141
                for (int i=0;i<preferredFonts.length; i++) {
142
                        if (workingFonts.contains(preferredFonts[i])){
143
                                setFont(preferredFonts[i]);
144
                                PluginServices.getLogger().info("FontSet: "+preferredFonts[i]);
145
                                return;
146
                        }
147
                }
148
                
149
                // try to set any working font
150
                Iterator iterator = workingFonts.iterator();
151
                
152
                if (iterator.hasNext()) {
153
                        String fontName = (String) iterator.next();
154
                        setFont(fontName);
155
                        PluginServices.getLogger().info("FontSet: "+fontName);        
156
                }
157
        }
158
        
159
        /**
160
         * Returns an ordered list of preferred fonts for the provided language.
161
         * As there is several fonts which can be used to display each language,
162
         * this list provides a hint about which is the more suitable font for
163
         * this language.
164
         * 
165
         * This method does not check whether the fonts are available in the
166
         * running system.
167
         * 
168
         * @param lang
169
         * @return
170
         */
171
        private static String [] getPreferedFonts(String lang) {
172
                //TODO this should be read from a file, for each language
173
                if (lang.equals("zh")) {
174
                        String []preferredChineseFonts = {
175
                                        "AR PL KaitiM GB",
176
                                        "AR PL SungtiL GB",
177
                                        "Kochi Gothic",
178
                                        "SimSun"
179
                        } ;
180
                        return preferredChineseFonts;
181
                }
182
                return new String[0];
183
        }
184
        
185
        private static void listSystemFonts() {
186
                Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
187

    
188
                for (int i=allfonts.length-1; i>=0; i--) {
189
                        System.out.println(allfonts[i].getName()+" --- "+allfonts[i].getFontName());
190
                }
191
        }
192
}