GUIChooser.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 7k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    GUIChooser.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui;
  22. import weka.gui.explorer.Explorer;
  23. import weka.gui.experiment.Experimenter;
  24. import java.awt.Panel;
  25. import java.awt.Button;
  26. import java.awt.GridLayout;
  27. import java.awt.Frame;
  28. import java.awt.Label;
  29. import java.awt.BorderLayout;
  30. import java.awt.event.WindowAdapter;
  31. import java.awt.event.WindowEvent;
  32. import java.awt.event.ActionListener;
  33. import java.awt.event.ActionEvent;
  34. import java.awt.Image;
  35. import java.awt.Toolkit;
  36. import javax.swing.ImageIcon;
  37. import javax.swing.JFrame;
  38. import javax.swing.JPanel;
  39. import javax.swing.JLabel;
  40. import javax.swing.SwingConstants;
  41. import javax.swing.BorderFactory;
  42. /** 
  43.  * The main class for the Weka GUIChooser. Lets the user choose
  44.  * which GUI they want to run.
  45.  *
  46.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  47.  * @author Mark Hall (mhall@cs.waikato.ac.nz)
  48.  * @version $Revision: 1.9 $
  49.  */
  50. public class GUIChooser extends Frame {
  51.   /** Click to open the simplecli */
  52.   protected Button m_SimpleBut = new Button("Simple CLI");
  53.   /** Click to open the Explorer */
  54.   protected Button m_ExplorerBut = new Button("Explorer");
  55.   /** Click to open the Explorer */
  56.   protected Button m_ExperimenterBut = new Button("Experimenter");
  57.   /** The SimpleCLI */
  58.   protected SimpleCLI m_SimpleCLI;
  59.   /** The frame containing the explorer interface */
  60.   protected JFrame m_ExplorerFrame;
  61.   /** The frame containing the experiment interface */
  62.   protected JFrame m_ExperimenterFrame;
  63.   /** The weka image */
  64.   Image m_weka = Toolkit.getDefaultToolkit().
  65.     getImage(ClassLoader.getSystemResource("weka/gui/weka3.gif"));
  66.   
  67.   /**
  68.    * Creates the experiment environment gui with no initial experiment
  69.    */
  70.   public GUIChooser() {
  71.     super("Weka GUI Chooser");
  72.     boolean haveSwing = true;
  73.     try {
  74.       Class c = Class.forName("javax.swing.JFrame");
  75.     } catch (Exception ex) {
  76.       haveSwing = false;
  77.     }
  78.     if (!haveSwing) {
  79.       System.err.println("Swing is not installed");
  80.       // Pop up dialog saying you get extra if you have swing
  81.       setLayout(new GridLayout(2, 1));
  82.       add(m_SimpleBut);
  83.       add(new Label("Swing is not installed"));
  84.     } else {
  85.       Image icon = Toolkit.getDefaultToolkit().
  86. getImage(ClassLoader.getSystemResource("weka/gui/weka_icon.gif"));
  87.       setIconImage(icon);
  88.       setLayout(new BorderLayout());
  89.       JPanel wbuts = new JPanel();
  90.       wbuts.setBorder(BorderFactory.createTitledBorder("GUI"));
  91.       wbuts.setLayout(new GridLayout(1, 3));
  92.       wbuts.add(m_SimpleBut);
  93.       wbuts.add(m_ExplorerBut);
  94.       wbuts.add(m_ExperimenterBut);
  95.       add(wbuts, BorderLayout.SOUTH);
  96.       JPanel wekaPan = new JPanel();
  97.       wekaPan.setToolTipText("Weka, a native bird of New Zealand");
  98.       ImageIcon wii = new ImageIcon(m_weka);
  99.       JLabel wekaLab = new JLabel(wii);
  100.       wekaPan.add(wekaLab);
  101.       add(wekaPan, BorderLayout.CENTER);
  102.       JPanel titlePan = new JPanel();
  103.       titlePan.setLayout(new GridLayout(6,1));
  104.       titlePan.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
  105.       titlePan.add(new JLabel("Waikato Environment for", 
  106.       SwingConstants.CENTER));
  107.       titlePan.add(new JLabel("Knowledge Analysis", 
  108.       SwingConstants.CENTER));
  109.       titlePan.add(new JLabel(""));
  110.       titlePan.add(new JLabel("(c) 1999 - 2002", 
  111.       SwingConstants.CENTER));
  112.       titlePan.add(new JLabel("University of Waikato", 
  113.       SwingConstants.CENTER));
  114.       titlePan.add(new JLabel("New Zealand",
  115.       SwingConstants.CENTER));
  116.       add(titlePan, BorderLayout.NORTH);
  117.     }
  118.     m_SimpleBut.addActionListener(new ActionListener() {
  119.       public void actionPerformed(ActionEvent e) {
  120. if (m_SimpleCLI == null) {
  121.   m_SimpleBut.setEnabled(false);
  122.   try {
  123.     m_SimpleCLI = new SimpleCLI();
  124.   } catch (Exception ex) {
  125.     throw new Error("Couldn't start SimpleCLI!");
  126.   }
  127.   m_SimpleCLI.addWindowListener(new WindowAdapter() {
  128.     public void windowClosing(WindowEvent w) {
  129.       m_SimpleCLI.dispose();
  130.       m_SimpleCLI = null;
  131.       m_SimpleBut.setEnabled(true);
  132.       checkExit();
  133.     }
  134.   });
  135.   m_SimpleCLI.setVisible(true);
  136. }
  137.       }
  138.     });
  139.     m_ExplorerBut.addActionListener(new ActionListener() {
  140.       public void actionPerformed(ActionEvent e) {
  141. if (m_ExplorerFrame == null) {
  142.   m_ExplorerBut.setEnabled(false);
  143.   m_ExplorerFrame = new JFrame("Weka Knowledge Explorer");
  144.   m_ExplorerFrame.getContentPane().setLayout(new BorderLayout());
  145.   m_ExplorerFrame.getContentPane()
  146.     .add(new Explorer(), BorderLayout.CENTER);
  147.   m_ExplorerFrame.addWindowListener(new WindowAdapter() {
  148.     public void windowClosing(WindowEvent w) {
  149.       m_ExplorerFrame.dispose();
  150.       m_ExplorerFrame = null;
  151.       m_ExplorerBut.setEnabled(true);
  152.       checkExit();
  153.     }
  154.   });
  155.   m_ExplorerFrame.pack();
  156.   m_ExplorerFrame.setSize(800, 600);
  157.   m_ExplorerFrame.setVisible(true);
  158. }
  159.       }
  160.     });
  161.     m_ExperimenterBut.addActionListener(new ActionListener() {
  162.       public void actionPerformed(ActionEvent e) {
  163. if (m_ExperimenterFrame == null) {
  164.   m_ExperimenterBut.setEnabled(false);
  165.   m_ExperimenterFrame = new JFrame("Weka Experiment Environment");
  166.   m_ExperimenterFrame.getContentPane().setLayout(new BorderLayout());
  167.   m_ExperimenterFrame.getContentPane()
  168.     .add(new Experimenter(false), BorderLayout.CENTER);
  169.   m_ExperimenterFrame.addWindowListener(new WindowAdapter() {
  170.     public void windowClosing(WindowEvent w) {
  171.       m_ExperimenterFrame.dispose();
  172.       m_ExperimenterFrame = null;
  173.       m_ExperimenterBut.setEnabled(true);
  174.       checkExit();
  175.     }
  176.   });
  177.   m_ExperimenterFrame.pack();
  178.   m_ExperimenterFrame.setSize(800, 600);
  179.   m_ExperimenterFrame.setVisible(true);
  180. }
  181.       }
  182.     });
  183.     addWindowListener(new WindowAdapter() {
  184.       public void windowClosing(WindowEvent w) {
  185. dispose();
  186. checkExit();
  187.       }
  188.     });
  189.     pack();
  190.   }
  191.   /**
  192.    * Kills the JVM if all windows have been closed.
  193.    */
  194.   private void checkExit() {
  195.     if (!isVisible()
  196. && (m_SimpleCLI == null)
  197. && (m_ExplorerFrame == null)
  198. && (m_ExperimenterFrame == null)) {
  199.       System.exit(0);
  200.     }
  201.   }
  202.   
  203.   /**
  204.    * Tests out the GUIChooser environment.
  205.    *
  206.    * @param args ignored.
  207.    */
  208.   public static void main(String [] args) {
  209.     try {
  210.       GUIChooser c = new GUIChooser();
  211.       c.setVisible(true);
  212.     } catch (Exception ex) {
  213.       ex.printStackTrace();
  214.       System.err.println(ex.getMessage());
  215.     }
  216.   }
  217. }