Skip to content

Commit

Permalink
Make sure the projections() method of feature is called only once,
Browse files Browse the repository at this point in the history
when creating the table header and collecting values.
This is to protect against a bug that appear with features
that are not careful to always return the same set of
projections when the projections() method is called. This
happend for the link target feature and generated the bug below:
#217

One can fix the bug by imposing the same order on features, but
it is also a plus to make the table rendering independent of
this order being preserved.
  • Loading branch information
tinevez committed Oct 22, 2023
1 parent f8368f4 commit 4d41480
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/main/java/org/mastodon/views/table/FeatureTagTablePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EventObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -403,19 +405,32 @@ private void refreshColumns()
lastHeaderLine.add( "ID" );
columnClasses.add( Integer.class );
tableColumnModel.addColumn( new TableColumn( colIndex++ ) );

/*
* Store the map feature spect -> projection list, so that we don't have
* to call the projections() method several time.
*/
final Map< FeatureSpec< ?, ? >, List< FeatureProjection< ? > > > specToProjList = new HashMap<>();

// Units for feature columns.
for ( final FeatureSpec< ?, ? > fs : featureMap.keySet() )
{
final Feature< ? > feature = featureMap.get( fs );
if ( null == feature.projections() )
final String tooltip = "<html><p width=\"300\">" + fs.getInfo() + "</p></html>";
@SuppressWarnings( "rawtypes" )
final Feature feature = featureMap.get( fs );
@SuppressWarnings( "unchecked" )
final List< FeatureProjection< ? > > projections = ( feature.projections() == null )
? Collections.emptyList()
: new ArrayList<>( feature.projections() );
specToProjList.put( fs, projections );
if ( projections.isEmpty() )
continue;
final List< FeatureProjection< ? > > projections = new ArrayList<>( feature.projections() );
for ( final FeatureProjection< ? > projection : projections )
{
@SuppressWarnings( "unchecked" )
final FeatureProjection< O > fp = ( FeatureProjection< O > ) projection;
mapToProjections.add( fp );
mapToTooltip.add( "<html><p width=\"300\">" + fs.getInfo() + "</p></html>" );
mapToTooltip.add( tooltip );
final String units = fp.units();
lastHeaderLine.add( ( units == null || units.isEmpty() ) ? "" : "(" + units + ")" );
tableColumnModel.addColumn( new TableColumn( colIndex++ ) );
Expand Down Expand Up @@ -449,10 +464,9 @@ private void refreshColumns()
{
final ColumnGroup featureGroup = new ColumnGroup( fs.getKey() );
featureGroup.setHeaderRenderer( headerRenderer );
final Feature< ? > feature = featureMap.get( fs );
if ( null == feature.projections() )
final List< FeatureProjection< ? > > projections = specToProjList.get( fs );
if ( projections.isEmpty() )
continue;
final List< FeatureProjection< ? > > projections = new ArrayList<>( feature.projections() );
if ( projections.size() == 1 && projections.iterator().next().getKey().toString().equals( fs.getKey().toString() ) )
{
final ColumnGroup projectionGroup = new ColumnGroup( " " );
Expand Down

0 comments on commit 4d41480

Please sign in to comment.