Skip to content

Commit 10db74a

Browse files
committed
[feature] When opening the Query Dialog, load the list of Collections lazily and in a background thread
1 parent e2ce895 commit 10db74a

File tree

1 file changed

+69
-13
lines changed

1 file changed

+69
-13
lines changed

exist-core/src/main/java/org/exist/client/QueryDialog.java

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@
5757
import javax.swing.JToolBar;
5858
import javax.swing.SpinnerNumberModel;
5959
import javax.swing.border.BevelBorder;
60+
import javax.swing.event.PopupMenuEvent;
61+
import javax.swing.event.PopupMenuListener;
6062
import javax.xml.transform.OutputKeys;
6163

6264
import org.exist.security.PermissionDeniedException;
65+
import org.exist.util.Holder;
6366
import org.exist.xmldb.EXistXQueryService;
6467
import org.exist.xmldb.LocalCollection;
6568
import org.exist.xmldb.UserManagementService;
@@ -84,6 +87,11 @@ public class QueryDialog extends JFrame {
8487

8588
private static final long serialVersionUID = 1L;
8689

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+
8795
private InteractiveClient client;
8896
private Collection collection;
8997
private Properties properties;
@@ -334,24 +342,48 @@ private JComponent createQueryBox() {
334342
label = new JLabel(Messages.getString("QueryDialog.contextlabel"));
335343
optionsPanel.add(label);
336344

337-
final List<String> data = new ArrayList<>();
345+
final Holder<Boolean> addedLoadingIndicator = new Holder<>(false);
346+
final List<String> collectionsList = new ArrayList<>();
338347
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+
});
342375
} catch (final XMLDBException e) {
343376
ClientFrame.showErrorMessage(
344377
Messages.getString("QueryDialog.collectionretrievalerrormessage") + ".", e);
345378
}
346-
collections = new JComboBox<>(new java.util.Vector<>(data));
347-
collections.setSelectedIndex(0);
379+
348380
collections.addActionListener(e -> {
349381
final int p = collections.getSelectedIndex();
350382
final String context;
351383
if (p == -1) {
352384
context = "/db";
353385
} else {
354-
context = data.get(p);
386+
context = collectionsList.get(p);
355387
}
356388
try {
357389
collection = client.getCollection(context);
@@ -372,8 +404,8 @@ private JComponent createQueryBox() {
372404
return tabs;
373405
}
374406

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())) {
377409
collectionsList.add(root.getName());
378410
}
379411
final String[] childCollections = root.listChildCollections();
@@ -392,7 +424,7 @@ private List<String> getCollections(final Collection root, final Collection coll
392424
continue;
393425
}
394426
try {
395-
getCollections(child, collection, collectionsList);
427+
getCollections(child, currentCollection, collectionsList);
396428
} catch (Exception ee) {
397429
System.out.println("Corrupted resource/collection skipped: " + child != null ? child.getName() != null ? child.getName() : "unknown" : "unknown");
398430
continue;
@@ -470,8 +502,6 @@ private void save(String stringToSave, String fileCategory) {
470502
}
471503
}
472504

473-
private static final AtomicInteger queryThreadId = new AtomicInteger();
474-
475505
private QueryRunnable doQuery() {
476506
final String xpath = query.getText();
477507
if (xpath.length() == 0) {
@@ -480,7 +510,7 @@ private QueryRunnable doQuery() {
480510
resultDisplay.setText("");
481511

482512
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);
484514
queryThread.start();
485515
return queryRunnable;
486516
}
@@ -665,4 +695,30 @@ private void addQuery(String query) {
665695
}
666696
history.addElement(Integer.toString(history.getSize() + 1) + ". " + query);
667697
}
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+
}
668724
}

0 commit comments

Comments
 (0)