|
83 | 83 | import javax.swing.KeyStroke; |
84 | 84 | import javax.swing.ScrollPaneLayout; |
85 | 85 | import javax.swing.SwingUtilities; |
| 86 | +import javax.swing.Timer; |
86 | 87 | import javax.swing.ToolTipManager; |
87 | 88 | import javax.swing.TransferHandler; |
88 | 89 | import javax.swing.UIManager; |
@@ -915,73 +916,61 @@ public void setAutoWaitCursor(boolean enable) { |
915 | 916 | autoWaitCursor = enable; |
916 | 917 | } |
917 | 918 |
|
918 | | - // |
919 | | - // showing and removing the wait cursor |
920 | | - // |
921 | | - private void showWaitCursor (boolean show) { |
922 | | - JRootPane rPane = getRootPane(); |
923 | | - if (rPane == null) { |
| 919 | + private void maybeShowWaitCursor(Node node) { |
| 920 | + if (node == null || !autoWaitCursor) { |
924 | 921 | return; |
925 | 922 | } |
926 | | - |
927 | | - if (SwingUtilities.isEventDispatchThread()) { |
928 | | - doShowWaitCursor(rPane.getGlassPane(), show); |
929 | | - } else { |
930 | | - SwingUtilities.invokeLater(new CursorR(rPane.getGlassPane(), show)); |
931 | | - } |
| 923 | + // not sure whenter throughput 1 is OK... |
| 924 | + ViewUtil.uiProcessor().post(() -> { |
| 925 | + try (DelayedWaitCursor cursor = new DelayedWaitCursor(getRootPane())) { |
| 926 | + cursor.enable(); |
| 927 | + node.getChildren().getNodesCount(true); // blocks until expanded |
| 928 | + } catch (Exception e) { |
| 929 | + LOG.log(Level.WARNING, "can't determine node count", e); |
| 930 | + } |
| 931 | + }); |
932 | 932 | } |
933 | 933 |
|
934 | | - private static void doShowWaitCursor (Component glassPane, boolean show) { |
935 | | - if (show) { |
936 | | - glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
937 | | - glassPane.setVisible(true); |
938 | | - } else { |
939 | | - glassPane.setVisible(false); |
940 | | - glassPane.setCursor(null); |
941 | | - } |
942 | | - } |
| 934 | + /// Shows the wait cursor after an initial delay. |
| 935 | + /// Can be used in ARM-blocks. |
| 936 | + private static class DelayedWaitCursor implements AutoCloseable { |
943 | 937 |
|
944 | | - private static class CursorR implements Runnable { |
945 | | - private Component glassPane; |
946 | | - private boolean show; |
| 938 | + private static final int DELAY = 200; |
947 | 939 |
|
948 | | - private CursorR(Component cont, boolean show) { |
949 | | - this.glassPane = cont; |
950 | | - this.show = show; |
951 | | - } |
| 940 | + private final JRootPane root; |
| 941 | + private final Timer timer; |
952 | 942 |
|
953 | | - @Override |
954 | | - public void run() { |
955 | | - doShowWaitCursor(glassPane, show); |
| 943 | + private DelayedWaitCursor(JRootPane root) { |
| 944 | + this.root = root; |
| 945 | + timer = new Timer(DELAY, (e) -> { |
| 946 | + if (root != null) { |
| 947 | + root.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
| 948 | + root.getGlassPane().setVisible(true); |
| 949 | + } |
| 950 | + }); |
| 951 | + timer.setRepeats(false); |
956 | 952 | } |
957 | | - } |
958 | 953 |
|
959 | | - private void prepareWaitCursor(final Node node) { |
960 | | - // check type of node |
961 | | - if (node == null || !autoWaitCursor) { |
962 | | - return; |
| 954 | + private void enable() { |
| 955 | + timer.start(); |
963 | 956 | } |
964 | 957 |
|
965 | | - showWaitCursor(true); |
966 | | - // not sure whenter throughput 1 is OK... |
967 | | - ViewUtil.uiProcessor().post(new Runnable() { |
968 | | - @Override |
969 | | - public void run() { |
970 | | - try { |
971 | | - node.getChildren().getNodesCount(true); |
972 | | - } catch (Exception e) { |
973 | | - // log a exception |
974 | | - LOG.log(Level.WARNING, null, e); |
975 | | - } finally { |
976 | | - // show normal cursor above all |
977 | | - showWaitCursor(false); |
| 958 | + private void disable() { |
| 959 | + timer.stop(); |
| 960 | + SwingUtilities.invokeLater(() -> { |
| 961 | + if (root != null) { |
| 962 | + root.getGlassPane().setVisible(false); |
| 963 | + root.getGlassPane().setCursor(null); |
978 | 964 | } |
979 | | - } |
980 | | - }); |
| 965 | + }); |
| 966 | + } |
| 967 | + |
| 968 | + @Override |
| 969 | + public void close() { |
| 970 | + disable(); |
| 971 | + } |
981 | 972 | } |
982 | | - |
983 | | - |
984 | | - |
| 973 | + |
985 | 974 | /** Synchronize the selected nodes from the manager of this Explorer. |
986 | 975 | * The default implementation does nothing. |
987 | 976 | */ |
@@ -1499,7 +1488,7 @@ public void treeWillExpand(TreeExpansionEvent event) |
1499 | 1488 | throws ExpandVetoException { |
1500 | 1489 | // prepare wait cursor and optionally show it |
1501 | 1490 | TreePath path = event.getPath(); |
1502 | | - prepareWaitCursor(DragDropUtilities.secureFindNode(path.getLastPathComponent())); |
| 1491 | + maybeShowWaitCursor(DragDropUtilities.secureFindNode(path.getLastPathComponent())); |
1503 | 1492 | } |
1504 | 1493 | } |
1505 | 1494 | // end of TreePropertyListener |
|
0 commit comments