Back to index

wims  3.65+svn20090927
Global.java
Go to the documentation of this file.
00001 package rene.gui;
00002 
00003 import java.awt.Color;
00004 import java.awt.Font;
00005 import java.awt.Frame;
00006 import java.awt.SystemColor;
00007 import java.io.FileInputStream;
00008 import java.io.FileOutputStream;
00009 import java.io.InputStream;
00010 import java.util.Enumeration;
00011 import java.util.Locale;
00012 import java.util.Properties;
00013 import java.util.ResourceBundle;
00014 import java.util.StringTokenizer;
00015 
00016 import rene.dialogs.Warning;
00017 import rene.util.FileName;
00018 import rene.util.parser.StringParser;
00019 
00027 public class Global
00028 {   
00029        // Fonts:
00030        static public Font NormalFont=null,FixedFont=null,BoldFont=null;
00031 
00032        static public void makeFonts ()
00033        {      NormalFont=createfont(
00034                      "normalfont","SansSerif",12,false);
00035               FixedFont=createfont(
00036                      "fixedfont","Monospaced",12,false);
00037               BoldFont=createfont(
00038                      "fixedfont","Monospaced",12,true);
00039        }      
00040        
00041        static
00042        {      makeFonts();
00043        }
00044        
00045        static public Font createfont (String name, String def, int defsize,
00046               boolean bold)
00047        {      String fontname=getParameter(name+".name",def);
00048               String mode=getParameter(name+".mode","plain");
00049            if (bold || mode.equals("bold"))
00050            {   return new Font(fontname,
00051                      Font.BOLD,getParameter(name+".size",defsize));
00052            }
00053            else if (mode.equals("italic"))
00054            {   return new Font(fontname,
00055                      Font.ITALIC,getParameter(name+".size",defsize));
00056            }
00057            else
00058            {   return new Font(fontname,
00059                      Font.PLAIN,Global.getParameter(name+".size",defsize));
00060            }      
00061        }
00062        
00063        static public Color Background=null,ControlBackground=null;
00064        
00065        static
00066        {      makeColors();
00067        }
00068        
00069        static public void makeColors ()
00070        {      if (haveParameter("color.background"))
00071                      Background=getParameter("color.background",Color.gray.brighter());
00072               else Background=SystemColor.window;
00073               if (haveParameter("color.control"))
00074                      ControlBackground=getParameter("color.control",Color.gray.brighter());
00075               else ControlBackground=SystemColor.control;
00076        }
00077        
00078        // Resources:
00079        static protected ResourceBundle B;
00080        
00081        public static Enumeration names ()
00082        {      if (B!=null) return B.getKeys(); 
00083               else return null;
00084        }
00085        public static String name (String tag, String def)
00086        {      String s;
00087               if (B==null) return def;
00088               try
00089               {      s=B.getString(tag);
00090               }
00091               catch (Exception e)
00092               {      s=def;
00093               }
00094               return s;
00095        }
00096        public static String name (String tag)
00097        {      return name(tag,tag.substring(tag.lastIndexOf(".")+1));
00098        }
00099        public static void initBundle (String file, boolean language)
00100        {      try
00101               {      B=ResourceBundle.getBundle(file);
00102                      String lang=getParameter("language","");
00103                      if (language && !lang.equals("") && !lang.equals("default"))
00104                      {      String langsec="";
00105                             if (lang.length()>3 && lang.charAt(2)=='_')
00106                             {      langsec=lang.substring(3);
00107                                    lang=lang.substring(0,2);
00108                             }
00109                             Locale.setDefault(new Locale(lang,langsec));
00110                             initBundle(file,false);
00111                      }
00112               }
00113               catch (RuntimeException e)
00114               {      B=null;
00115               }
00116        }
00117        public static void initBundle(String file)
00118        {      initBundle(file,false);
00119        }
00120        
00121        // Properties:
00122        static Properties P=new Properties();
00123        static String ConfigName;
00124        
00125        public static synchronized Enumeration properties () { return P.keys(); }
00126        public static synchronized void loadProperties (InputStream in)
00127        {      try
00128               {      P=new Properties();
00129                      P.load(in);
00130                      in.close();
00131               }
00132               catch (Exception e)
00133               {      P=new Properties();
00134               }
00135        }
00136        public static synchronized boolean loadPropertiesFromResource (String filename)
00137        {      try
00138               {      Object G=new Object();
00139                      InputStream in=G.getClass().getResourceAsStream(filename);
00140                      P=new Properties();
00141                      P.load(in);
00142                      in.close();
00143               }
00144               catch (Exception e)
00145               {      P=new Properties();
00146                      return false;
00147               }
00148               ConfigName=filename;
00149               return true;
00150        }
00151        public static synchronized boolean loadProperties (String filename)
00152        {      ConfigName=filename;
00153               try
00154               {      FileInputStream in=new FileInputStream(filename);
00155                      P=new Properties();
00156                      P.load(in);
00157                      in.close();
00158               }
00159               catch (Exception e)
00160               {      P=new Properties();
00161                      return false;
00162               }
00163               return true;
00164        }
00165        public static synchronized void loadProperties (String dir, String filename)
00166        {      try
00167               {      Properties p=System.getProperties();
00168                      ConfigName=dir+
00169                             p.getProperty("file.separator")+filename;
00170                      loadProperties(ConfigName);
00171               }
00172               catch (Exception e)
00173               {      P=new Properties();
00174               }
00175        }
00176        public static synchronized void loadPropertiesInHome (String filename)
00177        {      try
00178               {      Properties p=System.getProperties();
00179                      loadProperties(p.getProperty("user.home"),filename);
00180               }
00181               catch (Exception e)
00182               {      P=new Properties();
00183               }
00184        }
00185        public static synchronized void clearProperties ()
00186        {      P=new Properties();
00187        }
00188        public static synchronized void saveProperties (String text)
00189        {      try
00190               {      FileOutputStream out=new FileOutputStream(ConfigName);
00191                      P.save(out,text);
00192                      out.close();
00193               }
00194               catch (Exception e) {}
00195        }
00196        public static void saveProperties (String text, String filename)
00197        {      ConfigName=filename;
00198               saveProperties(text);
00199        }
00200        public static synchronized void setParameter (String key, boolean value)
00201        {      if (P==null) return;
00202               if (value) P.put(key,"true");
00203               else P.put(key,"false");
00204        }
00205        public static synchronized boolean getParameter (String key, boolean def)
00206        {      try
00207               {      String s=P.getProperty(key);
00208                      if (s.equals("true")) return true;
00209                      else if (s.equals("false")) return false;
00210                      return def;
00211               }
00212               catch (Exception e)
00213               {      return def;
00214               }
00215        }
00216        public static synchronized String getParameter (String key, String def)
00217        {      String res=def;
00218               try
00219               {      res=P.getProperty(key);
00220               }
00221               catch (Exception e) {}
00222               if (res!=null)
00223               {      if (res.startsWith("$")) res=res.substring(1);
00224                      return res;
00225               }
00226               else return def;
00227        }
00228        public static synchronized void setParameter (String key, String value)
00229        {      if (P==null) return;
00230               if (value.length()>0 && Character.isSpaceChar(value.charAt(0)))
00231                      value="$"+value;
00232               P.put(key,value);
00233        }
00234        public static synchronized int getParameter (String key, int def)
00235        {      try
00236               {      return Integer.parseInt(getParameter(key,""));
00237               }
00238               catch (Exception e)
00239               {      try
00240                      {      double x=new Double(getParameter(key,"")).doubleValue();
00241                             return (int)x;
00242                      }
00243                      catch (Exception ex) {}
00244                      return def;
00245               }
00246        }
00247        public static synchronized void setParameter (String key, int value)
00248        {      setParameter(key,""+value);
00249        }
00250        public static synchronized double getParameter (String key, double def)
00251        {      try
00252               {      return new Double(getParameter(key,"")).doubleValue();
00253               }
00254               catch (Exception e)
00255               {      return def;
00256               }
00257        }
00258        public static synchronized void setParameter (String key, double value)
00259        {      setParameter(key,""+value);
00260        }
00261        static public synchronized Color getParameter (String key, Color c)
00262        {      String s=getParameter(key,"");
00263               if (s.equals("")) return c;
00264               StringParser p=new StringParser(s);
00265               p.replace(',',' ');
00266               int red,green,blue;
00267               red=p.parseint(); green=p.parseint(); blue=p.parseint();
00268               try
00269               {      return new Color(red,green,blue);
00270               }
00271               catch (RuntimeException e)
00272               {      return c;
00273               }
00274        }
00275        static public synchronized Color getParameter (String key, int red, int green, int blue)
00276        {      String s=getParameter(key,"");
00277               if (s.equals("")) return new Color(red,green,blue);
00278               StringParser p=new StringParser(s);
00279               p.replace(',',' ');
00280               red=p.parseint(); green=p.parseint(); blue=p.parseint();
00281               try
00282               {      return new Color(red,green,blue);
00283               }
00284               catch (RuntimeException e)
00285               {      return Color.black;
00286               }
00287        }
00288        public static synchronized void setParameter (String key, Color c)
00289        {      setParameter(key,""+c.getRed()+","+c.getGreen()+","+c.getBlue());
00290        }
00294        public static synchronized void removeParameter (String key)
00295        {      P.remove((Object)key);
00296        }
00300        public static synchronized void removeAllParameters (String start)
00301        {      Enumeration e=P.keys();
00302               while (e.hasMoreElements())
00303               {      String key=(String)e.nextElement();
00304                      if (key.startsWith(start))
00305                      {      P.remove((Object)key);
00306                      }
00307               }      
00308        }
00316        public static synchronized void resetDefaults (String defaults)
00317        {      Enumeration e=P.keys();
00318               while (e.hasMoreElements())
00319               {      String key=(String)e.nextElement();
00320                      if (key.startsWith(defaults))
00321                      {      setParameter(key.substring(defaults.length()),
00322                                    getParameter(key,""));
00323                      }
00324               }      
00325        }
00326        public static void resetDefaults ()
00327        {      resetDefaults("default.");
00328        }
00332        public static synchronized boolean haveParameter (String key)
00333        {      try
00334               {      String res=P.getProperty(key);
00335                      if (res==null) return false;
00336               }
00337               catch (Exception e) { return false; }
00338               return true;
00339        }
00340        
00341        // Warnings
00342        
00343        static Frame F=null;
00344        public static void warning (String s)
00345        {      if (F==null)
00346               {      F=new Frame();
00347               }
00348               Warning W=new Warning(F,s,name("warning"),false);
00349               W.center();
00350               W.show();
00351        }
00352        
00353        // Clipboard for applets
00354        static public String AppletClipboard=null;
00355        
00356        static public boolean IsApplet=false;
00357        static public void setApplet (boolean flag) { IsApplet=flag; }
00358        static public boolean isApplet () { return IsApplet; }
00359        
00360        // Java Version
00361        
00362        public static double getJavaVersion ()
00363        {      String version=System.getProperty("java.version");
00364               if (version==null) return 0.0;
00365               double v=0.0;
00366               StringTokenizer t=new StringTokenizer(version,".");
00367               if (t.hasMoreTokens())
00368               {      v=convert(t.nextToken());
00369               }
00370               else return v;
00371               if (t.hasMoreTokens())
00372               {      v=v+convert(t.nextToken())/10;
00373               }
00374               else return v;
00375               if (t.hasMoreTokens())
00376               {      v=v+convert(t.nextToken())/100;
00377               }
00378               return v;
00379        }
00380        
00381        public static double convert (String s)
00382        {      try
00383               {      return Integer.parseInt(s);
00384               }
00385               catch (Exception e)
00386               {      return 0;
00387               }
00388        }
00389        
00390        public static synchronized String getUserDir ()
00391        {      String dir=System.getProperty("user.dir");
00392               return FileName.canonical(dir);
00393        }
00394        
00395        public static Object ExitBlock=new Object();
00396        
00397        public static synchronized void exit (int i)
00398        {      synchronized (ExitBlock)
00399               {      System.exit(i);
00400               }
00401        }
00402        
00403        public static void main (String args[])
00404        {      try
00405               {      System.out.println(new Color(4,5,600));
00406               }
00407               catch (RuntimeException e) {}
00408        }
00409 }