source: branches/MetisMQI/src/main/java/weka/gui/sql/InfoPanel.java

Last change on this file was 29, checked in by gnappo, 14 years ago

Taggata versione per la demo e aggiunto branch.

File size: 5.3 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 * InfoPanel.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.sql;
24
25import weka.gui.ComponentHelper;
26
27import java.awt.BorderLayout;
28import java.awt.Dimension;
29import java.awt.Toolkit;
30import java.awt.datatransfer.Clipboard;
31import java.awt.datatransfer.StringSelection;
32import java.awt.event.ActionEvent;
33import java.awt.event.ActionListener;
34
35import javax.swing.DefaultListModel;
36import javax.swing.JButton;
37import javax.swing.JFrame;
38import javax.swing.JLabel;
39import javax.swing.JList;
40import javax.swing.JPanel;
41import javax.swing.JScrollPane;
42import javax.swing.event.ListSelectionEvent;
43import javax.swing.event.ListSelectionListener;
44
45/**
46 * A simple panel for displaying information, e.g. progress information etc.
47 *
48 * @author      FracPete (fracpete at waikato dot ac dot nz)
49 * @version     $Revision: 1.3 $
50 */
51
52public class InfoPanel
53  extends JPanel {
54
55  /** for serialization */
56  private static final long serialVersionUID = -7701133696481997973L;
57 
58  /** the parent of this panel */
59  protected JFrame m_Parent;
60 
61  /** the list the contains the messages */
62  protected JList m_Info;
63
64  /** the model for the list */
65  protected DefaultListModel m_Model;
66
67  /** the button to clear the area */
68  protected JButton m_ButtonClear;
69
70  /** the button to copy the selected message */
71  protected JButton m_ButtonCopy;
72 
73  /**
74   * creates the panel
75   * @param parent      the parent of this panel
76   */
77  public InfoPanel(JFrame parent) {
78    super();
79    m_Parent = parent;
80    createPanel();
81  }
82
83  /**
84   * inserts the components into the panel
85   */
86  protected void createPanel() {
87    JPanel          panel;
88    JPanel          panel2;
89   
90    setLayout(new BorderLayout());
91    setPreferredSize(new Dimension(0, 80));
92
93    // text
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) {
99        setButtons(e);
100      }
101    });
102    add(new JScrollPane(m_Info), BorderLayout.CENTER);
103
104    // clear button
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) {
110          clear();
111        }
112      });
113    panel.add(m_ButtonClear, BorderLayout.NORTH);
114
115    // clear button
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) {
121          copyToClipboard();
122        }
123      });
124    panel2.add(m_ButtonCopy, BorderLayout.NORTH);
125  }
126 
127  /**
128   * sets the state of the buttons according to the selection state of the
129   * JList
130   */
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);
135    }
136  }
137
138  /**
139   * sets the focus in a designated control
140   */
141  public void setFocus() {
142    m_Info.requestFocus();
143  }
144
145  /**
146   * clears the content of the panel
147   */
148  public void clear() {
149    m_Model.clear();
150    setButtons(null);
151  }
152
153  /**
154   * copies the currently selected error message to the clipboard
155   *
156   * @return            true if the content was copied
157   */
158  public boolean copyToClipboard() {
159    StringSelection      selection;
160    Clipboard            clipboard;
161   
162    if (m_Info.getSelectedIndices().length != 1)
163      return false;
164   
165    selection = new StringSelection(((JLabel) m_Info.getSelectedValue()).getText());
166    clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
167    clipboard.setContents(selection, selection);
168    return true;
169  }
170 
171  /**
172   * adds the given message to the end of the list (with the associated icon
173   * at the beginning)
174   * @param msg       the message to append to the list
175   * @param icon      the filename of the icon
176   */
177  public void append(String msg, String icon) {
178    append(new JLabel(msg, ComponentHelper.getImageIcon(icon), JLabel.LEFT));
179  }
180
181  /**
182   * adds the given message to the end of the list
183   * @param msg       the message to append to the list
184   */
185  public void append(Object msg) {
186    if (msg instanceof String) {
187      append(msg.toString(), "empty_small.gif");
188      return;
189    }
190
191    m_Model.addElement(msg);
192    m_Info.setSelectedIndex(m_Model.getSize() - 1);
193    m_Info.ensureIndexIsVisible(m_Info.getSelectedIndex());
194   
195    setButtons(null);
196  }
197}
198
Note: See TracBrowser for help on using the repository browser.