ListSelectorDialog.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
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.  *    ListSelectorDialog.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui;
  22. import java.awt.Component;
  23. import java.awt.BorderLayout;
  24. import java.awt.Frame;
  25. import java.awt.Container;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import javax.swing.JPanel;
  29. import javax.swing.JFrame;
  30. import javax.swing.DefaultListModel;
  31. import javax.swing.JScrollPane;
  32. import javax.swing.JButton;
  33. import javax.swing.Box;
  34. import javax.swing.BoxLayout;
  35. import javax.swing.JDialog;
  36. import javax.swing.JList;
  37. /** 
  38.  * A dialog to present the user with a list of items, that the user can
  39.  * make a selection from, or cancel the selection.
  40.  *
  41.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  42.  * @version $Revision: 1.4 $
  43.  */
  44. public class ListSelectorDialog extends JDialog {
  45.   
  46.   /** Click to choose the currently selected property */
  47.   protected JButton m_SelectBut = new JButton("Select");
  48.   /** Click to cancel the property selection */
  49.   protected JButton m_CancelBut = new JButton("Cancel");
  50.   /** The list component */
  51.   protected JList m_List;
  52.   
  53.   /** Whether the selection was made or cancelled */
  54.   protected int m_Result;
  55.   /** Signifies an OK property selection */
  56.   public static final int APPROVE_OPTION = 0;
  57.   /** Signifies a cancelled property selection */
  58.   public static final int CANCEL_OPTION = 1;
  59.   
  60.   /**
  61.    * Create the list selection dialog.
  62.    *
  63.    * @param parentFrame the parent frame of the dialog
  64.    * @param userList the JList component the user will select from
  65.    */
  66.   public ListSelectorDialog(Frame parentFrame, JList userList) {
  67.     
  68.     super(parentFrame, "Select items", true);
  69.     m_List = userList;
  70.     m_CancelBut.addActionListener(new ActionListener() {
  71.       public void actionPerformed(ActionEvent e) {
  72. m_Result = CANCEL_OPTION;
  73. setVisible(false);
  74.       }
  75.     });
  76.     m_SelectBut.addActionListener(new ActionListener() {
  77.       public void actionPerformed(ActionEvent e) {
  78. m_Result = APPROVE_OPTION;
  79. setVisible(false);
  80.       }
  81.     });
  82.     
  83.     Container c = getContentPane();
  84.     c.setLayout(new BorderLayout());
  85.     //    setBorder(BorderFactory.createTitledBorder("Select a property"));
  86.     Box b1 = new Box(BoxLayout.X_AXIS);
  87.     b1.add(m_SelectBut);
  88.     b1.add(Box.createHorizontalStrut(10));
  89.     b1.add(m_CancelBut);
  90.     c.add(b1, BorderLayout.SOUTH);
  91.     c.add(new JScrollPane(m_List), BorderLayout.CENTER);
  92.     pack();
  93.   }
  94.   /**
  95.    * Pops up the modal dialog and waits for cancel or a selection.
  96.    *
  97.    * @return either APPROVE_OPTION, or CANCEL_OPTION
  98.    */
  99.   public int showDialog() {
  100.     m_Result = CANCEL_OPTION;
  101.     int [] origSelected = m_List.getSelectedIndices();
  102.     setVisible(true);
  103.     if (m_Result == CANCEL_OPTION) {
  104.       m_List.setSelectedIndices(origSelected);
  105.     }
  106.     return m_Result;
  107.   }
  108.   
  109.   /**
  110.    * Tests out the list selector from the command line.
  111.    *
  112.    * @param args ignored
  113.    */
  114.   public static void main(String [] args) {
  115.     try {
  116.       DefaultListModel lm = new DefaultListModel();      
  117.       lm.addElement("one");
  118.       lm.addElement("two");
  119.       lm.addElement("three");
  120.       lm.addElement("four");
  121.       lm.addElement("five");
  122.       JList jl = new JList(lm);
  123.       final ListSelectorDialog jd = new ListSelectorDialog(null, jl);
  124.       int result = jd.showDialog();
  125.       if (result == ListSelectorDialog.APPROVE_OPTION) {
  126. System.err.println("Fields Selected");
  127. int [] selected = jl.getSelectedIndices();
  128. for (int i = 0; i < selected.length; i++) {
  129.   System.err.println("" + selected[i]
  130.      + " " + lm.elementAt(selected[i]));
  131. }
  132.       } else {
  133. System.err.println("Cancelled");
  134.       }
  135.       System.exit(0);
  136.     } catch (Exception ex) {
  137.       ex.printStackTrace();
  138.       System.err.println(ex.getMessage());
  139.     }
  140.   }
  141. }