Move ownership of the table to the base class.

This commit is contained in:
kruland2607 2013-10-01 15:47:28 -05:00
parent 60a7014ca3
commit dca6c31241
4 changed files with 28 additions and 55 deletions

View File

@ -32,12 +32,13 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
protected final FlightConfigurationPanel flightConfigurationPanel; protected final FlightConfigurationPanel flightConfigurationPanel;
protected final Rocket rocket; protected final Rocket rocket;
protected final JTable table;
public FlightConfigurablePanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) { public FlightConfigurablePanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(new MigLayout("fill")); super(new MigLayout("fill"));
this.flightConfigurationPanel = flightConfigurationPanel; this.flightConfigurationPanel = flightConfigurationPanel;
this.rocket = rocket; this.rocket = rocket;
initializeTable(); table = initializeTable();
rocket.getDefaultConfiguration().addChangeListener( new StateChangeListener() { rocket.getDefaultConfiguration().addChangeListener( new StateChangeListener() {
@Override @Override
public void stateChanged(EventObject e) { public void stateChanged(EventObject e) {
@ -56,17 +57,13 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
// Nothing to do // Nothing to do
} else if ( id == null ) { } else if ( id == null ) {
// need to unselect // need to unselect
getTable().clearSelection(); table.clearSelection();
} else if ( !id.equals(selectedId)){ } else if ( !id.equals(selectedId)){
// Need to change selection // Need to change selection
// We'll select the correct row, in the currently selected column. // We'll select the correct row, in the currently selected column.
JTable table = getTable();
int col = table.getSelectedColumn(); int col = table.getSelectedColumn();
if ( col < 0 ) { if ( col < 0 ) {
col = 0; col = (table.getColumnCount() > 1) ? 1 : 0;
} }
for( int row = 0; row < table.getRowCount(); row++ ) { for( int row = 0; row < table.getRowCount(); row++ ) {
String rowId = rocket.getFlightConfigurationIDs()[row + 1]; String rowId = rocket.getFlightConfigurationIDs()[row + 1];
@ -78,7 +75,7 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
} }
private final void installTableListener() { private final void installTableListener() {
getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() { table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override @Override
public void valueChanged(ListSelectionEvent e) { public void valueChanged(ListSelectionEvent e) {
@ -90,7 +87,7 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
ListSelectionModel model = (ListSelectionModel) e.getSource(); ListSelectionModel model = (ListSelectionModel) e.getSource();
for( int row = firstrow; row <= lastrow; row ++) { for( int row = firstrow; row <= lastrow; row ++) {
if ( model.isSelectedIndex(row) ) { if ( model.isSelectedIndex(row) ) {
String id = (String) getTable().getValueAt(row, 0); String id = (String) table.getValueAt(row, 0);
rocket.getDefaultConfiguration().setFlightConfigurationID(id); rocket.getDefaultConfiguration().setFlightConfigurationID(id);
return; return;
} }
@ -107,20 +104,14 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
*/ */
protected abstract JTable initializeTable(); protected abstract JTable initializeTable();
/**
* Return the embedded JTable
* @return
*/
protected abstract JTable getTable();
protected T getSelectedComponent() { protected T getSelectedComponent() {
int col = getTable().getSelectedColumn(); int col = table.getSelectedColumn();
int row = getTable().getSelectedRow(); int row = table.getSelectedRow();
if ( row < 0 || col < 0 ) { if ( row < 0 || col < 0 ) {
return null; return null;
} }
Object tableValue = getTable().getModel().getValueAt(row, col); Object tableValue = table.getModel().getValueAt(row, col);
if ( tableValue instanceof Pair ) { if ( tableValue instanceof Pair ) {
Pair<String,T> selectedComponent = (Pair<String,T>) tableValue; Pair<String,T> selectedComponent = (Pair<String,T>) tableValue;
return selectedComponent.getV(); return selectedComponent.getV();
@ -129,12 +120,12 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
} }
protected String getSelectedConfigurationId() { protected String getSelectedConfigurationId() {
int col = getTable().getSelectedColumn(); int col = table.getSelectedColumn();
int row = getTable().getSelectedRow(); int row = table.getSelectedRow();
if ( row < 0 || col < 0 ) { if ( row < 0 || col < 0 ) {
return null; return null;
} }
Object tableValue = getTable().getModel().getValueAt(row, col); Object tableValue = table.getModel().getValueAt(row, col);
if ( tableValue instanceof Pair ) { if ( tableValue instanceof Pair ) {
Pair<String,T> selectedComponent = (Pair<String,T>) tableValue; Pair<String,T> selectedComponent = (Pair<String,T>) tableValue;
return selectedComponent.getU(); return selectedComponent.getU();

View File

@ -33,13 +33,12 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton; private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
protected JTable configurationTable;
protected FlightConfigurableTableModel<MotorMount> configurationTableModel; protected FlightConfigurableTableModel<MotorMount> configurationTableModel;
MotorConfigurationPanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) { MotorConfigurationPanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(flightConfigurationPanel,rocket); super(flightConfigurationPanel,rocket);
JScrollPane scroll = new JScrollPane(configurationTable); JScrollPane scroll = new JScrollPane(table);
this.add(scroll, "grow, wrap"); this.add(scroll, "grow, wrap");
//// Select motor //// Select motor
@ -97,7 +96,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
} }
}; };
configurationTable = new JTable(configurationTableModel); JTable configurationTable = new JTable(configurationTableModel);
configurationTable.setCellSelectionEnabled(true); configurationTable.setCellSelectionEnabled(true);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setDefaultRenderer(Object.class, new MotorTableCellRenderer()); configurationTable.setDefaultRenderer(Object.class, new MotorTableCellRenderer());
@ -106,7 +105,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
updateButtonState(); updateButtonState();
int selectedColumn = configurationTable.getSelectedColumn(); int selectedColumn = table.getSelectedColumn();
if (e.getClickCount() == 2) { if (e.getClickCount() == 2) {
if (selectedColumn > 0) { if (selectedColumn > 0) {
selectMotor(); selectMotor();
@ -117,17 +116,12 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
return configurationTable; return configurationTable;
} }
@Override
protected JTable getTable() {
return configurationTable;
}
public void fireTableDataChanged() { public void fireTableDataChanged() {
int selected = configurationTable.getSelectedRow(); int selected = table.getSelectedRow();
configurationTableModel.fireTableDataChanged(); configurationTableModel.fireTableDataChanged();
if (selected >= 0) { if (selected >= 0) {
selected = Math.min(selected, configurationTable.getRowCount() - 1); selected = Math.min(selected, table.getRowCount() - 1);
configurationTable.getSelectionModel().setSelectionInterval(selected, selected); table.getSelectionModel().setSelectionInterval(selected, selected);
} }
updateButtonState(); updateButtonState();
} }

View File

@ -36,7 +36,6 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class); private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
private FlightConfigurableTableModel<RecoveryDevice> recoveryTableModel; private FlightConfigurableTableModel<RecoveryDevice> recoveryTableModel;
private JTable recoveryTable;
private final JButton selectDeploymentButton; private final JButton selectDeploymentButton;
private final JButton resetDeploymentButton; private final JButton resetDeploymentButton;
@ -44,7 +43,7 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
RecoveryConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) { RecoveryConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(flightConfigurationPanel,rocket); super(flightConfigurationPanel,rocket);
JScrollPane scroll = new JScrollPane(recoveryTable); JScrollPane scroll = new JScrollPane(table);
this.add(scroll, "span, grow, wrap"); this.add(scroll, "span, grow, wrap");
//// Select deployment //// Select deployment
@ -74,7 +73,7 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
protected JTable initializeTable() { protected JTable initializeTable() {
//// Recovery selection //// Recovery selection
recoveryTableModel = new FlightConfigurableTableModel<RecoveryDevice>(RecoveryDevice.class, rocket); recoveryTableModel = new FlightConfigurableTableModel<RecoveryDevice>(RecoveryDevice.class, rocket);
recoveryTable = new JTable(recoveryTableModel); JTable recoveryTable = new JTable(recoveryTableModel);
recoveryTable.setCellSelectionEnabled(true); recoveryTable.setCellSelectionEnabled(true);
recoveryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); recoveryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
recoveryTable.addMouseListener(new MouseAdapter() { recoveryTable.addMouseListener(new MouseAdapter() {
@ -93,17 +92,12 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
return recoveryTable; return recoveryTable;
} }
@Override
protected JTable getTable() {
return recoveryTable;
}
public void fireTableDataChanged() { public void fireTableDataChanged() {
int selected = recoveryTable.getSelectedRow(); int selected = table.getSelectedRow();
recoveryTableModel.fireTableDataChanged(); recoveryTableModel.fireTableDataChanged();
if (selected >= 0) { if (selected >= 0) {
selected = Math.min(selected, recoveryTable.getRowCount() - 1); selected = Math.min(selected, table.getRowCount() - 1);
recoveryTable.getSelectionModel().setSelectionInterval(selected, selected); table.getSelectionModel().setSelectionInterval(selected, selected);
} }
updateButtonState(); updateButtonState();
} }

View File

@ -34,7 +34,6 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
static final Translator trans = Application.getTranslator(); static final Translator trans = Application.getTranslator();
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class); private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
private JTable separationTable;
private FlightConfigurableTableModel<Stage> separationTableModel; private FlightConfigurableTableModel<Stage> separationTableModel;
private final JButton selectSeparationButton; private final JButton selectSeparationButton;
private final JButton resetDeploymentButton; private final JButton resetDeploymentButton;
@ -43,7 +42,7 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
SeparationConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) { SeparationConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(flightConfigurationPanel,rocket); super(flightConfigurationPanel,rocket);
JScrollPane scroll = new JScrollPane(separationTable); JScrollPane scroll = new JScrollPane(table);
this.add(scroll, "span, grow, wrap"); this.add(scroll, "span, grow, wrap");
//// Select deployment //// Select deployment
@ -74,7 +73,7 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
protected JTable initializeTable() { protected JTable initializeTable() {
//// Separation selection //// Separation selection
separationTableModel = new FlightConfigurableTableModel<Stage>(Stage.class, rocket); separationTableModel = new FlightConfigurableTableModel<Stage>(Stage.class, rocket);
separationTable = new JTable(separationTableModel); JTable separationTable = new JTable(separationTableModel);
separationTable.setCellSelectionEnabled(true); separationTable.setCellSelectionEnabled(true);
separationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); separationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
separationTable.addMouseListener(new MouseAdapter() { separationTable.addMouseListener(new MouseAdapter() {
@ -92,17 +91,12 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
return separationTable; return separationTable;
} }
@Override
protected JTable getTable() {
return separationTable;
}
public void fireTableDataChanged() { public void fireTableDataChanged() {
int selected = separationTable.getSelectedRow(); int selected = table.getSelectedRow();
separationTableModel.fireTableDataChanged(); separationTableModel.fireTableDataChanged();
if (selected >= 0) { if (selected >= 0) {
selected = Math.min(selected, separationTable.getRowCount() - 1); selected = Math.min(selected, table.getRowCount() - 1);
separationTable.getSelectionModel().setSelectionInterval(selected, selected); table.getSelectionModel().setSelectionInterval(selected, selected);
} }
updateButtonState(); updateButtonState();
} }