Fix clicking on already selected tree item

The editor now scrolls back to the clicked node
This commit is contained in:
Clément Fournier
2018-03-30 12:48:09 +02:00
parent 5e819c5826
commit 8c2cc4f792
5 changed files with 25 additions and 23 deletions

View File

@@ -244,7 +244,7 @@ public class MainDesignerController implements Initializable, SettingsOwner {
public void onNameDeclarationSelected(NameDeclaration declaration) {
sourceEditorController.clearNodeHighlight();
Platform.runLater(() -> onNodeItemSelected(declaration.getNode()));
List<NameOccurrence> occ = declaration.getNode().getScope().getDeclarations().get(declaration);
if (occ != null) {
@@ -253,7 +253,6 @@ public class MainDesignerController implements Initializable, SettingsOwner {
.collect(Collectors.toList()));
}
sourceEditorController.highlightNodePrimary(declaration.getNode());
}

View File

@@ -28,6 +28,7 @@ import net.sourceforge.pmd.util.fxdesigner.util.beans.SettingsPersistenceUtil.Pe
import net.sourceforge.pmd.util.fxdesigner.util.codearea.AvailableSyntaxHighlighters;
import net.sourceforge.pmd.util.fxdesigner.util.codearea.CustomCodeArea;
import net.sourceforge.pmd.util.fxdesigner.util.codearea.SyntaxHighlighter;
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.TreeViewWrapper;
@@ -72,6 +73,8 @@ public class SourceEditorController implements Initializable, SettingsOwner {
treeViewWrapper = new TreeViewWrapper<>(astTreeView);
astTreeView.setCellFactory(treeView -> new ASTTreeCell(parent));
languageVersionProperty().values()
.filterMap(Objects::nonNull, LanguageVersion::getLanguage)
.distinct()

View File

@@ -5,10 +5,11 @@
package net.sourceforge.pmd.util.fxdesigner.util.controls;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.util.fxdesigner.MainDesignerController;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeView;
import javafx.util.Callback;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
/**
@@ -19,6 +20,14 @@ import javafx.util.Callback;
*/
public class ASTTreeCell extends TreeCell<Node> {
private final MainDesignerController controller;
public ASTTreeCell(MainDesignerController controller) {
this.controller = controller;
}
@Override
protected void updateItem(Node item, boolean empty) {
super.updateItem(item, empty);
@@ -26,13 +35,19 @@ public class ASTTreeCell extends TreeCell<Node> {
if (empty || item == null) {
setText(null);
setGraphic(null);
return;
} else {
setText(item.toString() + (item.getImage() == null ? "" : " \"" + item.getImage() + "\""));
}
}
// Reclicking the selected node in the ast will scroll back to the node in the editor
this.addEventHandler(MouseEvent.MOUSE_CLICKED, t -> {
if (t.getButton() == MouseButton.PRIMARY
&& getTreeView().getSelectionModel().getSelectedItem().getValue() == item) {
controller.onNodeItemSelected(item);
t.consume();
}
});
public static Callback<TreeView<Node>, ASTTreeCell> callback() {
return p -> new ASTTreeCell();
}
}

View File

@@ -13,8 +13,6 @@ import net.sourceforge.pmd.lang.symboltable.Scope;
import net.sourceforge.pmd.util.fxdesigner.MainDesignerController;
import javafx.scene.control.TreeCell;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
/**
@@ -43,15 +41,6 @@ public class ScopeHierarchyTreeCell extends TreeCell<Object> {
setText(item instanceof Scope ? getTextForScope((Scope) item)
: getTextForDeclaration((NameDeclaration) item));
}
this.addEventHandler(MouseEvent.MOUSE_CLICKED, t -> {
if (t.getButton() == MouseButton.PRIMARY
&& getTreeView().getSelectionModel().getSelectedItem() == item) {
if (item instanceof NameDeclaration) {
controller.onNodeItemSelected(((NameDeclaration) item).getNode());
}
}
});
}

View File

@@ -38,11 +38,7 @@
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<TreeView fx:id="astTreeView" prefHeight="200.0" prefWidth="200.0" styleClass="secondary-panel" BorderPane.alignment="CENTER">
<cellFactory>
<ASTTreeCell fx:factory="callback" />
</cellFactory>
</TreeView>
<TreeView fx:id="astTreeView" prefHeight="200.0" prefWidth="200.0" styleClass="secondary-panel" BorderPane.alignment="CENTER"/>
</center>
<top>
<ToolBar prefHeight="40.0" prefWidth="200.0" styleClass="accent-header" BorderPane.alignment="CENTER">