1 /*
   2  * Copyright (c) 2009, 2012, 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  */
  24 package test.scenegraph.fullscreen;
  25 
  26 
  27 
  28 import java.util.logging.Level;
  29 import java.util.logging.Logger;
  30 import javafx.animation.*;
  31 import javafx.application.Application;
  32 import javafx.beans.value.ChangeListener;
  33 import javafx.beans.value.ObservableValue;
  34 import javafx.event.ActionEvent;
  35 import javafx.event.Event;
  36 import javafx.event.EventHandler;
  37 import javafx.geometry.Insets;
  38 import javafx.geometry.Pos;
  39 import javafx.scene.Scene;
  40 import javafx.scene.control.*;
  41 import javafx.scene.effect.InnerShadow;
  42 import javafx.scene.input.KeyCombination;
  43 import javafx.scene.input.MouseEvent;
  44 import javafx.scene.layout.*;
  45 import javafx.scene.paint.Color;
  46 import javafx.scene.shape.Circle;
  47 import javafx.scene.shape.Line;
  48 import javafx.scene.text.Text;
  49 import javafx.stage.Modality;
  50 import javafx.stage.Screen;
  51 import javafx.stage.Stage;
  52 import javafx.util.Duration;
  53 import test.javaclient.shared.Utils;
  54 
  55 /**
  56 *
  57 * @author alexander
  58 */
  59 public class FSTestApp extends Application {
  60 
  61     //if stage resizable
  62     private static boolean resizable = true;
  63 
  64 
  65     private final StringBuilder log = new StringBuilder();
  66 
  67     /**
  68     * @param args the command line arguments
  69     */
  70     public static void main(String[] args) {
  71         if((args != null) && (args.length != 0))
  72             resizable = Boolean.valueOf(args[0]);
  73 
  74         Utils.launch(FSTestApp.class, args);
  75     }
  76 
  77     @Override
  78     public void start(final Stage primaryStage) {
  79         primaryStage.setTitle("FSTestApp");
  80         primaryStage.setResizable(resizable);
  81 
  82         GridPane buttonsPane = new GridPane();
  83                 buttonsPane.setPadding(new Insets(10));
  84                 buttonsPane.setAlignment(Pos.CENTER);
  85                 buttonsPane.getColumnConstraints().add(new ColumnConstraints(200));
  86                 buttonsPane.getColumnConstraints().add(new ColumnConstraints(200));
  87                 buttonsPane.getRowConstraints().add(new RowConstraints(30));
  88                 buttonsPane.getRowConstraints().add(new RowConstraints(30));
  89                 buttonsPane.getRowConstraints().add(new RowConstraints(30));
  90                 buttonsPane.getRowConstraints().add(new RowConstraints(30));
  91         buttonsPane.getRowConstraints().add(new RowConstraints(30));
  92 
  93         final TextArea logTextArea = new TextArea();
  94                 logTextArea.setEditable(false);
  95 
  96         //Timer for update logTextArea
  97         Timeline updateLogTimeline = new Timeline(new KeyFrame(Duration.seconds(0.1), new EventHandler<ActionEvent>() {
  98                     public void handle(ActionEvent t) {
  99                         logTextArea.insertText(logTextArea.getLength(), log.toString());
 100                         log.delete(0, log.length());
 101 
 102                     }
 103                 }, ( KeyValue[])null));  // cast null to suppress compiler warning
 104                 updateLogTimeline.setCycleCount(-1);
 105 
 106         //<editor-fold defaultstate="collapsed" desc="Menu">
 107                 Menu menu1 = new Menu("_Menu1");
 108                 menu1.setOnShown(new EventHandler<Event>() {
 109                         public void handle(Event t) {
 110                                 log.append("Menu 1 Shown\n");
 111                         }
 112                 });
 113                 menu1.setOnHidden(new EventHandler<Event>() {
 114                         public void handle(Event t) {
 115                                 log.append("Menu 1 Hidden\n");
 116                         }
 117                 });
 118                 menu1.setMnemonicParsing(true);
 119                 MenuItem mi11 = new MenuItem("Menu _Item 1");
 120                 mi11.setAccelerator(KeyCombination.keyCombination("ctrl+m"));
 121                 mi11.setMnemonicParsing(true);
 122                 mi11.setOnAction(new EventHandler<ActionEvent>() {
 123                         public void handle(ActionEvent t) {
 124                                 log.append("Menu Item 1 Action\n");
 125                         }
 126                 });
 127                 MenuItem mi12 = new MenuItem("Menu I_tem 2");
 128                 mi12.setMnemonicParsing(true);
 129                 menu1.getItems().addAll(mi11, mi12);
 130                 
 131                 Menu menu2 = new Menu("M_enu2");
 132                 menu2.setMnemonicParsing(true);
 133                 menu2.setOnShown(new EventHandler<Event>() {
 134                         public void handle(Event t) {
 135                                 log.append("Menu 2 Shown\n");
 136                         }
 137                 });
 138                 menu2.setOnHidden(new EventHandler<Event>() {
 139                         public void handle(Event t) {
 140                                 log.append("Menu 2 Hidden\n");
 141                         }
 142                 });
 143                 MenuItem mi21 = new MenuItem("Menu _Item 1");
 144                 mi21.setMnemonicParsing(true);
 145                 MenuItem mi22 = new MenuItem("Menu I_tem 2");
 146                 mi22.setMnemonicParsing(true);
 147                 MenuItem mi23 = new MenuItem("Menu Ite_m 3");
 148                 mi23.setMnemonicParsing(true);
 149                 menu1.getItems().addAll(mi21, mi22, mi23);
 150                 
 151         MenuBar menu = new MenuBar(menu1, menu2);
 152         menu.setUseSystemMenuBar(true);
 153         //</editor-fold>
 154 
 155         //<editor-fold defaultstate="collapsed" desc="Fullscreen indicator">
 156         final Circle fullscreenIndicator = new Circle(6);
 157                 fullscreenIndicator.setFill(Color.RED);
 158                 fullscreenIndicator.setEffect(new InnerShadow());
 159 
 160         primaryStage.fullScreenProperty().addListener(new ChangeListener<Boolean>() {
 161 
 162             @Override
 163             public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
 164                 fullscreenIndicator.setFill(newValue.booleanValue() ? Color.GREEN : Color.RED);
 165             }
 166         });
 167         //</editor-fold>
 168 
 169         //<editor-fold defaultstate="collapsed" desc="Fullscreen false Button">
 170         Button setFSFalseButton = new Button("Set fullscreen false");
 171                 setFSFalseButton.setAlignment(Pos.CENTER);
 172                 setFSFalseButton.setPrefWidth(180);
 173                 setFSFalseButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
 174                         @Override
 175                         public void handle(MouseEvent arg0) {
 176                                 primaryStage.setFullScreen(false);
 177                         }
 178                 });
 179         buttonsPane.add(setFSFalseButton, 0, 0);
 180         //</editor-fold>
 181 
 182         //<editor-fold defaultstate="collapsed" desc="Fullscreen true Button">
 183         Button setFSTrueButton = new Button("Set fullscreen true");
 184                 setFSTrueButton.setAlignment(Pos.CENTER);
 185                 setFSTrueButton.setPrefWidth(180);
 186                 setFSTrueButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
 187                         @Override
 188                         public void handle(MouseEvent arg0) {
 189                                 primaryStage.setFullScreen(true);
 190                         }
 191                 });
 192         buttonsPane.add(setFSTrueButton, 1, 0);
 193         //</editor-fold>
 194 
 195         //<editor-fold defaultstate="collapsed" desc="Center Button">
 196         Button centerButton = new Button("Center");
 197                 centerButton.setAlignment(Pos.CENTER);
 198                 centerButton.setPrefWidth(180);
 199                 centerButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
 200                         @Override
 201                         public void handle(MouseEvent arg0) {
 202                                 primaryStage.centerOnScreen();
 203                         }
 204                 });
 205         buttonsPane.add(centerButton, 0, 1);
 206         //</editor-fold>
 207 
 208         //<editor-fold defaultstate="collapsed" desc="Close Button">
 209         Button closeButton = new Button("Close");
 210                 closeButton.setAlignment(Pos.CENTER);
 211                 closeButton.setPrefWidth(180);
 212                 closeButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
 213                         @Override
 214                         public void handle(MouseEvent arg0) {
 215                                 primaryStage.close();
 216                         }
 217                 });
 218         buttonsPane.add(closeButton, 1, 1);
 219         //</editor-fold>
 220 
 221         //<editor-fold defaultstate="collapsed" desc="Size to scene Button">
 222         Button sizeToSceneButton = new Button("Size to scene");
 223                 sizeToSceneButton.setAlignment(Pos.CENTER);
 224                 sizeToSceneButton.setPrefWidth(180);
 225                 sizeToSceneButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
 226                         @Override
 227                         public void handle(MouseEvent arg0) {
 228                                 primaryStage.sizeToScene();
 229                         }
 230                 });
 231         buttonsPane.add(sizeToSceneButton, 0, 2);
 232         //</editor-fold>
 233 
 234         //<editor-fold defaultstate="collapsed" desc="Show modality window Button">
 235         Button showModalityWindowButton = new Button("Show modality window");
 236                 showModalityWindowButton.setAlignment(Pos.CENTER);
 237                 showModalityWindowButton.setPrefWidth(180);
 238                 showModalityWindowButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
 239                         @Override
 240                         public void handle(MouseEvent arg0) {
 241                                 Stage modalityStage = new Stage();
 242                                 modalityStage.initModality(Modality.APPLICATION_MODAL);
 243                                 modalityStage.setScene(createTestGridScene());
 244                                 modalityStage.setResizable(resizable);
 245                                 modalityStage.show();
 246                         }
 247                 });
 248         buttonsPane.add(showModalityWindowButton, 1, 2);
 249         //</editor-fold>
 250 
 251         //<editor-fold defaultstate="collapsed" desc="Resizable Button">
 252         Button resizableButton = new Button();
 253                 resizableButton.setText(resizable ? "Not resizable" : "Resizable");
 254                 resizableButton.setAlignment(Pos.CENTER);
 255                 resizableButton.setPrefWidth(180);
 256                 resizableButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
 257                         @Override
 258                         public void handle(MouseEvent arg0) {
 259                                 runJVMProcess(FSTestApp.this.getClass().getName(), String.valueOf(!resizable));
 260                                 primaryStage.close();
 261                         }
 262                 });
 263         buttonsPane.add(resizableButton, 0, 3);
 264         //</editor-fold>
 265 
 266         //<editor-fold defaultstate="collapsed" desc="Opacity Slider">
 267         Slider opacitySlider = new Slider(0, 1, 1);
 268                 opacitySlider.setMaxWidth(180);
 269         primaryStage.opacityProperty().bindBidirectional(opacitySlider.valueProperty());
 270                 buttonsPane.add(opacitySlider, 1, 3);
 271         //</editor-fold>
 272 
 273         //<editor-fold defaultstate="collapsed" desc="System menu ToggleButton">
 274         ToggleButton useSystemMenuToggleButton = new ToggleButton("Use system menu");
 275                 useSystemMenuToggleButton.setAlignment(Pos.CENTER);
 276                 useSystemMenuToggleButton.setPrefWidth(180);
 277                 menu.useSystemMenuBarProperty().bindBidirectional(useSystemMenuToggleButton.selectedProperty());
 278 
 279         buttonsPane.add(useSystemMenuToggleButton, 0, 4);
 280         //</editor-fold>
 281 
 282         HBox temp = new HBox(fullscreenIndicator, new Text("Fullscreen"));
 283                 temp.setAlignment(Pos.CENTER);
 284                 temp.setPadding(new Insets(5));
 285                 VBox root = new VBox(menu, temp, buttonsPane, logTextArea);
 286         primaryStage.setScene(new Scene(root, 400, 400));
 287         primaryStage.setMinHeight(400);
 288         primaryStage.setMinWidth(400);
 289         primaryStage.show();
 290 
 291         updateLogTimeline.play();
 292     }
 293 
 294     /**
 295      * Run class in new process
 296      * @param className name of class
 297      * @param arg run argument
 298      */
 299     private void runJVMProcess(String className, String arg) {
 300         try {
 301             String cp = System.getProperty("java.class.path");
 302             String pathToJava = System.getProperty("java.home") + "/bin/java";
 303 
 304             Runtime.getRuntime().exec(new String[]{pathToJava, "-cp", cp, className, arg});
 305         } catch (Exception ex) {
 306             Logger.getLogger(FSTestApp.class.getName()).log(Level.SEVERE, null, ex);
 307         }
 308     }
 309 
 310 
 311     /**
 312      * Create scene with grid.
 313      * @return new scene
 314      */
 315     private Scene createTestGridScene(){
 316         double height = getMaxScreenHeight();
 317         double width = getMaxScreenWidth();
 318 
 319 
 320         Pane root  = new Pane();
 321                 root.setMinHeight(height);
 322                 root.setMinWidth(width);
 323                 
 324         for (int x = 0; x < width; x+=10) {
 325             root.getChildren().add(new Line(x, x, 0, height));
 326         }
 327 
 328         for (int y = 0; y < height; y+=10) {
 329             root.getChildren().add(new Line(0, width, y, y));
 330         }
 331 
 332         return new Scene(root, 200, 200);
 333     }
 334 
 335     /**
 336      * Get maximum height of all monitors.
 337      * @return maximum height
 338      */
 339     private double getMaxScreenHeight(){
 340         double result = 0;
 341 
 342         for(Screen screen : Screen.getScreens()){
 343             if(screen.getBounds().getHeight() > result){
 344                 result = screen.getBounds().getHeight();
 345             }
 346         }
 347 
 348         return result;
 349     }
 350 
 351     /**
 352      * Get maximum width of all monitors.
 353      * @return maximum width
 354      */
 355     private double getMaxScreenWidth(){
 356         double result = 0;
 357 
 358         for(Screen screen : Screen.getScreens()){
 359             if(screen.getBounds().getWidth() > result){
 360                 result = screen.getBounds().getWidth();
 361             }
 362         }
 363 
 364         return result;
 365     }
 366 
 367 
 368 }