Skip to content

Commit

Permalink
Fixed a possible error in the creation of comboboxes in which the def…
Browse files Browse the repository at this point in the history
…ault selected index is outside the range of available channels. Reported by tolly.
  • Loading branch information
Jan Willem Janssen committed Jul 27, 2012
1 parent 705c5c3 commit 84598bc
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 49 deletions.
Expand Up @@ -301,8 +301,7 @@ private JComponent createSettingsPane()
SpringLayoutUtils.addSeparator( panel, "Settings" );

panel.add( createRightAlignedLabel( "1-Wire Line" ) );
this.owLine = SwingComponentUtils.createChannelSelector( channelCount );
this.owLine.setSelectedIndex( 0 );
this.owLine = SwingComponentUtils.createChannelSelector( channelCount, 0 );
panel.add( this.owLine );

panel.add( createRightAlignedLabel( "Bus Mode" ) );
Expand Down
Expand Up @@ -44,6 +44,7 @@
import nl.lxtreme.ols.util.ExportUtils.HtmlExporter.MacroResolver;
import nl.lxtreme.ols.util.ExportUtils.HtmlFileExporter;
import nl.lxtreme.ols.util.swing.*;
import nl.lxtreme.ols.util.swing.component.*;

import org.osgi.framework.*;

Expand Down Expand Up @@ -264,6 +265,24 @@ protected void setControlsEnabled( final boolean aEnabled )
this.exportAction.setEnabled( aEnabled );
}

/**
* {@inheritDoc}
*/
@Override
protected boolean validateToolSettings()
{
boolean result = super.validateToolSettings();

if ( result && ( getContext().getChannels() != Ols.MAX_CHANNELS ) )
{
JErrorDialog.showDialog( getOwner(), "Cannot start analysis!", "Not enough channels!",
"For the Asm45 decoder, you need to have 32 channels enabled." );
result = false;
}

return result;
}

/**
* Creates the HTML template for exports to HTML.
*
Expand Down Expand Up @@ -394,38 +413,31 @@ private JPanel createSettingsPane()
panel.add( this.bscLinesConfigLabel );

panel.add( this.lineSMCLabel );
this.lineSMC = SwingComponentUtils.createChannelSelector( channelCount );
this.lineSMC.setSelectedIndex( 22 );
this.lineSMC = SwingComponentUtils.createChannelSelector( channelCount, 22 );
panel.add( this.lineSMC );

panel.add( this.lineSTMLabel );
this.lineSTM = SwingComponentUtils.createChannelSelector( channelCount );
this.lineSTM.setSelectedIndex( 23 );
this.lineSTM = SwingComponentUtils.createChannelSelector( channelCount, 23 );
panel.add( this.lineSTM );

panel.add( this.lineEBGLabel );
this.lineEBG = SwingComponentUtils.createChannelSelector( channelCount );
this.lineEBG.setSelectedIndex( 25 );
this.lineEBG = SwingComponentUtils.createChannelSelector( channelCount, 25 );
panel.add( this.lineEBG );

panel.add( this.lineBYTELabel );
this.lineBYTE = SwingComponentUtils.createChannelSelector( channelCount );
this.lineBYTE.setSelectedIndex( 26 );
this.lineBYTE = SwingComponentUtils.createChannelSelector( channelCount, 26 );
panel.add( this.lineBYTE );

panel.add( this.lineBLLabel );
this.lineBL = SwingComponentUtils.createChannelSelector( channelCount );
this.lineBL.setSelectedIndex( 27 );
this.lineBL = SwingComponentUtils.createChannelSelector( channelCount, 27 );
panel.add( this.lineBL );

panel.add( this.lineWRTLabel );
this.lineWRT = SwingComponentUtils.createChannelSelector( channelCount );
this.lineWRT.setSelectedIndex( 29 );
this.lineWRT = SwingComponentUtils.createChannelSelector( channelCount, 29 );
panel.add( this.lineWRT );

panel.add( this.lineSYNCLabel );
this.lineSYNC = SwingComponentUtils.createChannelSelector( channelCount );
this.lineSYNC.setSelectedIndex( 30 );
this.lineSYNC = SwingComponentUtils.createChannelSelector( channelCount, 30 );
panel.add( this.lineSYNC );

this.showInst = new JCheckBox();
Expand Down
Expand Up @@ -412,13 +412,11 @@ private JPanel createSettingsPane()
this.lineBLabel = createRightAlignedLabel( "Line B" );

panel.add( this.lineALabel );
this.lineA = SwingComponentUtils.createChannelSelector( channelCount );
this.lineA.setSelectedIndex( 0 );
this.lineA = SwingComponentUtils.createChannelSelector( channelCount, 0 );
panel.add( this.lineA );

panel.add( this.lineBLabel );
this.lineB = SwingComponentUtils.createChannelSelector( channelCount );
this.lineB.setSelectedIndex( 1 );
this.lineB = SwingComponentUtils.createChannelSelector( channelCount, 1 );
panel.add( this.lineB );

this.detectSDA_SCL = new JCheckBox();
Expand Down
Expand Up @@ -317,23 +317,19 @@ private JPanel createSettingsPane()
SpringLayoutUtils.addSeparator( settings, "Settings" );

settings.add( createRightAlignedLabel( "TCK" ) );
this.tck = SwingComponentUtils.createChannelSelector( channelCount );
this.tck.setSelectedIndex( 0 );
this.tck = SwingComponentUtils.createChannelSelector( channelCount, 0 );
settings.add( this.tck );

settings.add( createRightAlignedLabel( "TMS" ) );
this.tms = SwingComponentUtils.createChannelSelector( channelCount );
this.tms.setSelectedIndex( 1 );
this.tms = SwingComponentUtils.createChannelSelector( channelCount, 1 );
settings.add( this.tms );

settings.add( createRightAlignedLabel( "TDI" ) );
this.tdi = SwingComponentUtils.createOptionalChannelSelector( channelCount );
this.tdi.setSelectedIndex( 3 );
this.tdi = SwingComponentUtils.createOptionalChannelSelector( channelCount, 3 );
settings.add( this.tdi );

settings.add( createRightAlignedLabel( "TDO" ) );
this.tdo = SwingComponentUtils.createOptionalChannelSelector( channelCount );
this.tdo.setSelectedIndex( 4 );
this.tdo = SwingComponentUtils.createOptionalChannelSelector( channelCount, 4 );
settings.add( this.tdo );

SpringLayoutUtils.makeEditorGrid( settings, 10, 4 );
Expand Down
Expand Up @@ -644,41 +644,35 @@ public void itemStateChanged( final ItemEvent aEvent )
settings.add( this.spifiMode );

settings.add( createRightAlignedLabel( "/CS" ) );
this.cs = SwingComponentUtils.createChannelSelector( channelCount );
this.cs.setSelectedIndex( 3 );
this.cs = SwingComponentUtils.createChannelSelector( channelCount, 3 );
settings.add( this.cs );

settings.add( createRightAlignedLabel( "SCK" ) );
this.sck = SwingComponentUtils.createChannelSelector( channelCount );
this.sck.setSelectedIndex( 0 );
this.sck = SwingComponentUtils.createChannelSelector( channelCount, 0 );
settings.add( this.sck );

this.mosiLabel = createRightAlignedLabel( "MOSI" );

settings.add( this.mosiLabel );
this.mosi = SwingComponentUtils.createOptionalChannelSelector( channelCount );
this.mosi.setSelectedIndex( 3 );
this.mosi = SwingComponentUtils.createOptionalChannelSelector( channelCount, 3 );
settings.add( this.mosi );

this.misoLabel = createRightAlignedLabel( "MISO" );

settings.add( this.misoLabel );
this.miso = SwingComponentUtils.createOptionalChannelSelector( channelCount );
this.miso.setSelectedIndex( 2 );
this.miso = SwingComponentUtils.createOptionalChannelSelector( channelCount, 2 );
settings.add( this.miso );

this.io2Label = createRightAlignedLabel( "IO2" );

settings.add( this.io2Label );
this.io2 = SwingComponentUtils.createOptionalChannelSelector( channelCount );
this.io2.setSelectedIndex( 0 );
this.io2 = SwingComponentUtils.createOptionalChannelSelector( channelCount, 0 );
settings.add( this.io2 );

this.io3Label = createRightAlignedLabel( "IO3" );

settings.add( this.io3Label );
this.io3 = SwingComponentUtils.createOptionalChannelSelector( channelCount );
this.io3.setSelectedIndex( 0 );
this.io3 = SwingComponentUtils.createOptionalChannelSelector( channelCount, 0 );
settings.add( this.io3 );

settings.add( createRightAlignedLabel( "SPI Mode" ) );
Expand Down
Expand Up @@ -227,8 +227,7 @@ private JPanel createContentPane()
{
final int channelCount = getData().getChannels();

this.channelSelect = SwingComponentUtils.createChannelSelector( channelCount );
this.channelSelect.setSelectedIndex( 0 );
this.channelSelect = SwingComponentUtils.createChannelSelector( channelCount, 0 );

this.edgeSelect = new JComboBox( new Edge[] { Edge.RISING, Edge.FALLING } );
this.edgeSelect.setSelectedIndex( 0 );
Expand Down
Expand Up @@ -181,7 +181,22 @@ public static JComponent createButtonPane( final JButton... aButtons )
*/
public static JComboBox createChannelSelector( final int aChannelCount )
{
return internalCreateChannelSelector( aChannelCount, false /* aAddUnusedOption */);
return internalCreateChannelSelector( aChannelCount, -1, false /* aAddUnusedOption */);
}

/**
* Creates a channel selector combobox, where only a valid channel can be
* selected.
*
* @param aChannelCount
* the number of channels to include in the combobox options;
* @param aDefaultSelectedIndex
* the default selected index for the created combobox.
* @return a combobox with channel selector options.
*/
public static JComboBox createChannelSelector( final int aChannelCount, final int aDefaultSelectedIndex )
{
return internalCreateChannelSelector( aChannelCount, aDefaultSelectedIndex, false /* aAddUnusedOption */);
}

/**
Expand Down Expand Up @@ -232,7 +247,22 @@ public static final KeyStroke createMenuKeyMask( final int aKeyStroke, final int
*/
public static JComboBox createOptionalChannelSelector( final int aChannelCount )
{
return internalCreateChannelSelector( aChannelCount, true /* aAddUnusedOption */);
return internalCreateChannelSelector( aChannelCount, -1, true /* aAddUnusedOption */);
}

/**
* Creates a channel selector combobox, where optionally a channel can be
* selected.
*
* @param aChannelCount
* the number of channels to include in the combobox options;
* @param aDefaultSelectedIndex
* the default selected index for the created combobox.
* @return a combobox with channel selector options.
*/
public static JComboBox createOptionalChannelSelector( final int aChannelCount, final int aDefaultSelectedIndex )
{
return internalCreateChannelSelector( aChannelCount, aDefaultSelectedIndex, true /* aAddUnusedOption */);
}

/**
Expand Down Expand Up @@ -1139,12 +1169,15 @@ else if ( comp instanceof Container )
*
* @param aChannelCount
* the number of channels to include in the combobox options;
* @param aDefaultSelectedindex
* the default selected index;
* @param aAddUnusedOption
* <code>true</code> to add "unused" as first option,
* <code>false</code> to omit this option.
* @return a combobox with channel selector options.
*/
private static JComboBox internalCreateChannelSelector( final int aChannelCount, final boolean aAddUnusedOption )
private static JComboBox internalCreateChannelSelector( final int aChannelCount, final int aDefaultSelectedindex,
final boolean aAddUnusedOption )
{
int modelSize = Math.max( 0, Math.min( 32, aChannelCount ) );
if ( aAddUnusedOption )
Expand All @@ -1165,11 +1198,10 @@ private static JComboBox internalCreateChannelSelector( final int aChannelCount,
dataChannels[i] = String.format( "Channel %d", Integer.valueOf( index ) );
}

int selectedIndex = aDefaultSelectedindex < 0 ? 0 : aDefaultSelectedindex % modelSize;

final JComboBox result = new JComboBox( dataChannels );
if ( aAddUnusedOption )
{
result.setSelectedIndex( 0 );
}
result.setSelectedIndex( selectedIndex );
return result;
}

Expand Down

0 comments on commit 84598bc

Please sign in to comment.