1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javafx.scene.control.test.utils.ptables;
  26 
  27 import java.util.logging.Level;
  28 import java.util.logging.Logger;
  29 import javafx.event.ActionEvent;
  30 import javafx.event.EventHandler;
  31 import javafx.scene.Node;
  32 import javafx.scene.control.*;
  33 import javafx.scene.layout.FlowPane;
  34 import javafx.scene.layout.HBox;
  35 import javafx.scene.layout.VBox;
  36 
  37 /**
  38  * @author Alexander Kirov
  39  *
  40  * NOTION: this class should be instantiated on JavaFX thread.
  41  */
  42 public class NodeControllerFactory {
  43 
  44     public static NodesStorage createFullController(Object node, TabPaneWithControl tabPane) {
  45         PropertiesTable pt = new PropertiesTable(node);
  46         PropertyTablesFactory.explorePropertiesList(node, pt);
  47         SpecialTablePropertiesProvider.provideForControl(node, pt);
  48 
  49         VBox vb = new VBox();
  50 
  51         if (node instanceof ToolBar) {
  52             vb.getChildren().add(new ToolBarControllers().getForNode((ToolBar) node, tabPane));
  53         }
  54 
  55         if (node instanceof Menu) {
  56             vb.getChildren().add(new MenuControllers().getForNode((Menu) node, tabPane));
  57         }
  58 
  59         if (node instanceof TreeItem) {
  60             vb.getChildren().add(new TreeItemControllers().getForNode((TreeItem) node, tabPane));
  61         }
  62 
  63         return new NodesStorage(pt, vb);
  64     }
  65 
  66     public static class NodesStorage extends VBox {
  67 
  68         public PropertiesTable pt;
  69         public Node storageOfTable;
  70         public Node storageOfControlElements;
  71 
  72         public NodesStorage(PropertiesTable pt, Node storageOfControlElements) {
  73             this.pt = pt;
  74             this.storageOfControlElements = storageOfControlElements;
  75             this.storageOfTable = pt;
  76 
  77             this.getChildren().addAll(storageOfControlElements, storageOfTable);
  78         }
  79     }
  80 
  81     public interface ControllingNodesCreator<T extends Object> {
  82 
  83         public abstract Node getForNode(T node, TabPaneWithControl tabPane);
  84     }
  85 
  86     public static class ToolBarControllers extends HBox implements ControllingNodesCreator<ToolBar> {
  87 
  88         public final String TOOLBAR_ADD_INDEX_TEXT_FIELD_ID = "TOOLBAR_ADD_INDEX_TEXT_FIELD_ID";
  89 
  90         @Override
  91         public Node getForNode(final ToolBar toolBar, final TabPaneWithControl tabPane) {
  92             final TextField tf = TextFieldBuilder.create().id(TOOLBAR_ADD_INDEX_TEXT_FIELD_ID).text("0").prefWidth(40).build();
  93 
  94             this.getChildren().addAll(new NodesChoserFactory("Add!", new NodesChoserFactory.NodeAction<Node>() {
  95                 @Override
  96                 public void execute(Node node) {
  97                     toolBar.getItems().add(Integer.parseInt(tf.getText()), node);
  98                     try {
  99                         tabPane.addPropertiesTable(node.getClass().getSimpleName(), NodeControllerFactory.createFullController(node, tabPane));
 100                     } catch (Throwable ex) {
 101                         Logger.getLogger(NodeControllerFactory.class.getName()).log(Level.SEVERE, null, ex);
 102                     }
 103                 }
 104             }, tf).getChildren());
 105 
 106             return this;
 107         }
 108     }
 109 
 110     public static class MenuControllers extends HBox implements ControllingNodesCreator<Menu> {
 111 
 112         public final String MENU_ADD_INDEX_TEXT_FIELD_ID = "MENU_ADD_INDEX_TEXT_FIELD_ID";
 113         public final String MENU_ADD_NAME_TEXT_FIELD_ID = "MENU_ADD_NAME_TEXT_FIELD_ID";
 114 
 115         @Override
 116         public Node getForNode(final Menu menu, final TabPaneWithControl tabPane) {
 117             final TextField tf = TextFieldBuilder.create().id(MENU_ADD_INDEX_TEXT_FIELD_ID).text("0").prefWidth(40).build();
 118             final TextField nameTF = TextFieldBuilder.create().id(MENU_ADD_NAME_TEXT_FIELD_ID).text("Menu").prefWidth(40).build();
 119             this.getChildren().addAll(new NodesChoserFactory("Add!", new NodesChoserFactory.NodeAction<MenuItem>() {
 120                 @Override
 121                 public void execute(MenuItem node) {
 122                     node.setText(nameTF.getText());
 123                     menu.getItems().add(Integer.parseInt(tf.getText()), node);
 124                     try {
 125                         tabPane.addPropertiesTable(nameTF.getText(), NodeControllerFactory.createFullController(node, tabPane));
 126                     } catch (Throwable ex) {
 127                         Logger.getLogger(NodeControllerFactory.class.getName()).log(Level.SEVERE, null, ex);
 128                     }
 129                 }
 130             }, tf, nameTF).getChildren());
 131 
 132             return this;
 133         }
 134     }
 135 
 136     public static class TreeItemControllers extends FlowPane implements ControllingNodesCreator<TreeItem> {
 137 
 138         public static final String GET_NEXT_SIBLING_TREEITEM_BUTTON_ID = "GET_NEXT_SIBLING_TREEITEM_BUTTON_ID";
 139         public static final String GET_NEXT_SIBLING_TREEITEM_TEXTFIELD_ID = "GET_NEXT_SIBLING_TREEITEM_TEXTFIELD_ID";
 140         public static final String GET_PREVIOUS_SIBLING_TREEITEM_BUTTON_ID = "GET_PREVIOUS_SIBLING_TREEITEM_BUTTON_ID";
 141         public static final String GET_PREVIOUS_SIBLING_TREEITEM_TEXTFIELD_ID = "GET_PREVIOUS_SIBLING_TREEITEM_TEXTFIELD_ID";
 142         public final static String CHANGE_VALUE_BUTTON_ID = "CHANGE_VALUE_BUTTON_ID";
 143         public final static String NEW_VALUE_TEXT_FIELD_ID = "NEW_VALUE_TEXT_FIELD_ID";
 144 
 145         @Override
 146         public Node getForNode(final TreeItem item, final TabPaneWithControl tabPane) {
 147             HBox hb1 = getNextSiblingHBox(item);
 148             HBox hb2 = getPreviousSiblingHBox(item);
 149             HBox hb3 = getChangeValueHBox(item);
 150 
 151             this.getChildren().addAll(hb1, hb2, hb3);
 152 
 153             return this;
 154         }
 155 
 156         private HBox getPreviousSiblingHBox(final TreeItem item) {
 157             HBox hb = new HBox();
 158             Button button = ButtonBuilder.create().text("Get previous sibling").id(GET_PREVIOUS_SIBLING_TREEITEM_BUTTON_ID).build();
 159             final TextField tf = TextFieldBuilder.create().text("").promptText("Next sibling").id(GET_PREVIOUS_SIBLING_TREEITEM_TEXTFIELD_ID).prefWidth(100).build();
 160 
 161             button.setOnAction(new EventHandler<ActionEvent>() {
 162                 public void handle(ActionEvent t) {
 163                     TreeItem sibling = item.previousSibling();
 164                     TreeItem sibling2 = item.previousSibling(item);
 165                     if (sibling == null) {
 166                         if (sibling2 == null) {
 167                             tf.setText("null");
 168                         } else {
 169                             tf.setText("ERROR");
 170                         }
 171                     } else {
 172                         if (sibling.equals(sibling2)) {
 173                             tf.setText(sibling.getValue().toString());
 174                         } else {
 175                             tf.setText("ERROR");
 176                         }
 177                     }
 178                 }
 179             });
 180 
 181             hb.getChildren().addAll(button, tf);
 182             return hb;
 183         }
 184 
 185         private HBox getNextSiblingHBox(final TreeItem item) {
 186             HBox hb = new HBox();
 187             Button button = ButtonBuilder.create().text("Get next sibling").id(GET_NEXT_SIBLING_TREEITEM_BUTTON_ID).build();
 188             final TextField tf = TextFieldBuilder.create().text("").promptText("Next sibling").id(GET_NEXT_SIBLING_TREEITEM_TEXTFIELD_ID).prefWidth(100).build();
 189 
 190             button.setOnAction(new EventHandler<ActionEvent>() {
 191                 public void handle(ActionEvent t) {
 192                     TreeItem sibling = item.nextSibling();
 193                     if (sibling == null) {
 194                         tf.setText("null");
 195                     } else {
 196                         tf.setText(sibling.getValue().toString());
 197                     }
 198                 }
 199             });
 200 
 201             hb.getChildren().addAll(button, tf);
 202             return hb;
 203         }
 204 
 205         private HBox getChangeValueHBox(final TreeItem item) {
 206             Button button = ButtonBuilder.create().text("change value to").id(CHANGE_VALUE_BUTTON_ID).build();
 207             final TextField tfNew = TextFieldBuilder.create().promptText("new value").id(NEW_VALUE_TEXT_FIELD_ID).prefWidth(50).build();
 208 
 209             button.setOnAction(new EventHandler<ActionEvent>() {
 210                 public void handle(ActionEvent t) {
 211                     item.setValue(tfNew.getText());
 212                 }
 213             });
 214 
 215             HBox hb = new HBox();
 216             hb.getChildren().addAll(button, tfNew);
 217 
 218             return hb;
 219         }
 220     }
 221 }