Skip to content

Commit

Permalink
ttools: CombinationConfigKey may optionally allow null value
Browse files Browse the repository at this point in the history
The null value will be used to indicate 2-axis zooming for cube surface.
  • Loading branch information
mbtaylor committed Nov 7, 2013
1 parent 1459ae6 commit f771745
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
Expand Up @@ -21,6 +21,7 @@ public class CombinationConfigKey extends ConfigKey<boolean[]> {

private final int nopt_;
private final String[] optNames_;
private final boolean nullPermitted_;

/**
* Constructs an instance with a specified default.
Expand All @@ -29,40 +30,58 @@ public class CombinationConfigKey extends ConfigKey<boolean[]> {
* @param dflt default array of selection flags
* @param optNames labels for each of the options that may be selected,
* same length as <code>dflt</code>
* @param nullPermitted true iff null is an acceptable value for
* this key
*/
public CombinationConfigKey( ConfigMeta meta, boolean[] dflt,
String[] optNames ) {
String[] optNames, boolean nullPermitted ) {
super( meta, boolean[].class, dflt );
optNames_ = optNames;
nullPermitted_ = nullPermitted;
nopt_ = optNames.length;
if ( dflt.length != nopt_ ) {
throw new IllegalArgumentException( "Array length mismatch" );
if ( dflt == null ) {
if ( ! nullPermitted ) {
throw new NullPointerException();
}
}
else {
if ( dflt.length != nopt_ ) {
throw new IllegalArgumentException( "Array length mismatch" );
}
}
}

/**
* Constructs an instance where all the default inclusion flags are true.
* Constructs an instance where all the default inclusion flags are true
* and a null value is not permitted.
*
* @param meta metadata
* @param optNames labels for each of the options that may be selected,
*/
public CombinationConfigKey( ConfigMeta meta, String[] optNames ) {
this( meta, createTrueArray( optNames.length ), optNames );
this( meta, createTrueArray( optNames.length ), optNames, false );
}

public boolean[] stringToValue( String txt ) throws ConfigException {
boolean[] value = new boolean[ nopt_ ];
for ( int ic = 0; ic < txt.length(); ic++ ) {
value[ optCharToIndex( txt.charAt( ic ) ) ] = true;
if ( nullPermitted_ && ( txt == null || txt.trim().length() == 0 ) ) {
return null;
}
else {
boolean[] value = new boolean[ nopt_ ];
for ( int ic = 0; ic < txt.length(); ic++ ) {
value[ optCharToIndex( txt.charAt( ic ) ) ] = true;
}
return value;
}
return value;
}

public String valueToString( boolean[] opts ) {
StringBuffer sbuf = new StringBuffer();
for ( int io = 0; io < nopt_; io++ ) {
if ( opts[ io ] ) {
sbuf.append( optIndexToChar( io ) );
if ( opts != null ) {
for ( int io = 0; io < nopt_; io++ ) {
if ( opts[ io ] ) {
sbuf.append( optIndexToChar( io ) );
}
}
}
return sbuf.toString();
Expand Down
Expand Up @@ -30,7 +30,8 @@ public class CubeNavigator implements Navigator<CubeAspect> {
/** Config key to select which axes zoom will operate on. */
public static final ConfigKey<boolean[]> ZOOMAXES_KEY =
new CombinationConfigKey( new ConfigMeta( "zoomaxes", "Zoom Axes" ),
new String[] { "X", "Y", "Z" } );
new boolean[] { true, true, true },
new String[] { "X", "Y", "Z" }, true );

/**
* Constructor.
Expand Down
Expand Up @@ -130,7 +130,7 @@ private static class AxisCombinationConfigKey extends CombinationConfigKey {
AxisCombinationConfigKey( ConfigMeta meta,
boolean tDflt, boolean yDflt) {
super( meta, new boolean[] { tDflt, yDflt },
new String[] { "Time", "Y" } );
new String[] { "Time", "Y" }, false );
}

/**
Expand Down

0 comments on commit f771745

Please sign in to comment.