source: src/main/java/weka/gui/experiment/HostListPanel.java @ 26

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

Import di weka.

File size: 5.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 *    HostListPanel.java
19 *    Copyright (C) 2000 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.experiment;
24
25import weka.experiment.RemoteExperiment;
26
27import java.awt.BorderLayout;
28import java.awt.GridBagConstraints;
29import java.awt.GridBagLayout;
30import java.awt.Insets;
31import java.awt.event.ActionEvent;
32import java.awt.event.ActionListener;
33import java.awt.event.WindowAdapter;
34import java.awt.event.WindowEvent;
35
36import javax.swing.BorderFactory;
37import javax.swing.DefaultListModel;
38import javax.swing.JButton;
39import javax.swing.JFrame;
40import javax.swing.JList;
41import javax.swing.JPanel;
42import javax.swing.JScrollPane;
43import javax.swing.JTextField;
44
45/**
46 * This panel controls setting a list of hosts for a RemoteExperiment to
47 * use.
48 *
49 * @author Mark Hall (mhall@cs.waikato.ac.nz)
50 * @version $Revision: 1.5 $
51 */
52public class HostListPanel
53  extends JPanel
54  implements ActionListener {
55
56  /** for serialization */
57  private static final long serialVersionUID = 7182791134585882197L;
58 
59  /** The remote experiment to set the host list of */
60  protected RemoteExperiment m_Exp;
61
62 /** The component displaying the host list */
63  protected JList m_List;
64
65  /** Click to remove the selected host from the list */
66  protected JButton m_DeleteBut = new JButton("Delete selected");
67
68  /** The field with which to enter host names */
69  protected JTextField m_HostField = new JTextField(25);
70
71  /**
72   * Creates the host list panel with the given experiment.
73   *
74   * @param exp a value of type 'Experiment'
75   */
76  public HostListPanel(RemoteExperiment exp) {
77    this();
78    setExperiment(exp);
79  }
80
81  /**
82   * Create the host list panel initially disabled.
83   */
84  public HostListPanel() {
85    m_List = new JList();
86    m_List.setModel(new DefaultListModel());
87    m_DeleteBut.setEnabled(false);
88    m_DeleteBut.addActionListener(this);
89    m_HostField.addActionListener(this);
90    setLayout(new BorderLayout());
91    setBorder(BorderFactory.createTitledBorder("Hosts"));
92
93    JPanel topLab = new JPanel();
94    GridBagLayout gb = new GridBagLayout();
95    GridBagConstraints constraints = new GridBagConstraints();
96    topLab.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
97    //    topLab.setLayout(new GridLayout(1,2,5,5));
98    topLab.setLayout(gb);
99   
100    constraints.gridx=0;constraints.gridy=0;constraints.weightx=5;
101    constraints.fill = GridBagConstraints.HORIZONTAL;
102    constraints.gridwidth=1;constraints.gridheight=1;
103    constraints.insets = new Insets(0,2,0,2);
104    topLab.add(m_DeleteBut,constraints);
105    constraints.gridx=1;constraints.gridy=0;constraints.weightx=5;
106    constraints.gridwidth=1;constraints.gridheight=1;
107    topLab.add(m_HostField,constraints);
108   
109    add(topLab, BorderLayout.NORTH);
110    add(new JScrollPane(m_List), BorderLayout.CENTER);
111  }
112
113  /**
114   * Tells the panel to act on a new experiment.
115   *
116   * @param exp a value of type 'Experiment'
117   */
118  public void setExperiment(RemoteExperiment exp) {
119    m_Exp = exp;
120    m_List.setModel(m_Exp.getRemoteHosts());
121    if (((DefaultListModel)m_List.getModel()).size() > 0) {
122      m_DeleteBut.setEnabled(true);
123    }
124  }
125
126  /**
127   * Handle actions when text is entered into the host field or the
128   * delete button is pressed.
129   *
130   * @param e a value of type 'ActionEvent'
131   */
132  public void actionPerformed(ActionEvent e) {
133    if (e.getSource() == m_HostField) {
134      ((DefaultListModel)m_List.getModel())
135        .addElement(m_HostField.getText());
136      m_DeleteBut.setEnabled(true);
137    } else if (e.getSource() == m_DeleteBut) {
138      int [] selected = m_List.getSelectedIndices();
139      if (selected != null) {
140        for (int i = selected.length - 1; i >= 0; i--) {
141          int current = selected[i];
142          ((DefaultListModel)m_List.getModel()).removeElementAt(current);
143          if (((DefaultListModel)m_List.getModel()).size() > current) {
144            m_List.setSelectedIndex(current);
145          } else {
146            m_List.setSelectedIndex(current - 1);
147          }
148        }
149      }
150      if (((DefaultListModel)m_List.getModel()).size() == 0) {
151        m_DeleteBut.setEnabled(false);
152      }
153    }
154  }
155
156  /**
157   * Tests out the host list panel from the command line.
158   *
159   * @param args ignored
160   */
161  public static void main(String [] args) {
162
163    try {
164      final JFrame jf = new JFrame("Host List Editor");
165      jf.getContentPane().setLayout(new BorderLayout());
166      HostListPanel dp = new HostListPanel();
167      jf.getContentPane().add(dp,
168                              BorderLayout.CENTER);
169      jf.addWindowListener(new WindowAdapter() {
170        public void windowClosing(WindowEvent e) {
171          jf.dispose();
172          System.exit(0);
173        }
174      });
175      jf.pack();
176      jf.setVisible(true);
177      /* System.err.println("Short nap");
178      Thread.currentThread().sleep(3000);
179      System.err.println("Done"); */
180      //      dp.setExperiment(new Experiment());
181    } catch (Exception ex) {
182      ex.printStackTrace();
183      System.err.println(ex.getMessage());
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.