source: src/main/java/weka/gui/beans/TextViewer.java @ 9

Last change on this file since 9 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 17.9 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 *    TextViewer.java
19 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.beans;
24
25import weka.gui.ResultHistoryPanel;
26import weka.gui.SaveBuffer;
27import weka.gui.Logger;
28
29import java.awt.BorderLayout;
30import java.awt.Font;
31import java.awt.event.ActionEvent;
32import java.awt.event.ActionListener;
33import java.awt.event.InputEvent;
34import java.awt.event.MouseAdapter;
35import java.awt.event.MouseEvent;
36import java.beans.EventSetDescriptor;
37import java.beans.PropertyChangeListener;
38import java.beans.VetoableChangeListener;
39import java.beans.beancontext.BeanContext;
40import java.beans.beancontext.BeanContextChild;
41import java.beans.beancontext.BeanContextChildSupport;
42import java.text.SimpleDateFormat;
43import java.util.Date;
44import java.util.Enumeration;
45import java.util.Vector;
46
47import javax.swing.BorderFactory;
48import javax.swing.JFrame;
49import javax.swing.JMenuItem;
50import javax.swing.JPanel;
51import javax.swing.JPopupMenu;
52import javax.swing.JScrollPane;
53import javax.swing.JTextArea;
54
55/**
56 * Bean that collects and displays pieces of text
57 *
58 * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>
59 * @version $Revision: 4806 $
60 */
61public class TextViewer 
62  extends JPanel
63  implements TextListener, DataSourceListener, 
64             TrainingSetListener, TestSetListener,
65             Visible, UserRequestAcceptor, 
66             BeanContextChild,
67             BeanCommon,
68             EventConstraints {
69
70  /** for serialization */
71  private static final long serialVersionUID = 104838186352536832L;
72
73  protected BeanVisual m_visual;
74
75
76  private transient JFrame m_resultsFrame = null;
77
78  /**
79   * Output area for a piece of text
80   */
81  private transient JTextArea m_outText = null;// = new JTextArea(20, 80);
82
83  /**
84   * List of text revieved so far
85   */
86  protected transient ResultHistoryPanel m_history;
87
88  /**
89   * True if this bean's appearance is the design mode appearance
90   */
91  protected boolean m_design;
92 
93  /**
94   * BeanContex that this bean might be contained within
95   */
96  protected transient BeanContext m_beanContext = null;
97
98  /**
99   * BeanContextChild support
100   */
101  protected BeanContextChildSupport m_bcSupport = 
102    new BeanContextChildSupport(this);
103
104  /**
105   * Objects listening for text events
106   */
107  private Vector m_textListeners = new Vector();
108
109  private transient Logger m_log = null;
110
111 
112  public TextViewer() {
113    /*    setUpResultHistory();
114    setLayout(new BorderLayout());
115    add(m_visual, BorderLayout.CENTER); */
116    java.awt.GraphicsEnvironment ge = 
117      java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); 
118    if (!ge.isHeadless()) {
119      appearanceFinal();
120    }
121  }
122
123  protected void appearanceDesign() {
124    setUpResultHistory();
125    removeAll();
126    m_visual =  new BeanVisual("TextViewer", 
127                               BeanVisual.ICON_PATH+"DefaultText.gif",
128                               BeanVisual.ICON_PATH+"DefaultText_animated.gif");
129    setLayout(new BorderLayout());
130    add(m_visual, BorderLayout.CENTER);
131  }
132
133  protected void appearanceFinal() {
134    removeAll();
135    setLayout(new BorderLayout());
136    setUpFinal();
137  }
138
139  protected void setUpFinal() {
140    setUpResultHistory();
141    JPanel holder = new JPanel();
142    holder.setLayout(new BorderLayout());
143    JScrollPane js = new JScrollPane(m_outText);
144    js.setBorder(BorderFactory.createTitledBorder("Text"));
145    holder.add(js, BorderLayout.CENTER);
146    holder.add(m_history, BorderLayout.WEST);
147
148    add(holder, BorderLayout.CENTER);
149  }
150
151  /**
152   * Global info for this bean
153   *
154   * @return a <code>String</code> value
155   */
156  public String globalInfo() {
157    return "General purpose text display.";
158  }
159
160  private void setUpResultHistory() {
161    java.awt.GraphicsEnvironment ge = 
162      java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); 
163    if(!ge.isHeadless()) {
164      if (m_outText == null) {
165        m_outText = new JTextArea(20, 80);
166        m_history = new ResultHistoryPanel(m_outText);
167      }
168      m_outText.setEditable(false);
169      m_outText.setFont(new Font("Monospaced", Font.PLAIN, 12));
170      m_outText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
171      m_history.setBorder(BorderFactory.createTitledBorder("Result list"));
172      m_history.setHandleRightClicks(false);
173      m_history.getList().addMouseListener(new MouseAdapter() {
174          public void mouseClicked(MouseEvent e) {
175            if (((e.getModifiers() & InputEvent.BUTTON1_MASK)
176                 != InputEvent.BUTTON1_MASK) || e.isAltDown()) {
177              int index = m_history.getList().locationToIndex(e.getPoint());
178              if (index != -1) {
179                String name = m_history.getNameAtIndex(index);
180                visualize(name, e.getX(), e.getY());
181              } else {
182                visualize(null, e.getX(), e.getY());
183              }
184            }
185          }
186        });
187    }
188  }
189
190  /**
191   * Handles constructing a popup menu with visualization options.
192   * @param name the name of the result history list entry clicked on by
193   * the user
194   * @param x the x coordinate for popping up the menu
195   * @param y the y coordinate for popping up the menu
196   */
197  protected void visualize(String name, int x, int y) {
198    final JPanel panel = this;
199    final String selectedName = name;
200    JPopupMenu resultListMenu = new JPopupMenu();
201   
202    JMenuItem visMainBuffer = new JMenuItem("View in main window");
203    if (selectedName != null) {
204      visMainBuffer.addActionListener(new ActionListener() {
205          public void actionPerformed(ActionEvent e) {
206            m_history.setSingle(selectedName);
207          }
208        });
209    } else {
210      visMainBuffer.setEnabled(false);
211    }
212    resultListMenu.add(visMainBuffer);
213   
214    JMenuItem visSepBuffer = new JMenuItem("View in separate window");
215    if (selectedName != null) {
216      visSepBuffer.addActionListener(new ActionListener() {
217        public void actionPerformed(ActionEvent e) {
218          m_history.openFrame(selectedName);
219        }
220      });
221    } else {
222      visSepBuffer.setEnabled(false);
223    }
224    resultListMenu.add(visSepBuffer);
225   
226    JMenuItem saveOutput = new JMenuItem("Save result buffer");
227    if (selectedName != null) {
228      saveOutput.addActionListener(new ActionListener() {
229          public void actionPerformed(ActionEvent e) {
230            SaveBuffer m_SaveOut = new SaveBuffer(null, panel);
231            StringBuffer sb = m_history.getNamedBuffer(selectedName);
232            if (sb != null) {
233              m_SaveOut.save(sb);
234            }
235          }
236        });
237    } else {
238      saveOutput.setEnabled(false);
239    }
240    resultListMenu.add(saveOutput);
241   
242    JMenuItem deleteOutput = new JMenuItem("Delete result buffer");
243    if (selectedName != null) {
244      deleteOutput.addActionListener(new ActionListener() {
245        public void actionPerformed(ActionEvent e) {
246          m_history.removeResult(selectedName);
247        }
248      });
249    } else {
250      deleteOutput.setEnabled(false);
251    }
252    resultListMenu.add(deleteOutput);
253
254    resultListMenu.show(m_history.getList(), x, y);
255  }
256
257  /**
258   * Accept a data set for displaying as text
259   *
260   * @param e a <code>DataSetEvent</code> value
261   */
262  public synchronized void acceptDataSet(DataSetEvent e) {
263    TextEvent nt = new TextEvent(e.getSource(), 
264                                 e.getDataSet().toString(),
265                                 e.getDataSet().relationName());
266    acceptText(nt);
267  }
268
269  /**
270   * Accept a training set for displaying as text
271   *
272   * @param e a <code>TrainingSetEvent</code> value
273   */
274  public synchronized void acceptTrainingSet(TrainingSetEvent e) {
275    TextEvent nt = new TextEvent(e.getSource(), 
276                                 e.getTrainingSet().toString(),
277                                 e.getTrainingSet().relationName());
278    acceptText(nt);
279  }
280
281  /**
282   * Accept a test set for displaying as text
283   *
284   * @param e a <code>TestSetEvent</code> value
285   */
286  public synchronized void acceptTestSet(TestSetEvent e) {
287    TextEvent nt = new TextEvent(e.getSource(), 
288                                 e.getTestSet().toString(),
289                                 e.getTestSet().relationName());
290    acceptText(nt);
291  }
292
293  /**
294   * Accept some text
295   *
296   * @param e a <code>TextEvent</code> value
297   */
298  public synchronized void acceptText(TextEvent e) {
299    if (m_outText == null) {
300      setUpResultHistory();
301    }
302    StringBuffer result = new StringBuffer();
303    result.append(e.getText());
304    //    m_resultsString.append(e.getText());
305    //    m_outText.setText(m_resultsString.toString());
306    String name = (new SimpleDateFormat("HH:mm:ss - "))
307      .format(new Date());
308    name += e.getTextTitle();
309    //    System.err.println(name);
310    if (name.length() > 30) {
311      name = name.substring(0, 30);
312    }
313
314    if (m_outText != null) {
315      // see if there is an entry with this name already in the list -
316      // could happen if two items with the same name arrive at the same second
317      int mod = 2;
318      String nameOrig = new String(name);
319      while (m_history.getNamedBuffer(name) != null) {
320        name = new String(nameOrig+""+mod);
321        mod++;
322      }
323      m_history.addResult(name, result);
324      m_history.setSingle(name);
325    }
326
327    // pass on the event to any listeners
328    notifyTextListeners(e);
329  }
330
331  /**
332   * Describe <code>setVisual</code> method here.
333   *
334   * @param newVisual a <code>BeanVisual</code> value
335   */
336  public void setVisual(BeanVisual newVisual) {
337    m_visual = newVisual;
338  }
339
340  /**
341   * Get the visual appearance of this bean
342   */
343  public BeanVisual getVisual() {
344    return m_visual;
345  }
346 
347  /**
348   * Use the default visual appearance for this bean
349   */
350  public void useDefaultVisual() {
351    m_visual.loadIcons(BeanVisual.ICON_PATH+"DefaultText.gif",
352                       BeanVisual.ICON_PATH+"DefaultText_animated.gif");
353  }
354
355  /**
356   * Popup a component to display the selected text
357   */
358  public void showResults() {
359    if (m_resultsFrame == null) {
360      if (m_outText == null) {
361        setUpResultHistory();
362      }
363      m_resultsFrame = new JFrame("Text Viewer");
364      m_resultsFrame.getContentPane().setLayout(new BorderLayout());
365      final JScrollPane js = new JScrollPane(m_outText);
366      js.setBorder(BorderFactory.createTitledBorder("Text"));
367      m_resultsFrame.getContentPane().add(js, BorderLayout.CENTER);
368      m_resultsFrame.getContentPane().add(m_history, BorderLayout.WEST);
369      m_resultsFrame.addWindowListener(new java.awt.event.WindowAdapter() {
370          public void windowClosing(java.awt.event.WindowEvent e) {
371            m_resultsFrame.dispose();
372            m_resultsFrame = null;
373          }
374        });
375      m_resultsFrame.pack();
376      m_resultsFrame.setVisible(true);
377    } else {
378      m_resultsFrame.toFront();
379    }
380  }
381
382  /**
383   * Get a list of user requests
384   *
385   * @return an <code>Enumeration</code> value
386   */
387  public Enumeration enumerateRequests() {
388    Vector newVector = new Vector(0);
389
390    newVector.addElement("Show results");
391
392    newVector.addElement("?Clear results");
393    return newVector.elements();
394  }
395
396  /**
397   * Perform the named request
398   *
399   * @param request a <code>String</code> value
400   * @exception IllegalArgumentException if an error occurs
401   */
402  public void performRequest(String request) {
403    if (request.compareTo("Show results") == 0) {
404      showResults();
405    } else if (request.compareTo("Clear results") == 0) {
406      m_outText.setText("");
407      m_history.clearResults();
408    } else {
409      throw new 
410        IllegalArgumentException(request
411                    + " not supported (TextViewer)");
412    }
413  }
414
415  /**
416   * Add a property change listener to this bean
417   *
418   * @param name the name of the property of interest
419   * @param pcl a <code>PropertyChangeListener</code> value
420   */
421  public void addPropertyChangeListener(String name,
422                                        PropertyChangeListener pcl) {
423    m_bcSupport.addPropertyChangeListener(name, pcl);
424  }
425
426  /**
427   * Remove a property change listener from this bean
428   *
429   * @param name the name of the property of interest
430   * @param pcl a <code>PropertyChangeListener</code> value
431   */
432  public void removePropertyChangeListener(String name,
433                                           PropertyChangeListener pcl) {
434    m_bcSupport.removePropertyChangeListener(name, pcl);
435  }
436
437  /**
438   * Add a vetoable change listener to this bean
439   *
440   * @param name the name of the property of interest
441   * @param vcl a <code>VetoableChangeListener</code> value
442   */
443  public void addVetoableChangeListener(String name,
444                                       VetoableChangeListener vcl) {
445    m_bcSupport.addVetoableChangeListener(name, vcl);
446  }
447 
448  /**
449   * Remove a vetoable change listener from this bean
450   *
451   * @param name the name of the property of interest
452   * @param vcl a <code>VetoableChangeListener</code> value
453   */
454  public void removeVetoableChangeListener(String name,
455                                           VetoableChangeListener vcl) {
456    m_bcSupport.removeVetoableChangeListener(name, vcl);
457  }
458
459  /**
460   * Set a bean context for this bean
461   *
462   * @param bc a <code>BeanContext</code> value
463   */
464  public void setBeanContext(BeanContext bc) {
465    m_beanContext = bc;
466    m_design = m_beanContext.isDesignTime();
467    if (m_design) {
468      appearanceDesign();
469    } else {
470      java.awt.GraphicsEnvironment ge = 
471        java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); 
472      if (!ge.isHeadless()){
473        appearanceFinal();
474      }
475    }
476  }
477
478  /**
479   * Notify all text listeners of a text event
480   *
481   * @param ge a <code>TextEvent</code> value
482   */
483  private void notifyTextListeners(TextEvent ge) {
484    Vector l;
485    synchronized (this) {
486      l = (Vector)m_textListeners.clone();
487    }
488    if (l.size() > 0) {
489      for(int i = 0; i < l.size(); i++) {
490        ((TextListener)l.elementAt(i)).acceptText(ge);
491      }
492    }
493  }
494
495  /**
496   * Return the bean context (if any) that this bean is embedded in
497   *
498   * @return a <code>BeanContext</code> value
499   */
500  public BeanContext getBeanContext() {
501    return m_beanContext;
502  }
503
504  /**
505   * Stop any processing that the bean might be doing.
506   */
507  public void stop() {
508  }
509 
510  /**
511   * Returns true if. at this time, the bean is busy with some
512   * (i.e. perhaps a worker thread is performing some calculation).
513   *
514   * @return true if the bean is busy.
515   */
516  public boolean isBusy() {
517    return false;
518  }
519
520  /**
521   * Set a logger
522   *
523   * @param logger a <code>Logger</code> value
524   */
525  public void setLog(Logger logger) {
526    m_log = logger;
527  }
528
529  /**
530   * Set a custom (descriptive) name for this bean
531   *
532   * @param name the name to use
533   */
534  public void setCustomName(String name) {
535    m_visual.setText(name);
536  }
537
538  /**
539   * Get the custom (descriptive) name for this bean (if one has been set)
540   *
541   * @return the custom name (or the default name)
542   */
543  public String getCustomName() {
544    return m_visual.getText();
545  }
546
547  /**
548   * Returns true if, at this time,
549   * the object will accept a connection via the supplied
550   * EventSetDescriptor
551   *
552   * @param esd the EventSetDescriptor
553   * @return true if the object will accept a connection
554   */
555  public boolean connectionAllowed(EventSetDescriptor esd) {
556    return connectionAllowed(esd.getName());
557  }
558
559  /**
560   * Returns true if, at this time,
561   * the object will accept a connection via the named event
562   *
563   * @param eventName the name of the event
564   * @return true if the object will accept a connection
565   */
566  public boolean connectionAllowed(String eventName) {
567    return true;
568  }
569
570  /**
571   * Notify this object that it has been registered as a listener with
572   * a source for recieving events described by the named event
573   * This object is responsible for recording this fact.
574   *
575   * @param eventName the event
576   * @param source the source with which this object has been registered as
577   * a listener
578   */
579  public void connectionNotification(String eventName, Object source) {
580  }
581
582  /**
583   * Notify this object that it has been deregistered as a listener with
584   * a source for named event. This object is responsible
585   * for recording this fact.
586   *
587   * @param eventName the event
588   * @param source the source with which this object has been registered as
589   * a listener
590   */
591  public void disconnectionNotification(String eventName, Object source) {
592  }
593
594  /**
595   * Returns true, if at the current time, the named event could
596   * be generated. Assumes that the supplied event name is
597   * an event that could be generated by this bean
598   *
599   * @param eventName the name of the event in question
600   * @return true if the named event could be generated at this point in
601   * time
602   */
603  public boolean eventGeneratable(String eventName) {
604    if (eventName.equals("text")) {
605      return true;
606    }
607    return false;
608  }
609
610  /**
611   * Add a text listener
612   *
613   * @param cl a <code>TextListener</code> value
614   */
615  public synchronized void addTextListener(TextListener cl) {
616    m_textListeners.addElement(cl);
617  }
618
619  /**
620   * Remove a text listener
621   *
622   * @param cl a <code>TextListener</code> value
623   */
624  public synchronized void removeTextListener(TextListener cl) {
625    m_textListeners.remove(cl);
626  }
627
628  public static void main(String [] args) {
629    try {
630      final javax.swing.JFrame jf = new javax.swing.JFrame();
631      jf.getContentPane().setLayout(new java.awt.BorderLayout());
632
633      final TextViewer tv = new TextViewer();
634
635      tv.acceptText(new TextEvent(tv, "Here is some test text from the main "
636                                  +"method of this class.", "The Title"));
637      jf.getContentPane().add(tv, java.awt.BorderLayout.CENTER);
638      jf.addWindowListener(new java.awt.event.WindowAdapter() {
639        public void windowClosing(java.awt.event.WindowEvent e) {
640          jf.dispose();
641          System.exit(0);
642        }
643      });
644      jf.setSize(800,600);
645      jf.setVisible(true);
646    } catch (Exception ex) {
647      ex.printStackTrace();
648    }
649  }
650}
Note: See TracBrowser for help on using the repository browser.