-
Notifications
You must be signed in to change notification settings - Fork 23
/
PlotPositionSpecifier.java
160 lines (151 loc) · 6.42 KB
/
PlotPositionSpecifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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() {
super( false );
List<ConfigKey> keyList = new ArrayList();
keyList.add( XPIX_KEY );
keyList.add( YPIX_KEY );
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 ) throws ConfigException {
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();
}
};
}
};
}
}