56
56
import javax .swing .JTextField ;
57
57
import javax .swing .JToolBar ;
58
58
import javax .swing .SpinnerNumberModel ;
59
+ import javax .swing .SwingWorker ;
59
60
import javax .swing .border .BevelBorder ;
61
+ import javax .swing .event .PopupMenuEvent ;
62
+ import javax .swing .event .PopupMenuListener ;
60
63
import javax .xml .transform .OutputKeys ;
61
64
62
65
import org .exist .security .PermissionDeniedException ;
66
+ import org .exist .util .Holder ;
63
67
import org .exist .xmldb .EXistXQueryService ;
64
68
import org .exist .xmldb .LocalCollection ;
65
69
import org .exist .xmldb .UserManagementService ;
@@ -84,6 +88,11 @@ public class QueryDialog extends JFrame {
84
88
85
89
private static final long serialVersionUID = 1L ;
86
90
91
+ private static final AtomicInteger QUERY_THREAD_ID = new AtomicInteger ();
92
+ private static final AtomicInteger GET_COLLECTIONS_THREAD_ID = new AtomicInteger ();
93
+
94
+ private static final String LOADING_INDICATOR = "Loading..." ;
95
+
87
96
private InteractiveClient client ;
88
97
private Collection collection ;
89
98
private Properties properties ;
@@ -334,24 +343,47 @@ private JComponent createQueryBox() {
334
343
label = new JLabel (Messages .getString ("QueryDialog.contextlabel" ));
335
344
optionsPanel .add (label );
336
345
337
- final List <String > data = new ArrayList <>();
346
+ final Holder <Boolean > addedLoadingIndicator = new Holder <>(false );
347
+ final List <String > collectionsList = new ArrayList <>();
338
348
try {
339
- final Collection root = client .getCollection (XmldbURI .ROOT_COLLECTION );
340
- data .add (collection .getName ());
341
- getCollections (root , collection , data );
349
+ final String currentCollectionName = collection .getName ();
350
+
351
+ collectionsList .add (currentCollectionName );
352
+
353
+ collections = new JComboBox <>(new java .util .Vector <>(collectionsList ));
354
+
355
+ collections .addPopupMenuListener (new PopupMenuListener () {
356
+ @ Override
357
+ public void popupMenuWillBecomeVisible (final PopupMenuEvent e ) {
358
+ if (!addedLoadingIndicator .value ) {
359
+ collections .addItem (LOADING_INDICATOR );
360
+ addedLoadingIndicator .value = true ;
361
+
362
+ final GetCollectionsListSwingWorker getCollectionsListSwingWorker = new GetCollectionsListSwingWorker (currentCollectionName );
363
+ getCollectionsListSwingWorker .execute ();
364
+ }
365
+ }
366
+
367
+ @ Override
368
+ public void popupMenuWillBecomeInvisible (final PopupMenuEvent e ) {
369
+ }
370
+
371
+ @ Override
372
+ public void popupMenuCanceled (final PopupMenuEvent e ) {
373
+ }
374
+ });
342
375
} catch (final XMLDBException e ) {
343
376
ClientFrame .showErrorMessage (
344
377
Messages .getString ("QueryDialog.collectionretrievalerrormessage" ) + "." , e );
345
378
}
346
- collections = new JComboBox <>(new java .util .Vector <>(data ));
347
- collections .setSelectedIndex (0 );
379
+
348
380
collections .addActionListener (e -> {
349
381
final int p = collections .getSelectedIndex ();
350
382
final String context ;
351
383
if (p == -1 ) {
352
384
context = "/db" ;
353
385
} else {
354
- context = data .get (p );
386
+ context = collectionsList .get (p );
355
387
}
356
388
try {
357
389
collection = client .getCollection (context );
@@ -372,35 +404,6 @@ private JComponent createQueryBox() {
372
404
return tabs ;
373
405
}
374
406
375
- private List <String > getCollections (final Collection root , final Collection collection , final List <String > collectionsList ) throws XMLDBException {
376
- if (!collection .getName ().equals (root .getName ())) {
377
- collectionsList .add (root .getName ());
378
- }
379
- final String [] childCollections = root .listChildCollections ();
380
- Collection child = null ;
381
- for (String childCollection : childCollections ) {
382
- try {
383
- child = root .getChildCollection (childCollection );
384
- } catch (final XMLDBException xmldbe ) {
385
- if (xmldbe .getCause () instanceof PermissionDeniedException ) {
386
- continue ;
387
- } else {
388
- throw xmldbe ;
389
- }
390
- } catch (Exception npe ) {
391
- System .out .println ("Corrupted resource/collection skipped: " + child != null ? child .getName () != null ? child .getName () : "unknown" : "unknown" );
392
- continue ;
393
- }
394
- try {
395
- getCollections (child , collection , collectionsList );
396
- } catch (Exception ee ) {
397
- System .out .println ("Corrupted resource/collection skipped: " + child != null ? child .getName () != null ? child .getName () : "unknown" : "unknown" );
398
- continue ;
399
- }
400
- }
401
- return collectionsList ;
402
- }
403
-
404
407
private void open () {
405
408
final String workDir = properties .getProperty ("working-dir" , System .getProperty ("user.dir" ));
406
409
final JFileChooser chooser = new JFileChooser ();
@@ -470,8 +473,6 @@ private void save(String stringToSave, String fileCategory) {
470
473
}
471
474
}
472
475
473
- private static final AtomicInteger queryThreadId = new AtomicInteger ();
474
-
475
476
private QueryRunnable doQuery () {
476
477
final String xpath = query .getText ();
477
478
if (xpath .length () == 0 ) {
@@ -480,7 +481,7 @@ private QueryRunnable doQuery() {
480
481
resultDisplay .setText ("" );
481
482
482
483
final QueryRunnable queryRunnable = new QueryRunnable (xpath );
483
- final Thread queryThread = client .newClientThread ("query-" + queryThreadId .getAndIncrement (), queryRunnable );
484
+ final Thread queryThread = client .newClientThread ("query-" + QUERY_THREAD_ID .getAndIncrement (), queryRunnable );
484
485
queryThread .start ();
485
486
return queryRunnable ;
486
487
}
@@ -665,4 +666,72 @@ private void addQuery(String query) {
665
666
}
666
667
history .addElement (Integer .toString (history .getSize () + 1 ) + ". " + query );
667
668
}
669
+
670
+ private class GetCollectionsListSwingWorker extends SwingWorker <Void , String > {
671
+ private final String currentCollectionName ;
672
+
673
+ public GetCollectionsListSwingWorker (final String currentCollectionName ) {
674
+ this .currentCollectionName = currentCollectionName ;
675
+ }
676
+
677
+ @ Override
678
+ public Void doInBackground () {
679
+ try {
680
+ final Collection root = client .getCollection (XmldbURI .ROOT_COLLECTION );
681
+ getCollections (root , currentCollectionName );
682
+ } catch (final XMLDBException e ) {
683
+ ClientFrame .showErrorMessage (
684
+ Messages .getString ("QueryDialog.collectionretrievalerrormessage" ) + "." , e );
685
+ }
686
+
687
+ return null ;
688
+ }
689
+
690
+ @ Override
691
+ protected void process (final List <String > collectionNames ) {
692
+ final int idxLast = collections .getItemCount () - 1 ;
693
+ final String lastItem = collections .getItemAt (idxLast );
694
+ if (LOADING_INDICATOR .equals (lastItem )) {
695
+ collections .removeItemAt (idxLast );
696
+ }
697
+
698
+ for (final String collectionName : collectionNames ) {
699
+ collections .addItem (collectionName );
700
+ }
701
+ }
702
+
703
+ private void getCollections (final Collection root , final String currentCollection ) throws XMLDBException {
704
+ if (isCancelled ()) {
705
+ return ;
706
+ }
707
+
708
+ if (!currentCollection .equals (root .getName ())) {
709
+ publish (root .getName ());
710
+ }
711
+
712
+ final String [] childCollections = root .listChildCollections ();
713
+ Collection child = null ;
714
+ for (int i = 0 ; i < childCollections .length ; i ++) {
715
+ try {
716
+ child = root .getChildCollection (childCollections [i ]);
717
+ } catch (final XMLDBException xmldbe ) {
718
+ if (xmldbe .getCause () instanceof PermissionDeniedException ) {
719
+ continue ;
720
+ } else {
721
+ throw xmldbe ;
722
+ }
723
+ } catch (Exception npe ) {
724
+ System .out .println ("Corrupted resource/collection skipped: " + child != null ? child .getName () != null ? child .getName () : "unknown" : "unknown" );
725
+ continue ;
726
+ }
727
+
728
+ try {
729
+ getCollections (child , currentCollection );
730
+ } catch (Exception ee ) {
731
+ System .out .println ("Corrupted resource/collection skipped: " + child != null ? child .getName () != null ? child .getName () : "unknown" : "unknown" );
732
+ continue ;
733
+ }
734
+ }
735
+ }
736
+ }
668
737
}
0 commit comments