Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Replace cell-based YAML context menu with tree-based context menu

AuthorDaveJarvis <email>
Date2020-05-30 19:25:42 GMT-0700
Commit31b19afbd500f0cf8b991ce42a69fc9e4c83e423
Parent173bcb1
Delta17 lines added, 230 lines removed, 213-line decrease
src/main/resources/com/scrivenvar/messages.properties
# ########################################################################
-Definition.menu.add=Add
+Definition.menu.create=Create
+Definition.menu.rename=Rename
Definition.menu.remove=Delete
+Definition.menu.add.default=Undefined
# ########################################################################
src/main/java/com/scrivenvar/definition/TextFieldTreeCell.java
-/*
- * Copyright 2016 White Magic Software, Ltd.
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.scrivenvar.definition;
-
-import javafx.event.ActionEvent;
-import javafx.scene.control.*;
-import javafx.scene.input.KeyEvent;
-
-import static com.scrivenvar.Messages.get;
-
-/**
- * Provides behaviour of adding, removing, and editing tree view items.
- *
- * @author White Magic Software, Ltd.
- */
-public class TextFieldTreeCell extends TreeCell<String> {
-
- private TextField textField;
- private final ContextMenu editMenu = new ContextMenu();
-
- public TextFieldTreeCell() {
- initEditMenu();
- }
-
- private void initEditMenu() {
- final MenuItem addItem = createMenuItem( "Definition.menu.add" );
- final MenuItem removeItem = createMenuItem( "Definition.menu.remove" );
-
- addItem.setOnAction( ( ActionEvent e ) -> {
- final VariableTreeItem<String> treeItem = new VariableTreeItem<>(
- "Undefined" );
- getTreeItem().getChildren().add( treeItem );
- } );
-
- removeItem.setOnAction( ( ActionEvent e ) -> {
- final TreeItem<?> c = getTreeItem();
- c.getParent().getChildren().remove( c );
- } );
-
- getEditMenu().getItems().add( addItem );
- getEditMenu().getItems().add( removeItem );
- }
-
- private ContextMenu getEditMenu() {
- return this.editMenu;
- }
-
- private MenuItem createMenuItem( String label ) {
- return new MenuItem( get( label ) );
- }
-
- @Override
- public void startEdit() {
- if( getTreeItem().isLeaf() ) {
- super.startEdit();
-
- final TextField inputField = getTextField();
-
- setText( null );
- setGraphic( inputField );
- inputField.selectAll();
- inputField.requestFocus();
- }
- }
-
- @Override
- public void cancelEdit() {
- super.cancelEdit();
-
- setText( getItem() );
- setGraphic( getTreeItem().getGraphic() );
- }
-
- @Override
- public void updateItem( String item, boolean empty ) {
- super.updateItem( item, empty );
-
- if( empty ) {
- setText( null );
- setGraphic( null );
- }
- else if( isEditing() ) {
- TextField tf = getTextField();
- tf.setText( getItemValue() );
-
- setText( null );
- setGraphic( tf );
- }
- else {
- setText( getItemValue() );
- setGraphic( getTreeItem().getGraphic() );
-
- if( !getTreeItem().isLeaf() && getTreeItem().getParent() != null ) {
- setContextMenu( getEditMenu() );
- }
- }
- }
-
- private TextField createTextField() {
- final TextField tf = new TextField( getItemValue() );
-
- tf.setOnKeyReleased( ( KeyEvent t ) -> {
- switch( t.getCode() ) {
- case ENTER:
- commitEdit( tf.getText() );
- break;
- case ESCAPE:
- cancelEdit();
- break;
- }
- } );
-
- return tf;
- }
-
- /**
- * Returns the item's text value.
- *
- * @return A non-null String, possibly empty.
- */
- private String getItemValue() {
- return getItem() == null ? "" : getItem();
- }
-
- private TextField getTextField() {
- if( this.textField == null ) {
- this.textField = createTextField();
- }
-
- return this.textField;
- }
-}
src/main/java/com/scrivenvar/definition/VariableTreeItem.java
package com.scrivenvar.definition;
-import com.scrivenvar.decorators.VariableDecorator;
-import com.scrivenvar.decorators.YamlVariableDecorator;
+import javafx.scene.control.TreeItem;
+
+import java.text.Normalizer;
+import java.util.Stack;
+
import static com.scrivenvar.definition.yaml.YamlParser.SEPARATOR;
import static com.scrivenvar.editors.VariableNameInjector.DEFAULT_MAX_VAR_LENGTH;
-import java.text.Normalizer;
import static java.text.Normalizer.Form.NFD;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Stack;
-import javafx.scene.control.TreeItem;
/**
* Provides behaviour afforded to variable names and their corresponding value.
*
- * @author White Magic Software, Ltd.
* @param <T> The type of TreeItem (usually String).
+ * @author White Magic Software, Ltd.
*/
public class VariableTreeItem<T> extends TreeItem<T> {
-
- private final static int DEFAULT_MAP_SIZE = 1000;
-
- private final static VariableDecorator VARIABLE_DECORATOR
- = new YamlVariableDecorator();
-
- /**
- * Flattened tree.
- */
- private Map<String, String> map;
/**
*
* @param text The text to match against each leaf in the tree.
- *
* @return The leaf that has a value starting with the given text.
*/
* value.
*
- * @param text The text to match against each leaf in the tree.
+ * @param text The text to match against each leaf in the tree.
* @param contains Set to true to perform a substring match if starts with
- * fails.
- *
+ * fails.
* @return The leaf that has a value starting with the given text.
*/
public VariableTreeItem<T> findLeaf(
- final String text,
- final boolean contains ) {
-
+ final String text, final boolean contains ) {
final Stack<VariableTreeItem<T>> stack = new Stack<>();
final VariableTreeItem<T> root = this;
else {
for( final TreeItem<T> child : node.getChildren() ) {
- stack.push( (VariableTreeItem<T>)child );
+ stack.push( (VariableTreeItem<T>) child );
}
/**
* Returns the value of the string without diacritic marks.
- *
+ *
* @return A non-null, possibly empty string.
*/
private String getDiacriticlessValue() {
final String value = getValue().toString();
- final String normalized = Normalizer.normalize(value, NFD);
-
- return normalized.replaceAll("\\p{M}", "");
+ final String normalized = Normalizer.normalize( value, NFD );
+
+ return normalized.replaceAll( "\\p{M}", "" );
}
/**
* Returns true if this node is a leaf and its value starts with the given
* text.
*
* @param s The text to compare against the node value.
- *
* @return true Node is a leaf and its value starts with the given value.
*/
*
* @param s The text to compare against the node value.
- *
* @return true Node is a leaf and its value contains the given value.
*/
return sb.toString();
- }
-
- /**
- * Returns the hierarchy, flattened to key-value pairs.
- *
- * @return A map of this tree's key-value pairs.
- */
- public Map<String, String> getMap() {
- if( this.map == null ) {
- this.map = new HashMap<>( DEFAULT_MAP_SIZE );
- populate( this, this.map );
- }
-
- return this.map;
- }
-
- private void populate( final TreeItem<T> parent, final Map<String, String> map ) {
- for( final TreeItem<T> child : parent.getChildren() ) {
- if( child.isLeaf() ) {
- final String key = toVariable( ((VariableTreeItem<String>)child).toPath() );
- final String value = child.getValue().toString();
-
- map.put( key, value );
- }
- else {
- populate( child, map );
- }
- }
- }
-
- /**
- * Converts the name of the key to a simple variable by enclosing it with
- * dollar symbols.
- *
- * @param key The key name to change to a variable.
- *
- * @return $key$
- */
- public String toVariable( final String key ) {
- return VARIABLE_DECORATOR.decorate( key );
}
}