Improved common base of configuration panels.

This commit is contained in:
kruland2607 2013-10-01 10:20:44 -05:00
parent 9e01ff4e76
commit e36a90adfc
5 changed files with 146 additions and 133 deletions

View File

@ -4,6 +4,9 @@ import java.util.EventObject;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.formatting.RocketDescriptor;
@ -26,45 +29,84 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
super(new MigLayout("fill"));
this.flightConfigurationPanel = flightConfigurationPanel;
this.rocket = rocket;
initializeTable();
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);
}
FlightConfigurablePanel.this.synchronizeConfigurationSelection();
}
});
installTableListener();
synchronizeConfigurationSelection();
}
protected final void synchronizeConfigurationSelection() {
String id = rocket.getDefaultConfiguration().getFlightConfigurationID();
String selectedId = getSelectedConfigurationId();
if ( id == null && selectedId == null ) {
// Nothing to do
} else if ( id == null ) {
// need to unselect
getTable().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;
}
for( int row = 0; row < table.getRowCount(); row++ ) {
String rowId = rocket.getFlightConfigurationIDs()[row + 1];
if ( rowId.equals(id) ) {
table.changeSelection(row, col, true, false);
}
}
}
}
private final void installTableListener() {
getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if ( e.getValueIsAdjusting() ) {
return;
}
int firstrow = e.getFirstIndex();
int lastrow = e.getLastIndex();
ListSelectionModel model = (ListSelectionModel) e.getSource();
for( int row = firstrow; row <= lastrow; row ++) {
if ( model.isSelectedIndex(row) ) {
String id = (String) getTable().getValueAt(row, 0);
rocket.getDefaultConfiguration().setFlightConfigurationID(id);
return;
}
}
}
});
}
protected abstract JTable getTable();
/**
* Override this method to create the embedded JTable and it's backing Model.
*
* @return
*/
protected abstract JTable initializeTable();
/**
* Return the embedded JTable
* @return
*/
protected abstract JTable getTable();
protected T getSelectedComponent() {
int col = getTable().getSelectedColumn();
int row = getTable().getSelectedRow();
if ( row < 0 || col < 0 ) {
@ -77,7 +119,7 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
}
return null;
}
protected String getSelectedConfigurationId() {
int col = getTable().getSelectedColumn();
int row = getTable().getSelectedRow();

View File

@ -185,9 +185,4 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
}
public void setCurrentConfiguration(String id) {
rocket.getDefaultConfiguration().setFlightConfigurationID(id);
updateButtonState();
}
}

View File

@ -1,8 +1,6 @@
package net.sf.openrocket.gui.main.flightconfigpanel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
@ -14,13 +12,10 @@ import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import net.sf.openrocket.gui.dialogs.flightconfiguration.IgnitionSelectionDialog;
import net.sf.openrocket.gui.dialogs.motor.MotorChooserDialog;
import net.sf.openrocket.gui.util.GUIUtil;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.rocketcomponent.IgnitionConfiguration;
import net.sf.openrocket.rocketcomponent.MotorConfiguration;
@ -38,53 +33,12 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
protected final JTable configurationTable;
protected final MotorConfigurationTableModel configurationTableModel;
protected JTable configurationTable;
protected MotorConfigurationTableModel configurationTableModel;
MotorConfigurationPanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(flightConfigurationPanel,rocket);
//// Motor selection table.
configurationTableModel = new MotorConfigurationTableModel(rocket);
configurationTable = new JTable(configurationTableModel);
configurationTable.setCellSelectionEnabled(true);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setDefaultRenderer(Object.class, new MotorTableCellRenderer());
configurationTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
updateButtonState();
int selectedColumn = configurationTable.getSelectedColumn();
if (e.getClickCount() == 2) {
if (selectedColumn > 0) {
selectMotor();
}
}
}
});
configurationTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if ( e.getValueIsAdjusting() ) {
return;
}
int firstrow = e.getFirstIndex();
int lastrow = e.getLastIndex();
ListSelectionModel model = (ListSelectionModel) e.getSource();
for( int row = firstrow; row <= lastrow; row ++) {
if ( model.isSelectedIndex(row) ) {
String id = (String) configurationTableModel.getValueAt(row, 0);
flightConfigurationPanel.setCurrentConfiguration(id);
return;
}
}
}
});
JScrollPane scroll = new JScrollPane(configurationTable);
this.add(scroll, "grow, wrap");
@ -132,6 +86,30 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
}
@Override
protected JTable initializeTable() {
//// Motor selection table.
configurationTableModel = new MotorConfigurationTableModel(rocket);
configurationTable = new JTable(configurationTableModel);
configurationTable.setCellSelectionEnabled(true);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setDefaultRenderer(Object.class, new MotorTableCellRenderer());
configurationTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
updateButtonState();
int selectedColumn = configurationTable.getSelectedColumn();
if (e.getClickCount() == 2) {
if (selectedColumn > 0) {
selectMotor();
}
}
}
});
return configurationTable;
}
@Override
protected JTable getTable() {
return configurationTable;

View File

@ -17,7 +17,6 @@ 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.DeploymentSelectionDialog;
import net.sf.openrocket.gui.util.GUIUtil;
@ -35,37 +34,14 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
Translator trans = Application.getTranslator();
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
private final FlightConfigurationPanel flightConfigurationPanel;
final Rocket rocket;
private final RecoveryTableModel recoveryTableModel;
private final JTable recoveryTable;
private RecoveryTableModel recoveryTableModel;
private JTable recoveryTable;
private final JButton selectDeploymentButton;
private final JButton resetDeploymentButton;
RecoveryConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(flightConfigurationPanel,rocket);
this.flightConfigurationPanel = flightConfigurationPanel;
this.rocket = rocket;
//// Recovery selection
recoveryTableModel = new RecoveryTableModel(rocket);
recoveryTable = new JTable(recoveryTableModel);
recoveryTable.setCellSelectionEnabled(true);
recoveryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
recoveryTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
updateButtonState();
if (e.getClickCount() == 2) {
// Double-click edits
selectDeployment();
}
}
});
recoveryTable.setDefaultRenderer(Object.class, new RecoveryTableCellRenderer());
JScrollPane scroll = new JScrollPane(recoveryTable);
this.add(scroll, "span, grow, wrap");
@ -93,6 +69,29 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
this.add(resetDeploymentButton, "sizegroup button, wrap");
}
@Override
protected JTable initializeTable() {
//// Recovery selection
recoveryTableModel = new RecoveryTableModel(rocket);
recoveryTable = new JTable(recoveryTableModel);
recoveryTable.setCellSelectionEnabled(true);
recoveryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
recoveryTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
updateButtonState();
if (e.getClickCount() == 2) {
// Double-click edits
selectDeployment();
}
}
});
recoveryTable.setDefaultRenderer(Object.class, new RecoveryTableCellRenderer());
return recoveryTable;
}
@Override
protected JTable getTable() {
return recoveryTable;

View File

@ -33,37 +33,14 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
static final Translator trans = Application.getTranslator();
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
private final FlightConfigurationPanel flightConfigurationPanel;
final Rocket rocket;
private final JTable separationTable;
private final SeparationTableModel separationTableModel;
private JTable separationTable;
private SeparationTableModel separationTableModel;
private final JButton selectSeparationButton;
private final JButton resetDeploymentButton;
SeparationConfigurationPanel(FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(flightConfigurationPanel,rocket);
this.flightConfigurationPanel = flightConfigurationPanel;
this.rocket = rocket;
//// Recovery selection
separationTableModel = new SeparationTableModel(rocket);
separationTable = new JTable(separationTableModel);
separationTable.setCellSelectionEnabled(true);
separationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
separationTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
updateButtonState();
if (e.getClickCount() == 2) {
// Double-click edits
selectDeployment();
}
}
});
separationTable.setDefaultRenderer(Object.class, new SeparationTableCellRenderer());
JScrollPane scroll = new JScrollPane(separationTable);
this.add(scroll, "span, grow, wrap");
@ -92,6 +69,28 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
}
@Override
protected JTable initializeTable() {
//// Separation selection
separationTableModel = new SeparationTableModel(rocket);
separationTable = new JTable(separationTableModel);
separationTable.setCellSelectionEnabled(true);
separationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
separationTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
updateButtonState();
if (e.getClickCount() == 2) {
// Double-click edits
selectDeployment();
}
}
});
separationTable.setDefaultRenderer(Object.class, new SeparationTableCellRenderer());
return separationTable;
}
@Override
protected JTable getTable() {
return separationTable;