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 /**
  26  *
  27  * @author Andrey Nazarov
  28  */
  29 package test.css.controls;
  30 
  31 import java.net.URL;
  32 import javafx.beans.property.SimpleStringProperty;
  33 import javafx.beans.property.StringProperty;
  34 import javafx.scene.Node;
  35 import javafx.scene.layout.Pane;
  36 import java.util.Set;
  37 import javafx.application.Platform;
  38 import javafx.scene.text.Text;
  39 import test.css.controls.styles.Style;
  40 
  41 import test.javaclient.shared.BasicButtonChooserApp;
  42 import test.javaclient.shared.ScrollablePageWithSlots;
  43 import test.javaclient.shared.TestNode;
  44 import test.javaclient.shared.Utils;
  45 
  46 public class ControlsCSSApp extends BasicButtonChooserApp {
  47 
  48     private static int width = 1500;
  49     private static int heigth = 900;
  50     private static boolean isSetterMode = false;
  51     public static final String SETTER_MODE = "SetterMode";
  52     TestNode rootTestNode = new TestNode();
  53 
  54     private static void setSetterMode(boolean isSetterMode) {
  55         ControlsCSSApp.isSetterMode = isSetterMode;
  56     }
  57 
  58     public ControlsCSSApp() {
  59         super(width, heigth, "CssControls", false);
  60     }
  61 
  62     public static void main(String args[]) {
  63         if (args != null && args.length > 0) {
  64             if (args[0].equals(SETTER_MODE)) {
  65                 ControlsCSSApp.setSetterMode(true); //Now there is no way to init app in Launcher
  66             }
  67         }
  68 
  69         Utils.launch(ControlsCSSApp.class, args);
  70     }
  71 
  72     private void createPages(ControlPage page, TestNode rootTestNode) {
  73         createPages(page, rootTestNode, null);
  74     }
  75 
  76 
  77     private void createPages(ControlPage page, TestNode rootTestNode, String styleName) {
  78         ScrollablePageWithSlots pageWithSlot = new ScrollablePageWithSlots(page.name(), width, height);
  79         rootTestNode.add(pageWithSlot);
  80         Set<Style> styles = ControlsCssStylesFactory.getStyles(page, isSetterMode);
  81         for (final Style style : styles) {
  82             if (styleName == null) {
  83                 createPage(pageWithSlot, page, style);
  84             }
  85             else if (style.name().equals(styleName)) {
  86                 createPage(pageWithSlot, page, style);
  87             }
  88         }
  89     }
  90 
  91     private void createPage(ScrollablePageWithSlots pageWithSlot, ControlPage page, Style style) {
  92         Pane slot = new Pane();
  93         slot.setPrefSize(page.slotWidth, page.slotHeight);
  94         final Node control = page.factory.createControl();
  95         Pane innerPane = new Pane();
  96         innerPane.setLayoutX(ControlPage.INNER_PANE_SHIFT);
  97         innerPane.setLayoutY(ControlPage.INNER_PANE_SHIFT);
  98         innerPane.setPrefSize(page.slotWidth - ControlPage.INNER_PANE_SHIFT, page.slotHeight - ControlPage.INNER_PANE_SHIFT);
  99         innerPane.setMaxSize(page.slotWidth - ControlPage.INNER_PANE_SHIFT, page.slotHeight - ControlPage.INNER_PANE_SHIFT);
 100         slot.getChildren().add(innerPane);
 101         style.decorate(control, innerPane);
 102         if (control instanceof Text) {
 103             control.relocate(0, 0); // workaround RT-24670
 104         }
 105         pageWithSlot.add(new StyleTestNode(style, control, innerPane));
 106     }
 107 
 108     public interface ControlFactory {
 109 
 110         /**
 111          * @return current control instance
 112          */
 113         public Node createControl();
 114     }
 115 
 116     @Override
 117     protected TestNode setup() {
 118         URL css = ControlsCSSApp.class.getResource("/test/css/resources/style.css");
 119         combinedTestChooserPresenter.getScene().getStylesheets().add(css.toExternalForm());
 120         if (showButtons) {
 121             for (ControlPage page : ControlPage.filteredValues()) {
 122                 createPages(page, rootTestNode);
 123             }
 124         }
 125         return rootTestNode;
 126     }
 127 
 128     public void open(final ControlPage page) {
 129         Platform.runLater(new Runnable() {
 130             @Override
 131             public void run() {
 132                 createPages(page, rootTestNode);
 133             }
 134         });
 135     }
 136 
 137     public static class Person {
 138 
 139         Person(String firstName, String lastName) {
 140             this.firstName = new SimpleStringProperty(firstName);
 141             this.lastName = new SimpleStringProperty(lastName);
 142         }
 143         public StringProperty firstName;
 144 
 145         public void setFirstName(String value) {
 146             firstName.set(value);
 147         }
 148 
 149         public String getFirstName() {
 150             return firstName.get();
 151         }
 152 
 153         public StringProperty firstNameProperty() {
 154             if (firstName == null) {
 155                 firstName = new SimpleStringProperty();
 156             }
 157             return firstName;
 158         }
 159         public StringProperty lastName;
 160 
 161         public void setLastName(String value) {
 162             lastName.set(value);
 163         }
 164 
 165         public String getLastName() {
 166             return lastName.get();
 167         }
 168 
 169         public StringProperty lastNameProperty() {
 170             if (lastName == null) {
 171                 lastName = new SimpleStringProperty();
 172             }
 173             return lastName;
 174         }
 175     }
 176 }