57
57
import javax .swing .JToolBar ;
58
58
import javax .swing .SpinnerNumberModel ;
59
59
import javax .swing .border .BevelBorder ;
60
+ import javax .swing .event .PopupMenuEvent ;
61
+ import javax .swing .event .PopupMenuListener ;
60
62
import javax .xml .transform .OutputKeys ;
61
63
62
64
import org .exist .security .PermissionDeniedException ;
65
+ import org .exist .util .Holder ;
63
66
import org .exist .xmldb .EXistXQueryService ;
64
67
import org .exist .xmldb .LocalCollection ;
65
68
import org .exist .xmldb .UserManagementService ;
@@ -84,6 +87,11 @@ public class QueryDialog extends JFrame {
84
87
85
88
private static final long serialVersionUID = 1L ;
86
89
90
+ private static final AtomicInteger QUERY_THREAD_ID = new AtomicInteger ();
91
+ private static final AtomicInteger GET_COLLECTIONS_THREAD_ID = new AtomicInteger ();
92
+
93
+ private static final String LOADING_INDICATOR = "Loading..." ;
94
+
87
95
private InteractiveClient client ;
88
96
private Collection collection ;
89
97
private Properties properties ;
@@ -334,24 +342,48 @@ private JComponent createQueryBox() {
334
342
label = new JLabel (Messages .getString ("QueryDialog.contextlabel" ));
335
343
optionsPanel .add (label );
336
344
337
- final List <String > data = new ArrayList <>();
345
+ final Holder <Boolean > addedLoadingIndicator = new Holder <>(false );
346
+ final List <String > collectionsList = new ArrayList <>();
338
347
try {
339
- final Collection root = client .getCollection (XmldbURI .ROOT_COLLECTION );
340
- data .add (collection .getName ());
341
- getCollections (root , collection , data );
348
+ final String currentCollectionName = collection .getName ();
349
+
350
+ collectionsList .add (currentCollectionName );
351
+
352
+ collections = new JComboBox <>(new java .util .Vector <>(collectionsList ));
353
+
354
+ collections .addPopupMenuListener (new PopupMenuListener () {
355
+ @ Override
356
+ public void popupMenuWillBecomeVisible (final PopupMenuEvent e ) {
357
+ if (!addedLoadingIndicator .value ) {
358
+ collections .addItem (LOADING_INDICATOR );
359
+ addedLoadingIndicator .value = true ;
360
+
361
+ final GetCollectionsListRunnable getCollectionsListRunnable = new GetCollectionsListRunnable (currentCollectionName , collections );
362
+ final Thread getCollectionsListThread = client .newClientThread ("get-collections-list-" + GET_COLLECTIONS_THREAD_ID .getAndIncrement (), getCollectionsListRunnable );
363
+ getCollectionsListThread .start ();
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,8 +404,8 @@ 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 ())) {
407
+ private static List <String > getCollections (final Collection root , final String currentCollection , final List <String > collectionsList ) throws XMLDBException {
408
+ if (!currentCollection .equals (root .getName ())) {
377
409
collectionsList .add (root .getName ());
378
410
}
379
411
final String [] childCollections = root .listChildCollections ();
@@ -392,7 +424,7 @@ private List<String> getCollections(final Collection root, final Collection coll
392
424
continue ;
393
425
}
394
426
try {
395
- getCollections (child , collection , collectionsList );
427
+ getCollections (child , currentCollection , collectionsList );
396
428
} catch (Exception ee ) {
397
429
System .out .println ("Corrupted resource/collection skipped: " + child != null ? child .getName () != null ? child .getName () : "unknown" : "unknown" );
398
430
continue ;
@@ -470,8 +502,6 @@ private void save(String stringToSave, String fileCategory) {
470
502
}
471
503
}
472
504
473
- private static final AtomicInteger queryThreadId = new AtomicInteger ();
474
-
475
505
private QueryRunnable doQuery () {
476
506
final String xpath = query .getText ();
477
507
if (xpath .length () == 0 ) {
@@ -480,7 +510,7 @@ private QueryRunnable doQuery() {
480
510
resultDisplay .setText ("" );
481
511
482
512
final QueryRunnable queryRunnable = new QueryRunnable (xpath );
483
- final Thread queryThread = client .newClientThread ("query-" + queryThreadId .getAndIncrement (), queryRunnable );
513
+ final Thread queryThread = client .newClientThread ("query-" + QUERY_THREAD_ID .getAndIncrement (), queryRunnable );
484
514
queryThread .start ();
485
515
return queryRunnable ;
486
516
}
@@ -665,4 +695,30 @@ private void addQuery(String query) {
665
695
}
666
696
history .addElement (Integer .toString (history .getSize () + 1 ) + ". " + query );
667
697
}
698
+
699
+ private class GetCollectionsListRunnable implements Runnable {
700
+ private final String currentCollection ;
701
+ private final JComboBox <String > collections ;
702
+
703
+ public GetCollectionsListRunnable (final String currentCollection , final JComboBox <String > collections ) {
704
+ this .currentCollection = currentCollection ;
705
+ this .collections = collections ;
706
+ }
707
+
708
+ @ Override
709
+ public void run () {
710
+ final List <String > collectionsList = new ArrayList <>();
711
+ collectionsList .add (currentCollection );
712
+
713
+ try {
714
+ final Collection root = client .getCollection (XmldbURI .ROOT_COLLECTION );
715
+ getCollections (root , currentCollection , collectionsList );
716
+
717
+ collections .setModel (new DefaultComboBoxModel (new java .util .Vector <>(collectionsList )));
718
+ } catch (final XMLDBException e ) {
719
+ ClientFrame .showErrorMessage (
720
+ Messages .getString ("QueryDialog.collectionretrievalerrormessage" ) + "." , e );
721
+ }
722
+ }
723
+ }
668
724
}
0 commit comments