Skip to content

Commit

Permalink
topcat: rearrange plot window instantiation
Browse files Browse the repository at this point in the history
TopcatWindowAction is generic-ised better, which allows it to be re-used
for new- and old-style plot windows.  New-style (plot2) plot windows
no longer add a default control in their constructor, this is handled
by the ControlWindow.  That makes it possible to construct a plot
window without a default control.
  • Loading branch information
mbtaylor authored and mmpcn committed Nov 27, 2014
1 parent 2077720 commit fd7b707
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 87 deletions.
143 changes: 74 additions & 69 deletions topcat/src/main/uk/ac/starlink/topcat/ControlWindow.java
Expand Up @@ -117,6 +117,7 @@
import uk.ac.starlink.topcat.plot.LinesWindow;
import uk.ac.starlink.topcat.plot.PlotWindow;
import uk.ac.starlink.topcat.plot.SphereWindow;
import uk.ac.starlink.topcat.plot2.Control;
import uk.ac.starlink.topcat.plot2.CubePlotWindow;
import uk.ac.starlink.topcat.plot2.PlanePlotWindow;
import uk.ac.starlink.topcat.plot2.SkyPlotWindow;
Expand Down Expand Up @@ -418,28 +419,28 @@ public void stateChanged( ChangeEvent evt ) {
DensityWindow.class ),
};
plot2Acts_ = new Action[] {
new TopcatWindowAction( "Plane Layer Plot",
ResourceIcon.PLOT2_PLANE,
"Plane plotting window",
PlanePlotWindow.class ),
new TopcatWindowAction( "Sky Layer Plot",
ResourceIcon.PLOT2_SKY,
"Sky plotting window",
SkyPlotWindow.class ),
new TopcatWindowAction( "Cube Layer Plot",
ResourceIcon.PLOT2_CUBE,
"3D plotting window"
+ " using Cartesian coordinates",
CubePlotWindow.class ),
new TopcatWindowAction( "Sphere Layer Plot",
ResourceIcon.PLOT2_SPHERE,
"3D plotting window"
+ " using spherical polar coordinates",
SpherePlotWindow.class ),
new TopcatWindowAction( "Time Layer Plot",
ResourceIcon.PLOT2_TIME,
"Time series plotting window",
TimePlotWindow.class ),
new Plot2WindowAction( "Plane Layer Plot",
ResourceIcon.PLOT2_PLANE,
"Plane plotting window",
PlanePlotWindow.class ),
new Plot2WindowAction( "Sky Layer Plot",
ResourceIcon.PLOT2_SKY,
"Sky plotting window",
SkyPlotWindow.class ),
new Plot2WindowAction( "Cube Layer Plot",
ResourceIcon.PLOT2_CUBE,
"3D plotting window"
+ " using Cartesian coordinates",
CubePlotWindow.class ),
new Plot2WindowAction( "Sphere Layer Plot",
ResourceIcon.PLOT2_SPHERE,
"3D plotting window"
+ " using spherical polar coordinates",
SpherePlotWindow.class ),
new Plot2WindowAction( "Time Layer Plot",
ResourceIcon.PLOT2_TIME,
"Time series plotting window",
TimePlotWindow.class ),
};

matchActs_ = new Action[] {
Expand Down Expand Up @@ -1694,69 +1695,73 @@ protected Window createWindow( TopcatModel tcModel ) {
}

/**
* Action implementation for graphics windows.
* Action class for old-style graphics windows.
*/
private class GraphicsWindowAction extends BasicAction {
final Constructor constructor_;
private class GraphicsWindowAction
extends TopcatWindowAction<GraphicsWindow> {

/**
* Constructor.
*
* @param name action name
* @param icon action icon
* @param shortdesc action short description
* @param winClass AuxWindow subclass - must have a
* @param winClass GraphicsWindow subclass - must have a
* constructor that takes (Component)
*/
GraphicsWindowAction( String name, Icon icon, String shortdesc,
Class winClass ) {
super( name, icon, shortdesc );
if ( ! GraphicsWindow.class.isAssignableFrom( winClass ) ) {
throw new IllegalArgumentException();
}
try {
constructor_ = winClass.getConstructor( new Class[] {
Component.class,
} );
}
catch ( NoSuchMethodException e ) {
throw (IllegalArgumentException)
new IllegalArgumentException( "No suitable constructor" )
.initCause( e );
}
Class<? extends GraphicsWindow> winClass ) {
super( name, icon, shortdesc, winClass );
}

@Override
public void actionPerformed( ActionEvent evt ) {
try {
Object[] args = new Object[] { ControlWindow.this };
try {
GraphicsWindow window =
(GraphicsWindow) constructor_.newInstance( args );
TopcatModel tcModel = getCurrentModel();
if ( tcModel != null ) {
int npoint =
(int) Math.min( tcModel.getDataModel()
.getRowCount(),
(long) Integer.MAX_VALUE );
window.setGuidePointCount( npoint );
}
window.setVisible( true );
if ( tcModel != null ) {
window.setMainTable( tcModel );
}
}
catch ( InvocationTargetException e ) {
throw e.getCause();
GraphicsWindow window = createWindow();
TopcatModel tcModel = getCurrentModel();
if ( tcModel != null ) {
int npoint =
(int) Math.min( tcModel.getDataModel().getRowCount(),
(long) Integer.MAX_VALUE );
window.setGuidePointCount( npoint );
window.setVisible( true );
if ( tcModel != null ) {
window.setMainTable( tcModel );
}
}
catch ( RuntimeException e ) {
throw e;
}
catch ( Error e ) {
throw e;
}
catch ( Throwable e ) {
throw new RuntimeException( "Window creation failed???", e );
window.setVisible( true );
}
}

/**
* Action class for new-style graphics windows.
*/
private class Plot2WindowAction
extends TopcatWindowAction<StackPlotWindow> {

/**
* Constructor.
*
* @param name action name
* @param icon action icon
* @param shortdesc action short description
* @param winClass StackPlotWindow subclass - must have a
* constructor that takes (Component)
*/
Plot2WindowAction( String name, Icon icon, String shortdesc,
Class<? extends StackPlotWindow> winClazz ) {
super( name, icon, shortdesc, winClazz );
}

@Override
public void actionPerformed( ActionEvent evt ) {
StackPlotWindow window = createWindow();
TopcatModel tcModel = getCurrentModel();
Control dfltControl =
window.getControlManager().createDefaultControl( tcModel );
if ( dfltControl != null ) {
window.getControlStack().addControl( dfltControl );
}
window.setVisible( true );
}
}

Expand Down
36 changes: 29 additions & 7 deletions topcat/src/main/uk/ac/starlink/topcat/TopcatWindowAction.java
Expand Up @@ -18,9 +18,11 @@
* @author Mark Taylor
* @since 24 Jul 2013
*/
public class TopcatWindowAction extends BasicAction
implements TopcatToolAction {
private final Constructor constructor_;
public class TopcatWindowAction<W extends JFrame>
extends BasicAction
implements TopcatToolAction {

private final Constructor<? extends W> constructor_;
private Component parent_;

/**
Expand All @@ -34,7 +36,7 @@ public class TopcatWindowAction extends BasicAction
* java.awt.Component giving the window parent
*/
public TopcatWindowAction( String name, Icon icon, String shortdesc,
Class<? extends JFrame> winClazz ) {
Class<? extends W> winClazz ) {
super( name, icon, shortdesc );
try {
constructor_ = winClazz.getConstructor( new Class[] {
Expand All @@ -54,12 +56,16 @@ public TopcatWindowAction( String name, Icon icon, String shortdesc,
}
}

public void actionPerformed( ActionEvent evt ) {
/**
* Creates an instance of the window class used by this action.
*
* @return window initialised with parent component
*/
protected W createWindow() {
try {
Object[] args = new Object[] { parent_ };
try {
JFrame window = (JFrame) constructor_.newInstance( args );
window.setVisible( true );
return constructor_.newInstance( args );
}
catch ( InvocationTargetException e ) {
throw e.getCause();
Expand All @@ -76,6 +82,22 @@ public void actionPerformed( ActionEvent evt ) {
}
}

/**
* Performs the action.
* The default immplementation just calls {@link #createWindow}
* and sets it visible. This may be overridden.
*/
public void actionPerformed( ActionEvent evt ) {
W window = createWindow();
window.setVisible( true );
}

/**
* Sets the parent component to use for initialising windows
* created by this action.
*
* @param parent parent component
*/
public void setParent( Component parent ) {
parent_ = parent;
}
Expand Down
33 changes: 22 additions & 11 deletions topcat/src/main/uk/ac/starlink/topcat/plot2/StackPlotWindow.java
Expand Up @@ -89,6 +89,7 @@ public class StackPlotWindow<P,A> extends AuxWindow {
private final PlotPanel<P,A> plotPanel_;
private final ControlStack stack_;
private final ControlStackModel stackModel_;
private final ControlManager controlManager_;
private final ToggleButtonModel showProgressModel_;
private final JLabel posLabel_;
private final JLabel countLabel_;
Expand Down Expand Up @@ -289,10 +290,10 @@ public void modelChanged( TopcatEvent evt ) {
}
}
};
ControlManager controlManager =
controlManager_ =
new GangControlManager( stack_, plotType, plotTypeGui, configger,
tcListener );
Action[] stackActions = controlManager.getStackActions();
Action[] stackActions = controlManager_.getStackActions();

/* Action for deleting a control from the stack. */
Action removeAction =
Expand Down Expand Up @@ -402,15 +403,6 @@ public void actionPerformed( ActionEvent evt ) {
cpanel.setLayout( new BoxLayout( cpanel, BoxLayout.X_AXIS ) );
cpanel.add( statusLine );

/* Try to set up a default control so that when the window opens
* something gets plotted immediately. */
Control dfltControl =
controlManager.createDefaultControl( ControlWindow.getInstance()
.getCurrentModel() );
if ( dfltControl != null ) {
stack_.addControl( dfltControl );
}

/* Set default component dimensions. */
displayPanel.setMinimumSize( new Dimension( 150, 150 ) );
displayPanel.setPreferredSize( new Dimension( 500, 400 ) );
Expand All @@ -422,6 +414,25 @@ public void actionPerformed( ActionEvent evt ) {
floater.init();
}

/**
* Returns the stack containing controls which define what this
* window is displaying.
*
* @return control stack
*/
public ControlStack getControlStack() {
return stack_;
}

/**
* Returns the manager object that controls this window's stack.
*
* @return control manager
*/
public ControlManager getControlManager() {
return controlManager_;
}

@Override
public void dispose() {
super.dispose();
Expand Down

0 comments on commit fd7b707

Please sign in to comment.