package textbender.g.lang; // Copyright 2005, 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.awt.Container; import java.beans.*; import javax.swing.*; import javax.swing.border.EmptyBorder; import textbender.o.awt.*; import textbender.g.hold.*; import static textbender.o.awt.FontX.EN; import static textbender.o.awt.FontX.EM; /** View of throwable holder, showing its full stack trace. */ @ThreadRestricted( "AWT event dispatch" ) final class ThrowableHolderVST { /** Constructs a ThrowableHolderVST in a container. * The container's layout manager ought to be vertically oriented, * like a BoxLayout along the Y_AXIS. * * @param m model per {@linkplain #getModel() getModel}(); or null to set it later */ ThrowableHolderVST( ThrowableHolderModel m, final Container container, final Spool spool ) { assert java.awt.EventQueue.isDispatchThread(); textArea = new JTextArea(); textArea.setBorder( new EmptyBorder( /*top*/EN, /*left*/EN, /*bottom*/EN, /*right*/0 )); textArea.setEditable( false ); scrollPane = new JScrollPane( textArea ); container.add( scrollPane ); setModel( m ); spool.add( new Hold() { public void release() { setModel( null ); } }); } // ------------------------------------------------------------------------------------ /** Returns the model that is viewed. * * @return model; or null if there is none */ public ThrowableHolderModel getModel() { return model; } private ThrowableHolderModel model; /** Sets the model that is viewed, per {@linkplain #getModel() getModel}(). */ public void setModel( ThrowableHolderModel newModel ) { assert java.awt.EventQueue.isDispatchThread(); setModel_spool.unwind(); model = newModel; if( model == null ) setModel_spool = Spool0.i(); else setModel_spool = new Spool1(); new Refresher( setModel_spool ); } private Spool setModel_spool = Spool0.i(); //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final JScrollPane scrollPane; private final JTextArea textArea; // ==================================================================================== /** Refreshes the view in sync with the model. */ private final class Refresher implements PropertyChangeListener { private Refresher( Spool spool ) { if( model != null ) { model.addPropertyChangeListener( property, Refresher.this ); spool.add( new Hold() { public void release() { model.removePropertyChangeListener( property, Refresher.this ); } }); } refresh(); // fire up // not dynamically bound when called from constructor/initializer } private void refresh() { assert java.awt.EventQueue.isDispatchThread(); final StringBuilder b = AWT.emptyStringBuilder(); Throwable t; if( model == null ) t = null; else t = model.getThrowable(); if( t != null ) { // b.append( "ERROR or EXCEPTION\n" ); for( ;; ) { b.append( t.getClass().getName() ); String message = t.getMessage(); if( message != null ) { b.append( "\n\u2014" ); // \em dash b.append( message ); b.append( '\u2014' ); } StackTraceElement[] traceElementArray = t.getStackTrace(); for( int e=0; e