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.pagination;
  26 
  27 import javafx.event.ActionEvent;
  28 import javafx.event.EventHandler;
  29 import javafx.scene.Node;
  30 import javafx.scene.Scene;
  31 import javafx.scene.control.Button;
  32 import javafx.scene.control.Label;
  33 import javafx.scene.control.Pagination;
  34 import javafx.scene.control.test.utils.*;
  35 import javafx.scene.control.test.utils.ptables.PropertiesTable;
  36 import javafx.scene.control.test.utils.ptables.PropertyTablesFactory;
  37 import javafx.scene.control.test.utils.ptables.SpecialTablePropertiesProvider;
  38 import javafx.scene.layout.HBox;
  39 import javafx.scene.layout.VBox;
  40 import javafx.util.Callback;
  41 import static org.junit.Assert.assertFalse;
  42 import static org.junit.Assert.assertTrue;
  43 import test.javaclient.shared.InteroperabilityApp;
  44 import test.javaclient.shared.Utils;
  45 
  46 /**
  47  * @author Alexander Kirov
  48  */
  49 public class PaginationApp extends InteroperabilityApp {
  50 
  51     public final static String TESTED_PAGINATION_ID = "TESTED_PAGINATION_ID";
  52     public final static String HARD_RESET_BUTTON_ID = "HARD_RESET_PAGINATION_BUTTON_ID";
  53     public final static String SOFT_RESET_BUTTON_ID = "SOFT_RESET_PAGINATION_BUTTON_ID";
  54     public final static String SET_NEW_PAGE_FACTORY_BUTTON_ID = "SET_NEW_PAGE_FACTORY_BUTTON_ID";
  55     public final static String SET_OLD_PAGE_FACTORY_BUTTON_ID = "SET_OLD_PAGE_FACTORY_BUTTON_ID";
  56     public final static String SET_BULLET_PAGE_INDICATOR_BUTTON_ID = "SET_BULLET_PAGE_INDICATOR_BUTTON_ID";
  57     public final static String SET_PAGE_COUNT_TO_INDETERMINATE_BUTTON_ID = "SET_PAGE_COUNT_TO_INDETERMINATE_BUTTON_ID";
  58     public final static String OLD_FACTORY_MARKER = "factory : old";
  59     public final static String NEW_FACTORY_MARKER = "factory : new";
  60     public final static String PAGE_INDEX_PREFIX = "PAGE : ";
  61     public final static int LABELS_PER_PAGE = 10;
  62 
  63     public static void main(String[] args) {
  64         Utils.launch(PaginationApp.class, args);
  65     }
  66 
  67     @Override
  68     protected Scene getScene() {
  69         Utils.setTitleToStage(stage, "PaginationTestApp");
  70         return new PaginationApp.PaginationScene();
  71     }
  72 
  73     class PaginationScene extends CommonPropertiesScene {
  74 
  75         PropertiesTable tb;
  76         Pagination testedPagination;
  77         final int i = 10;
  78         private String[] content;
  79         private static final int MAX_CONTENT = 2500;
  80 
  81         public PaginationScene() {
  82             super("Pagination", 800, 600);
  83 
  84             prepareScene();
  85         }
  86 
  87         @Override
  88         final protected void prepareScene() {
  89             Utils.addBrowser(this);
  90 
  91             content = new String[MAX_CONTENT];
  92             for (int i = 0; i < MAX_CONTENT; i++) {
  93                 content[i] = "some text " + i;
  94             }
  95 
  96             testedPagination = new Pagination(i);
  97             testedPagination.setId(TESTED_PAGINATION_ID);
  98 
  99             testedPagination.setPageFactory(new Callback<Integer, Node>() {
 100                 @Override
 101                 public Node call(Integer pageIndex) {
 102                     return createPage(pageIndex);
 103                 }
 104             });
 105 
 106             tb = new PropertiesTable(testedPagination);
 107             PropertyTablesFactory.explorePropertiesList(testedPagination, tb);
 108             SpecialTablePropertiesProvider.provideForControl(testedPagination, tb);
 109             tb.addDoublePropertyLine(testedPagination.minHeightProperty(), -100, 300, 50, testedPagination);
 110             tb.addDoublePropertyLine(testedPagination.minWidthProperty(), -100, 300, 50, testedPagination);
 111 
 112             Button hardResetButton = new Button("Hard reset");
 113             hardResetButton.setId(HARD_RESET_BUTTON_ID);
 114             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
 115                 public void handle(ActionEvent t) {
 116                     HBox hb = (HBox) getRoot();
 117                     hb.getChildren().clear();
 118                     prepareMainSceneStructure();
 119                     prepareScene();
 120                 }
 121             });
 122 
 123             Button setBulletPageIndicatorButton = new Button("Set bullet");
 124             setBulletPageIndicatorButton.setId(SET_BULLET_PAGE_INDICATOR_BUTTON_ID);
 125             setBulletPageIndicatorButton.setOnAction(new EventHandler<ActionEvent>() {
 126                 public void handle(ActionEvent t) {
 127                     //testedPagination.setStyle(Pagination.STYLE_CLASS_BULLET);
 128                     testedPagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);
 129                 }
 130             });
 131 
 132             Button setPageCountToIndeterminateButton = new Button("Set indeterminate\n page count");
 133             setPageCountToIndeterminateButton.setId(SET_PAGE_COUNT_TO_INDETERMINATE_BUTTON_ID);
 134             setPageCountToIndeterminateButton.setOnAction(new EventHandler<ActionEvent>() {
 135                 public void handle(ActionEvent t) {
 136                     testedPagination.setPageCount(Pagination.INDETERMINATE);
 137                 }
 138             });
 139 
 140             Button softResetButton = new Button("Soft reset");
 141             softResetButton.setId(SOFT_RESET_BUTTON_ID);
 142             softResetButton.setOnAction(new EventHandler<ActionEvent>() {
 143                 @Override
 144                 public void handle(ActionEvent t) {
 145                     tb.refresh();
 146                     Pagination newOne = new Pagination(i);
 147                     testedPagination.setCurrentPageIndex(newOne.getCurrentPageIndex());
 148                     testedPagination.setPageCount(newOne.getPageCount());
 149                     testedPagination.setPageFactory(newOne.getPageFactory());
 150                     testedPagination.setMaxPageIndicatorCount(newOne.maxPageIndicatorCountProperty().get());
 151                 }
 152             });
 153 
 154             Button setNewPageFactory = new Button("Set new page factory");
 155             setNewPageFactory.setId(SET_NEW_PAGE_FACTORY_BUTTON_ID);
 156             setNewPageFactory.setOnAction(new EventHandler<ActionEvent>() {
 157                 @Override
 158                 public void handle(ActionEvent t) {
 159                     Callback<Integer, Node> newOne = new Callback<Integer, Node>() {
 160                         @Override
 161                         public Node call(Integer pageIndex) {
 162                             return createPage2(pageIndex);
 163                         }
 164                     };
 165 
 166                     Callback<Integer, Node> oldOne = testedPagination.getPageFactory();
 167 
 168                     testedPagination.setPageFactory(newOne);
 169 
 170                     assertTrue(testedPagination.pageFactoryProperty().getValue().equals(newOne));
 171                     assertTrue(testedPagination.getPageFactory().equals(newOne));
 172                     assertFalse(testedPagination.pageFactoryProperty().getValue().equals(oldOne));
 173                     assertFalse(testedPagination.getPageFactory().equals(oldOne));
 174                 }
 175             });
 176 
 177             Button setOldPageFactory = new Button("Set old page factory");
 178             setOldPageFactory.setId(SET_OLD_PAGE_FACTORY_BUTTON_ID);
 179             setOldPageFactory.setOnAction(new EventHandler<ActionEvent>() {
 180                 @Override
 181                 public void handle(ActionEvent t) {
 182                     Callback<Integer, Node> newOne = new Callback<Integer, Node>() {
 183                         @Override
 184                         public Node call(Integer pageIndex) {
 185                             return createPage(pageIndex);
 186                         }
 187                     };
 188 
 189                     Callback<Integer, Node> oldOne = testedPagination.getPageFactory();
 190 
 191                     testedPagination.setPageFactory(newOne);
 192 
 193                     assertTrue(testedPagination.pageFactoryProperty().getValue().equals(newOne));
 194                     assertTrue(testedPagination.getPageFactory().equals(newOne));
 195                     assertFalse(testedPagination.pageFactoryProperty().getValue().equals(oldOne));
 196                     assertFalse(testedPagination.getPageFactory().equals(oldOne));
 197                 }
 198             });
 199 
 200             HBox resetButtonsHBox = new HBox();
 201             resetButtonsHBox.getChildren().addAll(hardResetButton, softResetButton);
 202 
 203             VBox vb = new VBox();
 204             vb.setSpacing(5);
 205             vb.getChildren().addAll(resetButtonsHBox, setNewPageFactory,
 206                     setOldPageFactory, setBulletPageIndicatorButton,
 207                     setPageCountToIndeterminateButton);
 208             setControllersContent(vb);
 209             setTestedControl(testedPagination);
 210             setPropertiesContent(tb);
 211         }
 212 
 213         public VBox createPage(int pageIndex) {
 214             checkIndexCorrectness(pageIndex);
 215 
 216             VBox box = new VBox(5);
 217             int page = pageIndex * 10;
 218             for (int i = 0; i < 10; i++) {
 219                 Label l = new Label(PAGE_INDEX_PREFIX + pageIndex + "; " + OLD_FACTORY_MARKER + "; " + content[page + i % MAX_CONTENT]);
 220                 box.getChildren().add(l);
 221             }
 222             box.getChildren().addAll(ComponentsFactory.createFormComponent(), ComponentsFactory.createCustomContent(100, 100));
 223             return box;
 224         }
 225 
 226         public VBox createPage2(int pageIndex) {
 227             checkIndexCorrectness(pageIndex);
 228 
 229             VBox box = new VBox(5);
 230             int page = pageIndex * 10;
 231             for (int i = 0; i < LABELS_PER_PAGE; i++) {
 232                 Label l = new Label(PAGE_INDEX_PREFIX + pageIndex + "; " + NEW_FACTORY_MARKER + "; " + content[page + i % MAX_CONTENT]);
 233                 box.getChildren().add(l);
 234             }
 235             box.getChildren().addAll(ComponentsFactory.createCustomContent(100, 100));
 236             return box;
 237         }
 238 
 239         private void checkIndexCorrectness(int index) {
 240             if (testedPagination.getPageCount() != Pagination.INDETERMINATE) {
 241                 if ((index < 0) || (index > Math.max(testedPagination.getPageCount() - 1, 0))) {
 242                     throw new IllegalArgumentException("Incorrect index : " + index);
 243                 }
 244             }
 245         }
 246     }
 247 }