[4] | 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 | * ResultHistoryPanel.java |
---|
| 19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | package weka.gui; |
---|
| 25 | |
---|
| 26 | import weka.gui.visualize.PrintableComponent; |
---|
| 27 | |
---|
| 28 | import java.awt.BorderLayout; |
---|
| 29 | import java.awt.Font; |
---|
| 30 | import java.awt.Point; |
---|
| 31 | import java.awt.event.InputEvent; |
---|
| 32 | import java.awt.event.KeyAdapter; |
---|
| 33 | import java.awt.event.KeyEvent; |
---|
| 34 | import java.awt.event.MouseAdapter; |
---|
| 35 | import java.awt.event.MouseEvent; |
---|
| 36 | import java.awt.event.WindowAdapter; |
---|
| 37 | import java.awt.event.WindowEvent; |
---|
| 38 | import java.io.Serializable; |
---|
| 39 | import java.util.Hashtable; |
---|
| 40 | |
---|
| 41 | import javax.swing.BorderFactory; |
---|
| 42 | import javax.swing.DefaultListModel; |
---|
| 43 | import javax.swing.JFrame; |
---|
| 44 | import javax.swing.JList; |
---|
| 45 | import javax.swing.JPanel; |
---|
| 46 | import javax.swing.JScrollPane; |
---|
| 47 | import javax.swing.JTextArea; |
---|
| 48 | import javax.swing.JViewport; |
---|
| 49 | import javax.swing.ListSelectionModel; |
---|
| 50 | import javax.swing.event.ChangeEvent; |
---|
| 51 | import javax.swing.event.ChangeListener; |
---|
| 52 | import javax.swing.event.ListSelectionEvent; |
---|
| 53 | import javax.swing.event.ListSelectionListener; |
---|
| 54 | import javax.swing.text.JTextComponent; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * A component that accepts named stringbuffers and displays the name in a list |
---|
| 58 | * box. When a name is right-clicked, a frame is popped up that contains |
---|
| 59 | * the string held by the stringbuffer. Optionally a text component may be |
---|
| 60 | * provided that will have it's text set to the named result text on a |
---|
| 61 | * left-click. |
---|
| 62 | * |
---|
| 63 | * @author Len Trigg (trigg@cs.waikato.ac.nz) |
---|
| 64 | * @version $Revision: 1.26 $ |
---|
| 65 | */ |
---|
| 66 | public class ResultHistoryPanel |
---|
| 67 | extends JPanel { |
---|
| 68 | |
---|
| 69 | /** for serialization */ |
---|
| 70 | static final long serialVersionUID = 4297069440135326829L; |
---|
| 71 | |
---|
| 72 | /** An optional component for single-click display */ |
---|
| 73 | protected JTextComponent m_SingleText; |
---|
| 74 | |
---|
| 75 | /** The named result being viewed in the single-click display */ |
---|
| 76 | protected String m_SingleName; |
---|
| 77 | |
---|
| 78 | /** The list model */ |
---|
| 79 | protected DefaultListModel m_Model = new DefaultListModel(); |
---|
| 80 | |
---|
| 81 | /** The list component */ |
---|
| 82 | protected JList m_List = new JList(m_Model); |
---|
| 83 | |
---|
| 84 | /** A Hashtable mapping names to result buffers */ |
---|
| 85 | protected Hashtable m_Results = new Hashtable(); |
---|
| 86 | |
---|
| 87 | /** A Hashtable mapping names to output text components */ |
---|
| 88 | protected Hashtable m_FramedOutput = new Hashtable(); |
---|
| 89 | |
---|
| 90 | /** A hashtable mapping names to arbitrary objects */ |
---|
| 91 | protected Hashtable m_Objs = new Hashtable(); |
---|
| 92 | |
---|
| 93 | /** Let the result history list handle right clicks in the default |
---|
| 94 | manner---ie, pop up a window displaying the buffer */ |
---|
| 95 | protected boolean m_HandleRightClicks = true; |
---|
| 96 | |
---|
| 97 | /** for printing the output to files */ |
---|
| 98 | protected PrintableComponent m_Printer = null; |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Extension of MouseAdapter that implements Serializable. |
---|
| 102 | */ |
---|
| 103 | public static class RMouseAdapter |
---|
| 104 | extends MouseAdapter implements Serializable { |
---|
| 105 | |
---|
| 106 | /** for serialization */ |
---|
| 107 | static final long serialVersionUID = -8991922650552358669L; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Extension of KeyAdapter that implements Serializable. |
---|
| 113 | */ |
---|
| 114 | public static class RKeyAdapter |
---|
| 115 | extends KeyAdapter implements Serializable { |
---|
| 116 | |
---|
| 117 | /** for serialization */ |
---|
| 118 | static final long serialVersionUID = -8675332541861828079L; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * Create the result history object |
---|
| 123 | * |
---|
| 124 | * @param text the optional text component for single-click display |
---|
| 125 | */ |
---|
| 126 | public ResultHistoryPanel(JTextComponent text) { |
---|
| 127 | m_SingleText = text; |
---|
| 128 | if (text != null) { |
---|
| 129 | m_Printer = new PrintableComponent(m_SingleText); |
---|
| 130 | } |
---|
| 131 | m_List.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
---|
| 132 | m_List.addMouseListener(new RMouseAdapter() { |
---|
| 133 | private static final long serialVersionUID = -9015397020486290479L; |
---|
| 134 | |
---|
| 135 | public void mouseClicked(MouseEvent e) { |
---|
| 136 | if ((e.getModifiers() & InputEvent.BUTTON1_MASK) |
---|
| 137 | == InputEvent.BUTTON1_MASK) { |
---|
| 138 | if ( ((e.getModifiers() & InputEvent.SHIFT_DOWN_MASK) == 0) |
---|
| 139 | && ((e.getModifiers() & InputEvent.CTRL_DOWN_MASK) == 0) ) { |
---|
| 140 | int index = m_List.locationToIndex(e.getPoint()); |
---|
| 141 | if ((index != -1) && (m_SingleText != null)) { |
---|
| 142 | setSingle((String)m_Model.elementAt(index)); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } else { |
---|
| 146 | // if there are stored objects then assume that the storer |
---|
| 147 | // will handle popping up the text in a seperate frame |
---|
| 148 | if (m_HandleRightClicks) { |
---|
| 149 | int index = m_List.locationToIndex(e.getPoint()); |
---|
| 150 | if (index != -1) { |
---|
| 151 | String name = (String)m_Model.elementAt(index); |
---|
| 152 | openFrame(name); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | }); |
---|
| 158 | |
---|
| 159 | m_List.addKeyListener(new RKeyAdapter() { |
---|
| 160 | private static final long serialVersionUID = 7910681776999302344L; |
---|
| 161 | |
---|
| 162 | public void keyReleased(KeyEvent e) { |
---|
| 163 | if (e.getKeyCode() == KeyEvent.VK_DELETE) { |
---|
| 164 | int selected = m_List.getSelectedIndex(); |
---|
| 165 | if (selected != -1) { |
---|
| 166 | removeResult((String)m_Model.elementAt(selected)); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | }); |
---|
| 171 | m_List.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
---|
| 172 | public void valueChanged(ListSelectionEvent e) { |
---|
| 173 | if (!e.getValueIsAdjusting()) { |
---|
| 174 | ListSelectionModel lm = (ListSelectionModel) e.getSource(); |
---|
| 175 | for (int i = e.getFirstIndex(); i <= e.getLastIndex(); i++) { |
---|
| 176 | if (lm.isSelectedIndex(i)) { |
---|
| 177 | //m_AttSummaryPanel.setAttribute(i); |
---|
| 178 | if ((i != -1) && (m_SingleText != null)) { |
---|
| 179 | setSingle((String)m_Model.elementAt(i)); |
---|
| 180 | } |
---|
| 181 | break; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | }); |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | setLayout(new BorderLayout()); |
---|
| 190 | // setBorder(BorderFactory.createTitledBorder("Result history")); |
---|
| 191 | final JScrollPane js = new JScrollPane(m_List); |
---|
| 192 | js.getViewport().addChangeListener(new ChangeListener() { |
---|
| 193 | private int lastHeight; |
---|
| 194 | public void stateChanged(ChangeEvent e) { |
---|
| 195 | JViewport vp = (JViewport)e.getSource(); |
---|
| 196 | int h = vp.getViewSize().height; |
---|
| 197 | if (h != lastHeight) { // i.e. an addition not just a user scrolling |
---|
| 198 | lastHeight = h; |
---|
| 199 | int x = h - vp.getExtentSize().height; |
---|
| 200 | vp.setViewPosition(new Point(0, x)); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | }); |
---|
| 204 | add(js, BorderLayout.CENTER); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
| 208 | * Adds a new result to the result list. |
---|
| 209 | * |
---|
| 210 | * @param name the name to associate with the result |
---|
| 211 | * @param result the StringBuffer that contains the result text |
---|
| 212 | */ |
---|
| 213 | public void addResult(String name, StringBuffer result) { |
---|
| 214 | |
---|
| 215 | m_Model.addElement(name); |
---|
| 216 | m_Results.put(name, result); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * Removes one of the result buffers from the history. Any windows currently |
---|
| 221 | * displaying the contents of the buffer are not affected. |
---|
| 222 | * |
---|
| 223 | * @param name the name of the buffer to remove. |
---|
| 224 | */ |
---|
| 225 | public void removeResult(String name) { |
---|
| 226 | |
---|
| 227 | StringBuffer buff = (StringBuffer) m_Results.get(name); |
---|
| 228 | if (buff != null) { |
---|
| 229 | m_Results.remove(name); |
---|
| 230 | m_Model.removeElement(name); |
---|
| 231 | m_Objs.remove(name); |
---|
| 232 | System.gc(); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * Removes all of the result buffers from the history. Any windows currently |
---|
| 238 | * displaying the contents of the buffer are not affected. |
---|
| 239 | */ |
---|
| 240 | public void clearResults() { |
---|
| 241 | m_Results.clear(); |
---|
| 242 | m_Model.clear(); |
---|
| 243 | m_Objs.clear(); |
---|
| 244 | System.gc(); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * Adds an object to the results list |
---|
| 249 | * @param name the name to associate with the object |
---|
| 250 | * @param o the object |
---|
| 251 | */ |
---|
| 252 | public void addObject(String name, Object o) { |
---|
| 253 | m_Objs.put(name, o); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | /** |
---|
| 257 | * Get the named object from the list |
---|
| 258 | * @param name the name of the item to retrieve the stored object |
---|
| 259 | * for |
---|
| 260 | * @return the object or null if there is no object at this index |
---|
| 261 | */ |
---|
| 262 | public Object getNamedObject(String name) { |
---|
| 263 | Object v = null; |
---|
| 264 | v = m_Objs.get(name); |
---|
| 265 | return v; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | /** |
---|
| 269 | * Gets the object associated with the currently |
---|
| 270 | * selected item in the list. |
---|
| 271 | * @return the object or null if there is no |
---|
| 272 | * object corresponding to the current selection in |
---|
| 273 | * the list |
---|
| 274 | */ |
---|
| 275 | public Object getSelectedObject() { |
---|
| 276 | Object v = null; |
---|
| 277 | int index = m_List.getSelectedIndex(); |
---|
| 278 | if (index != -1) { |
---|
| 279 | String name = (String)(m_Model.elementAt(index)); |
---|
| 280 | v = m_Objs.get(name); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | return v; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | /** |
---|
| 287 | * Gets the named buffer |
---|
| 288 | * @return the buffer or null if there are no items in |
---|
| 289 | * the list |
---|
| 290 | */ |
---|
| 291 | public StringBuffer getNamedBuffer(String name) { |
---|
| 292 | StringBuffer b = null; |
---|
| 293 | b = (StringBuffer)(m_Results.get(name)); |
---|
| 294 | return b; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /** |
---|
| 298 | * Gets the buffer associated with the currently |
---|
| 299 | * selected item in the list. |
---|
| 300 | * @return the buffer or null if there are no items in |
---|
| 301 | * the list |
---|
| 302 | */ |
---|
| 303 | public StringBuffer getSelectedBuffer() { |
---|
| 304 | StringBuffer b = null; |
---|
| 305 | int index = m_List.getSelectedIndex(); |
---|
| 306 | if (index != -1) { |
---|
| 307 | String name = (String)(m_Model.elementAt(index)); |
---|
| 308 | b = (StringBuffer)(m_Results.get(name)); |
---|
| 309 | } |
---|
| 310 | return b; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | /** |
---|
| 314 | * Get the name of the currently selected item in the list |
---|
| 315 | * @return the name of the currently selected item or null if no |
---|
| 316 | * item selected |
---|
| 317 | */ |
---|
| 318 | public String getSelectedName() { |
---|
| 319 | int index = m_List.getSelectedIndex(); |
---|
| 320 | if (index != -1) { |
---|
| 321 | return (String)(m_Model.elementAt(index)); |
---|
| 322 | } |
---|
| 323 | return null; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | /** |
---|
| 327 | * Gets the name of theitem in the list at the specified index |
---|
| 328 | * @return the name of item or null if there is no item at that index |
---|
| 329 | */ |
---|
| 330 | public String getNameAtIndex(int index) { |
---|
| 331 | if (index != -1) { |
---|
| 332 | return (String)(m_Model.elementAt(index)); |
---|
| 333 | } |
---|
| 334 | return null; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | /** |
---|
| 338 | * Sets the single-click display to view the named result. |
---|
| 339 | * |
---|
| 340 | * @param name the name of the result to display. |
---|
| 341 | */ |
---|
| 342 | public void setSingle(String name) { |
---|
| 343 | |
---|
| 344 | StringBuffer buff = (StringBuffer) m_Results.get(name); |
---|
| 345 | if (buff != null) { |
---|
| 346 | m_SingleName = name; |
---|
| 347 | m_SingleText.setText(buff.toString()); |
---|
| 348 | m_List.setSelectedValue(name, true); |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | /** |
---|
| 353 | * Opens the named result in a separate frame. |
---|
| 354 | * |
---|
| 355 | * @param name the name of the result to open. |
---|
| 356 | */ |
---|
| 357 | public void openFrame(String name) { |
---|
| 358 | |
---|
| 359 | StringBuffer buff = (StringBuffer) m_Results.get(name); |
---|
| 360 | JTextComponent currentText = (JTextComponent) m_FramedOutput.get(name); |
---|
| 361 | if ((buff != null) && (currentText == null)) { |
---|
| 362 | // Open the frame. |
---|
| 363 | JTextArea ta = new JTextArea(); |
---|
| 364 | ta.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
---|
| 365 | ta.setFont(new Font("Monospaced", Font.PLAIN, 12)); |
---|
| 366 | ta.setEditable(false); |
---|
| 367 | ta.setText(buff.toString()); |
---|
| 368 | m_FramedOutput.put(name, ta); |
---|
| 369 | final JFrame jf = new JFrame(name); |
---|
| 370 | jf.addWindowListener(new WindowAdapter() { |
---|
| 371 | public void windowClosing(WindowEvent e) { |
---|
| 372 | m_FramedOutput.remove(jf.getTitle()); |
---|
| 373 | jf.dispose(); |
---|
| 374 | } |
---|
| 375 | }); |
---|
| 376 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
| 377 | jf.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER); |
---|
| 378 | jf.pack(); |
---|
| 379 | jf.setSize(450, 350); |
---|
| 380 | jf.setVisible(true); |
---|
| 381 | } |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /** |
---|
| 385 | * Tells any component currently displaying the named result that the |
---|
| 386 | * contents of the result text in the StringBuffer have been updated. |
---|
| 387 | * |
---|
| 388 | * @param name the name of the result that has been updated. |
---|
| 389 | */ |
---|
| 390 | public void updateResult(String name) { |
---|
| 391 | |
---|
| 392 | StringBuffer buff = (StringBuffer) m_Results.get(name); |
---|
| 393 | if (buff == null) { |
---|
| 394 | return; |
---|
| 395 | } |
---|
| 396 | if (m_SingleName == name) { |
---|
| 397 | m_SingleText.setText(buff.toString()); |
---|
| 398 | } |
---|
| 399 | JTextComponent currentText = (JTextComponent) m_FramedOutput.get(name); |
---|
| 400 | if (currentText != null) { |
---|
| 401 | currentText.setText(buff.toString()); |
---|
| 402 | } |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | /** |
---|
| 406 | * Gets the selection model used by the results list. |
---|
| 407 | * |
---|
| 408 | * @return a value of type 'ListSelectionModel' |
---|
| 409 | */ |
---|
| 410 | public ListSelectionModel getSelectionModel() { |
---|
| 411 | |
---|
| 412 | return m_List.getSelectionModel(); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | /** |
---|
| 416 | * Gets the JList used by the results list |
---|
| 417 | * @return the JList |
---|
| 418 | */ |
---|
| 419 | public JList getList() { |
---|
| 420 | return m_List; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | /** |
---|
| 424 | * Set whether the result history list should handle right clicks |
---|
| 425 | * or whether the parent object will handle them. |
---|
| 426 | * @param tf false if parent object will handle right clicks |
---|
| 427 | */ |
---|
| 428 | public void setHandleRightClicks(boolean tf) { |
---|
| 429 | m_HandleRightClicks = tf; |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | |
---|
| 433 | /** |
---|
| 434 | * Tests out the result history from the command line. |
---|
| 435 | * |
---|
| 436 | * @param args ignored |
---|
| 437 | */ |
---|
| 438 | public static void main(String [] args) { |
---|
| 439 | |
---|
| 440 | try { |
---|
| 441 | final javax.swing.JFrame jf = |
---|
| 442 | new javax.swing.JFrame("Weka Explorer: Classifier"); |
---|
| 443 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
| 444 | final ResultHistoryPanel jd = new ResultHistoryPanel(null); |
---|
| 445 | jd.addResult("blah", new StringBuffer("Nothing to see here")); |
---|
| 446 | jd.addResult("blah1", new StringBuffer("Nothing to see here1")); |
---|
| 447 | jd.addResult("blah2", new StringBuffer("Nothing to see here2")); |
---|
| 448 | jd.addResult("blah3", new StringBuffer("Nothing to see here3")); |
---|
| 449 | jf.getContentPane().add(jd, BorderLayout.CENTER); |
---|
| 450 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
| 451 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
| 452 | jf.dispose(); |
---|
| 453 | System.exit(0); |
---|
| 454 | } |
---|
| 455 | }); |
---|
| 456 | jf.pack(); |
---|
| 457 | jf.setVisible(true); |
---|
| 458 | } catch (Exception ex) { |
---|
| 459 | ex.printStackTrace(); |
---|
| 460 | System.err.println(ex.getMessage()); |
---|
| 461 | } |
---|
| 462 | } |
---|
| 463 | } |
---|