|
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;
|
60 | 61 | import javax.swing.event.PopupMenuEvent;
|
61 | 62 | import javax.swing.event.PopupMenuListener;
|
@@ -358,9 +359,8 @@ public void popupMenuWillBecomeVisible(final PopupMenuEvent e) {
|
358 | 359 | collections.addItem(LOADING_INDICATOR);
|
359 | 360 | addedLoadingIndicator.value = true;
|
360 | 361 |
|
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(); |
364 | 364 | }
|
365 | 365 | }
|
366 | 366 |
|
@@ -404,35 +404,6 @@ public void popupMenuCanceled(final PopupMenuEvent e) {
|
404 | 404 | return tabs;
|
405 | 405 | }
|
406 | 406 |
|
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 |
| - |
436 | 407 | private void open() {
|
437 | 408 | final String workDir = properties.getProperty("working-dir", System.getProperty("user.dir"));
|
438 | 409 | final JFileChooser chooser = new JFileChooser();
|
@@ -696,29 +667,71 @@ private void addQuery(String query) {
|
696 | 667 | history.addElement(Integer.toString(history.getSize() + 1) + ". " + query);
|
697 | 668 | }
|
698 | 669 |
|
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; |
702 | 672 |
|
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; |
706 | 675 | }
|
707 | 676 |
|
708 | 677 | @Override
|
709 |
| - public void run() { |
710 |
| - final List<String> collectionsList = new ArrayList<>(); |
711 |
| - collectionsList.add(currentCollection); |
712 |
| - |
| 678 | + public Void doInBackground() { |
713 | 679 | try {
|
714 | 680 | 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); |
718 | 682 | } catch (final XMLDBException e) {
|
719 | 683 | ClientFrame.showErrorMessage(
|
720 | 684 | Messages.getString("QueryDialog.collectionretrievalerrormessage") + ".", e);
|
721 | 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 | + } |
722 | 735 | }
|
723 | 736 | }
|
724 | 737 | }
|
0 commit comments