59
59
import java .util .concurrent .TimeUnit ;
60
60
import java .util .concurrent .atomic .AtomicInteger ;
61
61
import java .util .concurrent .locks .ReentrantLock ;
62
+ import java .util .concurrent .atomic .AtomicReference ;
62
63
63
64
import javax .swing .JMenuItem ;
64
65
import javax .swing .JPanel ;
@@ -193,7 +194,7 @@ public void mouseClicked(MouseEvent e) {
193
194
if (e .getButton () != MouseEvent .BUTTON3 ) {
194
195
return ;
195
196
}
196
- if (e .getComponent (). isShowing ()) {
197
+ if (( e .getComponent () != null ) && ( e . getComponent (). isShowing () )) {
197
198
popup .show (e .getComponent (), e .getX (), e .getY ());
198
199
}
199
200
}
@@ -478,7 +479,7 @@ public void setOneUnitPerSheet(boolean oneUnitPerSheet) {
478
479
*
479
480
* @param selectedEntities The list of entities to display.
480
481
*/
481
- public void setEntities (List <BTObject > selectedEntities ) {
482
+ public void setEntities (List <? extends BTObject > selectedEntities ) {
482
483
List <BTObject > processedEntities ;
483
484
if (selectedEntities == null ) {
484
485
processedEntities = Collections .emptyList ();
@@ -491,35 +492,10 @@ public void setEntities(List<BTObject> selectedEntities) {
491
492
this .currentEntities = processedEntities ;
492
493
regenerateAndReset ();
493
494
} else {
494
- updateTimer . restart (); // Restart update timer to debounce
495
+ updateSheetContentInPlace ();
495
496
}
496
497
}
497
498
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
-
523
499
/**
524
500
* Set a single entity to be displayed in the record sheet preview.
525
501
*
@@ -695,6 +671,25 @@ private void updateSheetContentInPlace() {
695
671
updateTimer .restart (); // Restart update timer to debounce
696
672
}
697
673
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
+
698
693
private void performUpdateSheetContentInPlace () {
699
694
if (!isShowing ()) {
700
695
pendingInPlaceUpdate = true ;
@@ -719,9 +714,7 @@ private void performUpdateSheetContentInPlace() {
719
714
long start = System .nanoTime ();
720
715
// Regenerate sheets based on potentially updated entity state
721
716
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 );
725
718
long end = System .nanoTime ();
726
719
logger .debug ("Finished in-place UnitPrintManager.createSheets in {} ms" , (end - start ) / 1_000_000 );
727
720
@@ -797,8 +790,9 @@ private void performUpdateSheetContentInPlace() {
797
790
return ;
798
791
}
799
792
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 (
802
796
"Sheet structure changed during in-place update or error occurred. Performing full reset." );
803
797
regenerateAndReset (); // Use the full reset logic
804
798
return ;
0 commit comments