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.
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.
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.
19
* Copyright (C) 2000 University of Waikato, Hamilton, New Zealand
23
package weka.gui.experiment;
25
import weka.experiment.RemoteExperiment;
27
import java.awt.BorderLayout;
28
import java.awt.GridBagConstraints;
29
import java.awt.GridBagLayout;
30
import java.awt.Insets;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.awt.event.WindowAdapter;
34
import java.awt.event.WindowEvent;
36
import javax.swing.BorderFactory;
37
import javax.swing.DefaultListModel;
38
import javax.swing.JButton;
39
import javax.swing.JFrame;
40
import javax.swing.JList;
41
import javax.swing.JPanel;
42
import javax.swing.JScrollPane;
43
import javax.swing.JTextField;
46
* This panel controls setting a list of hosts for a RemoteExperiment to
49
* @author Mark Hall (mhall@cs.waikato.ac.nz)
50
* @version $Revision: 1.5 $
52
public class HostListPanel
54
implements ActionListener {
56
/** for serialization */
57
private static final long serialVersionUID = 7182791134585882197L;
59
/** The remote experiment to set the host list of */
60
protected RemoteExperiment m_Exp;
62
/** The component displaying the host list */
63
protected JList m_List;
65
/** Click to remove the selected host from the list */
66
protected JButton m_DeleteBut = new JButton("Delete selected");
68
/** The field with which to enter host names */
69
protected JTextField m_HostField = new JTextField(25);
72
* Creates the host list panel with the given experiment.
74
* @param exp a value of type 'Experiment'
76
public HostListPanel(RemoteExperiment exp) {
82
* Create the host list panel initially disabled.
84
public HostListPanel() {
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"));
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));
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);
109
add(topLab, BorderLayout.NORTH);
110
add(new JScrollPane(m_List), BorderLayout.CENTER);
114
* Tells the panel to act on a new experiment.
116
* @param exp a value of type 'Experiment'
118
public void setExperiment(RemoteExperiment exp) {
120
m_List.setModel(m_Exp.getRemoteHosts());
121
if (((DefaultListModel)m_List.getModel()).size() > 0) {
122
m_DeleteBut.setEnabled(true);
127
* Handle actions when text is entered into the host field or the
128
* delete button is pressed.
130
* @param e a value of type 'ActionEvent'
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);
146
m_List.setSelectedIndex(current - 1);
150
if (((DefaultListModel)m_List.getModel()).size() == 0) {
151
m_DeleteBut.setEnabled(false);
157
* Tests out the host list panel from the command line.
159
* @param args ignored
161
public static void main(String [] args) {
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) {
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());