Move ownership of the table to the base class.
This commit is contained in:
parent
60a7014ca3
commit
dca6c31241
@ -32,12 +32,13 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
|
||||
|
||||
protected final FlightConfigurationPanel flightConfigurationPanel;
|
||||
protected final Rocket rocket;
|
||||
protected final JTable table;
|
||||
|
||||
public FlightConfigurablePanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
|
||||
super(new MigLayout("fill"));
|
||||
this.flightConfigurationPanel = flightConfigurationPanel;
|
||||
this.rocket = rocket;
|
||||
initializeTable();
|
||||
table = initializeTable();
|
||||
rocket.getDefaultConfiguration().addChangeListener( new StateChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(EventObject e) {
|
||||
@ -56,17 +57,13 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
|
||||
// Nothing to do
|
||||
} else if ( id == null ) {
|
||||
// need to unselect
|
||||
getTable().clearSelection();
|
||||
table.clearSelection();
|
||||
} else if ( !id.equals(selectedId)){
|
||||
// Need to change selection
|
||||
|
||||
// We'll select the correct row, in the currently selected column.
|
||||
|
||||
JTable table = getTable();
|
||||
|
||||
int col = table.getSelectedColumn();
|
||||
if ( col < 0 ) {
|
||||
col = 0;
|
||||
col = (table.getColumnCount() > 1) ? 1 : 0;
|
||||
}
|
||||
for( int row = 0; row < table.getRowCount(); row++ ) {
|
||||
String rowId = rocket.getFlightConfigurationIDs()[row + 1];
|
||||
@ -78,7 +75,7 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
|
||||
}
|
||||
|
||||
private final void installTableListener() {
|
||||
getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
||||
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
||||
|
||||
@Override
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
@ -90,7 +87,7 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
|
||||
ListSelectionModel model = (ListSelectionModel) e.getSource();
|
||||
for( int row = firstrow; row <= lastrow; row ++) {
|
||||
if ( model.isSelectedIndex(row) ) {
|
||||
String id = (String) getTable().getValueAt(row, 0);
|
||||
String id = (String) table.getValueAt(row, 0);
|
||||
rocket.getDefaultConfiguration().setFlightConfigurationID(id);
|
||||
return;
|
||||
}
|
||||
@ -107,20 +104,14 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
|
||||
*/
|
||||
protected abstract JTable initializeTable();
|
||||
|
||||
/**
|
||||
* Return the embedded JTable
|
||||
* @return
|
||||
*/
|
||||
protected abstract JTable getTable();
|
||||
|
||||
protected T getSelectedComponent() {
|
||||
|
||||
int col = getTable().getSelectedColumn();
|
||||
int row = getTable().getSelectedRow();
|
||||
int col = table.getSelectedColumn();
|
||||
int row = table.getSelectedRow();
|
||||
if ( row < 0 || col < 0 ) {
|
||||
return null;
|
||||
}
|
||||
Object tableValue = getTable().getModel().getValueAt(row, col);
|
||||
Object tableValue = table.getModel().getValueAt(row, col);
|
||||
if ( tableValue instanceof Pair ) {
|
||||
Pair<String,T> selectedComponent = (Pair<String,T>) tableValue;
|
||||
return selectedComponent.getV();
|
||||
@ -129,12 +120,12 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
|
||||
}
|
||||
|
||||
protected String getSelectedConfigurationId() {
|
||||
int col = getTable().getSelectedColumn();
|
||||
int row = getTable().getSelectedRow();
|
||||
int col = table.getSelectedColumn();
|
||||
int row = table.getSelectedRow();
|
||||
if ( row < 0 || col < 0 ) {
|
||||
return null;
|
||||
}
|
||||
Object tableValue = getTable().getModel().getValueAt(row, col);
|
||||
Object tableValue = table.getModel().getValueAt(row, col);
|
||||
if ( tableValue instanceof Pair ) {
|
||||
Pair<String,T> selectedComponent = (Pair<String,T>) tableValue;
|
||||
return selectedComponent.getU();
|
||||
|
@ -33,13 +33,12 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
|
||||
|
||||
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
|
||||
|
||||
protected JTable configurationTable;
|
||||
protected FlightConfigurableTableModel<MotorMount> configurationTableModel;
|
||||
|
||||
MotorConfigurationPanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
|
||||
super(flightConfigurationPanel,rocket);
|
||||
|
||||
JScrollPane scroll = new JScrollPane(configurationTable);
|
||||
JScrollPane scroll = new JScrollPane(table);
|
||||
this.add(scroll, "grow, wrap");
|
||||
|
||||
//// 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.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
configurationTable.setDefaultRenderer(Object.class, new MotorTableCellRenderer());
|
||||
@ -106,7 +105,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
updateButtonState();
|
||||
int selectedColumn = configurationTable.getSelectedColumn();
|
||||
int selectedColumn = table.getSelectedColumn();
|
||||
if (e.getClickCount() == 2) {
|
||||
if (selectedColumn > 0) {
|
||||
selectMotor();
|
||||
@ -117,17 +116,12 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
|
||||
return configurationTable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JTable getTable() {
|
||||
return configurationTable;
|
||||
}
|
||||
|
||||
public void fireTableDataChanged() {
|
||||
int selected = configurationTable.getSelectedRow();
|
||||
int selected = table.getSelectedRow();
|
||||
configurationTableModel.fireTableDataChanged();
|
||||
if (selected >= 0) {
|
||||
selected = Math.min(selected, configurationTable.getRowCount() - 1);
|
||||
configurationTable.getSelectionModel().setSelectionInterval(selected, selected);
|
||||
selected = Math.min(selected, table.getRowCount() - 1);
|
||||
table.getSelectionModel().setSelectionInterval(selected, selected);
|
||||
}
|
||||
updateButtonState();
|
||||
}
|
||||
|
@ -36,7 +36,6 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
|
||||
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
|
||||
|
||||
private FlightConfigurableTableModel<RecoveryDevice> recoveryTableModel;
|
||||
private JTable recoveryTable;
|
||||
private final JButton selectDeploymentButton;
|
||||
private final JButton resetDeploymentButton;
|
||||
|
||||
@ -44,7 +43,7 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
|
||||
RecoveryConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
|
||||
super(flightConfigurationPanel,rocket);
|
||||
|
||||
JScrollPane scroll = new JScrollPane(recoveryTable);
|
||||
JScrollPane scroll = new JScrollPane(table);
|
||||
this.add(scroll, "span, grow, wrap");
|
||||
|
||||
//// Select deployment
|
||||
@ -74,7 +73,7 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
|
||||
protected JTable initializeTable() {
|
||||
//// Recovery selection
|
||||
recoveryTableModel = new FlightConfigurableTableModel<RecoveryDevice>(RecoveryDevice.class, rocket);
|
||||
recoveryTable = new JTable(recoveryTableModel);
|
||||
JTable recoveryTable = new JTable(recoveryTableModel);
|
||||
recoveryTable.setCellSelectionEnabled(true);
|
||||
recoveryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
recoveryTable.addMouseListener(new MouseAdapter() {
|
||||
@ -93,17 +92,12 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
|
||||
return recoveryTable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JTable getTable() {
|
||||
return recoveryTable;
|
||||
}
|
||||
|
||||
public void fireTableDataChanged() {
|
||||
int selected = recoveryTable.getSelectedRow();
|
||||
int selected = table.getSelectedRow();
|
||||
recoveryTableModel.fireTableDataChanged();
|
||||
if (selected >= 0) {
|
||||
selected = Math.min(selected, recoveryTable.getRowCount() - 1);
|
||||
recoveryTable.getSelectionModel().setSelectionInterval(selected, selected);
|
||||
selected = Math.min(selected, table.getRowCount() - 1);
|
||||
table.getSelectionModel().setSelectionInterval(selected, selected);
|
||||
}
|
||||
updateButtonState();
|
||||
}
|
||||
|
@ -34,7 +34,6 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
|
||||
static final Translator trans = Application.getTranslator();
|
||||
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
|
||||
|
||||
private JTable separationTable;
|
||||
private FlightConfigurableTableModel<Stage> separationTableModel;
|
||||
private final JButton selectSeparationButton;
|
||||
private final JButton resetDeploymentButton;
|
||||
@ -43,7 +42,7 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
|
||||
SeparationConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
|
||||
super(flightConfigurationPanel,rocket);
|
||||
|
||||
JScrollPane scroll = new JScrollPane(separationTable);
|
||||
JScrollPane scroll = new JScrollPane(table);
|
||||
this.add(scroll, "span, grow, wrap");
|
||||
|
||||
//// Select deployment
|
||||
@ -74,7 +73,7 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
|
||||
protected JTable initializeTable() {
|
||||
//// Separation selection
|
||||
separationTableModel = new FlightConfigurableTableModel<Stage>(Stage.class, rocket);
|
||||
separationTable = new JTable(separationTableModel);
|
||||
JTable separationTable = new JTable(separationTableModel);
|
||||
separationTable.setCellSelectionEnabled(true);
|
||||
separationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
separationTable.addMouseListener(new MouseAdapter() {
|
||||
@ -92,17 +91,12 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
|
||||
return separationTable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JTable getTable() {
|
||||
return separationTable;
|
||||
}
|
||||
|
||||
public void fireTableDataChanged() {
|
||||
int selected = separationTable.getSelectedRow();
|
||||
int selected = table.getSelectedRow();
|
||||
separationTableModel.fireTableDataChanged();
|
||||
if (selected >= 0) {
|
||||
selected = Math.min(selected, separationTable.getRowCount() - 1);
|
||||
separationTable.getSelectionModel().setSelectionInterval(selected, selected);
|
||||
selected = Math.min(selected, table.getRowCount() - 1);
|
||||
table.getSelectionModel().setSelectionInterval(selected, selected);
|
||||
}
|
||||
updateButtonState();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user