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