Package simtools.ui

Source Code of simtools.ui.ResourceFinder

/* ==============================================
* Simtools : The tools library used in JSynoptic
* ==============================================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 1999-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*     Nicolas Brodu
*
*
* $Id: ResourceFinder.java,v 1.1 2003/12/09 15:50:05 brodu Exp $
*
* Changes
* -------
* 09-Dec-2003 : Creation date (NB);
*
*/
package simtools.ui;

import java.lang.reflect.Method;
import java.util.ResourceBundle;

/**
* This class avoid to duplicate the same code over and over in all localized files.
* It works by looking for a resource names after the class name passed as argument.
* Example: "MyPackage.MyClass" => "MyPackage.resources.MyClassResources"
* It then tries to load the resources.
* Either it fails => program stops with an error message
* It succeed => return value never null
*/
public class ResourceFinder  {

  private static final Class[] setParentParameters = new Class[]{ResourceBundle.class};

  static public ResourceBundle get(Class c) {
    return get(c,null);
  }
 
  static public ResourceBundle get(Class c, ResourceBundle parent) {
    if (c==null) {
      System.err.println("Can't load null resources.");
      System.exit(0);
    }
    StringBuffer sb = new StringBuffer(c.getName());
    sb.insert(sb.lastIndexOf("."),".resources");
    sb.append("Resources");
    try {
      ResourceBundle rb = ResourceBundle.getBundle(sb.toString(),CustomizedLocale.get());
      if (parent!=null) {
        Method method = ResourceBundle.class.getDeclaredMethod("setParent",setParentParameters);
        method.setAccessible(true);
        method.invoke(rb,new Object[]{ parent });
      }
      return rb;
    } catch (Exception e) {
      System.err.println("Can't load resources: "+sb.toString());
      System.exit(0);
    }
    // Never reached
    return null;
  }

  static public MenuResourceBundle getMenu(Class c) {
    return (MenuResourceBundle)get(c);
  }

  static public MenuResourceBundle getMenu(Class c, ResourceBundle parent) {
    return (MenuResourceBundle)get(c, parent);
  }

  static public BasicMessageWriter getMessages(Class c) {
    return getMessages(c,null);
  }
 
  static public BasicMessageWriter getMessages(Class c, ResourceBundle parent) {
    if (c==null) {
      System.err.println("Can't load null resources.");
      System.exit(0);
    }
    StringBuffer sb = new StringBuffer(c.getName());
    sb.insert(sb.lastIndexOf("."),".resources");
    sb.append("Messages");
    try {
      return new BasicMessageWriter((StringsResourceBundle)ResourceBundle.getBundle(sb.toString(),CustomizedLocale.get()));
    } catch (Exception e) {
      System.err.println("Can't load resources: "+sb.toString());
      System.exit(0);
    }
    // Line never reached, whatever java compiler thinks.
    return null;
  }
 
}

TOP

Related Classes of simtools.ui.ResourceFinder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.