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) 2005 University of Waikato, Hamilton, New Zealand
25
import weka.gui.ComponentHelper;
27
import java.awt.BorderLayout;
28
import java.awt.Dimension;
29
import java.awt.Toolkit;
30
import java.awt.datatransfer.Clipboard;
31
import java.awt.datatransfer.StringSelection;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
35
import javax.swing.DefaultListModel;
36
import javax.swing.JButton;
37
import javax.swing.JFrame;
38
import javax.swing.JLabel;
39
import javax.swing.JList;
40
import javax.swing.JPanel;
41
import javax.swing.JScrollPane;
42
import javax.swing.event.ListSelectionEvent;
43
import javax.swing.event.ListSelectionListener;
46
* A simple panel for displaying information, e.g. progress information etc.
48
* @author FracPete (fracpete at waikato dot ac dot nz)
49
* @version $Revision: 1.3 $
52
public class InfoPanel
55
/** for serialization */
56
private static final long serialVersionUID = -7701133696481997973L;
58
/** the parent of this panel */
59
protected JFrame m_Parent;
61
/** the list the contains the messages */
62
protected JList m_Info;
64
/** the model for the list */
65
protected DefaultListModel m_Model;
67
/** the button to clear the area */
68
protected JButton m_ButtonClear;
70
/** the button to copy the selected message */
71
protected JButton m_ButtonCopy;
75
* @param parent the parent of this panel
77
public InfoPanel(JFrame parent) {
84
* inserts the components into the panel
86
protected void createPanel() {
90
setLayout(new BorderLayout());
91
setPreferredSize(new Dimension(0, 80));
94
m_Model = new DefaultListModel();
95
m_Info = new JList(m_Model);
96
m_Info.setCellRenderer(new InfoPanelCellRenderer());
97
m_Info.addListSelectionListener(new ListSelectionListener() {
98
public void valueChanged(ListSelectionEvent e) {
102
add(new JScrollPane(m_Info), BorderLayout.CENTER);
105
panel = new JPanel(new BorderLayout());
106
add(panel, BorderLayout.EAST);
107
m_ButtonClear = new JButton("Clear");
108
m_ButtonClear.addActionListener(new ActionListener() {
109
public void actionPerformed(ActionEvent e) {
113
panel.add(m_ButtonClear, BorderLayout.NORTH);
116
panel2 = new JPanel(new BorderLayout());
117
panel.add(panel2, BorderLayout.CENTER);
118
m_ButtonCopy = new JButton("Copy");
119
m_ButtonCopy.addActionListener(new ActionListener() {
120
public void actionPerformed(ActionEvent e) {
124
panel2.add(m_ButtonCopy, BorderLayout.NORTH);
128
* sets the state of the buttons according to the selection state of the
131
protected void setButtons(ListSelectionEvent e) {
132
if ( (e == null) || (e.getSource() == m_Info) ) {
133
m_ButtonClear.setEnabled(m_Model.getSize() > 0);
134
m_ButtonCopy.setEnabled(m_Info.getSelectedIndices().length == 1);
139
* sets the focus in a designated control
141
public void setFocus() {
142
m_Info.requestFocus();
146
* clears the content of the panel
148
public void clear() {
154
* copies the currently selected error message to the clipboard
156
* @return true if the content was copied
158
public boolean copyToClipboard() {
159
StringSelection selection;
162
if (m_Info.getSelectedIndices().length != 1)
165
selection = new StringSelection(((JLabel) m_Info.getSelectedValue()).getText());
166
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
167
clipboard.setContents(selection, selection);
172
* adds the given message to the end of the list (with the associated icon
174
* @param msg the message to append to the list
175
* @param icon the filename of the icon
177
public void append(String msg, String icon) {
178
append(new JLabel(msg, ComponentHelper.getImageIcon(icon), JLabel.LEFT));
182
* adds the given message to the end of the list
183
* @param msg the message to append to the list
185
public void append(Object msg) {
186
if (msg instanceof String) {
187
append(msg.toString(), "empty_small.gif");
191
m_Model.addElement(msg);
192
m_Info.setSelectedIndex(m_Model.getSize() - 1);
193
m_Info.ensureIndexIsVisible(m_Info.getSelectedIndex());