001 package de.mospace.swing;
002
003 import java.awt.Font;
004 import java.awt.GraphicsEnvironment;
005 import java.text.MessageFormat;
006 import java.util.Collection;
007 import java.util.Collections;
008 import java.util.ResourceBundle;
009
010 /** Provides static constants for the application. */
011 public final class GLOBALS{
012 private static ResourceBundle bundle = ResourceBundle.getBundle("de.mospace.swing.DefaultResourceBundle");
013 private static Collection bundleKeys = Collections.list(bundle.getKeys());
014
015 private GLOBALS(){
016 }
017
018 public static String getMessage(String key, Object arg){
019 return MessageFormat.format(GLOBALS.getString(key), new Object[]{arg});
020 }
021
022 public static String getMessage(String key, Object[] args){
023 return MessageFormat.format(GLOBALS.getString(key), args);
024 }
025
026 public static void setResourceBundle(ResourceBundle rb){
027 bundle = rb;
028 bundleKeys = Collections.list(rb.getKeys());
029 }
030
031 public static Object get(String s){
032 return bundleKeys.contains(s)? bundle.getString(s) : s;
033 }
034
035 public static String getString(String s){
036 return (String) get(s);
037 }
038
039 public static int getKey(String s){
040 return (int) getString(s).charAt(0);
041 }
042
043 public static Font getSuitableFont(Font f, char[] characters){
044 /* try the provided font */
045 if(canDisplay(f, characters)){
046 return f;
047 }
048
049 /* then try Lucida Sans Regular which is
050 bundled with Sun JREs since 1.4 */
051 Font myFont = new Font("Lucida Sans Regular",
052 Font.PLAIN, f.getSize());
053
054 /* test if the font we got (if Lucida Sans Regular
055 is not installed it will be another font) can
056 really display the characters */
057 if(!canDisplay(myFont, characters)){
058
059 /* if not we try all available fonts
060 one after the other */
061 Font[] ff = GraphicsEnvironment.
062 getLocalGraphicsEnvironment().getAllFonts();
063
064 int i = 0;
065 for(; i<ff.length; i++){
066
067 /* if we have found a suitable font
068 we use it. */
069 if(canDisplay(ff[i], characters)){
070 myFont = ff[i].deriveFont(f.getSize());
071 }
072 }
073
074 /* if we have not found a suitable font
075 we use the default font */
076 if(i == ff.length){
077 myFont = f;
078 }
079 }
080 return myFont;
081 }
082
083 private static boolean canDisplay(Font f, char[] characters){
084 boolean result = true;
085 for(int i=0; i<characters.length; i++){
086 if(!f.canDisplay(characters[i])){
087 result = false;
088 break;
089 }
090 }
091 return result;
092 }
093 }