Skip to content

Commit

Permalink
ttools: refactor captioner configuration
Browse files Browse the repository at this point in the history
The logic for selecting a font from configuration options is now
broken out into a new class CaptionerKeySet rather than using
opaque logic within the StyleKeys class.  This means it can be
reused, which in turn means the LabelPlotter class is able to use
a different instance of the relevant config keys than that used
for the axis labelling.  So now the logic in the topcat GUI classes
lets you choose a different font for point labelling than for
axis labelling.
  • Loading branch information
mbtaylor committed Mar 6, 2014
1 parent 26bd902 commit feb703c
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 165 deletions.
Expand Up @@ -146,7 +146,8 @@ protected void doSubmit( ActionEvent evt ) {

/* Font config tab. */
mainControl.addSpecifierTab( "Font",
new ConfigSpecifier( StyleKeys.getCaptionerKeys() ) );
new ConfigSpecifier( StyleKeys.CAPTIONER
.getKeys() ) );

assert assertHasKeys( surfFact.getProfileKeys() );
}
Expand Down
Expand Up @@ -83,7 +83,8 @@ public HistogramAxisController( ControlStack stack ) {

/* Font tab. */
mainControl.addSpecifierTab( "Font",
new ConfigSpecifier( StyleKeys.getCaptionerKeys() ) );
new ConfigSpecifier( StyleKeys.CAPTIONER
.getKeys() ) );


/* Bars control. */
Expand Down
Expand Up @@ -149,7 +149,7 @@ public Icon getLegendIcon() {
}
else {
Captioner captioner =
StyleKeys.createCaptioner( configger_.getConfig() );
StyleKeys.CAPTIONER.createValue( configger_.getConfig() );
boolean border = borderModel_.isSelected();
Color bgColor = opaqueModel_.isSelected() ? Color.WHITE : null;
return new LegendIcon( entries, captioner, border, bgColor );
Expand Down
Expand Up @@ -64,7 +64,8 @@ public PlaneAxisController( ControlStack stack ) {

/* Font tab. */
mainControl.addSpecifierTab( "Font",
new ConfigSpecifier( StyleKeys.getCaptionerKeys() ) );
new ConfigSpecifier( StyleKeys.CAPTIONER
.getKeys() ) );

assert assertHasKeys( surfFact.getProfileKeys() );
}
Expand Down
Expand Up @@ -143,7 +143,7 @@ public ShadeAxis createShadeAxis( Range range ) {
final boolean flip = config.get( StyleKeys.SHADE_FLIP );
final Color nullColor = config.get( StyleKeys.SHADE_NULL_COLOR );
final Captioner captioner =
StyleKeys.createCaptioner( configger_.getConfig() );
StyleKeys.CAPTIONER.createValue( configger_.getConfig() );
return new AxisFactory() {
public ShadeAxis createShadeAxis( Range range ) {
if ( range == null ) {
Expand Down
Expand Up @@ -82,7 +82,8 @@ protected SkySys getViewSystem() {

/* Font specifier. */
mainControl.addSpecifierTab( "Font",
new ConfigSpecifier( StyleKeys.getCaptionerKeys() ) );
new ConfigSpecifier( StyleKeys.CAPTIONER
.getKeys() ) );

assert assertHasKeys( surfFact.getProfileKeys() );
}
Expand Down
Expand Up @@ -59,7 +59,8 @@ public TimeAxisController( ControlStack stack ) {

/* Font tab. */
mainControl.addSpecifierTab( "Font",
new ConfigSpecifier( StyleKeys.getCaptionerKeys() ) );
new ConfigSpecifier( StyleKeys.CAPTIONER
.getKeys() ) );

assert assertHasKeys( surfFact.getProfileKeys() );
}
Expand Down
@@ -0,0 +1,144 @@
package uk.ac.starlink.ttools.plot2.config;

import java.awt.Font;
import org.scilab.forge.jlatexmath.TeXFormula;
import uk.ac.starlink.ttools.plot2.BasicCaptioner;
import uk.ac.starlink.ttools.plot2.Captioner;
import uk.ac.starlink.ttools.plot2.LatexCaptioner;

/**
* ConfigKeySet for specifying a captioner.
* Font size, type, weight are configurable as well as plain/latex
* rendering.
*
* @author Mark Taylor
* @since 6 Mar 2014
*/
public class CaptionerKeySet implements KeySet<Captioner> {

private final ConfigKey<TextSyntax> textSyntaxKey_;
private final ConfigKey<Integer> fontSizeKey_;
private final ConfigKey<FontType> fontTypeKey_;
private final ConfigKey<FontWeight> fontWeightKey_;

/**
* Constructor.
*/
public CaptionerKeySet() {
textSyntaxKey_ =
new OptionConfigKey<TextSyntax>( new ConfigMeta( "syntax",
"Text Syntax" ),
TextSyntax.class,
TextSyntax.values(),
TextSyntax.values()[ 0 ], true );
fontSizeKey_ =
IntegerConfigKey.createSpinnerKey( new ConfigMeta( "fontsize",
"Font Size" ),
12, 2, 64 );
fontTypeKey_ =
new OptionConfigKey<FontType>( new ConfigMeta( "fontstyle",
"Font Style" ),
FontType.class, FontType.values() );
fontWeightKey_ =
new OptionConfigKey<FontWeight>( new ConfigMeta( "fontweight",
"Font Weight" ),
FontWeight.class,
FontWeight.values() );
}

public ConfigKey[] getKeys() {
return new ConfigKey[] {
textSyntaxKey_,
fontSizeKey_,
fontTypeKey_,
fontWeightKey_,
};
}

public Captioner createValue( ConfigMap config ) {
TextSyntax syntax = config.get( textSyntaxKey_ );
int size = config.get( fontSizeKey_ );
FontType type = config.get( fontTypeKey_ );
FontWeight weight = config.get( fontWeightKey_ );
return syntax.createCaptioner( type, weight, size );
}

/**
* Font type enum for use with captioner configuration.
*/
private enum FontType {
SANSSERIF( "Standard", "Dialog", TeXFormula.SANSSERIF ),
SERIF( "Serif", "Serif", TeXFormula.ROMAN ),
MONO( "Mono", "Monospaced", TeXFormula.TYPEWRITER );

private final String name_;
private final String awtName_;
private final int texType_;

FontType( String name, String awtName, int texType ) {
name_ = name;
awtName_ = awtName;
texType_ = texType;
}

public String toString() {
return name_;
}
}

/**
* Font weight enum for use with captioner configuration.
*/
private enum FontWeight {
PLAIN( "Plain", Font.PLAIN, 0 ),
BOLD( "Bold", Font.BOLD, TeXFormula.BOLD ),
ITALIC( "Italic", Font.ITALIC, TeXFormula.ITALIC ),
BOLDITALIC( "Bold Italic", Font.BOLD | Font.ITALIC,
TeXFormula.BOLD | TeXFormula.ITALIC );

private final String name_;
private final int awtWeight_;
private final int texWeight_;

FontWeight( String name, int awtWeight, int texWeight ) {
name_ = name;
awtWeight_ = awtWeight;
texWeight_ = texWeight;
}

public String toString() {
return name_;
}
}

/**
* Text interpretation language enum for use with captioner configuration.
*/
private enum TextSyntax {
PLAIN( "Plain" ) {
public Captioner createCaptioner( FontType type, FontWeight weight,
int size ) {
return new BasicCaptioner( new Font( type.awtName_,
weight.awtWeight_,
size ) );
}
},
LATEX( "LaTeX" ) {
public Captioner createCaptioner( FontType type, FontWeight weight,
int size ) {
return new LatexCaptioner( size,
type.texType_ | weight.texWeight_ );
}
};
private final String name_;
TextSyntax( String name ) {
name_ = name;
}
public abstract Captioner createCaptioner( FontType type,
FontWeight weight,
int size );
public String toString() {
return name_;
}
}
}
Expand Up @@ -6,6 +6,11 @@
* this class has methods to allow a command-line or graphical interface
* to be constructed for the corresponding values automatically.
*
* <p>Note that this class does <em>not</em> sport the
* {@link uk.ac.starlink.ttools.plot2.Equality}
* annotation. A ConfigKey is normally considered equal only to itself,
* not to other similarly-named ConfigKeys.
*
* @author Mark Taylor
* @since 22 Feb 2013
*/
Expand Down
28 changes: 28 additions & 0 deletions ttools/src/main/uk/ac/starlink/ttools/plot2/config/KeySet.java
@@ -0,0 +1,28 @@
package uk.ac.starlink.ttools.plot2.config;

/**
* Aggregates a set of config keys which are used together to specify
* an object.
*
* @author Mark Taylor
* @since 6 Mar 2014
*/
public interface KeySet<T> {

/**
* The config keys used to specify an object.
*
* @return fixed list of config keys
*/
ConfigKey[] getKeys();

/**
* Creates a typed value based on the values in a map corresponding
* to this object's keys.
*
* @param map map for which the values corresponding to
* <code>getKeys</code> will be examined
* @return specified typed value
*/
T createValue( ConfigMap map );
}

0 comments on commit feb703c

Please sign in to comment.