source: src/main/java/weka/gui/scripting/ScriptingPanel.java @ 4

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

Import di weka.

File size: 7.7 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 * ScriptingPanel.java
19 * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.gui.scripting;
23
24import weka.core.Tee;
25import weka.gui.PropertyDialog;
26import weka.gui.ReaderToTextPane;
27import weka.gui.scripting.event.TitleUpdatedEvent;
28import weka.gui.scripting.event.TitleUpdatedListener;
29
30import java.awt.BorderLayout;
31import java.awt.Color;
32import java.awt.Dimension;
33import java.io.File;
34import java.io.InputStreamReader;
35import java.io.PipedInputStream;
36import java.io.PipedOutputStream;
37import java.io.PrintStream;
38import java.io.Reader;
39import java.util.HashSet;
40import java.util.Iterator;
41
42import javax.swing.ImageIcon;
43import javax.swing.JFrame;
44import javax.swing.JMenuBar;
45import javax.swing.JPanel;
46import javax.swing.JTextPane;
47
48/**
49 * Abstract ancestor for scripting panels.
50 *
51 * @author  fracpete (fracpete at waikato dot ac dot nz)
52 * @version $Revision: 5142 $
53 */
54public abstract class ScriptingPanel
55  extends JPanel
56  implements TitleUpdatedListener {
57
58  /** for serialization. */
59  private static final long serialVersionUID = 7593091442691911406L;
60
61  /** The new output stream for System.out. */
62  protected PipedOutputStream m_POO;
63
64  /** The new output stream for System.err. */
65  protected PipedOutputStream m_POE;
66
67  /** The thread that sends output from m_POO to the output box. */
68  protected ReaderToTextPane m_OutRedirector;
69
70  /** The thread that sends output from m_POE to the output box. */
71  protected ReaderToTextPane m_ErrRedirector;
72 
73  /** whether debug mode is on. */
74  protected boolean m_Debug;
75 
76  /** the listeners for the changes in the title. */
77  protected HashSet<TitleUpdatedListener> m_TitleUpdatedListeners;
78
79  /**
80   * Default constructor.
81   */
82  public ScriptingPanel() {
83    super();
84   
85    initialize();
86    initGUI();
87    initFinish();
88  }
89 
90  /**
91   * For initializing member variables.
92   */
93  protected void initialize() {
94    m_POO            = new PipedOutputStream();
95    m_POE            = new PipedOutputStream();
96    m_Debug          = false;
97    m_TitleUpdatedListeners = new HashSet<TitleUpdatedListener>();
98  }
99 
100  /**
101   * Sets up the GUI after initializing the members.
102   * The JTextArea returned via <code>getOutputArea()</code> must be setup here.
103   *
104   * @see       #initialize()
105   * @see       #getOutput()
106   */
107  protected void initGUI() {
108  }
109 
110  /**
111   * Finishes up after initializing members and setting up the GUI.
112   * Redirects stdout and stderr using <code>getOutputArea()</code>.
113   *
114   * @see       #initialize()
115   * @see       #initGUI()
116   * @see       #getOutput()
117   */
118  protected void initFinish() {
119    // Redirect System.out to the text area
120    try {
121      PipedInputStream pio = new PipedInputStream(m_POO);
122      Tee teeOut = new Tee(System.out);
123      System.setOut(teeOut);
124      teeOut.add(new PrintStream(m_POO));
125      Reader reader = new InputStreamReader(pio);
126      m_OutRedirector = new ReaderToTextPane(reader, getOutput(), Color.BLACK);
127      m_OutRedirector.start();
128    }
129    catch (Exception e) {
130      System.err.println("Error redirecting stdout");
131      e.printStackTrace();
132      m_OutRedirector = null;
133    }
134
135    // Redirect System.err to the text area
136    try {
137      PipedInputStream pie = new PipedInputStream(m_POE);
138      Tee teeErr = new Tee(System.err);
139      System.setErr(teeErr);
140      teeErr.add(new PrintStream(m_POE));
141      Reader reader = new InputStreamReader(pie);
142      m_ErrRedirector = new ReaderToTextPane(reader, getOutput(), Color.RED);
143      m_ErrRedirector.start();
144    }
145    catch (Exception e) {
146      System.err.println("Error redirecting stderr");
147      e.printStackTrace();
148      m_ErrRedirector = null;
149    }
150
151    addTitleUpdatedListener(this);
152  }
153 
154  /**
155   * Returns an icon to be used in a frame.
156   *
157   * @return            the icon
158   */
159  public abstract ImageIcon getIcon();
160 
161  /**
162   * Returns the current title for the frame/dialog.
163   *
164   * @return            the title
165   */
166  public abstract String getTitle();
167 
168  /**
169   * Returns the text area that is used for displaying output on stdout
170   * and stderr.
171   *
172   * @return            the JTextArea
173   */
174  public abstract JTextPane getOutput();
175 
176  /**
177   * Returns the menu bar to to be displayed in the frame.
178   *
179   * @return            the menu bar, null if not applicable
180   */
181  public abstract JMenuBar getMenuBar();
182 
183  /**
184   * Turns on/off debugging mode.
185   *
186   * @param value       if true, debug mode is turned on
187   */
188  public void setDebug(boolean value) {
189    m_Debug = value;
190  }
191 
192  /**
193   * Returns whether debugging mode is on.
194   *
195   * @return            true if debug mode is turned on
196   */
197  public boolean getDebug() {
198    return m_Debug;
199  }
200 
201  /**
202   * Adds the listener to the internal list.
203   *
204   * @param l           the listener to add
205   */
206  public void addTitleUpdatedListener(TitleUpdatedListener l) {
207    m_TitleUpdatedListeners.add(l);
208  }
209 
210  /**
211   * Removes the listener from the internal list.
212   *
213   * @param l           the listener to remove
214   */
215  public void removeTitleUpdatedListener(TitleUpdatedListener l) {
216    m_TitleUpdatedListeners.remove(l);
217  }
218 
219  /**
220   * Sends the event to all listeners for title updates.
221   *
222   * @param e           the event to send
223   */
224  protected void notifyTitleUpdatedListeners(TitleUpdatedEvent e) {
225    Iterator<TitleUpdatedListener>      iter;
226   
227    iter = m_TitleUpdatedListeners.iterator();
228    while (iter.hasNext())
229      iter.next().titleUpdated(e);
230  }
231 
232  /**
233   * Gets called when the title of the frame/dialog needs updating.
234   *
235   * @param event       the event that got sent
236   */
237  public void titleUpdated(TitleUpdatedEvent event) {
238    if (PropertyDialog.getParentDialog(ScriptingPanel.this) != null)
239      PropertyDialog.getParentDialog(ScriptingPanel.this).setTitle(getTitle());
240    else if (PropertyDialog.getParentFrame(ScriptingPanel.this) != null)
241      PropertyDialog.getParentFrame(ScriptingPanel.this).setTitle(getTitle());
242  }
243 
244  /**
245   * Displays the panel in a frame.
246   *
247   * @param panel       the panel to display
248   * @param args        currently ignored commandline parameters
249   */
250  public static void showPanel(ScriptingPanel panel, String[] args) {
251    showPanel(panel, args, 800, 600);
252  }
253 
254  /**
255   * Displays the panel in a frame.
256   *
257   * @param panel       the panel to display
258   * @param args        currently ignored commandline parameters
259   * @param width       the width of the frame
260   * @param height      the height of the frame
261   */
262  public static void showPanel(ScriptingPanel panel, String[] args, int width, int height) {
263    try {
264      JFrame frame = new JFrame();
265      frame.getContentPane().setLayout(new BorderLayout());
266      frame.getContentPane().add(panel, BorderLayout.CENTER);
267      frame.setJMenuBar(panel.getMenuBar());
268      frame.setSize(new Dimension(width, height));
269      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
270      frame.setTitle(panel.getTitle());
271      frame.setIconImage(panel.getIcon().getImage());
272      frame.setLocationRelativeTo(null);
273      if ((args.length > 0) && (panel instanceof FileScriptingPanel))
274        ((FileScriptingPanel) panel).open(new File(args[0]));
275      frame.setVisible(true);
276    }
277    catch (Exception e) {
278      e.printStackTrace();
279    }
280  }
281}
Note: See TracBrowser for help on using the repository browser.