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.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JLabel; import javax.swing.JLabel;
@ -36,10 +34,10 @@ public class MotorConfigurationPanel extends JPanel {
private final FlightConfigurationDialog flightConfigurationDialog; private final FlightConfigurationDialog flightConfigurationDialog;
private final Rocket rocket; private final Rocket rocket;
private final JTable configurationTable;
private final MotorConfigurationTableModel configurationTableModel; private final MotorConfigurationTableModel configurationTableModel;
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton; private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
final MotorMount[] mounts;
MotorConfigurationPanel(FlightConfigurationDialog flightConfigurationDialog, Rocket rocket) { MotorConfigurationPanel(FlightConfigurationDialog flightConfigurationDialog, Rocket rocket) {
super(new MigLayout("fill")); super(new MigLayout("fill"));
@ -49,7 +47,6 @@ public class MotorConfigurationPanel extends JPanel {
DescriptionArea desc = new DescriptionArea(trans.get("description"), 3, -1); DescriptionArea desc = new DescriptionArea(trans.get("description"), 3, -1);
this.add(desc, "spanx, growx, wrap para"); this.add(desc, "spanx, growx, wrap para");
mounts = getPotentialMotorMounts();
//// Motor mount selection //// Motor mount selection
JLabel label = new StyledLabel(trans.get("lbl.motorMounts"), Style.BOLD); JLabel label = new StyledLabel(trans.get("lbl.motorMounts"), Style.BOLD);
@ -61,7 +58,7 @@ public class MotorConfigurationPanel extends JPanel {
//// Motor Mount selection //// Motor Mount selection
JTable table = new JTable(new MotorMountTableModel(this)); JTable table = new JTable(new MotorMountTableModel(this, rocket));
table.setTableHeader(null); table.setTableHeader(null);
table.setShowVerticalLines(false); table.setShowVerticalLines(false);
table.setRowSelectionAllowed(false); table.setRowSelectionAllowed(false);
@ -81,27 +78,18 @@ public class MotorConfigurationPanel extends JPanel {
//// Motor selection table. //// Motor selection table.
configurationTableModel = new MotorConfigurationTableModel(rocket); configurationTableModel = new MotorConfigurationTableModel(rocket);
final JTable configurationTable = new JTable(configurationTableModel); configurationTable = new JTable(configurationTableModel);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setRowSelectionAllowed(true); configurationTable.setRowSelectionAllowed(true);
configurationTable.addMouseListener(new MouseAdapter() { configurationTable.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
int row = configurationTable.getSelectedRow(); updateButtonState();
if (e.getClickCount() == 2) {
if (e.getClickCount() == 1) {
// Single click updates selection
updateButtonState();
} else if (e.getClickCount() == 2) {
// Double-click edits motor // Double-click edits motor
selectMotor(); selectMotor();
} }
} }
}); });
@ -158,6 +146,7 @@ public class MotorConfigurationPanel extends JPanel {
public void updateButtonState() { public void updateButtonState() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID(); String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
selectMotorButton.setEnabled(currentMount != null && currentID != null); selectMotorButton.setEnabled(currentMount != null && currentID != null);
removeMotorButton.setEnabled(currentMount != null && currentID != null); removeMotorButton.setEnabled(currentMount != null && currentID != null);
selectIgnitionButton.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() { private void selectMotor() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID(); String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null) if (currentID == null || currentMount == null)
return; return;
@ -191,6 +203,7 @@ public class MotorConfigurationPanel extends JPanel {
private void removeMotor() { private void removeMotor() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID(); String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null) if (currentID == null || currentMount == null)
return; return;
@ -203,6 +216,7 @@ public class MotorConfigurationPanel extends JPanel {
private void selectIgnition() { private void selectIgnition() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID(); String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null) if (currentID == null || currentMount == null)
return; return;
@ -217,40 +231,4 @@ public class MotorConfigurationPanel extends JPanel {
updateButtonState(); 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; package net.sf.openrocket.gui.dialogs.flightconfiguration;
import java.util.List;
import javax.swing.table.AbstractTableModel; 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. * The table model for selecting whether components are motor mounts or not.
*/ */
class MotorMountTableModel extends AbstractTableModel { class MotorMountTableModel extends AbstractTableModel {
/**
*
*/
private final MotorConfigurationPanel motorConfigurationPanel; private final MotorConfigurationPanel motorConfigurationPanel;
private final List<MotorMount> potentialMounts = new ArrayList<MotorMount>();
/** /**
* @param motorConfigurationPanel * @param motorConfigurationPanel
*/ */
MotorMountTableModel(MotorConfigurationPanel motorConfigurationPanel) { MotorMountTableModel(MotorConfigurationPanel motorConfigurationPanel, Rocket rocket) {
this.motorConfigurationPanel = motorConfigurationPanel; this.motorConfigurationPanel = motorConfigurationPanel;
for (RocketComponent c : rocket) {
if (c instanceof MotorMount) {
potentialMounts.add((MotorMount) c);
}
}
} }
@Override @Override
public int getColumnCount() { public int getColumnCount() {
return 2; return 2;
@ -26,7 +38,7 @@ class MotorMountTableModel extends AbstractTableModel {
@Override @Override
public int getRowCount() { public int getRowCount() {
return this.motorConfigurationPanel.mounts.length; return potentialMounts.size();
} }
@Override @Override
@ -47,10 +59,10 @@ class MotorMountTableModel extends AbstractTableModel {
public Object getValueAt(int row, int column) { public Object getValueAt(int row, int column) {
switch (column) { switch (column) {
case 0: case 0:
return new Boolean(this.motorConfigurationPanel.mounts[row].isMotorMount()); return new Boolean(potentialMounts.get(row).isMotorMount());
case 1: case 1:
return this.motorConfigurationPanel.mounts[row].toString(); return potentialMounts.get(row).toString();
default: default:
throw new IndexOutOfBoundsException("column=" + column); throw new IndexOutOfBoundsException("column=" + column);
@ -68,6 +80,8 @@ class MotorMountTableModel extends AbstractTableModel {
throw new IllegalArgumentException("column=" + column + ", value=" + value); 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 java.util.HashMap;
import net.sf.openrocket.motor.Motor; import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.util.Utils;
class BaseMotorMount implements FlightConfigurable<MotorConfiguration>, Cloneable { 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(); private MotorConfiguration defaultConfiguration = MotorConfiguration.makeDefaultMotorConfiguration();
@Override @Override
public MotorConfiguration getFlightConfiguration(String configId) { public MotorConfiguration getFlightConfiguration(String configId) {
return motors.get(configId); return motors.get(configId);
} }
@Override @Override
public void setFlightConfiguration(String configId, MotorConfiguration config) { public void setFlightConfiguration(String configId, MotorConfiguration config) {
if ( config == null ) { if (config == null) {
motors.remove(configId); motors.remove(configId);
} else { } else {
motors.put(configId, config); motors.put(configId, config);
} }
} }
@Override @Override
public void cloneFlightConfiguration(String oldConfigId, String newConfigId) { public void cloneFlightConfiguration(String oldConfigId, String newConfigId) {
MotorConfiguration oldConfig = getFlightConfiguration(oldConfigId); MotorConfiguration oldConfig = getFlightConfiguration(oldConfigId);
if ( oldConfig != null ) { if (oldConfig != null) {
oldConfig = oldConfig.clone(); oldConfig = oldConfig.clone();
} }
setFlightConfiguration(newConfigId, oldConfig); setFlightConfiguration(newConfigId, oldConfig);
} }
@Override @Override
public MotorConfiguration getDefaultFlightConfiguration() { public MotorConfiguration getDefaultFlightConfiguration() {
return defaultConfiguration; return defaultConfiguration;
} }
@Override @Override
public void setDefaultFlightConfiguration(MotorConfiguration config) { public void setDefaultFlightConfiguration(MotorConfiguration config) {
defaultConfiguration = config; defaultConfiguration = config;
} }
Motor getMotor(String id) { Motor getMotor(String id) {
if (id == null) if (id == null)
return null; return null;
MotorConfiguration motorConfig =getFlightConfiguration(id); MotorConfiguration motorConfig = getFlightConfiguration(id);
if ( motorConfig == null ) { if (motorConfig == null) {
return null; return null;
} }
return motorConfig.getMotor(); return motorConfig.getMotor();
@ -65,26 +67,26 @@ class BaseMotorMount implements FlightConfigurable<MotorConfiguration>, Cloneabl
if (motor != null) { if (motor != null) {
throw new IllegalArgumentException("Cannot set non-null motor for id null"); throw new IllegalArgumentException("Cannot set non-null motor for id null");
} }
return false;
} }
if ( motor == null ) {
setFlightConfiguration(id, null);
}
MotorConfiguration current = getFlightConfiguration(id); MotorConfiguration current = getFlightConfiguration(id);
if ( current == null ) { if (current == null) {
current = new MotorConfiguration(); current = new MotorConfiguration();
setFlightConfiguration(id, current); setFlightConfiguration(id, current);
} }
Motor currentMotor = current.getMotor();
if (motor.equals(currentMotor)) { if (Utils.equals(motor, current.getMotor())) {
return false; return false;
} else {
current.setMotor(motor);
return true;
} }
current.setMotor(motor);
return true;
} }
double getMotorDelay(String id) { double getMotorDelay(String id) {
MotorConfiguration current = getFlightConfiguration(id); MotorConfiguration current = getFlightConfiguration(id);
Double delay = ( current == null ) ? null : current.getEjectionDelay(); Double delay = (current == null) ? null : current.getEjectionDelay();
if (delay == null) if (delay == null)
return Motor.PLUGGED; return Motor.PLUGGED;
return delay; return delay;
@ -99,24 +101,24 @@ class BaseMotorMount implements FlightConfigurable<MotorConfiguration>, Cloneabl
*/ */
boolean setMotorDelay(String id, double delay) { boolean setMotorDelay(String id, double delay) {
MotorConfiguration current = getFlightConfiguration(id); MotorConfiguration current = getFlightConfiguration(id);
if ( current == null ) { if (current == null) {
current = new MotorConfiguration(); current = new MotorConfiguration();
setFlightConfiguration(id, current); setFlightConfiguration(id, current);
} }
if ( current.getEjectionDelay() != delay ) { if (current.getEjectionDelay() != delay) {
current.setEjectionDelay(delay); current.setEjectionDelay(delay);
return true; return true;
} else { } else {
return false; return false;
} }
} }
@Override @Override
protected BaseMotorMount clone() { protected BaseMotorMount clone() {
BaseMotorMount clone = new BaseMotorMount(); BaseMotorMount clone = new BaseMotorMount();
clone.defaultConfiguration = defaultConfiguration.clone(); clone.defaultConfiguration = defaultConfiguration.clone();
clone.motors = (HashMap<String,MotorConfiguration>) motors.clone(); clone.motors = (HashMap<String, MotorConfiguration>) motors.clone();
return clone; return clone;
} }
} }