Skip to content

Commit

Permalink
topcat: add GUI for explicit plot geometry setting
Browse files Browse the repository at this point in the history
Each plot2 window Axes control now has a new Size tab to allow the
user to explicitly set some or all of plot image dimensions and
insets in pixels.
  • Loading branch information
mbtaylor committed Dec 19, 2014
1 parent a4cdf05 commit 6420845
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 2 deletions.
25 changes: 25 additions & 0 deletions topcat/src/main/uk/ac/starlink/topcat/plot2/AxisController.java
Expand Up @@ -42,6 +42,7 @@ public abstract class AxisController<P,A> implements Configger {
private final ActionForwarder actionForwarder_;
private final List<ConfigControl> controlList_;
private final Set<DataId> seenDataIdSet_;
private Specifier<PlotPosition> posSpecifier_;
private ConfigMap aspectConfig_;
private Range[] ranges_;
private A aspect_;
Expand Down Expand Up @@ -242,6 +243,30 @@ public Navigator<A> getNavigator() {
return surfFact_.createNavigator( config );
}

/**
* Adds a tab to the main control for specifying plot positioning.
*
* @param hasInsets true if the insets options are allowed;
* if false just the external dimensions are queried
*/
protected void addPositionTab( boolean hasInsets ) {
posSpecifier_ = new PlotPositionSpecifier( hasInsets );
posSpecifier_.addActionListener( getActionForwarder() );
mainControl_.addControlTab( "Size", posSpecifier_.getComponent(),
true );
}

/**
* Returns an object that can provide explicit settings for plot icon
* dimensions and positioning.
*
* @return plot position
*/
public PlotPosition getPlotPosition() {
return posSpecifier_ == null ? new PlotPosition()
: posSpecifier_.getSpecifiedValue();
}

/**
* Adds a tab to the main control for specifying the aspect.
* This is like a config tab for the aspect keys, but has additional
Expand Down
Expand Up @@ -153,6 +153,9 @@ protected void doSubmit( ActionEvent evt ) {
new ConfigSpecifier( StyleKeys.CAPTIONER
.getKeys() ) );

/* Positioner tab. */
addPositionTab( false );

assert assertHasKeys( surfFact.getProfileKeys() );
}

Expand Down
Expand Up @@ -92,6 +92,9 @@ public HistogramAxisController( ControlStack stack ) {
barControl.addSpecifierTab( "Bars", new ConfigSpecifier( BAR_KEYS ) );
addControl( barControl );

/* Positioner tab. */
addPositionTab( true );

assert assertHasKeys( surfFact.getProfileKeys() );
}

Expand Down
Expand Up @@ -67,6 +67,9 @@ public PlaneAxisController( ControlStack stack ) {
new ConfigSpecifier( StyleKeys.CAPTIONER
.getKeys() ) );

/* Positioner tab. */
addPositionTab( true );

assert assertHasKeys( surfFact.getProfileKeys() );
}

Expand Down
162 changes: 162 additions & 0 deletions topcat/src/main/uk/ac/starlink/topcat/plot2/PlotPositionSpecifier.java
@@ -0,0 +1,162 @@
package uk.ac.starlink.topcat.plot2;

import java.awt.Dimension;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import uk.ac.starlink.ttools.plot2.config.ConfigException;
import uk.ac.starlink.ttools.plot2.config.ConfigKey;
import uk.ac.starlink.ttools.plot2.config.ConfigMap;
import uk.ac.starlink.ttools.plot2.config.ConfigMeta;
import uk.ac.starlink.ttools.plot2.config.Specifier;
import uk.ac.starlink.ttools.plot2.config.SpecifierPanel;

/**
* Specifier for a PlotPosition.
*
* @author Mark Taylor
* @since 18 Dec 2014
*/
public class PlotPositionSpecifier extends SpecifierPanel<PlotPosition> {

private final ConfigSpecifier configSpecifier_;

private static final ConfigKey<Integer> XPIX_KEY =
createIntegerKey( new ConfigMeta( "xpix", "Outer width" ) );
private static final ConfigKey<Integer> YPIX_KEY =
createIntegerKey( new ConfigMeta( "ypix", "Outer height" ) );
private static final ConfigKey<Integer> TOP_KEY =
createIntegerKey( new ConfigMeta( "top", "Top border" ) );
private static final ConfigKey<Integer> LEFT_KEY =
createIntegerKey( new ConfigMeta( "left", "Left border" ) );
private static final ConfigKey<Integer> BOTTOM_KEY =
createIntegerKey( new ConfigMeta( "bottom", "Bottom border" ) );
private static final ConfigKey<Integer> RIGHT_KEY =
createIntegerKey( new ConfigMeta( "right", "Right border" ) );

/**
* Constructor.
*/
public PlotPositionSpecifier( boolean hasInsets ) {
super( false );
List<ConfigKey> keyList = new ArrayList();
keyList.add( XPIX_KEY );
keyList.add( YPIX_KEY );
if ( hasInsets ) {
keyList.add( TOP_KEY );
keyList.add( LEFT_KEY );
keyList.add( BOTTOM_KEY );
keyList.add( RIGHT_KEY );
}
ConfigKey[] keys = keyList.toArray( new ConfigKey[ 0 ] );
configSpecifier_ = new ConfigSpecifier( keys );
configSpecifier_.addActionListener( getActionForwarder() );
}

public PlotPosition getSpecifiedValue() {
ConfigMap config = configSpecifier_.getSpecifiedValue();
Integer xpix = config.get( XPIX_KEY );
Integer ypix = config.get( YPIX_KEY );
Dimension size = xpix == null && ypix == null
? null
: new Dimension( xpix == null ? -1 : xpix.intValue(),
ypix == null ? -1 : ypix.intValue() );
Integer top = config.get( TOP_KEY );
Integer left = config.get( LEFT_KEY );
Integer bottom = config.get( BOTTOM_KEY );
Integer right = config.get( RIGHT_KEY );
Insets insets =
top == null && left == null && bottom == null && right == null
? null
: new Insets( top == null ? -1 : top.intValue(),
left == null ? -1 : left.intValue(),
bottom == null ? -1 : bottom.intValue(),
right == null ? -1 : right.intValue() );
return new PlotPosition( size, insets );
}

public void setSpecifiedValue( PlotPosition plotpos ) {
ConfigMap config = new ConfigMap();
Dimension size = plotpos.getPlotSize();
if ( size != null ) {
config.put( XPIX_KEY, size.width );
config.put( YPIX_KEY, size.height );
}
Insets insets = plotpos.getPlotInsets();
if ( insets != null ) {
config.put( TOP_KEY, insets.top );
config.put( LEFT_KEY, insets.left );
config.put( BOTTOM_KEY, insets.bottom );
config.put( RIGHT_KEY, insets.right );
}
configSpecifier_.setSpecifiedValue( config );
}

public JComponent createComponent() {
return configSpecifier_.createComponent();
}

/**
* Returns a ConfigKey for an Integer value. The default is null,
* so make sure to acquire the result as an Integer object not
* using auto-unboxing to get an int primitive.
*
* @param meta key metadata
* @return Integer key
*/
private static ConfigKey<Integer> createIntegerKey( ConfigMeta meta ) {
return new ConfigKey<Integer>( meta, Integer.class, null ) {
public String valueToString( Integer value ) {
return value == null ? "" : value.toString();
}
public Integer stringToValue( String txt ) {
if ( txt == null || txt.trim().length() == 0 ) {
return null;
}
try {
return Integer.decode( txt.trim() );
}
catch ( NumberFormatException e ) {
throw new ConfigException( this,
"\"" + txt + "\" not integer",
e );
}
}
public Specifier<Integer> createSpecifier() {
return new SpecifierPanel<Integer>( false ) {
final JTextField txtField_ = new JTextField( 6 );
protected JComponent createComponent() {
txtField_.addActionListener( getActionForwarder() );
JComponent box = Box.createHorizontalBox();
box.add( txtField_ );
box.add( Box.createHorizontalStrut( 5 ) );
box.add( new JLabel( "pixels" ) );
return box;
}
public Integer getSpecifiedValue() {
try {
return stringToValue( txtField_.getText() );
}
catch ( ConfigException e ) {
JOptionPane
.showMessageDialog( txtField_, e.getMessage(),
"Bad Value",
JOptionPane.ERROR_MESSAGE );
txtField_.setText( "" );
return null;
}
}
public void setSpecifiedValue( Integer value ) {
txtField_.setText( valueToString( value ) );
fireAction();
}
};
}
};
}
}
Expand Up @@ -85,6 +85,9 @@ protected SkySys getViewSystem() {
new ConfigSpecifier( StyleKeys.CAPTIONER
.getKeys() ) );

/* Positioner tab. */
addPositionTab( true );

assert assertHasKeys( surfFact.getProfileKeys() );
}

Expand Down
Expand Up @@ -70,6 +70,7 @@
import uk.ac.starlink.ttools.plot2.Surface;
import uk.ac.starlink.ttools.plot2.SurfaceFactory;
import uk.ac.starlink.ttools.plot2.config.ConfigMap;
import uk.ac.starlink.ttools.plot2.config.Specifier;
import uk.ac.starlink.ttools.plot2.data.CachedDataStoreFactory;
import uk.ac.starlink.ttools.plot2.data.CoordGroup;
import uk.ac.starlink.ttools.plot2.data.DataSpec;
Expand Down Expand Up @@ -159,9 +160,8 @@ public PlotLayer[] getItem() {
}
};
Factory<PlotPosition> posFact = new Factory<PlotPosition>() {
private final PlotPosition pos = new PlotPosition();
public PlotPosition getItem() {
return pos;
return axisController_.getPlotPosition();
}
};
final LegendControl legendControl =
Expand Down
Expand Up @@ -61,6 +61,8 @@ public TimeAxisController( ControlStack stack ) {
mainControl.addSpecifierTab( "Font",
new ConfigSpecifier( StyleKeys.CAPTIONER
.getKeys() ) );
/* Positioner tab. */
addPositionTab( true );

assert assertHasKeys( surfFact.getProfileKeys() );
}
Expand Down

0 comments on commit 6420845

Please sign in to comment.