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.rocketcomponent.ComponentChangeEvent;
|
||||
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.RocketComponent;
|
||||
import net.sf.openrocket.startup.Application;
|
||||
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 final Rocket rocket;
|
||||
|
||||
private final List<RecoveryDevice> recoveryDevices = new ArrayList<RecoveryDevice>();
|
||||
|
||||
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>();
|
||||
|
||||
/**
|
||||
* @param recoveryConfigurationPanel
|
||||
*/
|
||||
RecoveryTableModel(Rocket rocket) {
|
||||
public FlightConfigurableTableModel(Class<T> clazz, Rocket rocket) {
|
||||
super();
|
||||
this.rocket = rocket;
|
||||
this.clazz = clazz;
|
||||
this.rocket.addComponentChangeListener(this);
|
||||
initialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentChanged(ComponentChangeEvent e) {
|
||||
if ( e.isTreeChange() ) {
|
||||
if ( e.isMotorChange() || e.isTreeChange() ) {
|
||||
initialize();
|
||||
fireTableStructureChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void initialize() {
|
||||
recoveryDevices.clear();
|
||||
protected void initialize() {
|
||||
components.clear();
|
||||
Iterator<RocketComponent> it = rocket.iterator();
|
||||
while (it.hasNext()) {
|
||||
RocketComponent c = it.next();
|
||||
if (c instanceof RecoveryDevice) {
|
||||
recoveryDevices.add( (RecoveryDevice) c);
|
||||
if (clazz.isAssignableFrom(c.getClass())) {
|
||||
components.add( (T) c);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -62,7 +58,7 @@ class RecoveryTableModel extends AbstractTableModel implements ComponentChangeLi
|
||||
|
||||
@Override
|
||||
public int getColumnCount() {
|
||||
return recoveryDevices.size() + 1;
|
||||
return components.size() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -74,8 +70,8 @@ class RecoveryTableModel extends AbstractTableModel implements ComponentChangeLi
|
||||
}
|
||||
default: {
|
||||
int index = column - 1;
|
||||
RecoveryDevice d = recoveryDevices.get(index);
|
||||
return new Pair<String, RecoveryDevice>(id, d);
|
||||
T d = components.get(index);
|
||||
return new Pair<String, T>(id, d);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -88,9 +84,9 @@ class RecoveryTableModel extends AbstractTableModel implements ComponentChangeLi
|
||||
}
|
||||
default: {
|
||||
int index = column - 1;
|
||||
RecoveryDevice d = recoveryDevices.get(index);
|
||||
T d = components.get(index);
|
||||
return d.toString();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -34,7 +34,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
|
||||
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
|
||||
|
||||
protected JTable configurationTable;
|
||||
protected MotorConfigurationTableModel configurationTableModel;
|
||||
protected FlightConfigurableTableModel<MotorMount> configurationTableModel;
|
||||
|
||||
MotorConfigurationPanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
|
||||
super(flightConfigurationPanel,rocket);
|
||||
@ -89,7 +89,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
|
||||
@Override
|
||||
protected JTable initializeTable() {
|
||||
//// Motor selection table.
|
||||
configurationTableModel = new MotorConfigurationTableModel(rocket);
|
||||
configurationTableModel = new FlightConfigurableTableModel<MotorMount>(MotorMount.class,rocket);
|
||||
configurationTable = new JTable(configurationTableModel);
|
||||
configurationTable.setCellSelectionEnabled(true);
|
||||
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();
|
||||
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
|
||||
|
||||
private RecoveryTableModel recoveryTableModel;
|
||||
private FlightConfigurableTableModel<RecoveryDevice> recoveryTableModel;
|
||||
private JTable recoveryTable;
|
||||
private final JButton selectDeploymentButton;
|
||||
private final JButton resetDeploymentButton;
|
||||
@ -72,7 +72,7 @@ public class RecoveryConfigurationPanel extends FlightConfigurablePanel<Recovery
|
||||
@Override
|
||||
protected JTable initializeTable() {
|
||||
//// Recovery selection
|
||||
recoveryTableModel = new RecoveryTableModel(rocket);
|
||||
recoveryTableModel = new FlightConfigurableTableModel<RecoveryDevice>(RecoveryDevice.class, rocket);
|
||||
recoveryTable = new JTable(recoveryTableModel);
|
||||
recoveryTable.setCellSelectionEnabled(true);
|
||||
recoveryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
|
@ -34,7 +34,7 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
|
||||
private RocketDescriptor descriptor = Application.getInjector().getInstance(RocketDescriptor.class);
|
||||
|
||||
private JTable separationTable;
|
||||
private SeparationTableModel separationTableModel;
|
||||
private FlightConfigurableTableModel<Stage> separationTableModel;
|
||||
private final JButton selectSeparationButton;
|
||||
private final JButton resetDeploymentButton;
|
||||
|
||||
@ -72,7 +72,7 @@ public class SeparationConfigurationPanel extends FlightConfigurablePanel<Stage>
|
||||
@Override
|
||||
protected JTable initializeTable() {
|
||||
//// Separation selection
|
||||
separationTableModel = new SeparationTableModel(rocket);
|
||||
separationTableModel = new FlightConfigurableTableModel<Stage>(Stage.class, rocket);
|
||||
separationTable = new JTable(separationTableModel);
|
||||
separationTable.setCellSelectionEnabled(true);
|
||||
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