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 = new Button("Press me"); 69 button.setId(FORM_BUTTON_ID); 70 final TextField tf1 = new TextField("0"); 71 tf1.setId(FORM_CLICK_TEXT_FIELD_ID); 72 tf1.setPrefWidth(50); 73 button.setOnAction((ActionEvent t) -> { 74 tf1.setText(String.valueOf(Integer.parseInt(tf1.getText()) + 1)); 75 }); 76 hb1.getChildren().addAll(button, tf1); 77 78 HBox hb2 = new HBox(); 79 ScrollBar sb = new ScrollBar(); 80 sb.setMax(10); 81 sb.setId(FORM_SCROLLBAR_ID); 82 final TextField tf2 = new TextField("0"); 83 tf2.setId(FORM_SCROLL_TEXT_FIELD_ID); 84 tf2.setPrefWidth(50); 85 sb.addEventHandler(ScrollEvent.ANY, (ScrollEvent t) -> { 86 tf2.setText(String.valueOf(Integer.parseInt(tf2.getText()) + 1)); 87 }); 88 hb2.getChildren().addAll(sb, tf2); 89 90 TextArea ta = new TextArea(); 91 ta.setMinHeight(50); 92 ta.setPrefHeight(100); 93 ta.setPrefWidth(50); 94 ta.setId(FORM_TEXT_AREA_ID); 95 for (int i = 0; i < 15; i++) { 96 ta.appendText("text" + i + "\n"); 97 } 98 99 vb.getChildren().addAll(hb1, hb2, ta); 100 vb.setStyle("-fx-border-color: blue;"); 101 return vb; 102 } 103 104 public static Group createCustomContent(int height, int width) { 105 Group res = new Group(); 106 107 Rectangle r = new Rectangle(); 108 r.setStroke(Color.BLACK); 109 r.setStyle("-fx-border-color: GREEN;"); 110 111 res.getChildren().add(r); 112 113 for (int i = 10; i < height; i += 10) { 114 Line line1 = new Line(0, i, i - 5, i); 115 Line line2 = new Line(i, 0, i, i - 5); 116 Line line3 = new Line(i - 5, i, i - 5, height); 117 Line line4 = new Line(i, i - 5, width, i - 5); 118 119 line1.setStroke(Color.RED); 120 line2.setStroke(Color.YELLOW); 121 line3.setStroke(Color.BLUE); 122 line4.setStroke(Color.MAGENTA); 123 124 res.getChildren().addAll(line1, line2, line3, line4); 125 } 126 127 Rectangle rec = new Rectangle(0, 0, width, height); 128 rec.setFill(Color.TRANSPARENT); 129 rec.setStroke(Color.RED); 130 131 res.getChildren().add(rec); 132 133 return res; 134 } 135 136 public static class MultipleIndexFormComponent extends VBox { 137 138 public static final String INDICES_DELIMITER = ";"; 139 private TextField indicesStorage = new TextField(); 140 private final Button actionButton = new Button(); 141 142 public MultipleIndexFormComponent(String actionName, Node additionalNodes, final MultipleIndicesAction action, 143 String actionBtnID, String indicesTxtFldId) { 144 145 actionButton.setText(actionName); 146 actionButton.setId(actionBtnID); 147 148 indicesStorage.setPromptText("int" + INDICES_DELIMITER + " int" + INDICES_DELIMITER + "..."); 149 indicesStorage.setId(indicesTxtFldId); 150 151 actionButton.setOnAction((ActionEvent t) -> { 152 int[] indices = parseFromString(indicesStorage.getText()); 153 Arrays.sort(indices); 154 action.onAction(indices); 155 }); 156 157 this.getChildren().addAll(indicesStorage, actionButton); 158 if (additionalNodes != null) { 159 this.getChildren().add(1, additionalNodes); 160 } 161 } 162 163 private static int[] parseFromString(String str) { 164 try { 165 String[] indices = str.trim().split(INDICES_DELIMITER); 166 int intIndices[] = new int[indices.length]; 167 for (int i = 0; i < indices.length; i++) { 168 intIndices[i] = Integer.parseInt(indices[i].trim()); 169 } 170 return intIndices; 171 } catch (NumberFormatException e) { 172 return null; 173 } 174 } 175 176 public interface MultipleIndicesAction { 177 178 public void onAction(int[] indices); 179 } 180 } 181 }