/*
* OpenBench LogicSniffer / SUMP project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*
*
* Copyright (C) 2010-2011 - J.W. Janssen, http://www.lxtreme.nl
*/
package nl.lxtreme.ols.tool.dmx512;
import static nl.lxtreme.ols.util.ExportUtils.HtmlExporter.*;
import static nl.lxtreme.ols.util.swing.SwingComponentUtils.*;
import java.awt.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.List;
import java.util.logging.*;
import javax.swing.*;
import nl.lxtreme.ols.api.*;
import nl.lxtreme.ols.api.tools.*;
import nl.lxtreme.ols.api.util.*;
import nl.lxtreme.ols.tool.base.*;
import nl.lxtreme.ols.tool.base.ToolUtils.RestorableAction;
import nl.lxtreme.ols.util.*;
import nl.lxtreme.ols.util.ExportUtils.CsvExporter;
import nl.lxtreme.ols.util.ExportUtils.HtmlExporter;
import nl.lxtreme.ols.util.ExportUtils.HtmlExporter.Element;
import nl.lxtreme.ols.util.ExportUtils.HtmlExporter.MacroResolver;
import nl.lxtreme.ols.util.ExportUtils.HtmlFileExporter;
import nl.lxtreme.ols.util.swing.*;
import org.osgi.framework.*;
/**
* The Dialog Class
*/
public final class DMX512AnalyzerDialog extends BaseToolDialog<DMX512DataSet> implements ExportAware<DMX512DataSet>
{
// CONSTANTS
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger( DMX512AnalyzerDialog.class.getName() );
// VARIABLES
private JComboBox dataLine;
private JEditorPane outText;
private RestorableAction runAnalysisAction;
private Action closeAction;
private Action exportAction;
// CONSTRUCTORS
/**
* Creates a new {@link DMX512AnalyzerDialog} instance.
*
* @param aOwner
* the owner of this dialog;
* @param aToolContext
* the tool context;
* @param aContext
* the OSGi bundle context to use;
* @param aTool
* the {@link DMX512Analyzer} tool.
*/
public DMX512AnalyzerDialog( final Window aOwner, final ToolContext aToolContext, final BundleContext aContext,
final DMX512Analyzer aTool )
{
super( aOwner, aToolContext, aContext, aTool );
initDialog();
setLocationRelativeTo( getOwner() );
}
// METHODS
/**
* {@inheritDoc}
*/
@Override
public void exportToFile( final File aOutputFile, final ExportFormat aFormat ) throws IOException
{
if ( ExportFormat.HTML.equals( aFormat ) )
{
storeToHtmlFile( aOutputFile, getLastResult() );
}
else if ( ExportFormat.CSV.equals( aFormat ) )
{
storeToCsvFile( aOutputFile, getLastResult() );
}
}
/**
* {@inheritDoc}
*/
@Override
public void readPreferences( final UserSettings aSettings )
{
// Issue #114: avoid setting illegal values...
setComboBoxIndex( this.dataLine, aSettings, "dataLine" );
}
/**
* {@inheritDoc}
*/
@Override
public void reset()
{
this.outText.setText( getEmptyHtmlPage() );
this.outText.setEditable( false );
this.runAnalysisAction.restore();
setControlsEnabled( true );
this.exportAction.setEnabled( false );
}
/**
* @see nl.lxtreme.ols.api.Configurable#writePreferences(nl.lxtreme.ols.api.UserSettings)
*/
@Override
public void writePreferences( final UserSettings aSettings )
{
aSettings.putInt( "dataLine", this.dataLine.getSelectedIndex() );
}
/**
* {@inheritDoc}
*/
@Override
protected void onToolEnded( final DMX512DataSet aAnalysisResult )
{
try
{
final String htmlPage;
if ( aAnalysisResult != null )
{
htmlPage = toHtmlPage( null /* aFile */, aAnalysisResult );
}
else
{
htmlPage = getEmptyHtmlPage();
}
this.outText.setText( htmlPage );
this.outText.setEditable( false );
this.runAnalysisAction.restore();
}
catch ( final IOException exception )
{
// Make sure to handle IO-interrupted exceptions properly!
if ( !HostUtils.handleInterruptedException( exception ) )
{
// Should not happen in this situation!
throw new RuntimeException( exception );
}
}
}
/**
* {@inheritDoc}
*/
@Override
protected void onToolStarted()
{
// NO-op
}
/**
* {@inheritDoc}
*/
@Override
protected void prepareToolTask( final ToolTask<DMX512DataSet> aToolTask )
{
final DMX512AnalyzerTask toolTask = ( DMX512AnalyzerTask )aToolTask;
// The value at index zero is "Unused", so extracting one of all items
// causes all "unused" values to be equivalent to -1, which is interpreted
// as not used...
toolTask.setDataLine( this.dataLine.getSelectedIndex() - 1 );
}
/**
* set the controls of the dialog enabled/disabled
*
* @param aEnable
* status of the controls
*/
@Override
protected void setControlsEnabled( final boolean aEnable )
{
this.dataLine.setEnabled( aEnable );
this.closeAction.setEnabled( aEnable );
this.exportAction.setEnabled( aEnable );
}
/**
* Creates the HTML template for exports to HTML.
*
* @param aExporter
* the HTML exporter instance to use, cannot be <code>null</code>.
* @return a HTML exporter filled with the template, never <code>null</code>.
*/
private HtmlExporter createHtmlTemplate( final HtmlExporter aExporter )
{
aExporter.addCssStyle( "body { font-family: sans-serif; } " );
aExporter.addCssStyle( "table { border-width: 1px; border-spacing: 0px; border-color: gray;"
+ " border-collapse: collapse; border-style: solid; margin-bottom: 15px; } " );
aExporter.addCssStyle( "table th { border-width: 1px; padding: 2px; border-style: solid; border-color: gray;"
+ " background-color: #C0C0FF; text-align: left; font-weight: bold; font-family: sans-serif; } " );
aExporter.addCssStyle( "table td { border-width: 1px; padding: 2px; border-style: solid; border-color: gray;"
+ " font-family: monospace; } " );
aExporter.addCssStyle( ".error { color: red; } " );
aExporter.addCssStyle( ".warning { color: orange; } " );
aExporter.addCssStyle( ".date { text-align: right; font-size: x-small; margin-bottom: 15px; } " );
aExporter.addCssStyle( ".w100 { width: 100%; } " );
aExporter.addCssStyle( ".w35 { width: 35%; } " );
aExporter.addCssStyle( ".w30 { width: 30%; } " );
aExporter.addCssStyle( ".w15 { width: 15%; } " );
aExporter.addCssStyle( ".w10 { width: 10%; } " );
aExporter.addCssStyle( ".w8 { width: 8%; } " );
aExporter.addCssStyle( ".w7 { width: 7%; } " );
final Element body = aExporter.getBody();
body.addChild( H1 ).addContent( "DMX512 Analysis results" );
body.addChild( HR );
body.addChild( DIV ).addAttribute( "class", "date" ).addContent( "Generated: ", "{date-now}" );
Element table, tr, thead, tbody;
table = body.addChild( TABLE ).addAttribute( "class", "w100" );
tbody = table.addChild( TBODY );
tr = tbody.addChild( TR );
tr.addChild( TH ).addAttribute( "colspan", "2" ).addContent( "Statistics" );
tr = tbody.addChild( TR );
tr.addChild( TD ).addAttribute( "class", "w30" ).addContent( "Decoded bytes" );
tr.addChild( TD ).addContent( "{decoded-bytes}" );
tr = tbody.addChild( TR );
tr.addChild( TD ).addAttribute( "class", "w30" ).addContent( "Detected bus errors" );
tr.addChild( TD ).addContent( "{detected-bus-errors}" );
tr = tbody.addChild( TR );
tr.addChild( TD ).addAttribute( "class", "w30" ).addContent( "Number of slots" );
tr.addChild( TD ).addContent( "{slot-count}" );
table = body.addChild( TABLE ).addAttribute( "class", "w100" );
thead = table.addChild( THEAD );
tr = thead.addChild( TR );
tr.addChild( TH ).addAttribute( "class", "w30" ).addAttribute( "colspan", "2" );
tr.addChild( TH ).addAttribute( "class", "w35" ).addAttribute( "colspan", "4" ).addContent( "Data" );
tr = thead.addChild( TR );
tr.addChild( TH ).addAttribute( "class", "w15" ).addContent( "Index" );
tr.addChild( TH ).addAttribute( "class", "w15" ).addContent( "Time" );
tr.addChild( TH ).addAttribute( "class", "w10" ).addContent( "Hex" );
tr.addChild( TH ).addAttribute( "class", "w10" ).addContent( "Bin" );
tr.addChild( TH ).addAttribute( "class", "w8" ).addContent( "Dec" );
tr.addChild( TH ).addAttribute( "class", "w7" ).addContent( "ASCII" );
tbody = table.addChild( TBODY );
tbody.addContent( "{decoded-data}" );
return aExporter;
}
/**
* @return
*/
private JPanel createPreviewPane()
{
final JPanel panTable = new JPanel( new GridLayout( 1, 1, 0, 0 ) );
this.outText = new JEditorPane( "text/html", getEmptyHtmlPage() );
this.outText.setEditable( false );
panTable.add( new JScrollPane( this.outText ) );
return panTable;
}
/**
* @return
*/
private JPanel createSettingsPane()
{
final int channelCount = getData().getChannels();
final JPanel settings = new JPanel( new SpringLayout() );
SpringLayoutUtils.addSeparator( settings, "Settings" );
settings.add( createRightAlignedLabel( "Data line" ) );
this.dataLine = SwingComponentUtils.createOptionalChannelSelector( channelCount );
settings.add( this.dataLine );
SpringLayoutUtils.makeEditorGrid( settings, 10, 4 );
return settings;
}
/**
* generate a HTML page
*
* @param empty
* if this is true an empty output is generated
* @return String with HTML data
*/
private String getEmptyHtmlPage()
{
final HtmlExporter exporter = createHtmlTemplate( ExportUtils.createHtmlExporter() );
return exporter.toString( new MacroResolver()
{
@Override
public Object resolve( final String aMacro, final Element aParent )
{
if ( "date-now".equals( aMacro ) )
{
final DateFormat df = DateFormat.getDateInstance( DateFormat.LONG );
return df.format( new Date() );
}
else if ( "decoded-bytes".equals( aMacro ) || "detected-bus-errors".equals( aMacro )
|| "baudrate".equals( aMacro ) )
{
return "-";
}
else if ( "decoded-data".equals( aMacro ) )
{
return null;
}
return null;
}
} );
}
/**
* Initializes this dialog.
*/
private void initDialog()
{
setMinimumSize( new Dimension( 640, 480 ) );
final JComponent settingsPane = createSettingsPane();
final JComponent previewPane = createPreviewPane();
final JPanel contentPane = new JPanel( new GridBagLayout() );
contentPane.add( settingsPane, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH,
GridBagConstraints.NONE, new Insets( 2, 0, 2, 0 ), 0, 0 ) );
contentPane.add( previewPane, new GridBagConstraints( 1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH,
GridBagConstraints.BOTH, new Insets( 2, 0, 2, 0 ), 0, 0 ) );
final JButton runAnalysisButton = ToolUtils.createRunAnalysisButton( this );
this.runAnalysisAction = ( RestorableAction )runAnalysisButton.getAction();
final JButton exportButton = ToolUtils.createExportButton( this );
this.exportAction = exportButton.getAction();
this.exportAction.setEnabled( false );
final JButton closeButton = ToolUtils.createCloseButton();
this.closeAction = closeButton.getAction();
final JComponent buttons = SwingComponentUtils.createButtonPane( runAnalysisButton, exportButton, closeButton );
SwingComponentUtils.setupWindowContentPane( this, contentPane, buttons, runAnalysisButton );
}
/**
* exports the data to a CSV file
*
* @param aFile
* File object
*/
private void storeToCsvFile( final File aFile, final DMX512DataSet aDataSet )
{
try
{
final CsvExporter exporter = ExportUtils.createCsvExporter( aFile );
exporter.setHeaders( "index", "start-time", "end-time", "event?", "event-type", "RxD event", "TxD event",
"RxD data", "TxD data" );
final List<DMX512Data> decodedData = aDataSet.getData();
for ( int i = 0; i < decodedData.size(); i++ )
{
final DMX512Data ds = decodedData.get( i );
final String startTime = Unit.Time.format( aDataSet.getTime( ds.getStartSampleIndex() ) );
final String endTime = Unit.Time.format( aDataSet.getTime( ds.getEndSampleIndex() ) );
String eventType = ds.getEventName();
String dataValue = Integer.toString( ds.getData() );
exporter.addRow( Integer.valueOf( i ), startTime, endTime, eventType, dataValue );
}
exporter.close();
}
catch ( final IOException exception )
{
// Make sure to handle IO-interrupted exceptions properly!
if ( !HostUtils.handleInterruptedException( exception ) )
{
LOG.log( Level.WARNING, "CSV export failed!", exception );
}
}
}
/**
* stores the data to a HTML file
*
* @param aFile
* file object
*/
private void storeToHtmlFile( final File aFile, final DMX512DataSet aDataSet )
{
try
{
toHtmlPage( aFile, aDataSet );
}
catch ( final IOException exception )
{
// Make sure to handle IO-interrupted exceptions properly!
if ( !HostUtils.handleInterruptedException( exception ) )
{
LOG.log( Level.WARNING, "HTML export failed!", exception );
}
}
}
/**
* generate a HTML page
*
* @param empty
* if this is true an empty output is generated
* @return String with HTML data
*/
private String toHtmlPage( final File aFile, final DMX512DataSet aDataSet ) throws IOException
{
final int bitCount = 8;
final int bitAdder = 0;
final MacroResolver macroResolver = new MacroResolver()
{
@Override
public Object resolve( final String aMacro, final Element aParent )
{
if ( "date-now".equals( aMacro ) )
{
final DateFormat df = DateFormat.getDateInstance( DateFormat.LONG );
return df.format( new Date() );
}
else if ( "decoded-bytes".equals( aMacro ) )
{
return Integer.valueOf( aDataSet.getDecodedSymbols() );
}
else if ( "slot-count".equals( aMacro ) )
{
return Integer.valueOf( aDataSet.getSlotCount() );
}
else if ( "detected-bus-errors".equals( aMacro ) )
{
return Integer.valueOf( aDataSet.getDetectedErrors() );
}
else if ( "decoded-data".equals( aMacro ) )
{
final List<DMX512Data> decodedData = aDataSet.getData();
Element tr;
for ( int i = 0; i < decodedData.size(); i++ )
{
final DMX512Data ds = decodedData.get( i );
String eventName = ds.getEventName();
String bgColor;
if ( "FRAME".equals( eventName ) )
{
eventName = "Frame error";
bgColor = "#ff6600";
}
else if ( "PARITY".equals( eventName ) )
{
eventName = "Parity error";
bgColor = "#ff9900";
}
else if ( "START".equals( eventName ) )
{
eventName = "Start error";
bgColor = "#ffcc00";
}
else
{
// symbol
bgColor = ( i % 2 ) == 0 ? "#ffffff" : "#eeeeee";
}
tr = aParent.addChild( TR ).addAttribute( "style", "background-color: " + bgColor + ";" );
tr.addChild( TD ).addContent( String.valueOf( i ) );
tr.addChild( TD ).addContent( Unit.Time.format( aDataSet.getTime( ds.getStartSampleIndex() ) ) );
if ( eventName == null )
{
// normal symbol...
int data = ds.getData();
String dataHex = "0x" + StringUtils.integerToHexString( data, ( bitCount / 4 ) + bitAdder );
String dataBin = "0b" + StringUtils.integerToBinString( data, bitCount );
String dataDec = String.valueOf( data );
String dataASCII = "";
if ( isPrintableCharacter( data ) )
{
dataASCII = String.valueOf( ( char )data );
}
tr.addChild( TD ).addContent( dataHex );
tr.addChild( TD ).addContent( dataBin );
tr.addChild( TD ).addContent( dataDec );
tr.addChild( TD ).addContent( dataASCII );
}
else
{
// error event...
tr.addChild( TD ).addAttribute( "colspan", "4" ).addContent( eventName );
}
}
}
return null;
}
/**
* Returns whether the given value can be represented as an
* ASCII-character.
*
* @param aValue
* the value to test.
* @return <code>true</code> if the given character can be represented as
* printable ASCII-character, <code>false</code> otherwise.
*/
private boolean isPrintableCharacter( final int aValue )
{
final boolean withinRange = ( aValue >= 32 ) && ( aValue < 255 );
return withinRange;
}
};
if ( aFile == null )
{
final HtmlExporter exporter = createHtmlTemplate( ExportUtils.createHtmlExporter() );
return exporter.toString( macroResolver );
}
else
{
final HtmlFileExporter exporter = ( HtmlFileExporter )createHtmlTemplate( ExportUtils.createHtmlExporter( aFile ) );
exporter.write( macroResolver );
exporter.close();
}
return null;
}
}