1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package com.oracle.javafx.scenebuilder.app.about;
  33 
  34 import com.oracle.javafx.scenebuilder.app.SceneBuilderApp;
  35 import com.oracle.javafx.scenebuilder.app.i18n.I18N;
  36 import com.oracle.javafx.scenebuilder.kit.editor.panel.util.AbstractFxmlWindowController;
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.io.InputStream;
  40 import java.util.Properties;
  41 import javafx.fxml.FXML;
  42 import javafx.scene.control.TextArea;
  43 import javafx.scene.input.MouseEvent;
  44 import javafx.scene.layout.VBox;
  45 import javafx.stage.Modality;
  46 import javafx.stage.WindowEvent;
  47 
  48 /**
  49  *
  50  */
  51 public final class AboutWindowController extends AbstractFxmlWindowController {
  52 
  53     @FXML
  54     private VBox vbox;
  55     @FXML
  56     private TextArea textArea;
  57     
  58     private String sbBuildInfo = "PLACEHOLDER"; //NOI18N
  59     private String sbBuildDate = "PLACEHOLDER"; //NOI18N
  60     private String sbBuildJavaVersion = "PLACEHOLDER"; //NOI18N
  61     // The resource bundle contains two keys: about.copyright and about.copyright.open
  62     private String sbAboutCopyrightKeyName = "about.copyright.open"; //NOI18N
  63     // File name must be in sync with what we use in logging.properties
  64     private final String LOG_FILE_NAME = "scenebuilder-2.0.1.log"; //NOI18N
  65 
  66     public AboutWindowController() {
  67         super(AboutWindowController.class.getResource("About.fxml"), //NOI18N
  68                 I18N.getBundle());
  69         try (InputStream in = getClass().getResourceAsStream("about.properties")) { //NOI18N
  70 
  71             if (in != null) {
  72                 Properties sbProps = new Properties();
  73                 sbProps.load(in);
  74                 sbBuildInfo = sbProps.getProperty("build.info", "UNSET"); //NOI18N
  75                 sbBuildDate = sbProps.getProperty("build.date", "UNSET"); //NOI18N
  76                 sbBuildJavaVersion = sbProps.getProperty("build.java.version", "UNSET"); //NOI18N
  77                 sbAboutCopyrightKeyName = sbProps.getProperty("copyright.key.name", "UNSET"); //NOI18N
  78             }
  79         } catch (IOException ex) {
  80             // We go with default values
  81         }
  82     }
  83     
  84     @FXML
  85     public void onMousePressed(MouseEvent event) {
  86         if ((event.getClickCount() == 2) && event.isAltDown()) {
  87             SceneBuilderApp.getSingleton().toggleDebugMenu();
  88         }
  89     }
  90 
  91     @Override
  92     public void onCloseRequest(WindowEvent event) {
  93         closeWindow();
  94     }
  95 
  96     /*
  97      * AbstractWindowController
  98      */
  99     @Override
 100     protected void controllerDidCreateStage() {
 101         assert getRoot() != null;
 102         assert getRoot().getScene() != null;
 103         assert getRoot().getScene().getWindow() != null;
 104 
 105         getStage().setTitle(I18N.getString("about.title"));
 106         getStage().initModality(Modality.APPLICATION_MODAL);
 107     }
 108 
 109     @Override
 110     protected void controllerDidLoadFxml() {
 111         super.controllerDidLoadFxml();
 112         assert vbox != null;
 113         assert textArea != null;
 114         textArea.setText(getAboutText());
 115     }
 116 
 117     private String getAboutText() {
 118 
 119         StringBuilder text = getVersionParagraph()
 120                 .append(getBuildInfoParagraph())
 121                 .append(getLoggingParagraph())
 122                 .append(getJavaParagraph())
 123                 .append(getOsParagraph())
 124                 .append(I18N.getString(sbAboutCopyrightKeyName));
 125         
 126         return text.toString();
 127     }
 128     
 129     /**
 130      *
 131      * @treatAsPrivate
 132      */
 133     public String getBuildJavaVersion() {
 134         return sbBuildJavaVersion;
 135     }
 136     
 137     /**
 138      *
 139      * @treatAsPrivate
 140      */
 141     public String getBuildInfo() {
 142         return sbBuildInfo;
 143     }
 144     
 145     private StringBuilder getVersionParagraph() {
 146         StringBuilder sb = new StringBuilder(I18N.getString("about.product.version"));
 147         sb.append("\nJavaFX Scene Builder 2.0.1\n\n"); //NOI18N
 148         return sb;
 149     }
 150     private String getLogFilePath() {
 151         StringBuilder sb = new StringBuilder(System.getProperty("java.io.tmpdir")); //NOI18N
 152         if (sb.charAt(sb.length() - 1) != File.separatorChar) {
 153             sb.append(File.separatorChar);
 154         }
 155         sb.append(LOG_FILE_NAME);
 156         return sb.toString();
 157         
 158     }
 159 
 160     private StringBuilder getBuildInfoParagraph() {
 161         StringBuilder sb = new StringBuilder(I18N.getString("about.build.information"));
 162         sb.append("\n").append(sbBuildInfo).append("\n") //NOI18N
 163                 .append(I18N.getString("about.build.date", sbBuildDate))
 164                 .append("\n\n"); //NOI18N
 165         return sb;
 166     }
 167 
 168     private StringBuilder getLoggingParagraph() {
 169         StringBuilder sb = new StringBuilder(I18N.getString("about.logging.title"));
 170         sb.append("\n") //NOI18N
 171                 .append(I18N.getString("about.logging.body.first", LOG_FILE_NAME))
 172                 .append("\n") //NOI18N
 173                 .append(I18N.getString("about.logging.body.second", getLogFilePath()))
 174                 .append("\n\n"); //NOI18N
 175         return sb;
 176     }
 177     
 178     private StringBuilder getJavaParagraph() {
 179         StringBuilder sb = new StringBuilder("Java\n"); //NOI18N
 180         sb.append(System.getProperty("java.runtime.version")).append(", ") //NOI18N
 181                 .append(System.getProperty("java.vendor")).append("\n\n"); //NOI18N
 182         return sb;
 183     }
 184     
 185     private StringBuilder getOsParagraph() {
 186         StringBuilder sb = new StringBuilder(I18N.getString("about.operating.system"));
 187         sb.append("\n").append(System.getProperty("os.name")).append(", ") //NOI18N
 188                 .append(System.getProperty("os.arch")).append(", ") //NOI18N
 189                 .append(System.getProperty("os.version")).append("\n\n"); //NOI18N
 190         return sb;
 191     }
 192 }