[Resolves #379] Can now toggle motormounts from the Motor Configuration Tab

- Actual Fix is at MotorMountTableModel:102:   re-added callback to the MotorMount component
- cleaned up unused variables in the other panels
- Tightened up variable re-use in GUIUtil class
  -- made several intermediate variables separate, uniquely-named, and final.
This commit is contained in:
Daniel_M_Williams 2017-10-29 17:35:30 -04:00
parent cfc1715cf4
commit 9bfaf8877e
4 changed files with 33 additions and 46 deletions

View File

@ -13,19 +13,14 @@ import net.sf.openrocket.gui.util.GUIUtil;
import net.sf.openrocket.rocketcomponent.Rocket;
@SuppressWarnings("serial")
public abstract class MotorMountConfigurationPanel extends JPanel {
private final Rocket rocket;
private final Component parent;
public class MotorMountConfigurationPanel extends JPanel {
public MotorMountConfigurationPanel( Component parent, Rocket rocket ) {
super(new MigLayout("") );
this.parent = parent;
this.rocket = rocket;
//// Motor Mount selection
JTable table = new JTable(new MotorMountTableModel(this, rocket));
//// Motor Mount selection
MotorMountTableModel model = new MotorMountTableModel( rocket);
JTable table = new JTable( model );
table.setTableHeader(null);
table.setShowVerticalLines(false);
table.setRowSelectionAllowed(false);
@ -43,6 +38,4 @@ public abstract class MotorMountConfigurationPanel extends JPanel {
this.add(scroll, "w 200lp, h 150lp, grow");
}
public abstract void onDataChanged();
}

View File

@ -17,18 +17,16 @@ import net.sf.openrocket.util.ArrayList;
* The table model for selecting whether components are motor mounts or not.
*/
class MotorMountTableModel extends AbstractTableModel implements ComponentChangeListener {
private final MotorMountConfigurationPanel motorConfigurationPanel;
private final List<MotorMount> potentialMounts = new ArrayList<MotorMount>();
private static final long serialVersionUID = 1956400848559941228L;
private final List<MotorMount> potentialMounts = new ArrayList<MotorMount>();
private final Rocket rocket;
/**
* @param motorConfigurationPanel
*/
MotorMountTableModel(MotorMountConfigurationPanel motorConfigurationPanel, Rocket rocket) {
this.motorConfigurationPanel = motorConfigurationPanel;
MotorMountTableModel( Rocket rocket) {
this.rocket = rocket;
initialize();
@ -101,7 +99,7 @@ class MotorMountTableModel extends AbstractTableModel implements ComponentChange
throw new IllegalArgumentException("column=" + column + ", value=" + value);
}
Log.warn("this method is no longer useful....: setValueAt(obj,int,int):104");
//this.motorConfigurationPanel.onDataChanged();
MotorMount mount = potentialMounts.get(row);
mount.setMotorMount((Boolean) value);
}
}

View File

@ -60,13 +60,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
JLabel label = new StyledLabel(trans.get("lbl.motorMounts"), Style.BOLD);
subpanel.add(label, "wrap");
MotorMountConfigurationPanel mountConfigPanel = new MotorMountConfigurationPanel(this,rocket) {
@Override
public void onDataChanged() {
MotorConfigurationPanel.this.fireTableDataChanged();
}
};
MotorMountConfigurationPanel mountConfigPanel = new MotorMountConfigurationPanel(this,rocket);
subpanel.add(mountConfigPanel, "grow");
this.add(subpanel, "split, w 200lp, growy");
}

View File

@ -372,7 +372,7 @@ public class GUIUtil {
for (int col = 0; col < columns; col++) {
System.err.println("Setting column " + col + " to width " + widths[col]);
//System.err.println("Setting column " + col + " to width " + widths[col]);
table.getColumnModel().getColumn(col).setPreferredWidth(Math.min(widths[col], max) * 100);
}
}
@ -576,6 +576,7 @@ public class GUIUtil {
public static class BooleanTableClickListener extends MouseAdapter {
private final JTable table;
// these are different because the MouseEvent and the model use different indexing (0- vs 1-)
private final int clickColumn;
private final int booleanColumn;
@ -596,34 +597,35 @@ public class GUIUtil {
if (e.getButton() != MouseEvent.BUTTON1)
return;
Point p = e.getPoint();
int col = table.columnAtPoint(p);
if (col < 0)
final Point p = e.getPoint();
final int tableColumn = table.columnAtPoint(p);
if (tableColumn < 0)
return;
col = table.convertColumnIndexToModel(col);
if (col != clickColumn)
final int modelColumn= table.convertColumnIndexToModel(tableColumn);
if (modelColumn != clickColumn)
return;
int row = table.rowAtPoint(p);
if (row < 0)
final int tableRow = table.rowAtPoint(p);
if (tableRow < 0)
return;
row = table.convertRowIndexToModel(row);
if (row < 0)
final int modelRow = table.convertRowIndexToModel(tableRow);
if ( modelRow < 0)
return;
TableModel model = table.getModel();
Object value = model.getValueAt(row, booleanColumn);
if (!(value instanceof Boolean)) {
throw new IllegalStateException("Table value at row=" + row + " col=" +
final Object value = model.getValueAt(modelRow, booleanColumn);
if (!(value instanceof Boolean)) {
throw new IllegalStateException("Table value at row=" + modelRow + " col=" +
booleanColumn + " is not a Boolean, value=" + value);
}
Boolean b = (Boolean) value;
b = !b;
model.setValueAt(b, row, booleanColumn);
final Boolean oldValue = (Boolean) value;
final Boolean newValue = !oldValue;
model.setValueAt(newValue, tableRow, booleanColumn);
if (model instanceof AbstractTableModel) {
((AbstractTableModel) model).fireTableCellUpdated(row, booleanColumn);
((AbstractTableModel) model).fireTableCellUpdated(tableRow, booleanColumn);
}
}