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.ScrollPane;
  26 
  27 import javafx.event.ActionEvent;
  28 import javafx.event.EventHandler;
  29 import javafx.scene.Group;
  30 import javafx.scene.Node;
  31 import javafx.scene.Scene;
  32 import javafx.scene.control.*;
  33 import javafx.scene.control.test.utils.*;
  34 import javafx.scene.control.test.utils.ptables.PropertiesTable;
  35 import javafx.scene.control.test.utils.ptables.PropertyTablesFactory;
  36 import javafx.scene.control.test.utils.ptables.SpecialTablePropertiesProvider;
  37 import javafx.scene.layout.GridPane;
  38 import javafx.scene.layout.HBox;
  39 import javafx.scene.layout.Pane;
  40 import javafx.scene.layout.VBox;
  41 import test.javaclient.shared.InteroperabilityApp;
  42 import test.javaclient.shared.Utils;
  43 
  44 /**
  45  * @author Alexander Kirov
  46  */
  47 public class NewScrollPaneApp extends InteroperabilityApp {
  48 
  49     public final static String CHANGE_CONTENT_BUTTON_ID = "CHANGE_CONTENT_BUTTON_ID";
  50     public final static String CONTENT_BUTTON = "CONTENT_BUTTON";
  51     public final static String CUSTOM_CONTENT_ID = "CUSTOM_CONTENT_ID";
  52     public final static String CONTENT_TEXT_AREA_ID = "CONTENT_TEXT_AREA_ID";
  53     public final static String CONTENT_TEXT_FIELD_ID = "CONTENT_TEXT_FIELD_ID";
  54     public final static String DECREASE_SCALE_BUTTON_ID = "DECREASE_SCALE_BUTTON_ID";
  55     public final static String DECREASE_SCROLLPANE_SCALE_BUTTON_ID = "DECREASE_SCROLLPANE_SCALE_BUTTON_ID";
  56     public final static String INCREASE_SCALE_BUTTON_ID = "INCREASE_SCALE_BUTTON_ID";
  57     public final static String INCREASE_SCROLLPANE_SCALE_BUTTON_ID = "INCREASE_SCROLLPANE_SCALE_BUTTON_ID";
  58     public final static String RESET_BUTTON_ID = "RESET_BUTTON_ID";
  59     public final static String ROTATE_BUTTON_ID = "ROTATE_BUTTON_ID";
  60     public final static String ROTATE_SCROLLPANE_BUTTON_ID = "ROTATE_SCROLLPANE_BUTTON_ID";
  61     public final static String START_MOTION_BUTTON_ID = "START_MOTION_BUTTON_ID";
  62     public final static String TESTED_SCROLLPANE_ID = "TESTED_SCROLLPANE_ID";
  63     public final static String WITHOUT_ACTION_BUTTON = "WITHOUT_ACTION_BUTTON";
  64     public final static String CHANGE_CONTENT_TO_RESIZABLE_BUTTON_ID = "CHANGE_CONTENT_TO_RESIZABLE_BUTTON_ID";
  65     public final static String CHANGE_CONTENT_TO_CUSTOM_BUTTON_ID = "CHANGE_CONTENT_TO_CUSTOM_BUTTON_ID";
  66     public final static String ADD_SIZE_BUTTON_ID = "ADD_SIZE_BUTTON_ID";
  67     public final static String GRID_DIMENSION_TEXTFIELD_ID = "GRID_DIMENSION_TEXTFIELD_ID";
  68     public final static String ADD_GRID_BUTTON_ID = "ADD_GRID_BUTTON_ID";
  69     private static int customContentWidth = 200;
  70     private static int customContentHeight = 200;
  71     private static int scrollPaneWidth = 0;
  72     private static int scrollPaneHeight = 0;
  73 
  74     public static void main(String[] args) {
  75         try {
  76             if (args != null) {
  77                 for (int i = 0; i < args.length; ++i) {
  78                     if (args[i].equals("--customContentWidth")) {
  79                         ++i;
  80                         customContentWidth = Integer.parseInt(args[i]);
  81                     }
  82                     if (args[i].equals("--customContentHeight")) {
  83                         ++i;
  84                         customContentHeight = Integer.parseInt(args[i]);
  85                     }
  86                     if (args[i].equals("--scrollPaneWidth")) {
  87                         ++i;
  88                         scrollPaneWidth = Integer.parseInt(args[i]);
  89                     }
  90                     if (args[i].equals("--scrollPaneHeight")) {
  91                         ++i;
  92                         scrollPaneHeight = Integer.parseInt(args[i]);
  93                     }
  94                 }
  95             }
  96         } catch (NumberFormatException ex) {
  97         }
  98 
  99         Utils.launch(NewScrollPaneApp.class, args);
 100     }
 101 
 102     @Override
 103     protected Scene getScene() {
 104         Utils.setTitleToStage(stage, "ScrollPaneTestApp");
 105         return new ScrollPaneScene();
 106     }
 107 
 108     class ScrollPaneScene extends CommonPropertiesScene {
 109 
 110         private PropertiesTable tb;
 111         //ScrollPane to be tested.
 112         private ScrollPane testedScrollPane;
 113 
 114         public ScrollPaneScene() {
 115             super("ScrollPane", 800, 600);
 116 
 117             prepareScene();
 118         }
 119 
 120         @Override
 121         protected final void prepareScene() {
 122             Utils.addBrowser(this);
 123             testedScrollPane = ScrollPaneBuilder.create().id(TESTED_SCROLLPANE_ID).build();

 124             final Node content = setCustomContent(customContentHeight, customContentWidth);
 125 
 126             final ContentMotion cm = new ContentMotion();
 127             testedScrollPane.setContent(content);
 128             cm.applyTransition(content);
 129 
 130             tb = new PropertiesTable(testedScrollPane);
 131             PropertyTablesFactory.explorePropertiesList(testedScrollPane, tb);
 132             SpecialTablePropertiesProvider.provideForControl(testedScrollPane, tb);
 133 
 134             if ((scrollPaneWidth > 0) && (scrollPaneHeight > 0)) {
 135                 testedScrollPane.setPrefViewportWidth(scrollPaneWidth);
 136                 testedScrollPane.setPrefViewportHeight(scrollPaneHeight);
 137             }
 138 
 139             Button changeContentButton = ButtonBuilder.create().id(CHANGE_CONTENT_BUTTON_ID).text("ChangeContent").build();

 140             changeContentButton.setOnAction(new EventHandler<ActionEvent>() {
 141                 public void handle(ActionEvent t) {
 142                     changeContent();
 143                     cm.applyTransition(content);
 144                 }
 145             });
 146 
 147             Button addPrefWidthAndHeightButton = ButtonBuilder.create().id(ADD_SIZE_BUTTON_ID).text("Add pref sizes").build();

 148             addPrefWidthAndHeightButton.setOnAction(new EventHandler<ActionEvent>() {
 149                 public void handle(ActionEvent t) {
 150                     tb.addDoublePropertyLine(testedScrollPane.prefWidthProperty(), -100, 200, 100);
 151                     tb.addDoublePropertyLine(testedScrollPane.prefHeightProperty(), -100, 200, 100);
 152                 }
 153             });
 154 
 155             Button setTextAreaAsContentButton = ButtonBuilder.create().id(CHANGE_CONTENT_TO_RESIZABLE_BUTTON_ID).text("Set blue pane as content").build();

 156             setTextAreaAsContentButton.setOnAction(new EventHandler<ActionEvent>() {
 157                 public void handle(ActionEvent t) {
 158                     setResizableContent();
 159                     Pane pane = (Pane) testedScrollPane.getContent();
 160                     tb.addDoublePropertyLine(pane.prefWidthProperty(), 0, 300, 100);
 161                     tb.addDoublePropertyLine(pane.prefHeightProperty(), 0, 300, 100);
 162                 }
 163             });
 164 
 165             Button setCustomContentButton = ButtonBuilder.create().id(CHANGE_CONTENT_TO_CUSTOM_BUTTON_ID).text("Set custom content").build();

 166             setCustomContentButton.setOnAction(new EventHandler<ActionEvent>() {
 167                 public void handle(ActionEvent t) {
 168                     setCustomContent();
 169                 }
 170             });
 171 
 172             Button buttonStart = ButtonBuilder.create().id(START_MOTION_BUTTON_ID).text("Start motion").build();

 173             buttonStart.setOnAction(new EventHandler<ActionEvent>() {
 174                 public void handle(ActionEvent t) {
 175                     cm.getTimeline().play();
 176                 }
 177             });
 178 
 179             Button rotateButton = ButtonBuilder.create().id(ROTATE_BUTTON_ID).text("Rotate on 30deg").build();

 180             rotateButton.setOnAction(new EventHandler<ActionEvent>() {
 181                 public void handle(ActionEvent t) {
 182                     content.setRotate(content.getRotate() + 30);
 183                 }
 184             });
 185 
 186             Button rotateScrollPaneButton = ButtonBuilder.create().id(ROTATE_SCROLLPANE_BUTTON_ID).text("Rotate scrollpane on 30deg").build();

 187             rotateScrollPaneButton.setOnAction(new EventHandler<ActionEvent>() {
 188                 public void handle(ActionEvent t) {
 189                     testedScrollPane.setRotate(testedScrollPane.getRotate() + 30);
 190                 }
 191             });
 192 
 193             Button increaseScaleButton = ButtonBuilder.create().id(INCREASE_SCALE_BUTTON_ID).text("Increase scale").build();

 194             increaseScaleButton.setOnAction(new EventHandler<ActionEvent>() {
 195                 public void handle(ActionEvent t) {
 196                     content.setScaleX(content.getScaleX() + 0.15);
 197                     content.setScaleY(content.getScaleY() + 0.15);
 198                 }
 199             });
 200 
 201             Button decreaseScaleButton = ButtonBuilder.create().id(DECREASE_SCALE_BUTTON_ID).text("Decrease scale").build();

 202             decreaseScaleButton.setOnAction(new EventHandler<ActionEvent>() {
 203                 public void handle(ActionEvent t) {
 204                     content.setScaleX(content.getScaleX() - 0.15);
 205                     content.setScaleY(content.getScaleY() - 0.15);
 206                 }
 207             });
 208 
 209             Button increaseScrollPaneScaleButton = ButtonBuilder.create().id(INCREASE_SCROLLPANE_SCALE_BUTTON_ID).text("Increase ScrollPane scale").build();

 210             increaseScrollPaneScaleButton.setOnAction(new EventHandler<ActionEvent>() {
 211                 public void handle(ActionEvent t) {
 212                     testedScrollPane.setScaleX(testedScrollPane.getScaleX() + 0.15);
 213                     testedScrollPane.setScaleY(testedScrollPane.getScaleY() + 0.15);
 214                 }
 215             });
 216 
 217             Button decreaseScrollPaneScaleButton = ButtonBuilder.create().id(DECREASE_SCROLLPANE_SCALE_BUTTON_ID).text("Decrease ScrollPane scale").build();

 218             decreaseScrollPaneScaleButton.setOnAction(new EventHandler<ActionEvent>() {
 219                 public void handle(ActionEvent t) {
 220                     testedScrollPane.setScaleX(testedScrollPane.getScaleX() - 0.15);
 221                     testedScrollPane.setScaleY(testedScrollPane.getScaleY() - 0.15);
 222                 }
 223             });
 224 
 225             Button resetButton = ButtonBuilder.create().id(RESET_BUTTON_ID).text("Reset").build();

 226             resetButton.setOnAction(new EventHandler<ActionEvent>() {
 227                 public void handle(ActionEvent t) {
 228                     HBox hb = (HBox) getRoot();
 229                     hb.getChildren().clear();
 230                     prepareMainSceneStructure();
 231                     prepareScene();
 232                 }
 233             });
 234 
 235             setTestedControl(testedScrollPane);
 236 
 237             VBox vb = new VBox();
 238             vb.setSpacing(5);
 239             vb.getChildren().addAll(changeContentButton, setCustomContentButton, setTextAreaAsContentButton,
 240                     buttonStart, rotateButton, rotateScrollPaneButton,
 241                     increaseScrollPaneScaleButton, decreaseScrollPaneScaleButton, getAddGridPaneForm(),
 242                     increaseScaleButton, decreaseScaleButton, resetButton, addPrefWidthAndHeightButton);
 243             setControllersContent(vb);
 244 
 245             setPropertiesContent(tb);
 246         }
 247 
 248         private Node getAddGridPaneForm() {
 249             final TextField dimension = TextFieldBuilder.create().id(GRID_DIMENSION_TEXTFIELD_ID).promptText("int-dimension").maxWidth(50).build();
 250             Button addButton = ButtonBuilder.create().id(ADD_GRID_BUTTON_ID).text("Add grid").build();




 251             addButton.setOnAction(new EventHandler<ActionEvent>() {
 252                 public void handle(ActionEvent t) {
 253                     int c = Integer.parseInt(dimension.getText());
 254 
 255                     GridPane gridPane = new GridPane();
 256                     for (int i = 0; i < c; i++) {
 257                         for (int j = 0; j < c; j++) {
 258                             final String name = "B-" + String.valueOf(i) + "-" + String.valueOf(j);
 259                             gridPane.add(ButtonBuilder.create().text(name).id(name).minHeight(10 * i).minWidth(10 * j).build(), i, j);




 260                         }
 261                     }
 262                     testedScrollPane.setContent(gridPane);
 263                 }
 264             });
 265 
 266             HBox hb = new HBox(5);
 267             hb.getChildren().addAll(dimension, addButton);
 268             return hb;
 269         }
 270 
 271         private void setResizableContent() {
 272             Pane canvas = new Pane();
 273             canvas.setStyle("-fx-background-color: blue;");
 274             testedScrollPane.setContent(canvas);
 275 
 276 
 277             canvas.setPrefHeight(100);
 278             canvas.setPrefWidth(100);
 279         }
 280 
 281         private Group setCustomContent(int height, int width) {
 282             Group g = ComponentsFactory.createCustomContent(height, width);
 283             g.setId(CUSTOM_CONTENT_ID);
 284             testedScrollPane.setContent(g);
 285             return g;
 286         }
 287 
 288         private Group setCustomContent() {
 289             return setCustomContent(200, 200);
 290         }
 291 
 292         private void changeContent() {
 293             VBox vb = new VBox();
 294             Button button = ButtonBuilder.create().id(CONTENT_BUTTON).text("Press me").build();
 295             final TextField tf1 = TextFieldBuilder.create().id(CONTENT_TEXT_FIELD_ID).text("0").build();


 296             button.setOnAction(new EventHandler<ActionEvent>() {
 297                 public void handle(ActionEvent t) {
 298                     tf1.setText(String.valueOf(Integer.parseInt(tf1.getText()) + 1));
 299                 }
 300             });
 301             TextArea tf2 = TextAreaBuilder.create().prefHeight(100).id(CONTENT_TEXT_AREA_ID).build();


 302             for (int i = 0; i < 15; i++) {
 303                 tf2.appendText("text" + i + "\n");
 304             }
 305             Button empty = ButtonBuilder.create().text("This is empty-action button").id(WITHOUT_ACTION_BUTTON).build();

 306 
 307             vb.getChildren().addAll(button, tf1, tf2, empty);
 308             vb.setStyle("-fx-border-color: blue;");
 309 
 310             testedScrollPane.setContent(vb);
 311         }
 312     }
 313 }
--- EOF ---