Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. package rene.gui;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Frame;
  6. import java.awt.SystemColor;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.InputStream;
  10. import java.util.Enumeration;
  11. import java.util.Locale;
  12. import java.util.Properties;
  13. import java.util.ResourceBundle;
  14. import java.util.StringTokenizer;
  15.  
  16. import rene.dialogs.Warning;
  17. import rene.util.FileName;
  18. import rene.util.parser.StringParser;
  19.  
  20. /**
  21. The Global class.
  22. <p>
  23. This class will load a resource bundle with local support. It will
  24. set various things from this resource file.
  25. */
  26.  
  27. public class Global
  28. {  
  29.         // Fonts:
  30.         static public Font NormalFont=null,FixedFont=null,BoldFont=null;
  31.  
  32.         static public void makeFonts ()
  33.         {       NormalFont=createfont(
  34.                         "normalfont","SansSerif",12,false);
  35.                 FixedFont=createfont(
  36.                         "fixedfont","Monospaced",12,false);
  37.                 BoldFont=createfont(
  38.                         "fixedfont","Monospaced",12,true);
  39.         }      
  40.        
  41.         static
  42.         {       makeFonts();
  43.         }
  44.        
  45.         static public Font createfont (String name, String def, int defsize,
  46.                 boolean bold)
  47.         {       String fontname=getParameter(name+".name",def);
  48.                 String mode=getParameter(name+".mode","plain");
  49.             if (bold || mode.equals("bold"))
  50.             {   return new Font(fontname,
  51.                         Font.BOLD,getParameter(name+".size",defsize));
  52.             }
  53.             else if (mode.equals("italic"))
  54.             {   return new Font(fontname,
  55.                         Font.ITALIC,getParameter(name+".size",defsize));
  56.             }
  57.             else
  58.             {   return new Font(fontname,
  59.                         Font.PLAIN,Global.getParameter(name+".size",defsize));
  60.             }      
  61.         }
  62.        
  63.         static public Color Background=null,ControlBackground=null;
  64.        
  65.         static
  66.         {       makeColors();
  67.         }
  68.        
  69.         static public void makeColors ()
  70.         {       if (haveParameter("color.background"))
  71.                         Background=getParameter("color.background",Color.gray.brighter());
  72.                 else Background=SystemColor.window;
  73.                 if (haveParameter("color.control"))
  74.                         ControlBackground=getParameter("color.control",Color.gray.brighter());
  75.                 else ControlBackground=SystemColor.control;
  76.         }
  77.        
  78.         // Resources:
  79.         static protected ResourceBundle B;
  80.        
  81.         public static Enumeration names ()
  82.         {       if (B!=null) return B.getKeys();
  83.                 else return null;
  84.         }
  85.         public static String name (String tag, String def)
  86.         {       String s;
  87.                 if (B==null) return def;
  88.                 try
  89.                 {       s=B.getString(tag);
  90.                 }
  91.                 catch (Exception e)
  92.                 {       s=def;
  93.                 }
  94.                 return s;
  95.         }
  96.         public static String name (String tag)
  97.         {       return name(tag,tag.substring(tag.lastIndexOf(".")+1));
  98.         }
  99.         public static void initBundle (String file, boolean language)
  100.         {       try
  101.                 {       B=ResourceBundle.getBundle(file);
  102.                         String lang=getParameter("language","");
  103.                         if (language && !lang.equals("") && !lang.equals("default"))
  104.                         {       String langsec="";
  105.                                 if (lang.length()>3 && lang.charAt(2)=='_')
  106.                                 {       langsec=lang.substring(3);
  107.                                         lang=lang.substring(0,2);
  108.                                 }
  109.                                 Locale.setDefault(new Locale(lang,langsec));
  110.                                 initBundle(file,false);
  111.                         }
  112.                 }
  113.                 catch (RuntimeException e)
  114.                 {       B=null;
  115.                 }
  116.         }
  117.         public static void initBundle(String file)
  118.         {       initBundle(file,false);
  119.         }
  120.        
  121.         // Properties:
  122.         static Properties P=new Properties();
  123.         static String ConfigName;
  124.        
  125.         public static synchronized Enumeration properties () { return P.keys(); }
  126.         public static synchronized void loadProperties (InputStream in)
  127.         {       try
  128.                 {       P=new Properties();
  129.                         P.load(in);
  130.                         in.close();
  131.                 }
  132.                 catch (Exception e)
  133.                 {       P=new Properties();
  134.                 }
  135.         }
  136.         public static synchronized boolean loadPropertiesFromResource (String filename)
  137.         {       try
  138.                 {       Object G=new Object();
  139.                         InputStream in=G.getClass().getResourceAsStream(filename);
  140.                         P=new Properties();
  141.                         P.load(in);
  142.                         in.close();
  143.                 }
  144.                 catch (Exception e)
  145.                 {       P=new Properties();
  146.                         return false;
  147.                 }
  148.                 ConfigName=filename;
  149.                 return true;
  150.         }
  151.         public static synchronized boolean loadProperties (String filename)
  152.         {       ConfigName=filename;
  153.                 try
  154.                 {       FileInputStream in=new FileInputStream(filename);
  155.                         P=new Properties();
  156.                         P.load(in);
  157.                         in.close();
  158.                 }
  159.                 catch (Exception e)
  160.                 {       P=new Properties();
  161.                         return false;
  162.                 }
  163.                 return true;
  164.         }
  165.         public static synchronized void loadProperties (String dir, String filename)
  166.         {       try
  167.                 {       Properties p=System.getProperties();
  168.                         ConfigName=dir+
  169.                                 p.getProperty("file.separator")+filename;
  170.                         loadProperties(ConfigName);
  171.                 }
  172.                 catch (Exception e)
  173.                 {       P=new Properties();
  174.                 }
  175.         }
  176.         public static synchronized void loadPropertiesInHome (String filename)
  177.         {       try
  178.                 {       Properties p=System.getProperties();
  179.                         loadProperties(p.getProperty("user.home"),filename);
  180.                 }
  181.                 catch (Exception e)
  182.                 {       P=new Properties();
  183.                 }
  184.         }
  185.         public static synchronized void clearProperties ()
  186.         {       P=new Properties();
  187.         }
  188.         public static synchronized void saveProperties (String text)
  189.         {       try
  190.                 {       FileOutputStream out=new FileOutputStream(ConfigName);
  191.                         P.save(out,text);
  192.                         out.close();
  193.                 }
  194.                 catch (Exception e) {}
  195.         }
  196.         public static void saveProperties (String text, String filename)
  197.         {       ConfigName=filename;
  198.                 saveProperties(text);
  199.         }
  200.         public static synchronized void setParameter (String key, boolean value)
  201.         {       if (P==null) return;
  202.                 if (value) P.put(key,"true");
  203.                 else P.put(key,"false");
  204.         }
  205.         public static synchronized boolean getParameter (String key, boolean def)
  206.         {       try
  207.                 {       String s=P.getProperty(key);
  208.                         if (s.equals("true")) return true;
  209.                         else if (s.equals("false")) return false;
  210.                         return def;
  211.                 }
  212.                 catch (Exception e)
  213.                 {       return def;
  214.                 }
  215.         }
  216.         public static synchronized String getParameter (String key, String def)
  217.         {       String res=def;
  218.                 try
  219.                 {       res=P.getProperty(key);
  220.                 }
  221.                 catch (Exception e) {}
  222.                 if (res!=null)
  223.                 {       if (res.startsWith("$")) res=res.substring(1);
  224.                         return res;
  225.                 }
  226.                 else return def;
  227.         }
  228.         public static synchronized void setParameter (String key, String value)
  229.         {       if (P==null) return;
  230.                 if (value.length()>0 && Character.isSpaceChar(value.charAt(0)))
  231.                         value="$"+value;
  232.                 P.put(key,value);
  233.         }
  234.         public static synchronized int getParameter (String key, int def)
  235.         {       try
  236.                 {       return Integer.parseInt(getParameter(key,""));
  237.                 }
  238.                 catch (Exception e)
  239.                 {       try
  240.                         {       double x=new Double(getParameter(key,"")).doubleValue();
  241.                                 return (int)x;
  242.                         }
  243.                         catch (Exception ex) {}
  244.                         return def;
  245.                 }
  246.         }
  247.         public static synchronized void setParameter (String key, int value)
  248.         {       setParameter(key,""+value);
  249.         }
  250.         public static synchronized double getParameter (String key, double def)
  251.         {       try
  252.                 {       return new Double(getParameter(key,"")).doubleValue();
  253.                 }
  254.                 catch (Exception e)
  255.                 {       return def;
  256.                 }
  257.         }
  258.         public static synchronized void setParameter (String key, double value)
  259.         {       setParameter(key,""+value);
  260.         }
  261.         static public synchronized Color getParameter (String key, Color c)
  262.         {       String s=getParameter(key,"");
  263.                 if (s.equals("")) return c;
  264.                 StringParser p=new StringParser(s);
  265.                 p.replace(',',' ');
  266.                 int red,green,blue;
  267.                 red=p.parseint(); green=p.parseint(); blue=p.parseint();
  268.                 try
  269.                 {       return new Color(red,green,blue);
  270.                 }
  271.                 catch (RuntimeException e)
  272.                 {       return c;
  273.                 }
  274.         }
  275.         static public synchronized Color getParameter (String key, int red, int green, int blue)
  276.         {       String s=getParameter(key,"");
  277.                 if (s.equals("")) return new Color(red,green,blue);
  278.                 StringParser p=new StringParser(s);
  279.                 p.replace(',',' ');
  280.                 red=p.parseint(); green=p.parseint(); blue=p.parseint();
  281.                 try
  282.                 {       return new Color(red,green,blue);
  283.                 }
  284.                 catch (RuntimeException e)
  285.                 {       return Color.black;
  286.                 }
  287.         }
  288.         public static synchronized void setParameter (String key, Color c)
  289.         {       setParameter(key,""+c.getRed()+","+c.getGreen()+","+c.getBlue());
  290.         }
  291.         /**
  292.         Remove a specific Paramater.
  293.         */
  294.         public static synchronized void removeParameter (String key)
  295.         {       P.remove((Object)key);
  296.         }
  297.         /**
  298.         Remove all Parameters that start with the string.
  299.         */
  300.         public static synchronized void removeAllParameters (String start)
  301.         {       Enumeration e=P.keys();
  302.                 while (e.hasMoreElements())
  303.                 {       String key=(String)e.nextElement();
  304.                         if (key.startsWith(start))
  305.                         {       P.remove((Object)key);
  306.                         }
  307.                 }      
  308.         }
  309.         /**
  310.          * Set default values for parameters
  311.          * resetDefaults("default.") is the same as:
  312.          * setParameter("xxx",getParameter("default.xxx",""));
  313.          * if "default.xxx" has a value.
  314.          * @param defaults
  315.          */
  316.         public static synchronized void resetDefaults (String defaults)
  317.         {       Enumeration e=P.keys();
  318.                 while (e.hasMoreElements())
  319.                 {       String key=(String)e.nextElement();
  320.                         if (key.startsWith(defaults))
  321.                         {       setParameter(key.substring(defaults.length()),
  322.                                         getParameter(key,""));
  323.                         }
  324.                 }      
  325.         }
  326.         public static void resetDefaults ()
  327.         {       resetDefaults("default.");
  328.         }
  329.         /**
  330.         @return if I have such a parameter.
  331.         */
  332.         public static synchronized boolean haveParameter (String key)
  333.         {       try
  334.                 {       String res=P.getProperty(key);
  335.                         if (res==null) return false;
  336.                 }
  337.                 catch (Exception e) { return false; }
  338.                 return true;
  339.         }
  340.        
  341.         // Warnings
  342.        
  343.         static Frame F=null;
  344.         public static void warning (String s)
  345.         {       if (F==null)
  346.                 {       F=new Frame();
  347.                 }
  348.                 Warning W=new Warning(F,s,name("warning"),false);
  349.                 W.center();
  350.                 W.setVisible(true);
  351.         }
  352.        
  353.         // Clipboard for applets
  354.         static public String AppletClipboard=null;
  355.        
  356.         static public boolean IsApplet=false;
  357.         static public void setApplet (boolean flag) { IsApplet=flag; }
  358.         static public boolean isApplet () { return IsApplet; }
  359.        
  360.         // Java Version
  361.        
  362.         public static double getJavaVersion ()
  363.         {       String version=System.getProperty("java.version");
  364.                 if (version==null) return 0.0;
  365.                 double v=0.0;
  366.                 StringTokenizer t=new StringTokenizer(version,".");
  367.                 if (t.hasMoreTokens())
  368.                 {       v=convert(t.nextToken());
  369.                 }
  370.                 else return v;
  371.                 if (t.hasMoreTokens())
  372.                 {       v=v+convert(t.nextToken())/10;
  373.                 }
  374.                 else return v;
  375.                 if (t.hasMoreTokens())
  376.                 {       v=v+convert(t.nextToken())/100;
  377.                 }
  378.                 return v;
  379.         }
  380.        
  381.         public static double convert (String s)
  382.         {       try
  383.                 {       return Integer.parseInt(s);
  384.                 }
  385.                 catch (Exception e)
  386.                 {       return 0;
  387.                 }
  388.         }
  389.        
  390.         public static synchronized String getUserDir ()
  391.         {       String dir=System.getProperty("user.dir");
  392.                 return FileName.canonical(dir);
  393.         }
  394.        
  395.         public static Object ExitBlock=new Object();
  396.        
  397.         public static synchronized void exit (int i)
  398.         {       synchronized (ExitBlock)
  399.                 {       System.exit(i);
  400.                 }
  401.         }
  402.        
  403.         public static void main (String args[])
  404.         {       try
  405.                 {       System.out.println(new Color(4,5,600));
  406.                 }
  407.                 catch (RuntimeException e) {}
  408.         }
  409. }
  410.