Skip to content

Commit e3c834e

Browse files
committed
[bugfix] Use Swing Worker concurrency model rather than threads directly
1 parent 10db74a commit e3c834e

File tree

1 file changed

+58
-45
lines changed

1 file changed

+58
-45
lines changed

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

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import javax.swing.JTextField;
5757
import javax.swing.JToolBar;
5858
import javax.swing.SpinnerNumberModel;
59+
import javax.swing.SwingWorker;
5960
import javax.swing.border.BevelBorder;
6061
import javax.swing.event.PopupMenuEvent;
6162
import javax.swing.event.PopupMenuListener;
@@ -358,9 +359,8 @@ public void popupMenuWillBecomeVisible(final PopupMenuEvent e) {
358359
collections.addItem(LOADING_INDICATOR);
359360
addedLoadingIndicator.value = true;
360361

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();
362+
final GetCollectionsListSwingWorker getCollectionsListSwingWorker = new GetCollectionsListSwingWorker(currentCollectionName);
363+
getCollectionsListSwingWorker.execute();
364364
}
365365
}
366366

@@ -404,35 +404,6 @@ public void popupMenuCanceled(final PopupMenuEvent e) {
404404
return tabs;
405405
}
406406

407-
private static List<String> getCollections(final Collection root, final String currentCollection, final List<String> collectionsList) throws XMLDBException {
408-
if (!currentCollection.equals(root.getName())) {
409-
collectionsList.add(root.getName());
410-
}
411-
final String[] childCollections = root.listChildCollections();
412-
Collection child = null;
413-
for (String childCollection : childCollections) {
414-
try {
415-
child = root.getChildCollection(childCollection);
416-
} catch (final XMLDBException xmldbe) {
417-
if (xmldbe.getCause() instanceof PermissionDeniedException) {
418-
continue;
419-
} else {
420-
throw xmldbe;
421-
}
422-
} catch (Exception npe) {
423-
System.out.println("Corrupted resource/collection skipped: " + child != null ? child.getName() != null ? child.getName() : "unknown" : "unknown");
424-
continue;
425-
}
426-
try {
427-
getCollections(child, currentCollection, collectionsList);
428-
} catch (Exception ee) {
429-
System.out.println("Corrupted resource/collection skipped: " + child != null ? child.getName() != null ? child.getName() : "unknown" : "unknown");
430-
continue;
431-
}
432-
}
433-
return collectionsList;
434-
}
435-
436407
private void open() {
437408
final String workDir = properties.getProperty("working-dir", System.getProperty("user.dir"));
438409
final JFileChooser chooser = new JFileChooser();
@@ -696,29 +667,71 @@ private void addQuery(String query) {
696667
history.addElement(Integer.toString(history.getSize() + 1) + ". " + query);
697668
}
698669

699-
private class GetCollectionsListRunnable implements Runnable {
700-
private final String currentCollection;
701-
private final JComboBox<String> collections;
670+
private class GetCollectionsListSwingWorker extends SwingWorker<Void, String> {
671+
private final String currentCollectionName;
702672

703-
public GetCollectionsListRunnable(final String currentCollection, final JComboBox<String> collections) {
704-
this.currentCollection = currentCollection;
705-
this.collections = collections;
673+
public GetCollectionsListSwingWorker(final String currentCollectionName) {
674+
this.currentCollectionName = currentCollectionName;
706675
}
707676

708677
@Override
709-
public void run() {
710-
final List<String> collectionsList = new ArrayList<>();
711-
collectionsList.add(currentCollection);
712-
678+
public Void doInBackground() {
713679
try {
714680
final Collection root = client.getCollection(XmldbURI.ROOT_COLLECTION);
715-
getCollections(root, currentCollection, collectionsList);
716-
717-
collections.setModel(new DefaultComboBoxModel(new java.util.Vector<>(collectionsList)));
681+
getCollections(root, currentCollectionName);
718682
} catch (final XMLDBException e) {
719683
ClientFrame.showErrorMessage(
720684
Messages.getString("QueryDialog.collectionretrievalerrormessage") + ".", e);
721685
}
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+
}
722735
}
723736
}
724737
}

0 commit comments

Comments
 (0)