From 661692744579de89c52269eff00a13983f99fb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 23 Feb 2019 11:49:53 +0100 Subject: [PATCH] Add button to add property --- .../util/fxdesigner/XPathPanelController.java | 43 +++++++++++++++++-- .../util/controls/PropertyTableView.java | 10 +++-- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java index 2c43b057e7..3fac3825ae 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java @@ -15,6 +15,8 @@ import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.function.Supplier; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; @@ -58,6 +60,7 @@ import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ContextMenu; +import javafx.scene.control.Hyperlink; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.control.MenuButton; @@ -67,6 +70,10 @@ import javafx.scene.control.TextArea; import javafx.scene.control.ToggleGroup; import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; import javafx.stage.Modality; import javafx.stage.Stage; import javafx.stage.StageStyle; @@ -365,14 +372,18 @@ public class XPathPanelController extends AbstractController results, String emptyResultsPlaceholder) { - Label emptyLabel = xpathError || otherError - ? new Label(emptyResultsPlaceholder, new FontIcon("fas-exclamation-triangle")) - : new Label(emptyResultsPlaceholder); + javafx.scene.Node emptyLabel = xpathError || otherError + ? getErrorPlaceholder(emptyResultsPlaceholder) + : new Label(emptyResultsPlaceholder); xpathResultListView.setPlaceholder(emptyLabel); @@ -383,5 +394,31 @@ public class XPathPanelController extends AbstractController { + Hyperlink hyperlink = new Hyperlink("Add property"); + hyperlink.setOnAction(e -> propertyTableView.onAddPropertyClicked(name)); + return new VBox(new Text("Undeclared property in XPath expression: $" + name), hyperlink); + } + ).orElse(new VBox(new Text(message))); + HBox hbox = new HBox(new FontIcon("fas-exclamation-triangle"), vbox); + hbox.setSpacing(10); + hbox.maxHeightProperty().bind(hbox.heightProperty()); + hbox.maxWidthProperty().bind(vbox.widthProperty()); + hbox.setFillHeight(false); + vbox.setFillWidth(false); + return hbox; + } + + + private Optional getMissingPropertyName(String errorMessage) { + Pattern nameExtractor = XPathRuleQuery.XPATH_1_0.equals(getXpathVersion()) + ? JAXEN_MISSING_PROPERTY_EXTRACTOR + : SAXON_MISSING_PROPERTY_EXTRACTOR; + + Matcher matcher = nameExtractor.matcher(errorMessage); + return matcher.matches() ? Optional.of(matcher.group(1)) : Optional.empty(); + } } diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/PropertyTableView.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/PropertyTableView.java index e8af15a954..cb2c69ed79 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/PropertyTableView.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/PropertyTableView.java @@ -100,14 +100,14 @@ public class PropertyTableView extends TableView { }); MenuItem addItem = new MenuItem("Add property..."); - addItem.setOnAction(e -> onAddPropertyClicked()); + addItem.setOnAction(e -> onAddPropertyClicked("name")); ContextMenu fullMenu = new ContextMenu(); fullMenu.getItems().addAll(editItem, removeItem, new SeparatorMenuItem(), addItem); // Reduced context menu, for when there are no properties or none is selected MenuItem addItem2 = new MenuItem("Add property..."); - addItem2.setOnAction(e -> onAddPropertyClicked()); + addItem2.setOnAction(e -> onAddPropertyClicked("name")); ContextMenu smallMenu = new ContextMenu(); smallMenu.getItems().add(addItem2); @@ -129,8 +129,12 @@ public class PropertyTableView extends TableView { } - private void onAddPropertyClicked() { + /** + * Call this to pop the "new property" popup. + */ + public void onAddPropertyClicked(String name) { PropertyDescriptorSpec spec = new PropertyDescriptorSpec(); + spec.setName(name); this.getItems().add(spec); popEditPropertyDialog(spec); }