Skip to content

Commit a58e0eb

Browse files
mbienlkishalmi
andcommitted
Implement wait cursor delay for TreeView.
- initial delay for wait cursor activation on tree node expansion - AutoClosable for convenience reasons, in case this is one day extracted into a utility Co-authored-by: Laszlo Kishalmi <laszlo.kishalmi@gmail.com>
1 parent bb4a594 commit a58e0eb

1 file changed

Lines changed: 44 additions & 55 deletions

File tree

  • platform/openide.explorer/src/org/openide/explorer/view

platform/openide.explorer/src/org/openide/explorer/view/TreeView.java

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import javax.swing.KeyStroke;
8484
import javax.swing.ScrollPaneLayout;
8585
import javax.swing.SwingUtilities;
86+
import javax.swing.Timer;
8687
import javax.swing.ToolTipManager;
8788
import javax.swing.TransferHandler;
8889
import javax.swing.UIManager;
@@ -915,73 +916,61 @@ public void setAutoWaitCursor(boolean enable) {
915916
autoWaitCursor = enable;
916917
}
917918

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) {
924921
return;
925922
}
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+
});
932932
}
933933

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 {
943937

944-
private static class CursorR implements Runnable {
945-
private Component glassPane;
946-
private boolean show;
938+
private static final int DELAY = 200;
947939

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;
952942

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);
956952
}
957-
}
958953

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();
963956
}
964957

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);
978964
}
979-
}
980-
});
965+
});
966+
}
967+
968+
@Override
969+
public void close() {
970+
disable();
971+
}
981972
}
982-
983-
984-
973+
985974
/** Synchronize the selected nodes from the manager of this Explorer.
986975
* The default implementation does nothing.
987976
*/
@@ -1499,7 +1488,7 @@ public void treeWillExpand(TreeExpansionEvent event)
14991488
throws ExpandVetoException {
15001489
// prepare wait cursor and optionally show it
15011490
TreePath path = event.getPath();
1502-
prepareWaitCursor(DragDropUtilities.secureFindNode(path.getLastPathComponent()));
1491+
maybeShowWaitCursor(DragDropUtilities.secureFindNode(path.getLastPathComponent()));
15031492
}
15041493
}
15051494
// end of TreePropertyListener

0 commit comments

Comments
 (0)