Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mitre/cvw/CVWFontController.java


1   /*
2    * Copyright (c) 1996-2000. The MITRE Corporation (http://www.mitre.org/).
3    * All rights reserved.
4    * CVW comes with ABSOLUTELY NO WARRANTY. See license for details.
5    */
6   package org.mitre.cvw;
7   
8   import java.util.Hashtable.*;
9   import javax.swing.*;
10  import java.awt.*;
11  import javax.swing.plaf.*;
12  import java.util.*;
13  import java.io.*;
14  import java.beans.*;
15  
16  /**
17   * The CVWFontController class provides a mechanism to 
18   * controls fonts in an application wide method.  Right now
19   * there are only component wide settings (i.e. all text areas).
20   * We want to add support for cvw specific settings (i.e. error boxes,
21   * text input, text output, etc) in the future.  This class is, for
22   * the most part, a proxy to the UIManager.  This could probably be 
23   * eliminated completely.
24   * @author Ramsay Key
25   * @version $Revision: 1.2 $
26   */
27  
28  public class CVWFontController {
29     
30      // There really four things we are concerned about with the 
31      // fonts. 1) a list of font family names 2) a list of available
32      // styles 3) a list of available sizes and 4) the available font
33      // UI names (i.e. Menu Font).  These are the variables that represent them.
34      
35      /**
36       * fontUINames are the font component User Interface names (i.e. TextArea, CVWError,
37       * etc.)  These are meant to 
38       */
39      private static String fontUINames[];
40  
41      /**
42       * fontFamilyNames is a list of the available font families
43       */
44      private static String fontFamilyNames[];
45  
46      /**
47       * fontSizes is a list of available font sizes
48       */
49      private static String fontSizes[] = {"4","8","10","12","16","20","24"};
50  
51      /**
52       * fontStyles is a list of available styles in a string format.
53       */
54      private static String fontStyles[] = {"PLAIN","BOLD","ITALIC","BOLD+ITALIC"};
55  
56      // this is just used to set defaults.  this can probably be removed.
57      //private static Hashtable cvwfonts = new Hashtable(38);
58  
59      private static Vector menuFonts = new Vector(6);
60      private static Vector menuAccelFonts = new Vector(6);
61      private static Vector textFonts = new Vector(6);
62      private static Vector compFonts = new Vector(6); 
63      private static Vector listFonts = new Vector(6); 
64      private static Vector btnFonts = new Vector(6);
65  
66      static {
67  
68    // * this constructs a large and expensive hash table.
69    // would like to get rid of this for speed.
70    // it is only constructed once though
71    // * this is also a registration like method...only fonts
72    // specified here can be examined/updated/etc.  fonts not 
73    // in this list are ignored.
74    
75    // we could leave out all of these as these are the listnager defaults
76    // however, this is a static place to set the defaults
77      
78  
79  /*  old way
80     cvwfonts.put("CheckBox.font",new Font("Dialog",Font.PLAIN,12));
81    cvwfonts.put("PopupMenu.font",new Font("Dialog",Font.PLAIN,12));
82          cvwfonts.put("TextPane.font",new Font("Serif",Font.PLAIN,12));
83    cvwfonts.put("MenuItem.font",new Font("Dialog",Font.PLAIN,12));
84    cvwfonts.put("ComboBox.font",new Font("Dialog",Font.PLAIN,12));
85    cvwfonts.put("Button.font",new Font("Dialog",Font.PLAIN,12));
86    cvwfonts.put("Tree.font",new Font("Dialog",Font.PLAIN,12));
87    cvwfonts.put("ScrollPane.font",new Font("Dialog",Font.PLAIN,12));
88    cvwfonts.put("TabbedPane.font",new Font("Dialog",Font.PLAIN,12));
89    cvwfonts.put("EditorPane.font",new Font("Serif",Font.PLAIN,12));
90    cvwfonts.put("TitledBorder.font",new Font("Dialog",Font.PLAIN,12));
91    cvwfonts.put("Menu.font",new Font("Dialog",Font.PLAIN,12));
92    cvwfonts.put("CheckBoxMenuItem.font",new Font("Dialog",Font.PLAIN,12));
93    cvwfonts.put("RadioButtonMenuItem.acceleratorFont",new Font("Dialog",Font.PLAIN,10));
94    cvwfonts.put("TextArea.font",new Font("Monospaced",Font.PLAIN,12));
95    cvwfonts.put("OptionPane.font",new Font("Dialog",Font.PLAIN,12));
96    cvwfonts.put("DesktopIcon.font",new Font("Dialog",Font.BOLD,12));
97    cvwfonts.put("MenuBar.font",new Font("Dialog",Font.PLAIN,12));
98    cvwfonts.put("ToolBar.font",new Font("Dialog",Font.PLAIN,12));
99    cvwfonts.put("RadioButton.font",new Font("Dialog",Font.BOLD,12));
100   cvwfonts.put("CheckBoxMenuItem.acceleratorFont",new Font("Dialog",Font.PLAIN,10));
101   cvwfonts.put("ToggleButton.font",new Font("Dialog",Font.PLAIN,12));
102   cvwfonts.put("ToolTip.font",new Font("Sansserif",Font.PLAIN,12));
103   cvwfonts.put("ProgressBar.font",new Font("Dialog",Font.PLAIN,12));
104   cvwfonts.put("InternalFrame.titleFont",new Font("Dialog",Font.BOLD,12));
105   cvwfonts.put("TableHeader.font",new Font("Dialog",Font.PLAIN,12));
106   cvwfonts.put("Panel.font",new Font("Dialog",Font.PLAIN,12));
107   cvwfonts.put("MenuItem.acceleratorFont", new Font("Dialog",Font.PLAIN,10));
108   cvwfonts.put("Menu.acceleratorFont",new Font("Dialog",Font.PLAIN,10));
109   cvwfonts.put("List.font",new Font("Dialog",Font.PLAIN,12));
110   cvwfonts.put("ColorChooser.font",new Font("Dialog",Font.PLAIN,12));
111   cvwfonts.put("PasswordField.font",new Font("Monospaced",Font.PLAIN,12));
112   cvwfonts.put("InternalFrame.font",new Font("Dialog",Font.PLAIN,12));
113   cvwfonts.put("TextField.font",new Font("Sansserif",Font.PLAIN,12));
114   cvwfonts.put("Table.font",new Font("Dialog",Font.PLAIN,12));
115   cvwfonts.put("Label.font",new Font("Dialog",Font.PLAIN,12));
116   cvwfonts.put("Viewport.font",new Font("Dialog",Font.PLAIN,12));
117   cvwfonts.put("RadioButtonMenuItem.font",new Font("Dialog",Font.PLAIN,12));
118   
119   //TO ADD SOMETHING HERE USE SOMETHING LIKE
120   //cvwfonts.put("CVWRadioButton",new Font("Dialog",Font.BOLD,12))
121 */
122 
123 //hard coded and bad!!!
124 
125   textFonts.addElement("TextPane.font");
126   textFonts.addElement("EditorPane.font");
127   textFonts.addElement("TextArea.font");
128   textFonts.addElement("TextField.font");
129   textFonts.addElement("PasswordField.font");
130   textFonts.addElement("ToolTip.font");
131 
132   menuFonts.addElement("Menu.font");
133   menuFonts.addElement("PopupMenu.font");
134   menuFonts.addElement("MenuItem.font");
135   menuFonts.addElement("RadioButtonMenuItem.font");
136   menuFonts.addElement("CheckBoxMenuItem.font");
137   menuFonts.addElement("MenuBar.font");
138 
139   menuAccelFonts.addElement("RadioButtonMenuItem.acceleratorFont");
140   menuAccelFonts.addElement("CheckBoxMenuItem.acceleratorFont");
141   menuAccelFonts.addElement("MenuItem.acceleratorFont");
142   menuAccelFonts.addElement("Menu.acceleratorFont");
143 
144 //not sure if checkbox/radio is button or label
145   btnFonts.addElement("CheckBox.font");
146   btnFonts.addElement("RadioButton.font");
147   btnFonts.addElement("Button.font");
148   btnFonts.addElement("ToggleButton.font");
149 
150   compFonts.addElement("Tree.font");
151   compFonts.addElement("ScrollPane.font");
152   compFonts.addElement("TabbedPane.font");
153   compFonts.addElement("TitledBorder.font");
154   compFonts.addElement("OptionPane.font");
155   compFonts.addElement("DesktopIcon.font");
156   compFonts.addElement("ToolBar.font");
157   compFonts.addElement("ProgressBar.font");
158   compFonts.addElement("InternalFrame.titleFont");
159   compFonts.addElement("Panel.font");
160   compFonts.addElement("ColorChooser.font");
161   compFonts.addElement("InternalFrame.font");
162   compFonts.addElement("Label.font");
163   compFonts.addElement("Viewport.font");
164   compFonts.addElement("ComboBox.font");
165 
166   //not sure where these go-- maybe text
167   listFonts.addElement("Table.font");
168   listFonts.addElement("List.font");
169   listFonts.addElement("TableHeader.font");
170 
171   // now we setup our own lists
172 
173   /* setup the fontFamilyNames */
174   fontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
175      
176   /* setup the fontUINames */
177    UIDefaults uiDefaults = UIManager.getDefaults();
178   Enumeration uiEnum = uiDefaults.keys();
179   // this next line wastes a little memory
180   Vector fontVector = new Vector(uiDefaults.size());
181 
182   while(uiEnum.hasMoreElements()){
183       Object key = uiEnum.nextElement();
184       /* we call this just to check that it is a font */
185       if(uiDefaults.getFont(key)!=null){
186     fontVector.addElement((String)key.toString());
187       }  
188   }     
189   fontUINames = new String[fontVector.size()];
190 
191   fontVector.copyInto(fontUINames);
192 
193   /* now we set our defaults */
194 /*
195   int index;
196     for(index = 0; index < fontUINames.length; index++){
197       Font tmp;
198       if((tmp = (Font)cvwfonts.get((Object)fontUINames[index]))!=null){
199     UIManager.put(fontUINames[index],tmp);
200       }
201   }   
202 */
203     }
204 
205     /**
206      * addFontPrefs adds the fonts specified in fontFilename to UIManager as defaults.
207      * @param fontFilename is the name of a font specifying fonts for the application.
208      */
209 
210     public static void addFontPrefs(String fontFilename) throws IOException, FileNotFoundException {
211 
212   // * this is the gimmic...we require the fontFilename
213   // to be in a properties file format.  We can then read it into
214   // the system properties and use getFont on class Font to lookup
215   // the font name
216   // this probably isn't the best idea, but then we can use Font.getFont 
217   // which makes life a little easier 
218   // * the alternative is to enumerate a properties object created from the fontFilename
219   // but then you have to parse the font line yourself
220 
221   Properties newProps = new Properties();
222   //Properties newProps = new Properties(System.getProperties());
223 
224     try {
225   newProps.load(new BufferedInputStream(new FileInputStream(fontFilename)));
226     } catch (Exception e){
227         //System.out.println("Couldn't find " + fontFilename);
228     }
229 
230 //only set the text font to sansserif if the cvwprefsfile is not found
231 // all other fonts are based on system defaults.
232    String font = newProps.getProperty("Text");
233   Font tf = new Font("sansserif", Font.PLAIN, 12);//the standard cvw font
234   if (font != null) 
235     tf = Font.decode(font);
236   for (int i=0; i< textFonts.size(); i++) {
237       UIManager.put(textFonts.elementAt(i),tf);
238    }
239 
240    font = newProps.getProperty("Menu");
241   if (font != null) {
242     Font f = Font.decode(font);
243     for (int i=0; i< menuFonts.size(); i++) {
244       UIManager.put(menuFonts.elementAt(i),f);
245      }
246     float s = f.getSize2D();
247     s = s-2;
248     f.deriveFont(s);
249     for (int i=0; i< menuAccelFonts.size(); i++) {
250       UIManager.put(menuAccelFonts.elementAt(i),f);
251      }
252    }
253 
254    font = newProps.getProperty("List");
255   if (font != null) {
256     Font f = Font.decode(font);
257     for (int i=0; i< listFonts.size(); i++) {
258       UIManager.put(listFonts.elementAt(i),f);
259      }
260   }
261       
262    font = newProps.getProperty("Button");
263   if (font != null) {
264     Font f = Font.decode(font);
265     for (int i=0; i< btnFonts.size(); i++) {
266       UIManager.put(btnFonts.elementAt(i),f);
267      }
268   }
269       
270    font = newProps.getProperty("Comp");
271   if (font != null) {
272     Font f = Font.decode(font);
273     for (int i=0; i< compFonts.size(); i++) {
274       UIManager.put(compFonts.elementAt(i),f);
275      }
276   }
277 
278 /*  debug printlns
279     try {
280     System.err.println("***************try to reset ui manager defaults");
281     UIDefaults ui = UIManager.getDefaults();
282     System.err.println(ui.size());
283     int i=0;
284     if (ui != null) {
285   for (Enumeration e = ui.keys() ; e.hasMoreElements() ;) {
286       Object k = e.nextElement();
287       String key = k.toString();
288       Object v = ui.get(k);
289       String val = v.toString();
290       //if (val.length() > 40) {
291                 //val = val.substring(0, 37) + "...";
292       //}
293       if ((val.indexOf("Font") >=  0) && (val.indexOf("Text") >= 0))
294         System.err.println(key + "=" + val);
295   }
296      }
297     System.err.println("***************done to reset ui manager defaults");
298     } catch (Exception e) {
299       System.err.println("Couldn't set system look and feel: " + e);
300     }
301  
302  */
303 
304 /* -old way
305   System.setProperties(newProps);
306   
307   int index;
308   for(index = 0; index < fontUINames.length; index++){
309       UIManager.put(fontUINames[index],Font.getFont(fontUINames[index],(Font)cvwfonts.get((Object)fontUINames[index])));  
310   }
311     */
312     }
313 
314     public static Font deriveBoldFont(JComponent comp) {
315         Font f = comp.getFont();
316   f = new Font(f.getFontName(), Font.BOLD, f.getSize());
317   return f;
318      }
319 
320     /**
321      * getFontFamilyNames returns a list of the available font families.
322      * @return a String array of the available font family names.
323      */
324     public static String[] getFontFamilyNames(){
325   return(fontFamilyNames);
326     }
327     
328     /**
329      * getFontSizes returns a list of available font sizes.
330      * This would be called in GUI methods (i.e. set/get fonts).
331      * @return a String array of the available font fizes.
332      */
333     public static String[] getFontSizes(){
334   return(fontSizes);
335     }
336 
337     /**
338      * getFontStyles returns a list of available styles.
339      * This would be called in GUI methods (i.e. set/get fonts).
340      * @return a String array of the available font styles
341      */
342     public static String[] getFontStyles(){  
343   return(fontStyles);
344     }
345 
346     /**
347      * getFontUINames returns a user-friendly list of font UIs
348      */
349     public static String[] getFontUINames(){
350   return (fontUINames);
351     }
352 
353     /**
354      * getStyleByString is used to lookup the integer associated with
355      * the string stylename
356      * @param stylename is the name of the style to lookup an integer
357      * for.
358      * @return returns the corresponding integer for stylename.
359      */
360     private static int getStyleByString(String stylename){
361   if(stylename.equalsIgnoreCase("BOLD")){
362       return(Font.BOLD);
363   } else if(stylename.equalsIgnoreCase("ITALIC")){
364       return(Font.ITALIC);
365   } else if(stylename.equalsIgnoreCase("BOLD+ITALIC")){
366       return(Font.ITALIC | Font.BOLD);
367   } else {
368       return(Font.PLAIN);
369   }  
370     }
371 
372     /**
373      * getStyleByInt is used to lookup the String associated with
374      * the integer styleint.
375      * @param styleint is an integer that should correspond to a style.
376      * @return a string that corresponds to the integer specified by
377      * styleint.
378      */
379     private static String getStyleByInt(int styleint){
380   if(styleint == Font.BOLD){
381       return ("BOLD");
382   } else if(styleint == Font.ITALIC){
383       return ("ITALIC");
384   } else if(styleint == (Font.BOLD | Font.ITALIC)){
385       return ("BOLD+ITALIC");
386   } else {
387       return ("PLAIN");
388   }
389     }
390 
391     /**
392      * setFontByName associates the fontName with font.
393      */
394     public static Object setFontByName(String fontUIName, String fontFamilyName, String style, String size){
395   return UIManager.put((Object)fontUIName,new FontUIResource(fontFamilyName.toLowerCase(),getStyleByString(style),new Integer(size).intValue()));
396 
397     }
398 
399     /**
400      * getFontByName returns the Font used by the fontUIName
401      */
402     public static String getSizeByFontName(String fontUIName){
403   Integer fontSize = new Integer(UIManager.getFont(fontUIName).getSize());
404   return (fontSize.toString());
405     }
406 
407     /**
408      * getStyleByFontName returns a String that is a descriptive style name
409      * of the font object.
410      */
411     public static String getStyleByFontName(String fontUIName){
412   int int_style = UIManager.getFont(fontUIName).getSize();
413   return getStyleByInt(int_style);
414     }
415 
416    
417     public static String getFamilyByFontName(String fontUIName){
418   String familyName = UIManager.getFont(fontUIName).getFamily();
419 
420   // we need to do this next little bit b/c the UIManager sometimes
421   // returns stuff in lowercase
422         int index;
423         for(index = 0; index<fontFamilyNames.length; index++){
424             if(familyName.compareToIgnoreCase(fontFamilyNames[index])==0){
425                 familyName = fontFamilyNames[index];
426                 break;
427             }
428         }
429 
430         if(index == fontFamilyNames.length)
431             familyName = null;
432         return(familyName);
433 
434     }
435 }