Skip to content

Commit 2512231

Browse files
Merge pull request #1831 from exeea/fixes
Fixes for RS preview and enhanced tabbed pane
2 parents cb18074 + 89daf41 commit 2512231

File tree

11 files changed

+66
-40
lines changed

11 files changed

+66
-40
lines changed

megameklab/src/megameklab/ui/battleArmor/BAMainUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ public JDialog getFloatingEquipmentDatabase() {
186186

187187
@Override
188188
public List<Mounted<?>> getUnallocatedMounted() {
189+
if (buildTab == null) {
190+
return List.of();
191+
}
189192
return buildTab.getBuildView().getEquipment();
190193
}
191194
}

megameklab/src/megameklab/ui/combatVehicle/CVMainUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ public JDialog getFloatingEquipmentDatabase() {
241241

242242
@Override
243243
public List<Mounted<?>> getUnallocatedMounted() {
244+
if (buildTab == null) {
245+
return List.of();
246+
}
244247
return buildTab.getUnallocatedView().getEquipment();
245248
}
246249
}

megameklab/src/megameklab/ui/fighterAero/ASMainUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ public JDialog getFloatingEquipmentDatabase() {
229229

230230
@Override
231231
public List<Mounted<?>> getUnallocatedMounted() {
232+
if (buildTab == null) {
233+
return List.of();
234+
}
232235
return buildTab.getBuildView().getEquipment();
233236
}
234237
}

megameklab/src/megameklab/ui/generalUnit/QuirksTab.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,14 @@ private void relayoutGroupPanel(JPanel groupPanel, List<DialogOptionComponent> q
384384
gbc.fill = GridBagConstraints.HORIZONTAL;
385385

386386
// Add quirks to the group panel in a grid layout
387+
int lastColIndex = 0;
387388
for (int i = 0; i < totalItems; i++) {
388-
final int colIndex = i % itemsPerCol;
389+
final int rowIndex = i % itemsPerCol;
389390
gbc.gridx = i / itemsPerCol;
390-
gbc.gridy = colIndex;
391+
gbc.gridy = rowIndex;
392+
if (gbc.gridx > lastColIndex) {
393+
lastColIndex = gbc.gridx;
394+
}
391395

392396
DialogOptionComponent comp = quirks.get(i);
393397
// Set preferred width so we match the width of the widest item
@@ -401,10 +405,9 @@ private void relayoutGroupPanel(JPanel groupPanel, List<DialogOptionComponent> q
401405
// Because we are auto-spacing them horizontally (bgc.weightx = 1 above), we
402406
// create fake columns in case
403407
// this group doesn't have enough (usually weapons)
404-
final int lastColUsed = totalItems % itemsPerCol;
405-
for (int i = lastColUsed + 1; i < numCols; i++) {
406-
gbc.gridx = numCols - 1;
407-
gbc.gridy = i;
408+
for (int i = lastColIndex + 1; i < numCols; i++) {
409+
gbc.gridx = i;
410+
gbc.gridy = 0;
408411
groupPanel.add(Box.createHorizontalStrut(globalMaxItemWidth), gbc);
409412
}
410413

megameklab/src/megameklab/ui/generalUnit/RecordSheetPreviewPanel.java

+27-33
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.util.concurrent.TimeUnit;
6060
import java.util.concurrent.atomic.AtomicInteger;
6161
import java.util.concurrent.locks.ReentrantLock;
62+
import java.util.concurrent.atomic.AtomicReference;
6263

6364
import javax.swing.JMenuItem;
6465
import javax.swing.JPanel;
@@ -193,7 +194,7 @@ public void mouseClicked(MouseEvent e) {
193194
if (e.getButton() != MouseEvent.BUTTON3) {
194195
return;
195196
}
196-
if (e.getComponent().isShowing()) {
197+
if ((e.getComponent() != null) && (e.getComponent().isShowing())) {
197198
popup.show(e.getComponent(), e.getX(), e.getY());
198199
}
199200
}
@@ -478,7 +479,7 @@ public void setOneUnitPerSheet(boolean oneUnitPerSheet) {
478479
*
479480
* @param selectedEntities The list of entities to display.
480481
*/
481-
public void setEntities(List<BTObject> selectedEntities) {
482+
public void setEntities(List<? extends BTObject> selectedEntities) {
482483
List<BTObject> processedEntities;
483484
if (selectedEntities == null) {
484485
processedEntities = Collections.emptyList();
@@ -491,35 +492,10 @@ public void setEntities(List<BTObject> selectedEntities) {
491492
this.currentEntities = processedEntities;
492493
regenerateAndReset();
493494
} else {
494-
updateTimer.restart(); // Restart update timer to debounce
495+
updateSheetContentInPlace();
495496
}
496497
}
497498

498-
/**
499-
* Set the entities to be displayed in the record sheet preview.
500-
*
501-
* @param selectedEntities The list of entities to display.
502-
*/
503-
public void setEntities(ArrayList<Entity> selectedEntities) {
504-
List<BTObject> processedEntities;
505-
if (selectedEntities == null) {
506-
processedEntities = Collections.emptyList();
507-
} else {
508-
// Create a new list to avoid external modifications affecting us
509-
processedEntities = new ArrayList<>(selectedEntities);
510-
}
511-
boolean entitiesChanged = !areEntityListsEffectivelyEqual(this.currentEntities, processedEntities);
512-
if (entitiesChanged) {
513-
this.currentEntities = processedEntities;
514-
regenerateAndReset();
515-
} else {
516-
updateTimer.restart(); // Restart update timer to debounce
517-
}
518-
}
519-
520-
521-
522-
523499
/**
524500
* Set a single entity to be displayed in the record sheet preview.
525501
*
@@ -695,6 +671,25 @@ private void updateSheetContentInPlace() {
695671
updateTimer.restart(); // Restart update timer to debounce
696672
}
697673

674+
private List<PrintRecordSheet> createSheetsInEDT(List<BTObject> entitiesToGenerate, boolean singlePrint,
675+
RecordSheetOptions options) {
676+
if (!SwingUtilities.isEventDispatchThread()) {
677+
// If not, use invokeAndWait to call this method on the EDT
678+
final AtomicReference<List<PrintRecordSheet>> resultHolder = new AtomicReference<>();
679+
try {
680+
SwingUtilities.invokeAndWait(() ->
681+
resultHolder.set(createSheetsInEDT(entitiesToGenerate, singlePrint, options))
682+
);
683+
return resultHolder.get();
684+
} catch (Exception e) {
685+
logger.error("Error dispatching createSheets to EDT", e);
686+
return Collections.emptyList();
687+
}
688+
}
689+
List<PrintRecordSheet> tempGeneratedSheets = UnitPrintManager.createSheets(entitiesToGenerate, singlePrint, options, true);
690+
return tempGeneratedSheets;
691+
}
692+
698693
private void performUpdateSheetContentInPlace() {
699694
if (!isShowing()) {
700695
pendingInPlaceUpdate = true;
@@ -719,9 +714,7 @@ private void performUpdateSheetContentInPlace() {
719714
long start = System.nanoTime();
720715
// Regenerate sheets based on potentially updated entity state
721716
RecordSheetOptions options = new RecordSheetOptions();
722-
newGeneratedSheets = UnitPrintManager.createSheets(
723-
currentEntities.subList(0, Math.min(currentEntities.size(), MAX_PRINTABLE_ENTITIES)),
724-
oneUnitPerSheet, options, true);
717+
newGeneratedSheets = createSheetsInEDT(currentEntities.subList(0, Math.min(currentEntities.size(), MAX_PRINTABLE_ENTITIES)), oneUnitPerSheet, options);
725718
long end = System.nanoTime();
726719
logger.debug("Finished in-place UnitPrintManager.createSheets in {} ms", (end - start) / 1_000_000);
727720

@@ -797,8 +790,9 @@ private void performUpdateSheetContentInPlace() {
797790
return;
798791
}
799792
if (finalStructureChanged) {
800-
// If structure changed (or error occurred), fall back to full reset
801-
logger.warn(
793+
// If structure changed (or error occurred), fall back to full reset.
794+
// It can happen when the tab is re-attached
795+
logger.debug(
802796
"Sheet structure changed during in-place update or error occurred. Performing full reset.");
803797
regenerateAndReset(); // Use the full reset logic
804798
return;

megameklab/src/megameklab/ui/largeAero/DSMainUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ public JDialog getFloatingEquipmentDatabase() {
256256

257257
@Override
258258
public List<Mounted<?>> getUnallocatedMounted() {
259+
if (buildTab == null) {
260+
return List.of();
261+
}
259262
return buildTab.getBuildView().getEquipment();
260263
}
261264
}

megameklab/src/megameklab/ui/largeAero/WSMainUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ public JDialog getFloatingEquipmentDatabase() {
271271

272272
@Override
273273
public List<Mounted<?>> getUnallocatedMounted() {
274+
if (buildTab == null) {
275+
return List.of();
276+
}
274277
return buildTab.getBuildView().getEquipment();
275278
}
276279
}

megameklab/src/megameklab/ui/mek/BMMainUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ public JDialog getFloatingEquipmentDatabase() {
260260

261261
@Override
262262
public List<Mounted<?>> getUnallocatedMounted() {
263+
if (buildTab == null) {
264+
return List.of();
265+
}
263266
return this.buildTab.getBuildView().getEquipment();
264267
}
265268
}

megameklab/src/megameklab/ui/protoMek/PMMainUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ public JDialog getFloatingEquipmentDatabase() {
209209

210210
@Override
211211
public List<Mounted<?>> getUnallocatedMounted() {
212+
if (buildTab == null) {
213+
return List.of();
214+
}
212215
return buildTab.getBuildView().getEquipment();
213216
}
214217
}

megameklab/src/megameklab/ui/supportVehicle/SVMainUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ public JDialog getFloatingEquipmentDatabase() {
256256

257257
@Override
258258
public List<Mounted<?>> getUnallocatedMounted() {
259+
if (buildTab == null) {
260+
return List.of();
261+
}
259262
return buildTab.getUnallocatedView().getEquipment();
260263
}
261264
}

megameklab/src/megameklab/ui/util/EnhancedTabbedPane.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,13 @@ public void mouseMoved(MouseEvent e) {
220220
}
221221
}
222222
});
223-
// Add double-click listener for tab area reattachment
223+
// Add double-click listener for tab area reattachment and cursor reset
224224
addMouseListener(new MouseAdapter() {
225+
@Override
226+
public void mouseExited(MouseEvent e) {
227+
// Reset cursor when mouse exits the tabbed pane
228+
setCursor(Cursor.getDefaultCursor());
229+
}
225230
@Override
226231
public void mouseClicked(MouseEvent e) {
227232
if (e.getClickCount() == 2 && hasDetachedTabs()) {

0 commit comments

Comments
 (0)