source: src/main/java/weka/gui/ListSelectorDialog.java @ 6

Last change on this file since 6 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 6.5 KB
Line 
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/*
18 *    ListSelectorDialog.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui;
24
25import java.awt.BorderLayout;
26import java.awt.Container;
27import java.awt.Dimension;
28import java.awt.Frame;
29import java.awt.Toolkit;
30import java.awt.event.ActionEvent;
31import java.awt.event.ActionListener;
32import java.util.regex.Pattern;
33
34import javax.swing.Box;
35import javax.swing.BoxLayout;
36import javax.swing.DefaultListModel;
37import javax.swing.JButton;
38import javax.swing.JDialog;
39import javax.swing.JList;
40import javax.swing.JOptionPane;
41import javax.swing.JScrollPane;
42
43/**
44 * A dialog to present the user with a list of items, that the user can
45 * make a selection from, or cancel the selection.
46 *
47 * @author Len Trigg (trigg@cs.waikato.ac.nz)
48 * @version $Revision: 1.9 $
49 */
50public class ListSelectorDialog
51  extends JDialog {
52
53  /** for serialization */
54  private static final long serialVersionUID = 906147926840288895L;
55 
56  /** Click to choose the currently selected property */
57  protected JButton m_SelectBut = new JButton("Select");
58
59  /** Click to cancel the property selection */
60  protected JButton m_CancelBut = new JButton("Cancel");
61
62  /** Click to enter a regex pattern for selection */
63  protected JButton m_PatternBut = new JButton("Pattern");
64
65  /** The list component */
66  protected JList m_List;
67 
68  /** Whether the selection was made or cancelled */
69  protected int m_Result;
70
71  /** Signifies an OK property selection */
72  public static final int APPROVE_OPTION = 0;
73
74  /** Signifies a cancelled property selection */
75  public static final int CANCEL_OPTION = 1;
76
77  /** The current regular expression. */
78  protected String m_PatternRegEx = ".*";
79 
80  /**
81   * Create the list selection dialog.
82   *
83   * @param parentFrame the parent frame of the dialog
84   * @param userList the JList component the user will select from
85   */
86  public ListSelectorDialog(Frame parentFrame, JList userList) {
87   
88    super(parentFrame, "Select items", true);
89    m_List = userList;
90    m_CancelBut.setMnemonic('C');
91    m_CancelBut.addActionListener(new ActionListener() {
92      public void actionPerformed(ActionEvent e) {
93        m_Result = CANCEL_OPTION;
94        setVisible(false);
95      }
96    });
97    m_SelectBut.setMnemonic('S');
98    m_SelectBut.addActionListener(new ActionListener() {
99      public void actionPerformed(ActionEvent e) {
100        m_Result = APPROVE_OPTION;
101        setVisible(false);
102      }
103    });
104    m_PatternBut.setMnemonic('P');
105    m_PatternBut.addActionListener(new ActionListener() {
106      public void actionPerformed(ActionEvent e) {
107        selectPattern();
108      }
109    });
110   
111    Container c = getContentPane();
112    c.setLayout(new BorderLayout());
113    //    setBorder(BorderFactory.createTitledBorder("Select a property"));
114    Box b1 = new Box(BoxLayout.X_AXIS);
115    b1.add(m_SelectBut);
116    b1.add(Box.createHorizontalStrut(10));
117    b1.add(m_PatternBut);
118    b1.add(Box.createHorizontalStrut(10));
119    b1.add(m_CancelBut);
120    c.add(b1, BorderLayout.SOUTH);
121    c.add(new JScrollPane(m_List), BorderLayout.CENTER);
122
123    getRootPane().setDefaultButton(m_SelectBut);
124   
125    pack();
126
127    // make sure, it's not bigger than the screen
128    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
129    int width  = getWidth() > screen.getWidth() 
130                    ? (int) screen.getWidth() : getWidth();
131    int height = getHeight() > screen.getHeight() 
132                    ? (int) screen.getHeight() : getHeight();
133    setSize(width, height);
134  }
135
136  /**
137   * Pops up the modal dialog and waits for cancel or a selection.
138   *
139   * @return either APPROVE_OPTION, or CANCEL_OPTION
140   */
141  public int showDialog() {
142
143    m_Result = CANCEL_OPTION;
144    int [] origSelected = m_List.getSelectedIndices();
145    setVisible(true);
146    if (m_Result == CANCEL_OPTION) {
147      m_List.setSelectedIndices(origSelected);
148    }
149    return m_Result;
150  }
151
152  /**
153   * opens a separate dialog for entering a regex pattern for selecting
154   * elements from the provided list
155   */
156  protected void selectPattern() {
157    String pattern = JOptionPane.showInputDialog(
158                        m_PatternBut.getParent(),
159                        "Enter a Perl regular expression ('.*' for all)",
160                        m_PatternRegEx);
161    if (pattern != null) {
162      try {
163        Pattern.compile(pattern);
164        m_PatternRegEx = pattern;
165        m_List.clearSelection();
166        for (int i = 0; i < m_List.getModel().getSize(); i++) {
167          if (Pattern.matches(
168                pattern, m_List.getModel().getElementAt(i).toString()))
169            m_List.addSelectionInterval(i, i);
170        }
171      }
172      catch (Exception ex) {
173        JOptionPane.showMessageDialog(
174          m_PatternBut.getParent(),
175          "'" + pattern + "' is not a valid Perl regular expression!\n" 
176          + "Error: " + ex, 
177          "Error in Pattern...", 
178          JOptionPane.ERROR_MESSAGE);
179      }
180    }
181  }
182 
183  /**
184   * Tests out the list selector from the command line.
185   *
186   * @param args ignored
187   */
188  public static void main(String [] args) {
189
190    try {
191      DefaultListModel lm = new DefaultListModel();     
192      lm.addElement("one");
193      lm.addElement("two");
194      lm.addElement("three");
195      lm.addElement("four");
196      lm.addElement("five");
197      JList jl = new JList(lm);
198      final ListSelectorDialog jd = new ListSelectorDialog(null, jl);
199      int result = jd.showDialog();
200      if (result == ListSelectorDialog.APPROVE_OPTION) {
201        System.err.println("Fields Selected");
202        int [] selected = jl.getSelectedIndices();
203        for (int i = 0; i < selected.length; i++) {
204          System.err.println("" + selected[i]
205                             + " " + lm.elementAt(selected[i]));
206        }
207      } else {
208        System.err.println("Cancelled");
209      }
210      System.exit(0);
211    } catch (Exception ex) {
212      ex.printStackTrace();
213      System.err.println(ex.getMessage());
214    }
215  }
216}
Note: See TracBrowser for help on using the repository browser.