Refactored the table models into a common base class FlightConfigurableTableModel.
This commit is contained in:
parent
fa7916f7b0
commit
c8f71fcc94
@ -9,48 +9,44 @@ import javax.swing.table.AbstractTableModel;
|
|||||||
import net.sf.openrocket.l10n.Translator;
|
import net.sf.openrocket.l10n.Translator;
|
||||||
import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
|
import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
|
||||||
import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
|
import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
|
||||||
import net.sf.openrocket.rocketcomponent.RecoveryDevice;
|
import net.sf.openrocket.rocketcomponent.FlightConfigurableComponent;
|
||||||
import net.sf.openrocket.rocketcomponent.Rocket;
|
import net.sf.openrocket.rocketcomponent.Rocket;
|
||||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
||||||
import net.sf.openrocket.startup.Application;
|
import net.sf.openrocket.startup.Application;
|
||||||
import net.sf.openrocket.util.Pair;
|
import net.sf.openrocket.util.Pair;
|
||||||
|
|
||||||
class RecoveryTableModel extends AbstractTableModel implements ComponentChangeListener {
|
public class FlightConfigurableTableModel<T extends FlightConfigurableComponent> extends AbstractTableModel implements ComponentChangeListener{
|
||||||
|
|
||||||
private static final Translator trans = Application.getTranslator();
|
private static final Translator trans = Application.getTranslator();
|
||||||
|
|
||||||
private final Rocket rocket;
|
|
||||||
|
|
||||||
private final List<RecoveryDevice> recoveryDevices = new ArrayList<RecoveryDevice>();
|
|
||||||
|
|
||||||
private static final String CONFIGURATION = trans.get("edtmotorconfdlg.col.configuration");
|
private static final String CONFIGURATION = trans.get("edtmotorconfdlg.col.configuration");
|
||||||
|
|
||||||
|
protected final Rocket rocket;
|
||||||
|
protected final Class<T> clazz;
|
||||||
|
private final List<T> components = new ArrayList<T>();
|
||||||
|
|
||||||
/**
|
public FlightConfigurableTableModel(Class<T> clazz, Rocket rocket) {
|
||||||
* @param recoveryConfigurationPanel
|
super();
|
||||||
*/
|
|
||||||
RecoveryTableModel(Rocket rocket) {
|
|
||||||
this.rocket = rocket;
|
this.rocket = rocket;
|
||||||
|
this.clazz = clazz;
|
||||||
this.rocket.addComponentChangeListener(this);
|
this.rocket.addComponentChangeListener(this);
|
||||||
initialize();
|
initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void componentChanged(ComponentChangeEvent e) {
|
public void componentChanged(ComponentChangeEvent e) {
|
||||||
if ( e.isTreeChange() ) {
|
if ( e.isMotorChange() || e.isTreeChange() ) {
|
||||||
initialize();
|
initialize();
|
||||||
fireTableStructureChanged();
|
fireTableStructureChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void initialize() {
|
||||||
private void initialize() {
|
components.clear();
|
||||||
recoveryDevices.clear();
|
|
||||||
Iterator<RocketComponent> it = rocket.iterator();
|
Iterator<RocketComponent> it = rocket.iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
RocketComponent c = it.next();
|
RocketComponent c = it.next();
|
||||||
if (c instanceof RecoveryDevice) {
|
if (clazz.isAssignableFrom(c.getClass())) {
|
||||||
recoveryDevices.add( (RecoveryDevice) c);
|
components.add( (T) c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,7 +58,7 @@ class RecoveryTableModel extends AbstractTableModel implements ComponentChangeLi
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getColumnCount() {
|
public int getColumnCount() {
|
||||||
return recoveryDevices.size() + 1;
|
return components.size() + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -74,8 +70,8 @@ class RecoveryTableModel extends AbstractTableModel implements ComponentChangeLi
|
|||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
int index = column - 1;
|
int index = column - 1;
|
||||||
RecoveryDevice d = recoveryDevices.get(index);
|
T d = components.get(index);
|
||||||
return new Pair<String, RecoveryDevice>(id, d);
|
return new Pair<String, T>(id, d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,7 +84,7 @@ class RecoveryTableModel extends AbstractTableModel implements ComponentChangeLi
|
|||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
int index = column - 1;
|
int index = column - 1;
|
||||||
RecoveryDevice d = recoveryDevices.get(index);
|
T d = components.get(index);
|
||||||
return d.toString();
|
return d.toString();
|
||||||
|
|
||||||
}
|
}
|
@ -34,7 +34,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
|
|||||||
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
|
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
|
||||||
|
|
||||||
protected JTable configurationTable;
|
protected JTable configurationTable;
|
||||||
protected MotorConfigurationTableModel configurationTableModel;
|
protected FlightConfigurableTableModel<MotorMount> configurationTableModel;
|
||||||
|
|
||||||
MotorConfigurationPanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
|
MotorConfigurationPanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
|
||||||
super(flightConfigurationPanel,rocket);
|
super(flightConfigurationPanel,rocket);
|
||||||
@ -89,7 +89,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
|
|||||||
@Override
|
@Override
|
||||||
protected JTable initializeTable() {
|
protected JTable initializeTable() {
|
||||||
//// Motor selection table.
|
//// Motor selection table.
|
||||||
configurationTableModel = new MotorConfigurationTableModel(rocket);
|
configurationTableModel = new FlightConfigurableTableModel<MotorMount>(MotorMount.class,rocket);
|
||||||
configurationTable = new JTable(configurationTableModel);
|
configurationTable = new JTable(configurationTableModel);
|
||||||
configurationTable.setCellSelectionEnabled(true);
|
configurationTable.setCellSelectionEnabled(true);
|
||||||
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||||
|
@ -1,102 +0,0 @@
|
|||||||
package net.sf.openrocket.gui.main.flightconfigpanel;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.swing.table.AbstractTableModel;
|
|
||||||
|
|
||||||
import net.sf.openrocket.l10n.Translator;
|
|
||||||
import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
|
|
||||||
import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
|
|
||||||
import net.sf.openrocket.rocketcomponent.MotorMount;
|
|
||||||
import net.sf.openrocket.rocketcomponent.Rocket;
|
|
||||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
|
||||||
import net.sf.openrocket.startup.Application;
|
|
||||||
import net.sf.openrocket.util.Pair;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The table model for selecting and editing the motor configurations.
|
|
||||||
*/
|
|
||||||
class MotorConfigurationTableModel extends AbstractTableModel implements ComponentChangeListener {
|
|
||||||
|
|
||||||
private static final Translator trans = Application.getTranslator();
|
|
||||||
|
|
||||||
private static final String CONFIGURATION = trans.get("edtmotorconfdlg.col.configuration");
|
|
||||||
|
|
||||||
private final Rocket rocket;
|
|
||||||
|
|
||||||
private List<MotorMount> motorMounts = new ArrayList<MotorMount>();
|
|
||||||
|
|
||||||
|
|
||||||
public MotorConfigurationTableModel(Rocket rocket) {
|
|
||||||
this.rocket = rocket;
|
|
||||||
this.rocket.addComponentChangeListener(this);
|
|
||||||
initializeMotorMounts();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void componentChanged(ComponentChangeEvent e) {
|
|
||||||
if ( e.isMotorChange() || e.isTreeChange() ) {
|
|
||||||
initializeMotorMounts();
|
|
||||||
fireTableStructureChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initializeMotorMounts() {
|
|
||||||
motorMounts.clear();
|
|
||||||
for (RocketComponent c : rocket) {
|
|
||||||
if (c instanceof MotorMount) {
|
|
||||||
if (((MotorMount) c).isMotorMount()) {
|
|
||||||
motorMounts.add((MotorMount) c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getColumnCount() {
|
|
||||||
return motorMounts.size() + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getRowCount() {
|
|
||||||
return rocket.getFlightConfigurationIDs().length - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getValueAt(int row, int column) {
|
|
||||||
String id = getConfiguration(row);
|
|
||||||
switch (column) {
|
|
||||||
case 0: {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
int mountIndex = column - 1;
|
|
||||||
MotorMount mount = motorMounts.get(mountIndex);
|
|
||||||
return new Pair<String, MotorMount>(id, mount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getColumnName(int column) {
|
|
||||||
switch (column) {
|
|
||||||
case 0: {
|
|
||||||
return CONFIGURATION;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
int mountIndex = column - 1;
|
|
||||||
MotorMount mount = motorMounts.get(mountIndex);
|
|
||||||
return mount.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getConfiguration(int row) {
|
|
||||||
String id = rocket.getFlightConfigurationIDs()[row + 1];
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -34,7 +34,7 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
|
|||||||
Translator trans = Application.getTranslator();
|
Translator trans = Application.getTranslator();
|
||||||
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
|
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
|
||||||
|
|
||||||
private RecoveryTableModel recoveryTableModel;
|
private FlightConfigurableTableModel<RecoveryDevice> recoveryTableModel;
|
||||||
private JTable recoveryTable;
|
private JTable recoveryTable;
|
||||||
private final JButton selectDeploymentButton;
|
private final JButton selectDeploymentButton;
|
||||||
private final JButton resetDeploymentButton;
|
private final JButton resetDeploymentButton;
|
||||||
@ -72,7 +72,7 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
|
|||||||
@Override
|
@Override
|
||||||
protected JTable initializeTable() {
|
protected JTable initializeTable() {
|
||||||
//// Recovery selection
|
//// Recovery selection
|
||||||
recoveryTableModel = new RecoveryTableModel(rocket);
|
recoveryTableModel = new FlightConfigurableTableModel<RecoveryDevice>(RecoveryDevice.class, rocket);
|
||||||
recoveryTable = new JTable(recoveryTableModel);
|
recoveryTable = new JTable(recoveryTableModel);
|
||||||
recoveryTable.setCellSelectionEnabled(true);
|
recoveryTable.setCellSelectionEnabled(true);
|
||||||
recoveryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
recoveryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||||
|
@ -34,7 +34,7 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
|
|||||||
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
|
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
|
||||||
|
|
||||||
private JTable separationTable;
|
private JTable separationTable;
|
||||||
private SeparationTableModel separationTableModel;
|
private FlightConfigurableTableModel<Stage> separationTableModel;
|
||||||
private final JButton selectSeparationButton;
|
private final JButton selectSeparationButton;
|
||||||
private final JButton resetDeploymentButton;
|
private final JButton resetDeploymentButton;
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
|
|||||||
@Override
|
@Override
|
||||||
protected JTable initializeTable() {
|
protected JTable initializeTable() {
|
||||||
//// Separation selection
|
//// Separation selection
|
||||||
separationTableModel = new SeparationTableModel(rocket);
|
separationTableModel = new FlightConfigurableTableModel<Stage>(Stage.class, rocket);
|
||||||
separationTable = new JTable(separationTableModel);
|
separationTable = new JTable(separationTableModel);
|
||||||
separationTable.setCellSelectionEnabled(true);
|
separationTable.setCellSelectionEnabled(true);
|
||||||
separationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
separationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||||
|
@ -1,104 +0,0 @@
|
|||||||
package net.sf.openrocket.gui.main.flightconfigpanel;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.swing.table.AbstractTableModel;
|
|
||||||
|
|
||||||
import net.sf.openrocket.l10n.Translator;
|
|
||||||
import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
|
|
||||||
import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
|
|
||||||
import net.sf.openrocket.rocketcomponent.Rocket;
|
|
||||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
|
||||||
import net.sf.openrocket.rocketcomponent.Stage;
|
|
||||||
import net.sf.openrocket.startup.Application;
|
|
||||||
import net.sf.openrocket.util.Pair;
|
|
||||||
|
|
||||||
class SeparationTableModel extends AbstractTableModel implements ComponentChangeListener {
|
|
||||||
|
|
||||||
private static final Translator trans = Application.getTranslator();
|
|
||||||
|
|
||||||
private final Rocket rocket;
|
|
||||||
|
|
||||||
private final List<Stage> stages = new ArrayList<Stage>();
|
|
||||||
|
|
||||||
private static final String CONFIGURATION = trans.get("edtmotorconfdlg.col.configuration");
|
|
||||||
|
|
||||||
SeparationTableModel(Rocket rocket ) {
|
|
||||||
this.rocket = rocket;
|
|
||||||
this.rocket.addComponentChangeListener(this);
|
|
||||||
initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void componentChanged(ComponentChangeEvent e) {
|
|
||||||
if ( e.isTreeChange() ) {
|
|
||||||
initialize();
|
|
||||||
fireTableStructureChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initialize() {
|
|
||||||
stages.clear();
|
|
||||||
Iterator<RocketComponent> it = rocket.iterator();
|
|
||||||
{
|
|
||||||
int stageIndex = -1;
|
|
||||||
while (it.hasNext()) {
|
|
||||||
RocketComponent c = it.next();
|
|
||||||
if (c instanceof Stage) {
|
|
||||||
if (stageIndex >= 0) {
|
|
||||||
stages.add( (Stage) c);
|
|
||||||
}
|
|
||||||
stageIndex++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getRowCount() {
|
|
||||||
return rocket.getFlightConfigurationIDs().length - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getColumnCount() {
|
|
||||||
return stages.size() + 1;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Object getValueAt(int row, int column) {
|
|
||||||
String id = getConfiguration(row);
|
|
||||||
switch (column) {
|
|
||||||
case 0: {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
int index = column - 1;
|
|
||||||
Stage d = stages.get(index);
|
|
||||||
return new Pair<String, Stage>(id, d);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getColumnName(int column) {
|
|
||||||
switch (column) {
|
|
||||||
case 0: {
|
|
||||||
return CONFIGURATION;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
int index = column - 1;
|
|
||||||
Stage d = stages.get(index);
|
|
||||||
return d.toString();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getConfiguration(int row) {
|
|
||||||
String id = rocket.getFlightConfigurationIDs()[row + 1];
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user