[29] | 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 | * ViewerDialog.java |
---|
| 19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui; |
---|
| 24 | |
---|
| 25 | import weka.core.Instances; |
---|
| 26 | import weka.gui.arffviewer.ArffPanel; |
---|
| 27 | |
---|
| 28 | import java.awt.BorderLayout; |
---|
| 29 | import java.awt.FlowLayout; |
---|
| 30 | import java.awt.Frame; |
---|
| 31 | import java.awt.event.ActionEvent; |
---|
| 32 | import java.awt.event.ActionListener; |
---|
| 33 | |
---|
| 34 | import javax.swing.JButton; |
---|
| 35 | import javax.swing.JDialog; |
---|
| 36 | import javax.swing.JPanel; |
---|
| 37 | import javax.swing.event.ChangeEvent; |
---|
| 38 | import javax.swing.event.ChangeListener; |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * A downsized version of the ArffViewer, displaying only one Instances-Object. |
---|
| 42 | * |
---|
| 43 | * |
---|
| 44 | * @see weka.gui.arffviewer.ArffViewer |
---|
| 45 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
| 46 | * @version $Revision: 1.4 $ |
---|
| 47 | */ |
---|
| 48 | public class ViewerDialog |
---|
| 49 | extends JDialog |
---|
| 50 | implements ChangeListener { |
---|
| 51 | |
---|
| 52 | /** for serialization */ |
---|
| 53 | private static final long serialVersionUID = 6747718484736047752L; |
---|
| 54 | |
---|
| 55 | /** Signifies an OK property selection */ |
---|
| 56 | public static final int APPROVE_OPTION = 0; |
---|
| 57 | |
---|
| 58 | /** Signifies a cancelled property selection */ |
---|
| 59 | public static final int CANCEL_OPTION = 1; |
---|
| 60 | |
---|
| 61 | /** the result of the user's action, either OK or CANCEL */ |
---|
| 62 | protected int m_Result = CANCEL_OPTION; |
---|
| 63 | |
---|
| 64 | /** Click to activate the current set parameters */ |
---|
| 65 | protected JButton m_OkButton = new JButton("OK"); |
---|
| 66 | |
---|
| 67 | /** Click to cancel the dialog */ |
---|
| 68 | protected JButton m_CancelButton = new JButton("Cancel"); |
---|
| 69 | |
---|
| 70 | /** Click to undo the last action */ |
---|
| 71 | protected JButton m_UndoButton = new JButton("Undo"); |
---|
| 72 | |
---|
| 73 | /** the panel to display the Instances-object */ |
---|
| 74 | protected ArffPanel m_ArffPanel = new ArffPanel(); |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * initializes the dialog with the given parent |
---|
| 78 | * |
---|
| 79 | * @param parent the parent for this dialog |
---|
| 80 | */ |
---|
| 81 | public ViewerDialog(Frame parent) { |
---|
| 82 | super(parent, true); |
---|
| 83 | createDialog(); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * creates all the elements of the dialog |
---|
| 88 | */ |
---|
| 89 | protected void createDialog() { |
---|
| 90 | JPanel panel; |
---|
| 91 | |
---|
| 92 | setTitle("Viewer"); |
---|
| 93 | |
---|
| 94 | getContentPane().setLayout(new BorderLayout()); |
---|
| 95 | |
---|
| 96 | // ArffPanel |
---|
| 97 | m_ArffPanel.addChangeListener(this); |
---|
| 98 | getContentPane().add(m_ArffPanel, BorderLayout.CENTER); |
---|
| 99 | |
---|
| 100 | // Buttons |
---|
| 101 | panel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
---|
| 102 | getContentPane().add(panel, BorderLayout.SOUTH); |
---|
| 103 | m_UndoButton.addActionListener(new ActionListener() { |
---|
| 104 | public void actionPerformed(ActionEvent e) { |
---|
| 105 | undo(); |
---|
| 106 | } |
---|
| 107 | }); |
---|
| 108 | getContentPane().add(panel, BorderLayout.SOUTH); |
---|
| 109 | m_CancelButton.addActionListener(new ActionListener() { |
---|
| 110 | public void actionPerformed(ActionEvent e) { |
---|
| 111 | m_Result = CANCEL_OPTION; |
---|
| 112 | setVisible(false); |
---|
| 113 | } |
---|
| 114 | }); |
---|
| 115 | m_OkButton.addActionListener(new ActionListener() { |
---|
| 116 | public void actionPerformed(ActionEvent e) { |
---|
| 117 | m_Result = APPROVE_OPTION; |
---|
| 118 | setVisible(false); |
---|
| 119 | } |
---|
| 120 | }); |
---|
| 121 | panel.add(m_UndoButton); |
---|
| 122 | panel.add(m_OkButton); |
---|
| 123 | panel.add(m_CancelButton); |
---|
| 124 | |
---|
| 125 | pack(); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * sets the instances to display |
---|
| 130 | */ |
---|
| 131 | public void setInstances(Instances inst) { |
---|
| 132 | m_ArffPanel.setInstances(new Instances(inst)); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * returns the currently displayed instances |
---|
| 137 | */ |
---|
| 138 | public Instances getInstances() { |
---|
| 139 | return m_ArffPanel.getInstances(); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * sets the state of the buttons |
---|
| 144 | */ |
---|
| 145 | protected void setButtons() { |
---|
| 146 | m_OkButton.setEnabled(true); |
---|
| 147 | m_CancelButton.setEnabled(true); |
---|
| 148 | m_UndoButton.setEnabled(m_ArffPanel.canUndo()); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * returns whether the data has been changed |
---|
| 153 | * |
---|
| 154 | * @return true if the data has been changed |
---|
| 155 | */ |
---|
| 156 | public boolean isChanged() { |
---|
| 157 | return m_ArffPanel.isChanged(); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * undoes the last action |
---|
| 162 | */ |
---|
| 163 | private void undo() { |
---|
| 164 | m_ArffPanel.undo(); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Invoked when the target of the listener has changed its state. |
---|
| 169 | */ |
---|
| 170 | public void stateChanged(ChangeEvent e) { |
---|
| 171 | setButtons(); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * Pops up the modal dialog and waits for Cancel or OK. |
---|
| 176 | * |
---|
| 177 | * @return either APPROVE_OPTION, or CANCEL_OPTION |
---|
| 178 | */ |
---|
| 179 | public int showDialog() { |
---|
| 180 | m_Result = CANCEL_OPTION; |
---|
| 181 | setVisible(true); |
---|
| 182 | setButtons(); |
---|
| 183 | return m_Result; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * Pops up the modal dialog and waits for Cancel or OK. |
---|
| 188 | * |
---|
| 189 | * @param inst the instances to display |
---|
| 190 | * @return either APPROVE_OPTION, or CANCEL_OPTION |
---|
| 191 | */ |
---|
| 192 | public int showDialog(Instances inst) { |
---|
| 193 | setInstances(inst); |
---|
| 194 | return showDialog(); |
---|
| 195 | } |
---|
| 196 | } |
---|