|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2023 Oak Ridge National Laboratory. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + *******************************************************************************/ |
| 8 | +package org.phoebus.applications.alarm; |
| 9 | + |
| 10 | +import org.phoebus.ui.javafx.ApplicationWrapper; |
| 11 | + |
| 12 | +import javafx.scene.Scene; |
| 13 | +import javafx.scene.control.Button; |
| 14 | +import javafx.scene.control.TreeItem; |
| 15 | +import javafx.scene.control.TreeView; |
| 16 | +import javafx.scene.layout.HBox; |
| 17 | +import javafx.scene.layout.Priority; |
| 18 | +import javafx.scene.layout.VBox; |
| 19 | +import javafx.stage.Stage; |
| 20 | + |
| 21 | +/** Demo of alarm tree cell update |
| 22 | + * |
| 23 | + * Demonstrates refresh issue for tree cells that are |
| 24 | + * not visible because they scrolled below the window bottom. |
| 25 | + * |
| 26 | + * Only replacing the TreeItem results in reliable tree updates. |
| 27 | + * |
| 28 | + * 1) Start this program, click the "On" and "Off" buttons, and observe how item "Two" is updated |
| 29 | + * 2) Click "Off", reduce the window height so that the items under "Top" are hidden, |
| 30 | + * click "On", increase window height to again show all items. |
| 31 | + * Would expect to see "On", but tree still shows "Off" |
| 32 | + * until the tree is collapsed and again expanded. |
| 33 | + * 3) Click "On", reduce the window height so that the items under "Top" are hidden, |
| 34 | + * click "Off", increase window height to again show all items. |
| 35 | + * Tree should indeed show "Off". |
| 36 | + * |
| 37 | + * Conclusion is that TreeItem.setValue() does not trigger updates of certain hidden items. |
| 38 | + * Basic google search suggests that the value must change, but "On" and "Off" |
| 39 | + * are different object references and they are not 'equal'. |
| 40 | + * |
| 41 | + * When instead replacing the complete TreeItem, the tree is always updated. |
| 42 | + * |
| 43 | + * Tested with JavaFX 15, 19, 20 |
| 44 | + * |
| 45 | + * @author Kay Kasemir |
| 46 | + */ |
| 47 | +public class TreeItemUpdateDemo extends ApplicationWrapper |
| 48 | +{ |
| 49 | + private TreeView<String> tree_view = new TreeView<>(); |
| 50 | + |
| 51 | + @Override |
| 52 | + public void start(final Stage stage) |
| 53 | + { |
| 54 | + final Button on = new Button("On"); |
| 55 | + on.setOnAction(event -> |
| 56 | + { // Just updating the value of TreeItem is ignored for hidden items? |
| 57 | + tree_view.getRoot().getChildren().get(1).setValue("On"); |
| 58 | + }); |
| 59 | + final Button off = new Button("Off"); |
| 60 | + off.setOnAction(event -> |
| 61 | + { // Replacing the TreeItem always "works"? |
| 62 | + // (Note this would be more work for intermediate items |
| 63 | + // that have child nodes, since child nodes need to be moved/copied) |
| 64 | + tree_view.getRoot().getChildren().set(1, new TreeItem<>("Off")); |
| 65 | + }); |
| 66 | + final HBox bottom = new HBox(on, off); |
| 67 | + on.setMaxWidth(1000); |
| 68 | + off.setMaxWidth(1000); |
| 69 | + HBox.setHgrow(on, Priority.ALWAYS); |
| 70 | + HBox.setHgrow(off, Priority.ALWAYS); |
| 71 | + |
| 72 | + final VBox root = new VBox(tree_view, bottom); |
| 73 | + VBox.setVgrow(tree_view, Priority.ALWAYS); |
| 74 | + |
| 75 | + TreeItem<String> top = new TreeItem<>("Top"); |
| 76 | + tree_view.setRoot(top); |
| 77 | + |
| 78 | + top.getChildren().add(new TreeItem<>("One")); |
| 79 | + top.getChildren().add(new TreeItem<>("Two")); |
| 80 | + top.getChildren().add(new TreeItem<>("Three")); |
| 81 | + top.setExpanded(true); |
| 82 | + |
| 83 | + final Scene scene = new Scene(root, 300, 300); |
| 84 | + stage.setScene(scene); |
| 85 | + stage.show(); |
| 86 | + } |
| 87 | + |
| 88 | + public static void main(final String[] args) |
| 89 | + { |
| 90 | + launch(TreeItemUpdateDemo.class, args); |
| 91 | + } |
| 92 | +} |
0 commit comments