From 016166975b9497eb9a930f94bb7467de7b86fc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 14 Jan 2019 18:08:37 +0100 Subject: [PATCH] Estimate constant padding --- pmd-ui/pom.xml | 2 +- .../fxdesigner/SourceEditorController.java | 4 +- ...umbBar.java => NodeParentageCrumbBar.java} | 38 +++++++++++++------ .../pmd/util/fxdesigner/fxml/editor.fxml | 10 ++--- 4 files changed, 35 insertions(+), 19 deletions(-) rename pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/{NodeParentageBreadCrumbBar.java => NodeParentageCrumbBar.java} (79%) diff --git a/pmd-ui/pom.xml b/pmd-ui/pom.xml index 8f83d13b5c..1b97e227d0 100644 --- a/pmd-ui/pom.xml +++ b/pmd-ui/pom.xml @@ -112,7 +112,7 @@ net.sourceforge.pmd.util.fxdesigner.DesignerStarter - pmd-ui + pmd-ui-${project.version} diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java index 697c07af59..9b8f0bb303 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java @@ -45,7 +45,7 @@ import net.sourceforge.pmd.util.fxdesigner.util.codearea.HighlightLayerCodeArea; import net.sourceforge.pmd.util.fxdesigner.util.codearea.HighlightLayerCodeArea.LayerId; import net.sourceforge.pmd.util.fxdesigner.util.controls.ASTTreeCell; import net.sourceforge.pmd.util.fxdesigner.util.controls.ASTTreeItem; -import net.sourceforge.pmd.util.fxdesigner.util.controls.NodeParentageBreadCrumbBar; +import net.sourceforge.pmd.util.fxdesigner.util.controls.NodeParentageCrumbBar; import net.sourceforge.pmd.util.fxdesigner.util.controls.ToolbarTitledPane; import net.sourceforge.pmd.util.fxdesigner.util.controls.TreeViewWrapper; @@ -84,7 +84,7 @@ public class SourceEditorController extends AbstractController { @FXML private HighlightLayerCodeArea codeEditorArea; @FXML - private NodeParentageBreadCrumbBar focusNodeParentageBreadCrumbBar; + private NodeParentageCrumbBar focusNodeParentageBreadCrumbBar; private ASTManager astManager; private TreeViewWrapper treeViewWrapper; diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/NodeParentageBreadCrumbBar.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/NodeParentageCrumbBar.java similarity index 79% rename from pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/NodeParentageBreadCrumbBar.java rename to pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/NodeParentageCrumbBar.java index d3ff34b08a..2ee0081a2b 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/NodeParentageBreadCrumbBar.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/NodeParentageCrumbBar.java @@ -34,14 +34,15 @@ import javafx.util.Callback; * @author Clément Fournier * @since 7.0.0 */ -public class NodeParentageBreadCrumbBar extends BreadCrumbBar { +public class NodeParentageCrumbBar extends BreadCrumbBar { /** Special item used to truncate paths when they're too long. */ private final TreeItem ellipsisCrumb = new TreeItem<>(null); /** number of nodes currently behind the ellipsis */ private int numElidedNodes = 0; - public NodeParentageBreadCrumbBar() { + + public NodeParentageCrumbBar() { // This allows to click on a parent crumb and keep the children crumb setAutoNavigationEnabled(false); @@ -104,7 +105,11 @@ public class NodeParentageBreadCrumbBar extends BreadCrumbBar { int totalNumChar = 0; int totalNumCrumbs = 0; // the sum of children width is the actual width with overflow + // the width of this control is the max acceptable width *without* overflow double totalChildrenWidth = 0; + // constant padding around the graphic of a BreadCrumbButton + // (difference between width of a BreadCrumbButton and that of its graphic) + double constantPadding = Double.NaN; for (javafx.scene.Node button : asReversed(getChildren())) { Node n = (Node) ((TreeItem) button.getUserData()).getValue(); @@ -115,8 +120,15 @@ public class NodeParentageBreadCrumbBar extends BreadCrumbBar { // update counters totalNumChar += ((Labeled) button).getText().length(); - totalChildrenWidth += ((Region) button).getWidth(); + double childWidth = ((Region) button).getWidth(); + totalChildrenWidth += childWidth; totalNumCrumbs++; + if (Double.isNaN(constantPadding)) { + Region graphic = (Region) ((Labeled) button).getGraphic(); + if (graphic != null) { + constantPadding = childWidth - graphic.getWidth(); + } + } if (node.equals(n)) { found = true; @@ -126,7 +138,7 @@ public class NodeParentageBreadCrumbBar extends BreadCrumbBar { if (!found) { // Then we reset the deepest node. - setDeepestNode(node, getWidthEstimator(totalNumChar, totalChildrenWidth, totalNumCrumbs)); + setDeepestNode(node, getWidthEstimator(totalNumChar, totalChildrenWidth, totalNumCrumbs, constantPadding)); // set the deepest as focused Platform.runLater(() -> getChildren() @@ -151,7 +163,7 @@ public class NodeParentageBreadCrumbBar extends BreadCrumbBar { Node parent = node.jjtGetParent(); double pathLength = widthEstimator.apply(node); - final double maxPathLength = getWidth() - 150; + final double maxPathLength = getWidth() * 0.9; while (parent != null && pathLength < maxPathLength) { TreeItem newItem = new TreeItem<>(parent); @@ -176,13 +188,17 @@ public class NodeParentageBreadCrumbBar extends BreadCrumbBar { } - private static Function getWidthEstimator(int totalNumDisplayedChars, double totalChildrenWidth, int totalNumCrumbs) { - double pxByChar = totalNumDisplayedChars == 0 - ? 5.0 // we have no data, too bad - // there's a ~19px constant padding per button (on my machine) - : (totalChildrenWidth - 19.0 * totalNumCrumbs) / totalNumDisplayedChars; + private Function getWidthEstimator(int totalNumDisplayedChars, double totalChildrenWidth, int totalNumCrumbs, double constantPadding) { - return node -> node.getXPathNodeName().length() * pxByChar + 19.0; + double safeConstantPadding = Double.isNaN(constantPadding) + ? 19 // that's the value on my machine + : constantPadding; + + double thisPxByChar = totalNumDisplayedChars == 0 + ? 5.0 // we have no data, too bad + : (totalChildrenWidth - safeConstantPadding * totalNumCrumbs) / totalNumDisplayedChars; + + return node -> node.getXPathNodeName().length() * thisPxByChar + safeConstantPadding; } } diff --git a/pmd-ui/src/main/resources/net/sourceforge/pmd/util/fxdesigner/fxml/editor.fxml b/pmd-ui/src/main/resources/net/sourceforge/pmd/util/fxdesigner/fxml/editor.fxml index b93051d25b..53ef2cbfd1 100644 --- a/pmd-ui/src/main/resources/net/sourceforge/pmd/util/fxdesigner/fxml/editor.fxml +++ b/pmd-ui/src/main/resources/net/sourceforge/pmd/util/fxdesigner/fxml/editor.fxml @@ -5,7 +5,7 @@ - + @@ -57,7 +57,7 @@ -