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;
  26 
  27 import java.util.Arrays;
  28 import javafx.event.ActionEvent;
  29 import javafx.scene.Group;
  30 import javafx.scene.Node;
  31 import javafx.scene.control.*;
  32 import javafx.scene.input.ScrollEvent;
  33 import javafx.scene.layout.HBox;
  34 import javafx.scene.layout.VBox;
  35 import javafx.scene.paint.Color;
  36 import javafx.scene.shape.Line;
  37 import javafx.scene.shape.Rectangle;
  38 
  39 /**
  40  * @author Alexander Kirov
  41  *
  42  * This class provides some widely used elements.
  43  *
  44  * Form: contains scrollBar (to check scrollEvents), textArea, Button (for
  45  * ClickEvents), and counters of events coming.
  46  *
  47  * Custom content: width and height are arguments. Some group of lines, which
  48  * has predefined size. can be used for cases, where screenshots are made.
  49  *
  50  * MultipleIndexFormComponent - can be used in tests, where you should provide
  51  * list of indices. (removeAll, selectAll functionality testing).
  52  */
  53 public class ComponentsFactory {
  54 
  55     public static final String CONTENT_RECTANGLE_ID = "CONTENT_RECTANGLE_ID";
  56     public static final String CONTENT_RECTANGLE_ID_PREFIX = "CONTENT_RECTANGLE_";
  57     public static final String FORM_BUTTON_ID = "FORM_BUTTON_ID";
  58     public static final String FORM_CLICK_TEXT_FIELD_ID = "FORM_CLICK_TEXT_FIELD_ID";
  59     public static final String FORM_SCROLLBAR_ID = "FORM_SCROLLBAR_ID";
  60     public static final String FORM_SCROLL_TEXT_FIELD_ID = "FORM_SCROLL_TEXT_FIELD_ID";
  61     public static final String FORM_TEXT_AREA_ID = "FORM_TEXT_AREA_ID";
  62     public static final String ID_SUFFIX = "_ID";
  63 
  64     public static VBox createFormComponent() {
  65         VBox vb = new VBox();
  66 
  67         HBox hb1 = new HBox();
  68         Button button = ButtonBuilder.create().id(FORM_BUTTON_ID).text("Press me").build();
  69         final TextField tf1 = TextFieldBuilder.create().id(FORM_CLICK_TEXT_FIELD_ID).prefWidth(50).text("0").build();
  70         button.setOnAction((ActionEvent t) -> {
  71             tf1.setText(String.valueOf(Integer.parseInt(tf1.getText()) + 1));
  72         });
  73         hb1.getChildren().addAll(button, tf1);
  74 
  75         HBox hb2 = new HBox();
  76         ScrollBar sb = new ScrollBar();
  77         sb.setMax(10);
  78         sb.setId(FORM_SCROLLBAR_ID);
  79         final TextField tf2 = TextFieldBuilder.create().id(FORM_SCROLL_TEXT_FIELD_ID).prefWidth(50).text("0").build();
  80         sb.addEventHandler(ScrollEvent.ANY, (ScrollEvent t) -> {
  81             tf2.setText(String.valueOf(Integer.parseInt(tf2.getText()) + 1));
  82         });
  83         hb2.getChildren().addAll(sb, tf2);
  84 
  85         TextArea ta = TextAreaBuilder.create().minHeight(50).prefHeight(100).prefWidth(50).id(FORM_TEXT_AREA_ID).build();
  86         for (int i = 0; i < 15; i++) {
  87             ta.appendText("text" + i + "\n");
  88         }
  89 
  90         vb.getChildren().addAll(hb1, hb2, ta);
  91         vb.setStyle("-fx-border-color: blue;");
  92         return vb;
  93     }
  94 
  95     public static Group createCustomContent(int height, int width) {
  96         Group res = new Group();
  97 
  98         Rectangle r = new Rectangle();
  99         r.setStroke(Color.BLACK);
 100         r.setStyle("-fx-border-color: GREEN;");
 101 
 102         res.getChildren().add(r);
 103 
 104         for (int i = 10; i < height; i += 10) {
 105             Line line1 = new Line(0, i, i - 5, i);
 106             Line line2 = new Line(i, 0, i, i - 5);
 107             Line line3 = new Line(i - 5, i, i - 5, height);
 108             Line line4 = new Line(i, i - 5, width, i - 5);
 109 
 110             line1.setStroke(Color.RED);
 111             line2.setStroke(Color.YELLOW);
 112             line3.setStroke(Color.BLUE);
 113             line4.setStroke(Color.MAGENTA);
 114 
 115             res.getChildren().addAll(line1, line2, line3, line4);
 116         }
 117 
 118         Rectangle rec = new Rectangle(0, 0, width, height);
 119         rec.setFill(Color.TRANSPARENT);
 120         rec.setStroke(Color.RED);
 121 
 122         res.getChildren().add(rec);
 123 
 124         return res;
 125     }
 126 
 127     public static class MultipleIndexFormComponent extends VBox {
 128 
 129         public static final String INDICES_DELIMITER = ";";
 130         private TextField indicesStorage = new TextField();
 131         private final Button actionButton = new Button();
 132 
 133         public MultipleIndexFormComponent(String actionName, Node additionalNodes, final MultipleIndicesAction action,
 134                 String actionBtnID, String indicesTxtFldId) {
 135 
 136             actionButton.setText(actionName);
 137             actionButton.setId(actionBtnID);
 138 
 139             indicesStorage.setPromptText("int" + INDICES_DELIMITER + " int" + INDICES_DELIMITER + "...");
 140             indicesStorage.setId(indicesTxtFldId);
 141 
 142             actionButton.setOnAction((ActionEvent t) -> {
 143                 int[] indices = parseFromString(indicesStorage.getText());
 144                 Arrays.sort(indices);
 145                 action.onAction(indices);
 146             });
 147 
 148             this.getChildren().addAll(indicesStorage, actionButton);
 149             if (additionalNodes != null) {
 150                 this.getChildren().add(1, additionalNodes);
 151             }
 152         }
 153 
 154         private static int[] parseFromString(String str) {
 155             try {
 156                 String[] indices = str.trim().split(INDICES_DELIMITER);
 157                 int intIndices[] = new int[indices.length];
 158                 for (int i = 0; i < indices.length; i++) {
 159                     intIndices[i] = Integer.parseInt(indices[i].trim());
 160                 }
 161                 return intIndices;
 162             } catch (NumberFormatException e) {
 163                 return null;
 164             }
 165         }
 166 
 167         public interface MultipleIndicesAction {
 168 
 169             public void onAction(int[] indices);
 170         }
 171     }
 172 }