Skip to content

Commit

Permalink
Merge pull request #1298 from Sleet01/fix-1230-fixed-wing-transport-d…
Browse files Browse the repository at this point in the history
…oes-not-meet-door-minimum

Fix #1230: 0 Max Bay Doors on Large Fixed-Wing Sup
  • Loading branch information
NickAragua authored Aug 8, 2023
2 parents 8988ac1 + 9fb3c72 commit 683fac6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
1 change: 1 addition & 0 deletions megameklab/resources/megameklab/resources/Tabs.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
TransportTab.lblCurrentBays.text=Current Bays
TransportTab.lblMaxDoors.text=Maximum Doors:
TransportTab.lblMinDoors.text=Minimum Doors:
TransportTab.lblAvailableBays.text=Available Bays
TransportTab.btnAddBay.text=Add Bay
TransportTab.btnAddBay.tooltip=Add a new transport bay of the selected type to the vessel.
Expand Down
76 changes: 44 additions & 32 deletions megameklab/src/megameklab/ui/generalUnit/TransportTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

/**
* Tab for adding and modifying aerospace and support vee transport bays.
*
*
* @author Neoancient
*/
public class TransportTab extends IView implements ActionListener, ChangeListener {
Expand All @@ -56,6 +56,7 @@ public class TransportTab extends IView implements ActionListener, ChangeListene
private final JSpinner spnTroopSpace = new JSpinner(new SpinnerNumberModel(0.0, 0.0, null, 0.5));
private final JSpinner spnPodTroopSpace = new JSpinner(new SpinnerNumberModel(0.0, 0.0, null, 0.5));
private final JLabel lblMaxDoors = new JLabel();
private final JLabel lblMinDoors = new JLabel();
private final InstalledBaysModel modelInstalled = new InstalledBaysModel();
private final JTable tblInstalled = new JTable(modelInstalled);
private final AvailableBaysModel modelAvailable = new AvailableBaysModel();
Expand All @@ -73,7 +74,7 @@ public TransportTab(EntitySource eSource) {
super(eSource);
initUI();
}

private void initUI() {
ResourceBundle resourceMap = ResourceBundle.getBundle("megameklab.resources.Tabs");

Expand Down Expand Up @@ -129,9 +130,15 @@ private void initUI() {
gbc.gridy++;
if (getEntity().isAero()) {
gbc.gridwidth = 1;
gbc.gridx = 0;
bayPanel.add(new JLabel(resourceMap.getString("TransportTab.lblMaxDoors.text")), gbc);
gbc.gridx = 1;
gbc.gridx = GridBagConstraints.RELATIVE;
bayPanel.add(lblMaxDoors, gbc);
gbc.gridy++;
gbc.gridx = 0;
bayPanel.add(new JLabel(resourceMap.getString("TransportTab.lblMinDoors.text")), gbc);
gbc.gridx = GridBagConstraints.RELATIVE;
bayPanel.add(lblMinDoors, gbc);
}

gbc.gridx = 0;
Expand Down Expand Up @@ -160,7 +167,7 @@ private void initUI() {
gbc.gridheight = 1;
bayPanel.add(new JLabel(resourceMap.getString("TransportTab.lblAvailableBays.text")), gbc);

gbc.gridy = 2;
gbc.gridy = (getEntity().isAero() ? 3 : 2);
btnAddBay.setText(resourceMap.getString("TransportTab.btnAddBay.text"));
btnAddBay.setToolTipText(resourceMap.getString("TransportTab.btnAddBay.tooltip"));
bayPanel.add(btnAddBay, gbc);
Expand Down Expand Up @@ -249,6 +256,7 @@ public void refresh() {

if (getEntity().isAero()) {
lblMaxDoors.setText(String.valueOf(TestAero.maxBayDoors(getAero())));
lblMinDoors.setText(String.valueOf(doorsRequired()));
}
if (canMountInfantryCompartment()) {
panTroopspace.setVisible(true);
Expand Down Expand Up @@ -313,14 +321,14 @@ private boolean canMountInfantryCompartment() {
return true;
}
}

private void checkButtons() {
btnRemoveBay.setEnabled(tblInstalled.getSelectedRow() >= 0);
btnAddBay.setEnabled(canAddSelectedBay());
btnAddToCargo.setEnabled(UnitUtil.getEntityVerifier(getEntity())
.calculateWeight() < getEntity().getWeight());
}

private int doorsAvailable() {
if (getEntity().isAero()) {
int total = TestAero.maxBayDoors(getAero());
Expand All @@ -333,22 +341,26 @@ private int doorsAvailable() {
}
}

private boolean canAddSelectedBay() {
// Door limits do not apply to support vehicles
if (getEntity().isSupportVehicle()) {
return true;
private int doorsRequired() {
int minimum = 0;
for (Bay bay : getAero().getTransportBays()) {
minimum += bay.getMinDoors();
}
return minimum;
}

private boolean canAddSelectedBay() {
int selected = tblAvailable.getSelectedRow();
if (selected < 0) {
return false;
}
BayData bayType = modelAvailable.getBayType(tblAvailable.convertRowIndexToModel(selected));
return (doorsAvailable() > 0) || bayType.isCargoBay() || bayType.isInfantryBay();
return (doorsAvailable() > 0) || bayType.isInfantryBay();
}

/**
* Removes the bay from the vessel and adjusts the crew count.
*
*
* @param bay The bay to remove
*/
private void removeBay(Bay bay) {
Expand All @@ -360,10 +372,10 @@ private void removeBay(Bay bay) {
}
getEntity().removeTransporter(bay);
}

/**
* Adds the bay to the vessel and adjusts the crew count.
*
*
* @param bay The bay to add
*/
private void addBay(Bay bay, boolean pod) {
Expand All @@ -375,7 +387,7 @@ private void addBay(Bay bay, boolean pod) {
getJumpship().setNCrew(getJumpship().getNCrew() + personnel);
}
}

/**
* Removing bays can cause undesirable gaps in bay numbers, and it would be nice to let the
* user order the bays. Since bay numbers are immutable we have to instantiate a new bay to
Expand Down Expand Up @@ -420,7 +432,7 @@ private void rebuildBays() {
podList.forEach(b -> getEntity().addTransporter(b, true));
modelInstalled.refreshBays();
}

@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == btnAddBay) {
Expand Down Expand Up @@ -477,7 +489,7 @@ public void actionPerformed(ActionEvent evt) {
}
}
}

@Override
public void stateChanged(ChangeEvent ev) {
if (ev.getSource() == spnDockingHardpoints) {
Expand Down Expand Up @@ -564,15 +576,15 @@ void refreshBays() {
// following the removal of any naval repair facilities
refreshDockingHardpoints();
}

Bay getBay(int row) {
return bayList.get(row);
}

Iterator<Bay> getBays() {
return bayList.iterator();
}

BayData getBayType(int row) {
return bayTypeList.get(row);
}
Expand Down Expand Up @@ -604,7 +616,7 @@ public String getColumnName(int column) {
public int getRowCount() {
return bayList.size();
}

@Override
public int getColumnCount() {
return NUM_COLS;
Expand Down Expand Up @@ -710,15 +722,15 @@ void reorder(int from, int to) {
refresh();
}
}

private class AvailableBaysModel extends AbstractTableModel {
private static final int COL_NAME = 0;
private static final int COL_SIZE = 1;
private static final int COL_PERSONNEL = 2;
private static final int NUM_COLS = 3;

private final List<BayData> bayList = new ArrayList<>();

void refreshBays() {
bayList.clear();
for (BayData bay : BayData.values()) {
Expand Down Expand Up @@ -776,16 +788,16 @@ public Object getValueAt(int rowIndex, int columnIndex) {
}
return bayList.get(rowIndex).getPersonnel();
}
}
}
return "?";
}

}

private class SpinnerCellEditor extends AbstractCellEditor implements TableCellEditor, ChangeListener {
private final JSpinner spinner = new JSpinner();
private final int column;

SpinnerCellEditor(int column) {
this.column = column;
spinner.addChangeListener(this);
Expand Down Expand Up @@ -847,7 +859,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, boolean
if (column == InstalledBaysModel.COL_DOORS) {
int doors = (Integer) modelInstalled.getValueAt(row, column);
SpinnerNumberModel model = new SpinnerNumberModel(doors,
(isCargo || isInfantry) ? 0 : 1,
(isInfantry) ? 0 : 1,
doorsAvailable() + doors, 1);
spinner.removeChangeListener(this);
spinner.setModel(model);
Expand All @@ -874,9 +886,9 @@ public Component getTableCellEditorComponent(JTable table, Object value, boolean
}
return null;
}

}

private class BayReorderTransferHandler extends TransferHandler {
@Override
protected Transferable createTransferable(JComponent c) {
Expand Down
5 changes: 5 additions & 0 deletions megameklab/src/megameklab/ui/supportVehicle/SVMainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public void refreshEquipment() {

@Override
public void refreshTransport() {
transportTab = new TransportTab(this);
transportTab.addRefreshedListener(this);
int idx = configPane.indexOfTab("Transport");
configPane.removeTabAt(idx);
configPane.insertTab("Transport", null, new TabScrollPane(transportTab), null,idx);
transportTab.refresh();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public void refreshSummary() {
public void chassisChanged(String chassis) {
getSV().setChassis(chassis);
refresh.refreshHeader();
refresh.refreshTransport();
refresh.refreshPreview();
}

Expand Down

0 comments on commit 683fac6

Please sign in to comment.