Package jsynoptic.plugins.lookandfeel

Source Code of jsynoptic.plugins.lookandfeel.LookAndFeelPlugin$LAFDialog

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program 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 program 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
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Nicolas Brodu
*
* $Id: LookAndFeelPlugin.java,v 1.8 2007/02/26 17:01:19 cazenave Exp $
*
* Changes
* -------
* 30-Oct-2003 : Initial release (NB);
*
*/

package jsynoptic.plugins.lookandfeel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;

import simtools.ui.ResourceFinder;

import jsynoptic.base.Plugin;
import jsynoptic.ui.JSynoptic;
import jsynoptic.ui.JSynopticPanels;
import jsynoptic.ui.PluginLookup;
import jsynoptic.ui.Run;

/**
* @author nbrodu
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class LookAndFeelPlugin extends Plugin implements ActionListener {

  public static ResourceBundle resources = ResourceFinder.get(LookAndFeelPlugin.class);

  protected JMenuItem jmi;
 
  public LookAndFeelPlugin() {
    String lafs = Run.getProperties().getString("plugin.lookandfeel.classes","");
    if (lafs.equals("")) {
      searchLookAndFeel();
    } else {
      LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
      StringTokenizer st = new StringTokenizer(lafs,", ;:\t|");
      while (st.hasMoreTokens()) {
        String classname = st.nextToken();
        boolean skip = false;
        for (int i=0; i<infos.length; ++i) {
          if (classname.equals(infos[i].getClassName())) skip = true;
        }
        // do not install already installed LAF.
        if (skip) continue;
        try {
          Class c = Class.forName(classname);
          LookAndFeel laf = (LookAndFeel)c.newInstance();
          UIManager.installLookAndFeel(laf.getName(), classname);
          // Skip this laf on error
        } catch (ClassNotFoundException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }
      }
    }

    String laf = Run.getProperties().getString("plugin.lookandfeel.default","");
    if (laf.equals("")) laf = "com.incors.plaf.kunststoff.KunststoffLookAndFeel";
    try {
      UIManager.setLookAndFeel((LookAndFeel)Class.forName(laf).newInstance());
      // Keep default look and feel on error, otherwise apply it imediately
      if (JSynoptic.gui!=null) SwingUtilities.updateComponentTreeUI(JSynoptic.gui.getOwner());
    } catch (UnsupportedLookAndFeelException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (ClassNotFoundException e) {
    }
  }

  /** Search all look and feels available in the current classpath, and add them to the UIManager list
   */
  public static void searchLookAndFeel() {
    Vector v = PluginLookup.search(LookAndFeel.class);
    LookAndFeelInfo[] infos;
    for (Iterator it = v.iterator(); it.hasNext();) {
      String classname = (String)it.next();
      infos = UIManager.getInstalledLookAndFeels();
      boolean skip = false;
      for (int i=0; i<infos.length; ++i) {
        if (classname.equals(infos[i].getClassName())) skip = true;
      }
      // do not install already installed LAF.
      if (skip) continue;
      LookAndFeel laf = null;
      try {
        Class c = Class.forName(classname);
        laf = (LookAndFeel)c.newInstance();
      } catch (ClassNotFoundException e) {
        // should not happen since we precisely looked in the classpath
      } catch (InstantiationException e) {
        // skip this laf
      } catch (IllegalAccessException e) {
        // skip this laf
      }
      if (laf==null) continue;
      // Now check if the laf installed itself...
      infos = UIManager.getInstalledLookAndFeels();
      skip = false;
      for (int i=0; i<infos.length; ++i) {
        if (classname.equals(infos[i].getClassName())) skip = true;
      }
      // do not install already installed LAF.
      if (skip) continue;
      UIManager.installLookAndFeel(laf.getName(), classname);
    }
   
    // Save in the user properties
    infos = UIManager.getInstalledLookAndFeels();
    String property = null;
    for (int i=0; i<infos.length; ++i) {
      if (property==null) property = "";
      else property += ",";
      property += infos[i].getClassName();
    }
    Run.getProperties().setString("plugin.lookandfeel.classes",property);
  }
 
 
 
 
 
  /** Adds a menu entry in the tools menu */
  public void setMenu(JMenuBar mb) {
    Component c[] = mb.getComponents();
    for (int i=0; i<c.length; ++i) {
      JMenu jm = (JMenu)c[i];
      if (jm.getText().equals(JSynopticPanels.resources.getString("toolsMenu"))) {
        jmi = new JMenuItem(resources.getString("LookAndFeel..."));
        jm.add(jmi);
        jmi.addActionListener(this);
      }
    }
  }
 
 
  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent e) {
    if (e.getSource()!=jmi) return;

    LAFDialog dialog = new LAFDialog(resources.getString("ChooseALookAndFeel"));
    dialog.pack();
    dialog.show();
  }

  public class LAFDialog extends JDialog {

    protected JButton bsearch, bapply, bok, bcancel;
    protected LookAndFeelInfo[] infos;
    protected JList list;
    protected DefaultListModel listModel;

    public LAFDialog(String title) {
      super(JSynoptic.gui.getOwner(),title,false); // Non modal => can play with L&F changes immediately
     
      listModel  = new DefaultListModel();

      //Create the list and put it in a scroll pane.
      list = new JList(listModel);
      list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      list.setVisibleRowCount(10);
      JScrollPane listScrollPane = new JScrollPane(list);
      getContentPane().add(listScrollPane);
     
      infos = UIManager.getInstalledLookAndFeels();
      for (int i=0; i<infos.length; ++i) {
        listModel.addElement(infos[i].getName());
      }

      JPanel buttons = new JPanel();
      buttons.add(bsearch =new JButton(resources.getString("Search")));
      buttons.add(bapply = new JButton(resources.getString("Apply")));
      buttons.add(bok = new JButton(resources.getString("OK")));
      buttons.add(bcancel = new JButton(resources.getString("Cancel")));
      getContentPane().add(buttons,BorderLayout.SOUTH);
     
      bsearch.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          String title = LAFDialog.this.getTitle();
          LAFDialog.this.setTitle(title + resources.getString("(searching...)"));
          searchLookAndFeel();
          listModel.clear();
          infos = UIManager.getInstalledLookAndFeels();
          for (int i=0; i<infos.length; ++i) {
            listModel.addElement(infos[i].getName());
          }
          LAFDialog.this.setTitle(title);
        }
      });
     
      bapply.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          apply();
        }
      });
     
      bcancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          dispose();
        }
      });
     
      bok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          apply();
          dispose();
        }
      });
     
    }

    protected void apply() {
      String name = (String)list.getSelectedValue();
      if (name==null) return;
      String classname = null;
      for (int i=0; i<infos.length; ++i) {
        if (name.equals(infos[i].getName())) {
          classname = infos[i].getClassName();
          break;
        }
      }
      if (classname==null) return;
      try {
        UIManager.setLookAndFeel((LookAndFeel)Class.forName(classname).newInstance());
        // Keep default look and feel on error, otherwise apply it imediately
        if (JSynoptic.gui!=null)
          SwingUtilities.updateComponentTreeUI(JSynoptic.gui.getOwner());
        SwingUtilities.updateComponentTreeUI(JSynoptic.gui.getOwner());
        Run.getProperties().setString("plugin.lookandfeel.default",classname);
      } catch (UnsupportedLookAndFeelException ex) {
      } catch (InstantiationException ex) {
      } catch (IllegalAccessException ex) {
      } catch (ClassNotFoundException ex) {
      }
    }
   
  } 
 
  /* (non-Javadoc)
   * @see jsynoptic.base.Plugin#about()
   */
  public String about() {
    return resources.getString("AboutText");
  }

}
TOP

Related Classes of jsynoptic.plugins.lookandfeel.LookAndFeelPlugin$LAFDialog

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.