Refactor Recovery and Separation panels to derive from

FlightConfigurablePanel.  Added DefaultConfiguration listener to the
FlightConfigurablePanel to keep the RocketFigure in sync.
This commit is contained in:
kruland2607 2013-09-30 21:59:37 -05:00
parent 43187b2b2c
commit 9e01ff4e76
3 changed files with 66 additions and 35 deletions

View File

@ -1,5 +1,7 @@
package net.sf.openrocket.gui.main.flightconfigpanel;
import java.util.EventObject;
import javax.swing.JPanel;
import javax.swing.JTable;
@ -10,6 +12,7 @@ import net.sf.openrocket.rocketcomponent.FlightConfigurableComponent;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.Pair;
import net.sf.openrocket.util.StateChangeListener;
public abstract class FlightConfigurablePanel<T extends FlightConfigurableComponent> extends JPanel {
@ -23,6 +26,39 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
super(new MigLayout("fill"));
this.flightConfigurationPanel = flightConfigurationPanel;
this.rocket = rocket;
rocket.getDefaultConfiguration().addChangeListener( new StateChangeListener() {
@Override
public void stateChanged(EventObject e) {
String id = FlightConfigurablePanel.this.rocket.getDefaultConfiguration().getFlightConfigurationID();
String selectedId = FlightConfigurablePanel.this.getSelectedConfigurationId();
if ( id == null && selectedId == null ) {
// Nothing to do
} else if ( id == null ) {
// need to unselect
FlightConfigurablePanel.this.getTable().clearSelection();
} else if ( !id.equals(selectedId)){
// Need to change selection
// We'll select the correct row, in the currently selected column.
JTable table = FlightConfigurablePanel.this.getTable();
int col = table.getSelectedColumn();
for( int row = 0; row < table.getRowCount(); row++ ) {
String rowId = FlightConfigurablePanel.this.rocket.getFlightConfigurationIDs()[row + 1];
if ( rowId.equals(id) ) {
table.changeSelection(row, col, true, false);
}
}
}
}
});
}
protected abstract JTable getTable();
@ -41,5 +77,21 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
}
return null;
}
protected String getSelectedConfigurationId() {
int col = getTable().getSelectedColumn();
int row = getTable().getSelectedRow();
if ( row < 0 || col < 0 ) {
return null;
}
Object tableValue = getTable().getModel().getValueAt(row, col);
if ( tableValue instanceof Pair ) {
Pair<String,T> selectedComponent = (Pair<String,T>) tableValue;
return selectedComponent.getU();
} else if ( tableValue instanceof String ){
return (String) tableValue;
}
return null;
}
}

View File

@ -11,7 +11,6 @@ import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
@ -31,7 +30,7 @@ import net.sf.openrocket.startup.Application;
import net.sf.openrocket.unit.UnitGroup;
import net.sf.openrocket.util.Pair;
public class RecoveryConfigurationPanel extends JPanel {
public class RecoveryConfigurationPanel extends FlightConfigurablePanel<RecoveryDevice> {
Translator trans = Application.getTranslator();
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
@ -46,7 +45,7 @@ public class RecoveryConfigurationPanel extends JPanel {
RecoveryConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(new MigLayout("fill"));
super(flightConfigurationPanel,rocket);
this.flightConfigurationPanel = flightConfigurationPanel;
this.rocket = rocket;
@ -94,6 +93,11 @@ public class RecoveryConfigurationPanel extends JPanel {
this.add(resetDeploymentButton, "sizegroup button, wrap");
}
@Override
protected JTable getTable() {
return recoveryTable;
}
public void fireTableDataChanged() {
int selected = recoveryTable.getSelectedRow();
recoveryTableModel.fireTableDataChanged();
@ -130,21 +134,6 @@ public class RecoveryConfigurationPanel extends JPanel {
resetDeploymentButton.setEnabled(componentSelected);
}
private RecoveryDevice getSelectedComponent() {
int col = recoveryTable.getSelectedColumn();
int row = recoveryTable.getSelectedRow();
if ( row < 0 || col < 0 ) {
return null;
}
Object tableValue = recoveryTable.getModel().getValueAt(row, col);
if ( tableValue instanceof Pair ) {
Pair<String,RecoveryDevice> selected = (Pair<String,RecoveryDevice>) tableValue;
return selected.getV();
}
return null;
}
class RecoveryTableCellRenderer extends DefaultTableCellRenderer {
RecoveryTableCellRenderer() {}

View File

@ -11,14 +11,12 @@ import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.formatting.RocketDescriptor;
import net.sf.openrocket.gui.dialogs.flightconfiguration.SeparationSelectionDialog;
import net.sf.openrocket.gui.util.GUIUtil;
@ -30,7 +28,7 @@ import net.sf.openrocket.startup.Application;
import net.sf.openrocket.unit.UnitGroup;
import net.sf.openrocket.util.Pair;
public class SeparationConfigurationPanel extends JPanel {
public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage> {
static final Translator trans = Application.getTranslator();
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
@ -45,7 +43,7 @@ public class SeparationConfigurationPanel extends JPanel {
SeparationConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(new MigLayout("fill"));
super(flightConfigurationPanel,rocket);
this.flightConfigurationPanel = flightConfigurationPanel;
this.rocket = rocket;
@ -94,6 +92,11 @@ public class SeparationConfigurationPanel extends JPanel {
}
@Override
protected JTable getTable() {
return separationTable;
}
public void fireTableDataChanged() {
int selected = separationTable.getSelectedRow();
separationTableModel.fireTableDataChanged();
@ -104,19 +107,6 @@ public class SeparationConfigurationPanel extends JPanel {
updateButtonState();
}
private Stage getSelectedComponent() {
int col = separationTable.getSelectedColumn();
int row = separationTable.getSelectedRow();
if ( row < 0 || col < 0 ) {
return null;
}
Object tableValue = separationTable.getModel().getValueAt(row, col);
if ( tableValue instanceof Pair ) {
Pair<String,Stage> selected = (Pair<String,Stage>) tableValue;
return selected.getV();
}
return null;
}
private void selectDeployment() {
Stage stage = getSelectedComponent();