/***************************************************************************** * Web3d.org Copyright (c) 2001 * Java Source * * This source is licensed under the GNU LGPL v2.1 * Please read http://www.gnu.org/copyleft/lgpl.html for more information * * This software comes with the standard NO WARRANTY disclaimer for any * purpose. Use it at your own risk. If there's a problem you get to fix it. * ****************************************************************************/ // Standard library imports import javax.swing.*; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.PrintWriter; import java.io.StringWriter; // Application specific imports import org.web3d.util.ErrorReporter; import org.web3d.vrml.lang.VRMLException; import org.web3d.vrml.sav.ErrorHandler; import org.web3d.vrml.sav.Locator; /** * A window that can act as console for error messages from the application. *

* * The window will print error messages for all the error levels and only * throw an exception for the fatalError. * * @author Justin Couch * @version $Revision: 1.1.1.1 $ */ public class ConsoleWindow extends JFrame implements ActionListener, ErrorHandler, ErrorReporter { /** A frame to put error information in */ private JFrame ErrorFrame; /** The textfield to put the error information in */ private JTextArea errorField; /** Clear button to remove stuff from window */ private JButton clearButton; /** Locator used for printing out line/column information */ private Locator docLocator; /** * Create an instance of the console window. */ public ConsoleWindow() { super("Browser Console"); Container content_pane = getContentPane(); errorField = new JTextArea(20, 60); JScrollPane scroller = new JScrollPane(errorField); content_pane.add(scroller, BorderLayout.CENTER); JPanel p1 = new JPanel(new FlowLayout()); content_pane.add(p1, BorderLayout.SOUTH); clearButton = new JButton("Clear"); clearButton.addActionListener(this); p1.add(clearButton); setSize(600, 400); setLocation(80, 80); setDefaultCloseOperation(HIDE_ON_CLOSE); } //---------------------------------------------------------- // Methods required by the ErrorHandler interface //---------------------------------------------------------- /** * Set the document locator that can be used by the implementing code to * find out information about the current line information. This method * is called by the parser to your code to give you a locator to work with. * If this has not been set by the time startDocument() has * been called, you can assume that you will not have one available. * * @param loc The locator instance to use */ public void setDocumentLocator(Locator loc) { docLocator = loc; } /** * Notification of a warning in the way the code has been handled. The * parser will continue through the file after this. Throw another * exception if we want the parser to halt as a result. * * @param vpe The exception that caused this warning * @throws VRMLException Create a further warning condition */ public void warning(VRMLException vpe) throws VRMLException { warning(vpe.getMessage(), true); } /** * Notification of a recoverable error in the parsing. The parser will * continue to keep parsing after this error. Throw another exception if * we really want the parser to stop at this point. * * @param vpe The exception that caused this warning * @throws VRMLException Create a further warning condition */ public void error(VRMLException vpe) throws VRMLException { error(vpe.getMessage(), true); } /** * Notification of a non-recoverable error. The parser will not continue * after calling this method. Throw another exception if we really want * to make note of this, the parser will stop anyway. * * @param vpe The exception that caused this warning * @throws VRMLException Create a further warning condition */ public void fatalError(VRMLException vpe) throws VRMLException { StringBuffer buf = new StringBuffer("Fatal Error: "); buf.append(" Line: "); buf.append(docLocator.getLineNumber()); buf.append(" Column: "); buf.append(docLocator.getColumnNumber()); buf.append(" "); buf.append(vpe.getMessage()); buf.append("\n"); errorField.append(buf.toString()); throw vpe; } //---------------------------------------------------------- // Methods required by the ErrorReporter interface //---------------------------------------------------------- /** * Notification of an informational message from the system. For example, * it may issue a message when a URL cannot be resolved. * * @param msg The text of the message to be displayed */ public void messageReport(String msg) { StringBuffer buf = new StringBuffer(msg); buf.append('\n'); errorField.append(buf.toString()); } /** * Notification of a warning in the way the system is currently operating. * This is a non-fatal, non-serious error. For example you will get an * warning when a value has been set that is out of range. * * @param msg The text of the message to be displayed * @param e The exception that caused this warning. May be null */ public void warningReport(String msg, Exception e) { StringBuffer buf = new StringBuffer("Warning: "); if(msg != null) { buf.append(msg); buf.append('\n'); } if(e != null) { String txt = e.getMessage(); if(txt == null) txt = e.getClass().getName(); buf.append(txt); buf.append('\n'); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); buf.append(sw.toString()); } errorField.append(buf.toString()); } /** * Notification of a recoverable error. This is a serious, but non-fatal * error, for example trying to add a route to a non-existent node or the * use of a node that the system cannot find the definition of. * * @param msg The text of the message to be displayed * @param e The exception that caused this warning. May be null */ public void errorReport(String msg, Exception e) { StringBuffer buf = new StringBuffer("Error: "); if(msg != null) { buf.append(msg); buf.append('\n'); } if(e != null) { String txt = e.getMessage(); if(txt == null) txt = e.getClass().getName(); buf.append(txt); buf.append('\n'); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); buf.append(sw.toString()); } errorField.append(buf.toString()); } /** * Notification of a non-recoverable error that halts the entire system. * After you recieve this report the runtime system will no longer * function - for example a non-recoverable parsing error. The best way * out is to reload the file or restart the application internals. * * @param msg The text of the message to be displayed * @param e The exception that caused this warning. May be null */ public void fatalErrorReport(String msg, Exception e) { StringBuffer buf = new StringBuffer("Fatal Error: "); if(msg != null) { buf.append(msg); buf.append('\n'); } if(e != null) { String txt = e.getMessage(); if(txt == null) txt = e.getClass().getName(); buf.append(txt); buf.append('\n'); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); buf.append(sw.toString()); } errorField.append(buf.toString()); } //---------------------------------------------------------- // Methods required by the ActionListener interface //---------------------------------------------------------- public void actionPerformed(ActionEvent evt) { Object src = evt.getSource(); if(src == clearButton) errorField.setText(""); } //---------------------------------------------------------- // Local Methods //---------------------------------------------------------- /** * Send a warning message to the screen with the option of using the * docLocator to present line number info.The string will have the prefix * "Warning:" prepended to it. * * @param msg The message to be written. * @param useLocator true if line number information should be used */ private void warning(String msg, boolean useLocator) { StringBuffer buf = new StringBuffer("Warning: "); if(useLocator) { buf.append(" Line: "); buf.append(docLocator.getLineNumber()); buf.append(" Column: "); buf.append(docLocator.getColumnNumber()); buf.append(" "); } buf.append(msg); buf.append("\n"); errorField.append(buf.toString()); } /** * Send an error message to the screen with the option of using the * docLocator to present line number info.The string will have the prefix * "Error:" prepended to it. * * @param msg The message to be written * @param useLocator true if line number information should be used */ private void error(String msg, boolean useLocator) { StringBuffer buf = new StringBuffer("Error: "); if(useLocator) { buf.append(" Line: "); buf.append(docLocator.getLineNumber()); buf.append(" Column: "); buf.append(docLocator.getColumnNumber()); buf.append(" "); } buf.append(msg); buf.append("\n"); errorField.append(buf.toString()); } }