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.ListView;
  26 
  27 import javafx.beans.value.ChangeListener;
  28 import javafx.beans.value.ObservableValue;
  29 import javafx.collections.FXCollections;
  30 import javafx.collections.ObservableList;
  31 import javafx.event.ActionEvent;
  32 import javafx.event.Event;
  33 import javafx.event.EventHandler;
  34 import javafx.scene.Group;
  35 import javafx.scene.Node;
  36 import javafx.scene.Scene;
  37 import javafx.scene.control.*;
  38 import javafx.scene.control.cell.ChoiceBoxListCell;
  39 import javafx.scene.control.cell.ComboBoxListCell;
  40 import javafx.scene.control.cell.TextFieldListCell;
  41 import javafx.scene.control.test.cellapps.CellCustomStringConverter;
  42 import javafx.scene.control.test.cellapps.CellsApp;
  43 import javafx.scene.control.test.utils.*;
  44 import static javafx.scene.control.test.utils.ComponentsFactory.createCustomContent;
  45 import static javafx.scene.control.test.utils.ComponentsFactory.createFormComponent;
  46 import javafx.scene.control.test.utils.ptables.NodesChoserFactory;
  47 import javafx.scene.control.test.utils.ptables.PropertiesTable;
  48 import javafx.scene.control.test.utils.ptables.PropertyTablesFactory;
  49 import javafx.scene.control.test.utils.ptables.SpecialTablePropertiesProvider;
  50 import javafx.scene.control.test.utils.ptables.StaticLogger;
  51 import javafx.scene.layout.HBox;
  52 import javafx.scene.layout.VBox;
  53 import test.javaclient.shared.InteroperabilityApp;
  54 import test.javaclient.shared.Utils;
  55 
  56 /**
  57  * @author Alexander Kirov
  58  */
  59 public class NewListViewApp extends InteroperabilityApp {
  60 
  61     public final static String ADD_ITEM_BUTTON_ID = "ADD_ITEM_BUTTON_ID";
  62     public final static String ADD_ITEM_POSITION_TEXT_FIELD_ID = "ADD_ITEM_POSITION_TEXT_FIELD_ID";
  63     public final static String ADD_ITEM_TEXT_FIELD_ID = "ADD_ITEM_TEXT_FIELD_ID";
  64     public final static String REMOVE_BUTTON_ID = "REMOVE_BUTTON_ID";
  65     public final static String REMOVE_ITEM_POS_TEXT_FIELD_ID = "REMOVE_ITEM_POS_TEXT_FIELD_ID";
  66     public final static String HARD_RESET_BUTTON_ID = "HARD_RESET_BUTTON_ID";
  67     public final static String SOFT_RESET_BUTTON_ID = "SOFT_RESET_BUTTON_ID";
  68     public final static String SCROLL_TO_BUTTON_ID = "SCROLL_TO_BUTTON_ID";
  69     public final static String SCROLL_TO_TEXT_FIELD_ID = "SCTOLL_TO_TEXT_FIELD_ID";
  70     public final static String ADD_RECTANGLE_BUTTON_ID = "ADD_RECTANGLE_BUTTON_ID";
  71     public final static String ADD_RECTANGLE_TEXT_FIELD_ID = "ADD_RECTANGLE_POSITION_TEXT_FIELD_ID";
  72     public final static String ADD_TEXT_FIELD_BUTTON_ID = "ADD_TEXT_FIELD_BUTTON_ID";
  73     public final static String ADD_TEXT_FIELD_TEXT_FIELD_ID = "ADD_TEXT_FIELD_POSITION_TEXT_FIELD_ID";
  74     public final static String TESTED_LIST_VIEW_ID = "TESTED_LIST_VIEW_ID";
  75     public final static String DECREASE_SCALE_BUTTON_ID = "DECREASE_SCALE_BUTTON_ID";
  76     public final static String DECREASE_SCALE_TEXT_FIELD_ID = "DECREASE_SCALE_TEXT_FIELD_ID";
  77     public final static String INCREASE_SCALE_BUTTON_ID = "INCREASE_SCALE_BUTTON_ID";
  78     public final static String INCREASE_SCALE_TEXT_FIELD_ID = "INCREASE_SCALE_TEXT_FIELD_ID";
  79     public final static String START_MOTION_BUTTON_ID = "START_MOTION_BUTTON_ID";
  80     public final static String START_MOTION_TEXT_FIELD_ID = "START_MOTION_TEXT_FIELD";
  81     public final static String CHANGE_SELECTION_MODEL_BUTTON_ID = "CHANGE_SELECTION_MODEL_BUTTON_ID";
  82     public final static String ADD_FORM_BUTTON_ID = "ADD_FORM_BUTTON_ID";
  83     public final static String ADD_FORM_TEXT_FIELD_ID = "ADD_FORM_TEXT_FIELD_ID";
  84     public final static String LIST_VIEW_CONTROL_ADD_INDEX_TEXT_FIELD_ID = "LIST_VIEW_CONTROL_ADD_INDEX_TEXT_FIELD_ID";
  85     public final static String LIST_FACTORY_CHOICE_ID = "LIST_FACTORY_CHOICE_ID";
  86     public static final ObservableList someValues = FXCollections.observableArrayList();
  87 
  88     public static void main(String[] args) {
  89         Utils.launch(NewListViewApp.class, args);
  90     }
  91 
  92     @Override
  93     protected Scene getScene() {
  94         Utils.setTitleToStage(stage, "ListViewTestApp");
  95         return new ListViewScene();
  96     }
  97 
  98     class ListViewScene extends CommonPropertiesScene {
  99 
 100         private PropertiesTable tb;
 101         private ListView testedControl;
 102         private ContentMotion cm;
 103         private int textFieldsCounter = 0;
 104 
 105         public ListViewScene() {
 106             super("ListView", 800, 600);
 107             someValues.addAll("Data item A", "Data item B", "Data item C");
 108             prepareScene();
 109         }
 110 
 111         @Override
 112         final protected void prepareScene() {
 113             testedControl = getNewTestedControl();
 114             tb = new PropertiesTable(testedControl);
 115             cm = new ContentMotion();
 116 
 117             final ListView testedListView = (ListView) testedControl;
 118             PropertyTablesFactory.explorePropertiesList(testedListView, tb);
 119             SpecialTablePropertiesProvider.provideForControl(testedControl, tb);
 120 
 121             final TextField tf = new TextField("0");
 122             tf.setId(LIST_VIEW_CONTROL_ADD_INDEX_TEXT_FIELD_ID);
 123             tf.setPrefWidth(40);
 124 
 125             HBox nodeshb = new NodesChoserFactory("Add!", new NodesChoserFactory.NodeAction<Node>() {
 126                 @Override
 127                 public void execute(Node node) {
 128                     testedControl.getItems().add(Integer.parseInt(tf.getText()), node);
 129                 }
 130             }, tf);
 131 
 132             HBox resetButtons = new HBox();
 133             Button hardResetButton = new Button("Hard reset");
 134             hardResetButton.setId(HARD_RESET_BUTTON_ID);
 135             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
 136                 public void handle(ActionEvent t) {
 137                     HBox hb = (HBox) getRoot();
 138                     hb.getChildren().clear();
 139                     prepareMainSceneStructure();
 140                     prepareScene();
 141                 }
 142             });
 143 
 144             Button softResetButton = new Button("Soft reset");
 145             softResetButton.setId(SOFT_RESET_BUTTON_ID);
 146             softResetButton.setOnAction(new EventHandler<ActionEvent>() {
 147                 public void handle(ActionEvent t) {
 148                     softReset();
 149                 }
 150             });
 151             resetButtons.getChildren().addAll(hardResetButton, softResetButton);
 152 
 153             Button flushLogger = new Button("Flush logger");
 154             final CustomMultipleSelectionModel cmsm = new CustomMultipleSelectionModel(testedListView.getItems());
 155             flushLogger.setOnAction(new EventHandler<ActionEvent>() {
 156                 public void handle(ActionEvent t) {
 157                     StaticLogger.log(cmsm.getLog());
 158                 }
 159             });
 160 
 161             Button changeSelectionModel = new Button("Change selection model to custom");
 162             changeSelectionModel.setId(CHANGE_SELECTION_MODEL_BUTTON_ID);
 163             changeSelectionModel.setOnAction(new EventHandler<ActionEvent>() {
 164                 public void handle(ActionEvent t) {
 165                     testedListView.setSelectionModel(cmsm);
 166                 }
 167             });
 168 
 169             setTestedControl(testedControl);
 170 
 171             VBox vb = new VBox();
 172             vb.setSpacing(5);
 173             vb.getChildren().addAll(resetButtons, getAddTextFieldHbox(), getAddRectangleHbox(),
 174                     getAddFormHbox(),
 175                     getAddItemHBox(), getRemoveItemHBox(), getStartMotionHBox(),
 176                     getScrollToHBox(), getDecreaseScaleHBox(), getIncreaseScaleHBox(),
 177                     changeSelectionModel, flushLogger, nodeshb, getEditFactoryComboBoxChoser());
 178             setControllersContent(vb);
 179 
 180             tb.setStyle("-fx-border-color : yellow;");
 181             setPropertiesContent(tb);
 182         }
 183 
 184         private ListView getNewTestedControl() {
 185             ListView lv = new ListView();
 186             lv.setId(TESTED_LIST_VIEW_ID);
 187             return lv;
 188         }
 189 
 190         private void softReset() {
 191             tb.refresh();
 192             ListView newOne = new ListView();
 193 
 194             while (testedControl.getItems().size() != 0) {
 195                 testedControl.getItems().remove(0);
 196             }
 197             testedControl.setPrefHeight(newOne.getPrefHeight());
 198             testedControl.setPrefWidth(newOne.getPrefWidth());
 199             testedControl.setOrientation(newOne.getOrientation());
 200             testedControl.setEditable(newOne.isEditable());
 201             testedControl.getSelectionModel().setSelectionMode(newOne.getSelectionModel().getSelectionMode());
 202         }
 203 
 204         private ComboBox getEditFactoryComboBoxChoser() {
 205             ComboBox<CellsApp.CellType> cb = new ComboBox<CellsApp.CellType>();
 206             cb.getItems().addAll(FXCollections.observableArrayList(CellsApp.CellType.values()));
 207             cb.setId(LIST_FACTORY_CHOICE_ID);
 208             cb.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<CellsApp.CellType>() {
 209                 public void changed(ObservableValue<? extends CellsApp.CellType> ov, CellsApp.CellType t, CellsApp.CellType t1) {
 210 
 211                     switch (t1) {
 212                         case ChoiceBox:
 213                             testedControl.setCellFactory(ChoiceBoxListCell.forListView(new CellCustomStringConverter(), someValues));
 214                             break;
 215                         case ComboBox:
 216                             testedControl.setCellFactory(ComboBoxListCell.forListView(new CellCustomStringConverter(), someValues));
 217                             break;
 218                         case TextField:
 219                             testedControl.setCellFactory(TextFieldListCell.forListView(new CellCustomStringConverter()));
 220                             break;
 221                         default:
 222                             testedControl.setCellFactory(new ListView().getCellFactory());
 223                     }
 224                 }
 225             });
 226             return cb;
 227         }
 228 
 229         private HBox getAddItemHBox() {
 230             HBox hb = new HBox();
 231             Label lb = new Label("Add item");
 232             final TextField tf = new TextField();
 233             tf.setPrefWidth(50);
 234             tf.setId(ADD_ITEM_TEXT_FIELD_ID);
 235 
 236             Label atLb = new Label("at pos");
 237             final TextField tfPos = new TextField();
 238             tfPos.setPrefWidth(50);
 239             tfPos.setId(ADD_ITEM_POSITION_TEXT_FIELD_ID);
 240 
 241             Button bt = new Button("Add!");
 242             bt.setId(ADD_ITEM_BUTTON_ID);
 243             bt.setOnAction(new EventHandler() {
 244                 public void handle(Event t) {
 245                     ((ListView) testedControl).getItems().add(Integer.parseInt(tfPos.getText()), tf.getText());
 246                 }
 247             });
 248             hb.getChildren().addAll(lb, tf, atLb, tfPos, bt);
 249             return hb;
 250         }
 251 
 252         private HBox getRemoveItemHBox() {
 253             HBox hb = new HBox();
 254             Label lb = new Label("From position");
 255             final TextField tf = new TextField("0");
 256             tf.setPrefWidth(50);
 257             tf.setId(REMOVE_ITEM_POS_TEXT_FIELD_ID);
 258             Button bt = new Button("Remove!");
 259             bt.setId(REMOVE_BUTTON_ID);
 260             bt.setOnAction(new EventHandler() {
 261                 public void handle(Event t) {
 262                     int index = Integer.parseInt(tf.getText());
 263                     ((ListView) testedControl).getItems().remove(index);
 264                 }
 265             });
 266             hb.getChildren().addAll(lb, tf, bt);
 267             return hb;
 268         }
 269 
 270         private HBox getScrollToHBox() {
 271             HBox hb = new HBox();
 272             Button button = new Button("ScrollTo");
 273             button.setId(SCROLL_TO_BUTTON_ID);
 274             final TextField tf = new TextField("0");
 275             tf.setId(SCROLL_TO_TEXT_FIELD_ID);
 276 
 277             button.setOnAction(new EventHandler<ActionEvent>() {
 278                 public void handle(ActionEvent t) {
 279                     ((ListView) testedControl).scrollTo(Integer.parseInt(tf.getText()));
 280                 }
 281             });
 282 
 283             hb.getChildren().addAll(button, tf);
 284             return hb;
 285         }
 286 
 287         private HBox getAddRectangleHbox() {
 288             HBox hb = new HBox();
 289             Label lb = new Label("Add rectangle at pos");
 290             final TextField tfPos = new TextField();
 291             tfPos.setPrefWidth(50);
 292             tfPos.setId(ADD_RECTANGLE_TEXT_FIELD_ID);
 293             Button bt = new Button("Add!");
 294             bt.setId(ADD_RECTANGLE_BUTTON_ID);
 295             bt.setOnAction(new EventHandler() {
 296                 public void handle(Event t) {
 297                     int index = Integer.parseInt(tfPos.getText());
 298                     ((ListView) testedControl).getItems().add(index, new Group(getNewRectangle()));
 299                 }
 300             });
 301             hb.getChildren().addAll(lb, tfPos, bt);
 302             return hb;
 303         }
 304 
 305         private HBox getAddFormHbox() {
 306             HBox hb = new HBox();
 307             Label lb = new Label("Add form at pos");
 308             final TextField tfPos = new TextField();
 309             tfPos.setPrefWidth(50);
 310             tfPos.setId(ADD_FORM_TEXT_FIELD_ID);
 311             Button bt = new Button("Add!");
 312             bt.setId(ADD_FORM_BUTTON_ID);
 313             bt.setOnAction(new EventHandler() {
 314                 public void handle(Event t) {
 315                     int index = Integer.parseInt(tfPos.getText());
 316                     ((ListView) testedControl).getItems().add(index, new Group(getNewForm()));
 317                 }
 318             });
 319             hb.getChildren().addAll(lb, tfPos, bt);
 320             return hb;
 321         }
 322 
 323         private HBox getAddTextFieldHbox() {
 324             HBox hb = new HBox();
 325             Label lb = new Label("Add textField at pos");
 326             final TextField tfPos = new TextField();
 327             tfPos.setPrefWidth(50);
 328             tfPos.setId(ADD_TEXT_FIELD_TEXT_FIELD_ID);
 329             Button bt = new Button("Add!");
 330             bt.setId(ADD_TEXT_FIELD_BUTTON_ID);
 331             bt.setOnAction(new EventHandler() {
 332                 public void handle(Event t) {
 333                     int index = Integer.parseInt(tfPos.getText());
 334                     ((ListView) testedControl).getItems().add(index, new Group(getNewTextField()));
 335                 }
 336             });
 337             hb.getChildren().addAll(lb, tfPos, bt);
 338             return hb;
 339         }
 340 
 341         private HBox getStartMotionHBox() {
 342             HBox hb = new HBox();
 343             Button startMotion = new Button("start motion");
 344             startMotion.setId(START_MOTION_BUTTON_ID);
 345             Label label = new Label(" of element at position ");
 346             final TextField tf = new TextField("0");
 347             tf.setId(START_MOTION_TEXT_FIELD_ID);
 348             startMotion.setOnAction(new EventHandler() {
 349                 public void handle(Event t) {
 350                     cm.applyTransition((Node) ((ListView) testedControl).getItems().get(Integer.parseInt(tf.getText())));
 351                     cm.getTimeline().play();
 352                 }
 353             });
 354             hb.getChildren().addAll(startMotion, label, tf);
 355             return hb;
 356         }
 357 
 358         private HBox getIncreaseScaleHBox() {
 359             HBox hb = new HBox();
 360             final TextField tf = new TextField("0");
 361             tf.setId(INCREASE_SCALE_TEXT_FIELD_ID);
 362             Label label = new Label(" of node on position ");
 363             Button increaseScaleButton = new Button("Increase scale");
 364             increaseScaleButton.setId(INCREASE_SCALE_BUTTON_ID);
 365             increaseScaleButton.setOnAction(new EventHandler<ActionEvent>() {
 366                 public void handle(ActionEvent t) {
 367                     Node content = (Node) ((ListView) testedControl).getItems().get(Integer.parseInt(tf.getText()));
 368                     content.setScaleX(content.getScaleX() + 0.15);
 369                     content.setScaleY(content.getScaleY() + 0.15);
 370                 }
 371             });
 372             hb.getChildren().addAll(increaseScaleButton, label, tf);
 373             return hb;
 374         }
 375 
 376         private HBox getDecreaseScaleHBox() {
 377             HBox hb = new HBox();
 378             final TextField tf = new TextField("0");
 379             tf.setId(DECREASE_SCALE_TEXT_FIELD_ID);
 380             Label label = new Label(" of node on position ");
 381             Button increaseScaleButton = new Button("Decrease scale");
 382             increaseScaleButton.setId(DECREASE_SCALE_BUTTON_ID);
 383             increaseScaleButton.setOnAction(new EventHandler<ActionEvent>() {
 384                 public void handle(ActionEvent t) {
 385                     Node content = (Node) ((ListView) testedControl).getItems().get(Integer.parseInt(tf.getText()));
 386                     content.setScaleX(content.getScaleX() - 0.15);
 387                     content.setScaleY(content.getScaleY() - 0.15);
 388                 }
 389             });
 390             hb.getChildren().addAll(increaseScaleButton, label, tf);
 391             return hb;
 392         }
 393 
 394         private Group getNewRectangle() {
 395             return createCustomContent(200, 200);
 396         }
 397 
 398         private VBox getNewForm() {
 399             return createFormComponent();
 400         }
 401 
 402         private TextField getNewTextField() {
 403             TextField tf = new TextField();
 404             tf.setId("CONTENT_TEXT_FIELD_" + (textFieldsCounter++) + "_ID");
 405             return tf;
 406         }
 407     }
 408 }