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 = new TextField("0");
  93             tf.setId(TOOLBAR_ADD_INDEX_TEXT_FIELD_ID);
  94             tf.setPrefWidth(40);
  95 
  96             this.getChildren().addAll(new NodesChoserFactory("Add!", new NodesChoserFactory.NodeAction<Node>() {
  97                 @Override
  98                 public void execute(Node node) {
  99                     toolBar.getItems().add(Integer.parseInt(tf.getText()), node);
 100                     try {
 101                         tabPane.addPropertiesTable(node.getClass().getSimpleName(), NodeControllerFactory.createFullController(node, tabPane));
 102                     } catch (Throwable ex) {
 103                         Logger.getLogger(NodeControllerFactory.class.getName()).log(Level.SEVERE, null, ex);
 104                     }
 105                 }
 106             }, tf).getChildren());
 107 
 108             return this;
 109         }
 110     }
 111 
 112     public static class MenuControllers extends HBox implements ControllingNodesCreator<Menu> {
 113 
 114         public final String MENU_ADD_INDEX_TEXT_FIELD_ID = "MENU_ADD_INDEX_TEXT_FIELD_ID";
 115         public final String MENU_ADD_NAME_TEXT_FIELD_ID = "MENU_ADD_NAME_TEXT_FIELD_ID";
 116 
 117         @Override
 118         public Node getForNode(final Menu menu, final TabPaneWithControl tabPane) {
 119             final TextField tf = new TextField("0");
 120             tf.setId(MENU_ADD_INDEX_TEXT_FIELD_ID);
 121             tf.setPrefWidth(40);
 122             final TextField nameTF = new TextField("Menu");
 123             nameTF.setId(MENU_ADD_NAME_TEXT_FIELD_ID);
 124             nameTF.setPrefWidth(40);
 125             this.getChildren().addAll(new NodesChoserFactory("Add!", new NodesChoserFactory.NodeAction<MenuItem>() {
 126                 @Override
 127                 public void execute(MenuItem node) {
 128                     node.setText(nameTF.getText());
 129                     menu.getItems().add(Integer.parseInt(tf.getText()), node);
 130                     try {
 131                         tabPane.addPropertiesTable(nameTF.getText(), NodeControllerFactory.createFullController(node, tabPane));
 132                     } catch (Throwable ex) {
 133                         Logger.getLogger(NodeControllerFactory.class.getName()).log(Level.SEVERE, null, ex);
 134                     }
 135                 }
 136             }, tf, nameTF).getChildren());
 137 
 138             return this;
 139         }
 140     }
 141 
 142     public static class TreeItemControllers extends FlowPane implements ControllingNodesCreator<TreeItem> {
 143 
 144         public static final String GET_NEXT_SIBLING_TREEITEM_BUTTON_ID = "GET_NEXT_SIBLING_TREEITEM_BUTTON_ID";
 145         public static final String GET_NEXT_SIBLING_TREEITEM_TEXTFIELD_ID = "GET_NEXT_SIBLING_TREEITEM_TEXTFIELD_ID";
 146         public static final String GET_PREVIOUS_SIBLING_TREEITEM_BUTTON_ID = "GET_PREVIOUS_SIBLING_TREEITEM_BUTTON_ID";
 147         public static final String GET_PREVIOUS_SIBLING_TREEITEM_TEXTFIELD_ID = "GET_PREVIOUS_SIBLING_TREEITEM_TEXTFIELD_ID";
 148         public final static String CHANGE_VALUE_BUTTON_ID = "CHANGE_VALUE_BUTTON_ID";
 149         public final static String NEW_VALUE_TEXT_FIELD_ID = "NEW_VALUE_TEXT_FIELD_ID";
 150 
 151         @Override
 152         public Node getForNode(final TreeItem item, final TabPaneWithControl tabPane) {
 153             HBox hb1 = getNextSiblingHBox(item);
 154             HBox hb2 = getPreviousSiblingHBox(item);
 155             HBox hb3 = getChangeValueHBox(item);
 156 
 157             this.getChildren().addAll(hb1, hb2, hb3);
 158 
 159             return this;
 160         }
 161 
 162         private HBox getPreviousSiblingHBox(final TreeItem item) {
 163             HBox hb = new HBox();
 164             Button button = new Button("Get previous sibling");
 165             button.setId(GET_PREVIOUS_SIBLING_TREEITEM_BUTTON_ID);
 166             final TextField tf = new TextField("");
 167             tf.setPromptText("Next sibling");
 168             tf.setId(GET_PREVIOUS_SIBLING_TREEITEM_TEXTFIELD_ID);
 169             tf.setPrefWidth(100);
 170 
 171             button.setOnAction(new EventHandler<ActionEvent>() {
 172                 public void handle(ActionEvent t) {
 173                     TreeItem sibling = item.previousSibling();
 174                     TreeItem sibling2 = item.previousSibling(item);
 175                     if (sibling == null) {
 176                         if (sibling2 == null) {
 177                             tf.setText("null");
 178                         } else {
 179                             tf.setText("ERROR");
 180                         }
 181                     } else {
 182                         if (sibling.equals(sibling2)) {
 183                             tf.setText(sibling.getValue().toString());
 184                         } else {
 185                             tf.setText("ERROR");
 186                         }
 187                     }
 188                 }
 189             });
 190 
 191             hb.getChildren().addAll(button, tf);
 192             return hb;
 193         }
 194 
 195         private HBox getNextSiblingHBox(final TreeItem item) {
 196             HBox hb = new HBox();
 197             Button button = new Button("Get next sibling");
 198             button.setId(GET_NEXT_SIBLING_TREEITEM_BUTTON_ID);
 199             final TextField tf = new TextField("");
 200             tf.setPromptText("Next sibling");
 201             tf.setId(GET_NEXT_SIBLING_TREEITEM_TEXTFIELD_ID);
 202             tf.setPrefWidth(100);
 203 
 204             button.setOnAction(new EventHandler<ActionEvent>() {
 205                 public void handle(ActionEvent t) {
 206                     TreeItem sibling = item.nextSibling();
 207                     if (sibling == null) {
 208                         tf.setText("null");
 209                     } else {
 210                         tf.setText(sibling.getValue().toString());
 211                     }
 212                 }
 213             });
 214 
 215             hb.getChildren().addAll(button, tf);
 216             return hb;
 217         }
 218 
 219         private HBox getChangeValueHBox(final TreeItem item) {
 220             Button button = new Button("change value to");
 221             button.setId(CHANGE_VALUE_BUTTON_ID);
 222             final TextField tfNew = new TextField();
 223             tfNew.setPromptText("new value");
 224             tfNew.setId(NEW_VALUE_TEXT_FIELD_ID);
 225             tfNew.setPrefWidth(50);
 226 
 227             button.setOnAction(new EventHandler<ActionEvent>() {
 228                 public void handle(ActionEvent t) {
 229                     item.setValue(tfNew.getText());
 230                 }
 231             });
 232 
 233             HBox hb = new HBox();
 234             hb.getChildren().addAll(button, tfNew);
 235 
 236             return hb;
 237         }
 238     }
 239 }