Motor configuration dialog enhancements

This commit is contained in:
Sampo Niskanen 2013-02-10 13:06:59 +02:00
parent d2035ce702
commit 5161a0e967
3 changed files with 84 additions and 90 deletions

View File

@ -4,8 +4,6 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
@ -36,10 +34,10 @@ public class MotorConfigurationPanel extends JPanel {
private final FlightConfigurationDialog flightConfigurationDialog;
private final Rocket rocket;
private final JTable configurationTable;
private final MotorConfigurationTableModel configurationTableModel;
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
final MotorMount[] mounts;
MotorConfigurationPanel(FlightConfigurationDialog flightConfigurationDialog, Rocket rocket) {
super(new MigLayout("fill"));
@ -49,7 +47,6 @@ public class MotorConfigurationPanel extends JPanel {
DescriptionArea desc = new DescriptionArea(trans.get("description"), 3, -1);
this.add(desc, "spanx, growx, wrap para");
mounts = getPotentialMotorMounts();
//// Motor mount selection
JLabel label = new StyledLabel(trans.get("lbl.motorMounts"), Style.BOLD);
@ -61,7 +58,7 @@ public class MotorConfigurationPanel extends JPanel {
//// Motor Mount selection
JTable table = new JTable(new MotorMountTableModel(this));
JTable table = new JTable(new MotorMountTableModel(this, rocket));
table.setTableHeader(null);
table.setShowVerticalLines(false);
table.setRowSelectionAllowed(false);
@ -81,27 +78,18 @@ public class MotorConfigurationPanel extends JPanel {
//// Motor selection table.
configurationTableModel = new MotorConfigurationTableModel(rocket);
final JTable configurationTable = new JTable(configurationTableModel);
configurationTable = new JTable(configurationTableModel);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setRowSelectionAllowed(true);
configurationTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int row = configurationTable.getSelectedRow();
if (e.getClickCount() == 1) {
// Single click updates selection
updateButtonState();
} else if (e.getClickCount() == 2) {
updateButtonState();
if (e.getClickCount() == 2) {
// Double-click edits motor
selectMotor();
}
}
});
@ -158,6 +146,7 @@ public class MotorConfigurationPanel extends JPanel {
public void updateButtonState() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
selectMotorButton.setEnabled(currentMount != null && currentID != null);
removeMotorButton.setEnabled(currentMount != null && currentID != null);
selectIgnitionButton.setEnabled(currentMount != null && currentID != null);
@ -165,8 +154,31 @@ public class MotorConfigurationPanel extends JPanel {
}
private MotorMount getCurrentMount() {
int row = configurationTable.getSelectedRow();
if (row < 0) {
return null;
}
int count = 0;
for (RocketComponent c : rocket) {
if (c instanceof MotorMount) {
MotorMount mount = (MotorMount) c;
if (mount.isMotorMount()) {
count++;
}
if (count > row) {
return mount;
}
}
}
throw new IndexOutOfBoundsException("Invalid row, row=" + row + " count=" + count);
}
private void selectMotor() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null)
return;
@ -191,6 +203,7 @@ public class MotorConfigurationPanel extends JPanel {
private void removeMotor() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null)
return;
@ -203,6 +216,7 @@ public class MotorConfigurationPanel extends JPanel {
private void selectIgnition() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null)
return;
@ -217,40 +231,4 @@ public class MotorConfigurationPanel extends JPanel {
updateButtonState();
}
public void makeMotorMount(MotorMount mount, boolean isMotorMount) {
mount.setMotorMount(isMotorMount);
configurationTableModel.fireTableStructureChanged();
updateButtonState();
}
private MotorMount[] getPotentialMotorMounts() {
List<MotorMount> list = new ArrayList<MotorMount>();
for (RocketComponent c : rocket) {
if (c instanceof MotorMount) {
list.add((MotorMount) c);
}
}
return list.toArray(new MotorMount[0]);
}
public MotorMount findMount(int row) {
MotorMount mount = null;
int count = row;
for (MotorMount m : mounts) {
if (m.isMotorMount())
count--;
if (count < 0) {
mount = m;
break;
}
}
if (mount == null) {
throw new IndexOutOfBoundsException("motor mount not found, row=" + row);
}
return mount;
}
}

View File

@ -1,24 +1,36 @@
package net.sf.openrocket.gui.dialogs.flightconfiguration;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import net.sf.openrocket.rocketcomponent.MotorMount;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.util.ArrayList;
/**
* The table model for selecting whether components are motor mounts or not.
*/
class MotorMountTableModel extends AbstractTableModel {
/**
*
*/
private final MotorConfigurationPanel motorConfigurationPanel;
private final List<MotorMount> potentialMounts = new ArrayList<MotorMount>();
/**
* @param motorConfigurationPanel
*/
MotorMountTableModel(MotorConfigurationPanel motorConfigurationPanel) {
MotorMountTableModel(MotorConfigurationPanel motorConfigurationPanel, Rocket rocket) {
this.motorConfigurationPanel = motorConfigurationPanel;
for (RocketComponent c : rocket) {
if (c instanceof MotorMount) {
potentialMounts.add((MotorMount) c);
}
}
}
@Override
public int getColumnCount() {
return 2;
@ -26,7 +38,7 @@ class MotorMountTableModel extends AbstractTableModel {
@Override
public int getRowCount() {
return this.motorConfigurationPanel.mounts.length;
return potentialMounts.size();
}
@Override
@ -47,10 +59,10 @@ class MotorMountTableModel extends AbstractTableModel {
public Object getValueAt(int row, int column) {
switch (column) {
case 0:
return new Boolean(this.motorConfigurationPanel.mounts[row].isMotorMount());
return new Boolean(potentialMounts.get(row).isMotorMount());
case 1:
return this.motorConfigurationPanel.mounts[row].toString();
return potentialMounts.get(row).toString();
default:
throw new IndexOutOfBoundsException("column=" + column);
@ -68,6 +80,8 @@ class MotorMountTableModel extends AbstractTableModel {
throw new IllegalArgumentException("column=" + column + ", value=" + value);
}
this.motorConfigurationPanel.makeMotorMount( this.motorConfigurationPanel.mounts[row], (Boolean) value);
MotorMount mount = potentialMounts.get(row);
mount.setMotorMount((Boolean) value);
this.motorConfigurationPanel.fireTableDataChanged();
}
}

View File

@ -3,51 +3,53 @@ package net.sf.openrocket.rocketcomponent;
import java.util.HashMap;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.util.Utils;
class BaseMotorMount implements FlightConfigurable<MotorConfiguration>, Cloneable {
private HashMap<String, MotorConfiguration > motors = new HashMap<String,MotorConfiguration>();
private HashMap<String, MotorConfiguration> motors = new HashMap<String, MotorConfiguration>();
private MotorConfiguration defaultConfiguration = MotorConfiguration.makeDefaultMotorConfiguration();
@Override
public MotorConfiguration getFlightConfiguration(String configId) {
return motors.get(configId);
}
@Override
public void setFlightConfiguration(String configId, MotorConfiguration config) {
if ( config == null ) {
public void setFlightConfiguration(String configId, MotorConfiguration config) {
if (config == null) {
motors.remove(configId);
} else {
motors.put(configId, config);
motors.put(configId, config);
}
}
@Override
public void cloneFlightConfiguration(String oldConfigId, String newConfigId) {
MotorConfiguration oldConfig = getFlightConfiguration(oldConfigId);
if ( oldConfig != null ) {
if (oldConfig != null) {
oldConfig = oldConfig.clone();
}
setFlightConfiguration(newConfigId, oldConfig);
}
@Override
public MotorConfiguration getDefaultFlightConfiguration() {
return defaultConfiguration;
}
@Override
public void setDefaultFlightConfiguration(MotorConfiguration config) {
defaultConfiguration = config;
}
Motor getMotor(String id) {
if (id == null)
return null;
MotorConfiguration motorConfig =getFlightConfiguration(id);
if ( motorConfig == null ) {
MotorConfiguration motorConfig = getFlightConfiguration(id);
if (motorConfig == null) {
return null;
}
return motorConfig.getMotor();
@ -65,26 +67,26 @@ class BaseMotorMount implements FlightConfigurable<MotorConfiguration>, Cloneabl
if (motor != null) {
throw new IllegalArgumentException("Cannot set non-null motor for id null");
}
return false;
}
if ( motor == null ) {
setFlightConfiguration(id, null);
}
MotorConfiguration current = getFlightConfiguration(id);
if ( current == null ) {
if (current == null) {
current = new MotorConfiguration();
setFlightConfiguration(id, current);
}
Motor currentMotor = current.getMotor();
if (motor.equals(currentMotor)) {
if (Utils.equals(motor, current.getMotor())) {
return false;
} else {
current.setMotor(motor);
return true;
}
current.setMotor(motor);
return true;
}
double getMotorDelay(String id) {
MotorConfiguration current = getFlightConfiguration(id);
Double delay = ( current == null ) ? null : current.getEjectionDelay();
Double delay = (current == null) ? null : current.getEjectionDelay();
if (delay == null)
return Motor.PLUGGED;
return delay;
@ -99,24 +101,24 @@ class BaseMotorMount implements FlightConfigurable<MotorConfiguration>, Cloneabl
*/
boolean setMotorDelay(String id, double delay) {
MotorConfiguration current = getFlightConfiguration(id);
if ( current == null ) {
if (current == null) {
current = new MotorConfiguration();
setFlightConfiguration(id, current);
}
if ( current.getEjectionDelay() != delay ) {
if (current.getEjectionDelay() != delay) {
current.setEjectionDelay(delay);
return true;
} else {
return false;
}
}
@Override
protected BaseMotorMount clone() {
BaseMotorMount clone = new BaseMotorMount();
clone.defaultConfiguration = defaultConfiguration.clone();
clone.motors = (HashMap<String,MotorConfiguration>) motors.clone();
clone.motors = (HashMap<String, MotorConfiguration>) motors.clone();
return clone;
}
}