Skip to content

Commit

Permalink
topcat: refactor CoordPanel/PositionCoordPanel
Browse files Browse the repository at this point in the history
PositionCoordPanel was an interface with a wrapper implementation
rather than an extension of the CoordPanel class, which was silly.
Made it sensible; less jumping through hoops required.
  • Loading branch information
mbtaylor committed Dec 12, 2013
1 parent 907f453 commit 3dc769a
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 205 deletions.
89 changes: 8 additions & 81 deletions topcat/src/main/uk/ac/starlink/topcat/plot2/CoordPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
Expand All @@ -13,15 +11,12 @@
import uk.ac.starlink.table.ColumnData;
import uk.ac.starlink.table.ColumnInfo;
import uk.ac.starlink.table.ConstantColumn;
import uk.ac.starlink.table.DefaultValueInfo;
import uk.ac.starlink.table.DomainMapper;
import uk.ac.starlink.table.ValueInfo;
import uk.ac.starlink.table.gui.LabelledComponentStack;
import uk.ac.starlink.topcat.ActionForwarder;
import uk.ac.starlink.topcat.ColumnDataComboBoxModel;
import uk.ac.starlink.topcat.TopcatModel;
import uk.ac.starlink.ttools.plot2.data.Coord;
import uk.ac.starlink.ttools.plot2.data.StorageType;
import uk.ac.starlink.util.gui.ComboBoxBumper;

/**
Expand All @@ -30,12 +25,13 @@
* @author Mark Taylor
* @since 13 Mar 2013
*/
public class CoordPanel extends JPanel {
public class CoordPanel {

private final Coord[] coords_;
private final ActionForwarder forwarder_;
private final JComboBox[][] colSelectors_;
private final boolean autoPopulate_;
private final JComponent panel_;

/**
* Constructor.
Expand All @@ -46,7 +42,7 @@ public class CoordPanel extends JPanel {
* when a table is selected
*/
public CoordPanel( Coord[] coords, boolean autoPopulate ) {
super( new BorderLayout() );
panel_ = new JPanel( new BorderLayout() );
coords_ = coords;
forwarder_ = new ActionForwarder();
autoPopulate_ = autoPopulate;
Expand Down Expand Up @@ -84,20 +80,16 @@ public CoordPanel( Coord[] coords, boolean autoPopulate ) {

/* Place the lot at the top of the component so it doesn't fill
* vertical space. */
add( stack, BorderLayout.NORTH );
panel_.add( stack, BorderLayout.NORTH );
}

/**
* Constructs a coord panel for entry of multiple copies of a single group
* of coordinates.
* Returns the graphical component for this object.
*
* @param coords template coordinates
* @param ncopy number of copies of group
* @param autoPopulate whether to attempt to fill in values
* when table changes
* @return component
*/
public CoordPanel( Coord[] coords, int ncopy, boolean autoPopulate ) {
this( multiplyCoords( coords, ncopy ), autoPopulate );
public JComponent getComponent() {
return panel_;
}

/**
Expand Down Expand Up @@ -228,69 +220,4 @@ public void setColumnSelector( int ic, int iu,
ColumnDataComboBoxModel model ) {
colSelectors_[ ic ][ iu ].setModel( model );
}

/**
* Returns a list of coordinates which is like multiple copies of a
* supplied group. The returned coords have metadata which
* distinguish them from each other, currently an integer appended
* to their name. The returned coords are not totally respectable,
* but their metadata is OK.
*
* @param coords basic coordinates
* @param ncopy number of copies to make
* @return array of ncopy copies of coords
*/
private static Coord[] multiplyCoords( Coord[] coords, int ncopy ) {
List<Coord> coordList = new ArrayList<Coord>();
for ( int ig = 0; ig < ncopy; ig++ ) {
for ( int ic = 0; ic < coords.length; ic++ ) {
Coord coord = coords[ ic ];
coordList.add( ncopy == 1 ? coord : relabel( coord, ig ) );
}
}
return coordList.toArray( new Coord[ 0 ] );
}

/**
* Returns a Coord like a given one but with modified metadata.
*
* <p>The returned Coord is not of the right subclass, hence does not
* have the appropriate type-specific read*Coord method.
* However that doesn't matter, because we're just using the results
* from this call to represent coordinate metadata, not for reading data.
*
* @param baseCoord coord on which to base the copy
* @param point index, used to label the coordinate
* @return new coord like the input one
*/
private static Coord relabel( final Coord baseCoord, int iPoint ) {
String iptxt = Integer.toString( iPoint + 1 );
final ValueInfo[] infos = baseCoord.getUserInfos().clone();
int nuc = infos.length;
for ( int iuc = 0; iuc < nuc; iuc++ ) {
DefaultValueInfo info = new DefaultValueInfo( infos[ iuc ] );
info.setName( info.getName() + iptxt );
info.setDescription( info.getDescription()
+ " for point " + iptxt );
infos[ iuc ] = info;
}
return new Coord() {
public ValueInfo[] getUserInfos() {
return infos;
}
public boolean isRequired() {
return true;
}
public StorageType getStorageType() {
return baseCoord.getStorageType();
}
public List<Class<? extends DomainMapper>> getUserDomains() {
return baseCoord.getUserDomains();
}
public Object userToStorage( Object[] userCoords,
DomainMapper[] userMappers ) {
return baseCoord.userToStorage( userCoords, userMappers );
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public ModeFormControl( Configger baseConfigger, ModePlotter[] plotters,
false );
if ( ! commonExtraCoordList.isEmpty() ) {
commonExtraCoordPanel_
.getComponent()
.setBorder( AuxWindow.makeTitledBorder( "Coordinates" ) );
}
commonExtraCoordPanel_.addActionListener( forwarder );
Expand Down Expand Up @@ -153,7 +154,8 @@ public void actionPerformed( ActionEvent evt ) {
modePanel.setBorder( AuxWindow.makeTitledBorder( "Shading" ) );
panel_ = new JPanel( new BorderLayout() );
panel_.add( modePanel, BorderLayout.NORTH );
panel_.add( commonExtraCoordPanel_, BorderLayout.CENTER );
panel_.add( commonExtraCoordPanel_.getComponent(),
BorderLayout.CENTER );
modeSpecifier.setSpecifiedValue( modeMap_.keySet().iterator().next() );
}

Expand Down Expand Up @@ -232,7 +234,7 @@ private void setMode( ModePlotter.Mode mode ) {
if ( state_ != null ) {
CoordPanel coordPanel = state_.modeCoordPanel_;
coordPanel.addActionListener( forwarder );
modeCoordHolder_.add( coordPanel );
modeCoordHolder_.add( coordPanel.getComponent() );
ConfigSpecifier configSpecifier = state_.modeConfigSpecifier_;
configSpecifier.addActionListener( forwarder );
modeConfigHolder_.add( configSpecifier.getComponent() );
Expand Down Expand Up @@ -336,7 +338,8 @@ private static class ModeState {
plotter_ = plotter;
modeCoordPanel_ = new CoordPanel( modeCoords, false );
if ( modeCoords.length > 0 ) {
modeCoordPanel_.setBorder( BorderFactory
modeCoordPanel_.getComponent()
.setBorder( BorderFactory
.createEmptyBorder( 0, 0, 5, 0 ) );
}
modeConfigSpecifier_ = new ConfigSpecifier( configKeys );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,14 @@ protected LayerControl createLayerControl() {
}

/* Not great - no options for miscellaneous plotters with both
* positional and non-positional coordinates. There's no reason
* it can't be done, but the (probably unnecessarily messy)
* way that CoordPanel and PositionCoordPanel are currently
* defined makes it fiddly to do. */
* positional and non-positional coordinates. Could be done if
* necessary. */
else if ( plotter.getPositionCount() == 0 ) {
return new PlotterStackAction( plotter, stack ) {
protected LayerControl createLayerControl() {
PositionCoordPanel coordPanel =
new SimplePositionCoordPanel(
new CoordPanel( plotter.getExtraCoords(), false ),
null );
new SimplePositionCoordPanel( plotter.getExtraCoords(),
false, null );
return new BasicCoordLayerControl( plotter, coordPanel );
}
};
Expand Down
109 changes: 74 additions & 35 deletions topcat/src/main/uk/ac/starlink/topcat/plot2/PositionCoordPanel.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,104 @@
package uk.ac.starlink.topcat.plot2;

import java.awt.event.ActionListener;
import javax.swing.JComponent;
import uk.ac.starlink.topcat.TopcatModel;
import java.util.ArrayList;
import java.util.List;
import uk.ac.starlink.table.DefaultValueInfo;
import uk.ac.starlink.table.DomainMapper;
import uk.ac.starlink.table.ValueInfo;
import uk.ac.starlink.ttools.plot2.DataGeom;
import uk.ac.starlink.ttools.plot2.data.Coord;
import uk.ac.starlink.ttools.plot2.data.StorageType;

/**
* GUI component for obtaining data position coordinates.
*
* @author Mark Taylor
* @since 13 Mar 2013
*/
public interface PositionCoordPanel {
public abstract class PositionCoordPanel extends CoordPanel {

/**
* Returns the graphical component for user interaction.
* Constructor.
*
* @return component
* @param coords coordinate definitions for which values are required
* @param autoPopulate if true, some attempt will be made to
* fill in the fields with non-blank values
* when a table is selected
*/
JComponent getComponent();

/**
* Sets the table with reference to which this panel will resolve
* coordinate descriptions.
*
* @param tcModel table from which coordinate values will be drawn
*/
void setTable( TopcatModel tcModel );

/**
* Returns the coordinate values currently selected in this panel.
* If there is insufficient information to contribute to a plot
* (not all of the
* {@link uk.ac.starlink.ttools.plot2.data.Coord#isRequired required}
* coord values are filled in)
* then null will be returned.
*
* @return nCoord-element array of coord contents, or null
*/
GuiCoordContent[] getContents();
protected PositionCoordPanel( Coord[] coords, boolean autoPopulate ) {
super( coords, autoPopulate );
}

/**
* Returns the position geometry that defines the mapping of input
* to data coordinates.
*
* @return data geom
*/
DataGeom getDataGeom();
public abstract DataGeom getDataGeom();

/**
* Adds a listener which will be notified when the coordinate selection
* changes.
* Returns a list of coordinates which is like multiple copies of a
* supplied group. The returned coords have metadata which
* distinguish them from each other, currently an integer appended
* to their name. The returned coords are not totally respectable,
* but their metadata is OK.
*
* @param listener listener
* @param coords basic coordinates
* @param ncopy number of copies to make
* @return array of ncopy copies of coords
*/
void addActionListener( ActionListener listener );
public static Coord[] multiplyCoords( Coord[] coords, int ncopy ) {
List<Coord> coordList = new ArrayList<Coord>();
for ( int ig = 0; ig < ncopy; ig++ ) {
for ( int ic = 0; ic < coords.length; ic++ ) {
Coord coord = coords[ ic ];
coordList.add( ncopy == 1 ? coord : relabel( coord, ig ) );
}
}
return coordList.toArray( new Coord[ 0 ] );
}

/**
* Removes a listener which was added previously.
* Returns a Coord like a given one but with modified metadata.
*
* <p>The returned Coord is not of the right subclass, hence does not
* have the appropriate type-specific read*Coord method.
* However that doesn't matter, because we're just using the results
* from this call to represent coordinate metadata, not for reading data.
*
* @param listener listener
* @param baseCoord coord on which to base the copy
* @param iPoint point index, used to label the coordinate
* @return new coord like the input one
*/
void removeActionListener( ActionListener listener );
private static Coord relabel( final Coord baseCoord, int iPoint ) {
String iptxt = Integer.toString( iPoint + 1 );
final ValueInfo[] infos = baseCoord.getUserInfos().clone();
int nuc = infos.length;
for ( int iuc = 0; iuc < nuc; iuc++ ) {
DefaultValueInfo info = new DefaultValueInfo( infos[ iuc ] );
info.setName( info.getName() + iptxt );
info.setDescription( info.getDescription()
+ " for point " + iptxt );
infos[ iuc ] = info;
}
return new Coord() {
public ValueInfo[] getUserInfos() {
return infos;
}
public boolean isRequired() {
return true;
}
public StorageType getStorageType() {
return baseCoord.getStorageType();
}
public List<Class<? extends DomainMapper>> getUserDomains() {
return baseCoord.getUserDomains();
}
public Object userToStorage( Object[] userCoords,
DomainMapper[] userMappers ) {
return baseCoord.userToStorage( userCoords, userMappers );
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public SimpleFormControl( Configger baseConfigger, Plotter plotter ) {
Coord[] extraCoords = plotter.getExtraCoords();
extraCoordPanel_ = new CoordPanel( extraCoords, false );
if ( extraCoords.length > 0 ) {
extraCoordPanel_.setBorder( AuxWindow
extraCoordPanel_.getComponent()
.setBorder( AuxWindow
.makeTitledBorder( "Coordinates" ) );
}
extraCoordPanel_.addActionListener( getActionForwarder() );
Expand All @@ -46,7 +47,7 @@ protected ConfigKey[] getConfigKeys() {
}

protected JComponent getCoordPanel() {
return extraCoordPanel_;
return extraCoordPanel_.getComponent();
}

public GuiCoordContent[] getExtraCoordContents() {
Expand Down

0 comments on commit 3dc769a

Please sign in to comment.