Package simtools.ui

Source Code of simtools.ui.MenuResourceBundle$FileFilter

/* ==============================================
* 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:
*         Claude Cazenave
*     Nicolas Brodu
*
*
* $Id: MenuResourceBundle.java,v 1.12 2008/10/07 10:52:59 ogor Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB);
*
*/

package simtools.ui;

import java.awt.Font;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.ListResourceBundle;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.KeyStroke;

/**
* This class is used as a base class for ResourceBundle files
* which are specialized for menu, tool box and file filter parameters.
* Providing a menu key name this class returns :
* a Jmenu with the right name and the mnemonic
* a JmenuItem with the right name, the mnemonic and the shortcut if any,
* a JButton with an image and a tooltip
* Providing a file filter extension this class returns :
* a FileFilter with the filtering of the files with the given extension
* and the related file description
* Chilren classes have to store key/objects pairs as usual
* but the key names are computed as follows
* nameMenu=String=menu item title
* nameMnemonic=char=menu item mnemonic (by default the first char in menu item name is used)
* nameShortcut=KeyStroke=menu item shortcut
* nameImage=String="images/file.gif"=box icon file name
* nameTip=String=box icon tip
* where name is the menu item name
* nameDescription=String the related description
* where name is the file filter name
*
* @author Claude Cazenave
*
* @version 1.0 1999
*/
public abstract class MenuResourceBundle extends ListResourceBundle{

    /**<b>(HashMap)</b> _iconsHashMap: The _iconsHashMap: contains the Cached icons.*/
    private HashMap _iconsHashMap;
   
    public MenuResourceBundle(){
        _iconsHashMap = new HashMap();
    }
   
  /**
   * Add defaults values from an other MenuResourceBundle
   * @param newparent default values
   */
  public void setParent(MenuResourceBundle newparent){
    // check if already loaded
    if(newparent==this){
      return;
    }
    // look for resources without parent to avoid overloading
    if(parent==null){
      setParent((ResourceBundle)newparent);
    }
    else{
      ((MenuResourceBundle)parent).setParent(newparent);
    }
  }

  public String getStringValue(String name){
    String iname=null;
    try{
      iname=getString(name);
    }
    catch(MissingResourceException mre){
    }
    return iname;
  }

  public long getLongValue(String name){
    long ret = 0;
    try {
      ret = Long.decode(getString(name)).longValue();
    } catch (NumberFormatException nfe) {
    } catch(MissingResourceException mre) {
    }
    return ret;
  }

  public int getIntValue(String name){
    return (int)getLongValue(name);
  }

  public short getShortValue(String name){
    return (short)getLongValue(name);
  }

  public byte getByteValue(String name){
    return (byte)getLongValue(name);
  }

  public double getDoubleValue(String name){
    double ret = 0;
    try {
      ret = Double.parseDouble(getString(name));
    } catch (NumberFormatException nfe) {
    } catch(MissingResourceException mre) {
    }
    return ret;
  }

  public float getFloatValue(String name){
    return (float)getDoubleValue(name);
  }



  /**
     * This method returns the icon found in the resource bundle,
     * using the given key.
     * It will cache the ImageIcon in a HashMap, for multiple access.
   * @param name                  The unique key to find the icon.
   * @return <b>(ImageIcon)</b>   The ImageIcon that correspond to the key
     * , or null if can't be found.
   */
  public ImageIcon getIcon(String name) {
        //The result of the method.
        ImageIcon result = null;
        //check if icon already exist in cache.
        result = (ImageIcon) _iconsHashMap.get(name);
        if (result == null) {
            //Icons has not been cached, try to load it.
            try {
                String iname = getString(name);
                if (iname != null) {
                    // the subclass defines the directory
                    URL url = this.getClass().getResource(iname);
                    if (url != null) {
                        //we have found the url of the icon
                        //Create it !
                        result = new ImageIcon(url);
                        //And cache it
                        _iconsHashMap.put(name, result);
                    }//Else we can't found the url, return null.
                }//Else we can't found iname, return null.
            } catch (MissingResourceException mre) {
                //An error has occured, return null.
            }
        }//Else, we had retrieve the cached icon, just return it.
        //return the result
        return result;
    }

  /**
   * This method returns the file found in the resource bundle using the given key
   * @param name - The unique key to find the file.
   * @return The file that correspond to the key, or null if can't be found.
   */
  public Image getImage(String name){
      Image res =null;
      res = Toolkit.getDefaultToolkit().getImage(getURL("name"));
      return res;
  }

  public File getFile(String name){
      URL url = getURL(name);
      return new File(url.toString());   // TODO it does work when file is located inside a jar file
  }
 
  public URL getURL(String name){
      URL res = null;
      try {
          String iname = getString(name);
          if (iname != null) {
              res = this.getClass().getResource(iname);
          }
      } catch (MissingResourceException mre) {
          //An error has occured, return null.
      }
      return res;
    }

  public JMenu getMenu(String name){
    String iname=null;
    try{
      iname=getString(name+"Menu");
    }
    catch(MissingResourceException mre){
    }
    if(iname==null){
      return null;
    }
    JMenu res=new JMenu(iname);

    String mname=null;
    try{
      mname=getString(name+"Mnemonic");
    }
    catch(MissingResourceException mre){
    }
    if(mname!=null){
      res.setMnemonic(mname.charAt(0));
    }
    else{
      res.setMnemonic(iname.charAt(0));
    }
    return res;
  }

  public JLabel getLabel(String name){
      JLabel res;
      res = new JLabel( getString(name) );
      return res;
  }

  public KeyStroke getKeyStroke(String name) {
    Object o = null;
    try {
      o = getObject(name);
    } catch (MissingResourceException mre){}

    if (!(o instanceof KeyStroke)) return null;
    return (KeyStroke)o; // o may be null
  }

  public JMenuItem getItem(String name, ActionListener listener){
    String iname=null;
    try{
      iname=getString(name+"Menu");
    }
    catch(MissingResourceException mre){
    }
    if(iname==null){
      return null;
    }
    JMenuItem res=new JMenuItem(iname);

    String mname=null;
    try{
      mname=getString(name+"Mnemonic");
    }
    catch(MissingResourceException mre){
    }
    if(mname!=null){
      res.setMnemonic(mname.charAt(0));
    }
    else{
      res.setMnemonic(iname.charAt(0));
    }

    KeyStroke k = getKeyStroke(name+"Shortcut");
    if (k != null) res.setAccelerator(k);

    if(listener!=null){
      res.addActionListener(listener);
    }
    return res;
  }

  public JCheckBoxMenuItem getCheckBoxItem(String name, ActionListener listener){
    String iname = null;
    try {
      iname = getString(name+"Menu");
    } catch (MissingResourceException mre) {}

    if (iname == null) {
      return null;
    }
    JCheckBoxMenuItem res = new JCheckBoxMenuItem(iname);

    String mname = null;
    try {
      mname=getString(name+"Mnemonic");
    } catch (MissingResourceException mre){}

    if(mname != null) {
      res.setMnemonic(mname.charAt(0));
    } else {
      res.setMnemonic(iname.charAt(0));
    }

    KeyStroke k = getKeyStroke(name+"Shortcut");
    if (k != null) res.setAccelerator(k);

    if (listener != null) {
      res.addActionListener(listener);
    }
    return res;
  }

  public JButton getBox(String name, ActionListener listener){
    String iname=null;
    try{
      iname=getString(name+"Image");
    }
    catch(MissingResourceException mre){
    }
    if(iname==null){
      return null;
    }
    // the subclass defines the directory
    URL url = this.getClass().getResource(iname);
    if(url==null){
      return null;
    }

          JButton res = new JButton(new ImageIcon(url));
          res.setMargin(new Insets(1,1,1,1));

    String tip=null;
    try{
      tip=getString(name+"Tip");
    }
    catch(MissingResourceException mre){
    }
    if (tip != null) {
      res.setToolTipText(tip);
    }
    if(listener!=null){
      res.addActionListener(listener);
    }
    return res;
  }

  public JButton getButton(String name, ActionListener listener){
    String iname=null;
    try{
      iname=getString(name+"Text");
    }
    catch(MissingResourceException mre){
    }
    if(iname==null){
      return null;
    }

           JButton res = new JButton(iname);
          res.setMargin(new Insets(2,10,2,10));

    String tip=null;
    try{
      tip=getString(name+"Tip");
    }
    catch(MissingResourceException mre){
    }
    if (tip != null) {
      res.setToolTipText(tip);
    }
    if(listener!=null){
      res.addActionListener(listener);
    }
    return res;
  }

  public JRadioButton getRadioButton(String name, ActionListener listener) {
    String iname=null;
    try{
      iname=getString(name+"Text");
    }
    catch(MissingResourceException mre){
    }
    JRadioButton res;
    if (iname==null) {
        res = new JRadioButton();
    } else {
        res = new JRadioButton(iname);
    }
   
    String tip=null;
    try{
      tip=getString(name+"Tip");
    }
    catch(MissingResourceException mre){
    }
    if (tip != null) {
      res.setToolTipText(tip);
    }
    if(listener!=null){
      res.addActionListener(listener);
    }
    return res;
  }

  public JCheckBox getCheckBox(String name, ActionListener listener) {
    String iname=null;
    try{
      iname=getString(name+"Text");
    }
    catch(MissingResourceException mre){
    }
    if (iname==null) {
        return null;
    }

    JCheckBox res;
    if (iname==null) {
        res = new JCheckBox();
    }
    else {
        res = new JCheckBox(iname);
    }
    String tip=null;
    try{
      tip=getString(name+"Tip");
    }
    catch(MissingResourceException mre){
    }
    if (tip != null) {
      res.setToolTipText(tip);
    }
    if(listener!=null){
      res.addActionListener(listener);
    }
    return res;
  }

  public MenuResourceBundle.FileFilter getFileFilter(String name, String extension){
    String dname=null;
    try{
      dname=getString(name+"Description");
    }
    catch(MissingResourceException mre){
    }
    if(dname==null){
      dname="";
    }
    return new FileFilter(extension,dname);
  }

  public MenuResourceBundle.FileFilter getFileFilter(String name, String[] extension){
    String dname=null;
    try{
      dname=getString(name+"Description");
    }
    catch(MissingResourceException mre){
    }
    if(dname==null){
      dname="";
    }
    return new FileFilter(extension,dname);
  }

  public MenuResourceBundle.FileFilter getFileFilter(String name){
    String dname=null;
    try{
      dname=getString(name+"Description");
    }
    catch(MissingResourceException mre){
    }
    if(dname==null){
      dname="";
    }
    String ext=null;
    try{
      ext=getString(name+"Extension");
    }
    catch(MissingResourceException mre){
    }
    if(ext==null){
      ext="";
    }
    return new FileFilter(ext,dname);
  }

  public class FileFilter extends javax.swing.filechooser.FileFilter {

    protected String[] _extension;  // extension must be in lower case
    protected String _description;
    protected boolean _canProcessDirectory;
    protected boolean _acceptFilesWithoutExtension;

    /**
     * Creates a new  file filter
     * @param ext the file extensions, separated by spaces
     * @param desc the file description
      */
    public FileFilter(String ext, String desc){
      StringTokenizer st = new StringTokenizer(ext);
      _extension = new String[st.countTokens()];
      for (int i=0; i<_extension.length; ++i){
        _extension[i] = st.nextToken().toLowerCase();
      }
      _description=desc;
      _canProcessDirectory = false;
      _acceptFilesWithoutExtension = false;
    }

    /**
     * Creates a new  file filter
     * @param ext the array of file extension
     * @param desc the file description
      */
    public FileFilter(String[] ext, String desc){
      _extension=new String[ext.length];
      for(int i=0;i<_extension.length;i++){
        _extension[i]=ext[i].toLowerCase();
      }
      _description=desc;
      _canProcessDirectory = false;
    }

    public boolean accept(File f){
      if(f.isDirectory()){
        return true;
      }
      return canSelect(f);
    }
   
    public boolean canProcess(File f){
        if(f.isDirectory()){
                return _canProcessDirectory;
            }
            return canSelect(f);
        }
   
    public boolean canSelect(File f) {
      for(int i=0;i<_extension.length;i++){
        if (_extension[i].equals("*")) {
            return true;
        }
        if ( (f.getName().lastIndexOf(".") ==-1) && _acceptFilesWithoutExtension) {
                    return true;
                }
        if ( (f.getName().lastIndexOf(".")!=-1) && ( (f.getName().substring(f.getName().lastIndexOf(".")+1).toLowerCase().equals(_extension[i])))){
          return true;
        }
      }
      return false;
    }
   
    public String getDescription(){
      return _description;
    }
                /**
                 * Get the default extension
                 * @return the default extension
                 */
     public String getExtension(){
      return _extension[0];
    }

                /**
                 * Force the default extension to a file
                 * @param a file with or without extension
                 * @return a file with the the default extension
                 */
    public File forceExtension(File f){
                   String n=f.getName();
                  if(n.endsWith(_extension[0])){
                    return f;
                  }
                  String p=f.getAbsolutePath();
      p+=_extension[0];
      f=new File(p);
                  return f;
    }

    public boolean equals(Object o) {
      if (o==null) return false;
      if (_description==null) return false;
      if (!(o instanceof FileFilter)) return false;
      FileFilter f = (FileFilter)o;
      if (f._description==null) return false;
      if (_description.equals(f._description)) return true;
      return false;
    }

        public boolean canProcessDirectory() {
            return _canProcessDirectory;
        }

        public void setCanProcessDirectory(boolean canProcessDirectory) {
            this._canProcessDirectory = canProcessDirectory;
        }
       
        public boolean acceptFilesWithoutExtention() {
            return _acceptFilesWithoutExtension;
        }

        public void setAcceptFilesWithoutExtention(boolean acceptFilesWithoutExtension) {
            this._acceptFilesWithoutExtension = acceptFilesWithoutExtension;
        }
       
       
       
  }
}
TOP

Related Classes of simtools.ui.MenuResourceBundle$FileFilter

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.