1 /* 2 * Copyright (c) 2011, 2013, 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 package com.sun.javafx.appmanager; 27 28 import java.io.ByteArrayOutputStream; 29 import java.io.IOException; 30 import java.io.OutputStream; 31 import java.io.PrintStream; 32 import java.util.concurrent.CountDownLatch; 33 import javafx.application.Application; 34 import javafx.application.Platform; 35 import javafx.scene.Node; 36 import javafx.scene.Scene; 37 import javafx.scene.control.Label; 38 import javafx.scene.control.TextArea; 39 import javafx.scene.layout.BorderPane; 40 import javafx.scene.text.Font; 41 import javafx.stage.Stage; 42 43 public final class BootstrapApplication extends Application { 44 private static final CountDownLatch appStartedLatch = 45 new CountDownLatch(1); 46 47 @Override 48 public void start(final Stage primaryStage) { 49 // Platform.setImplicitExit(false); 50 final BorderPane root = new BorderPane(); 51 root.setCenter(new Label("Mobile Center")); 52 root.setBottom(createOutputArea()); 53 54 primaryStage.setTitle("Mobile Center"); 55 primaryStage.setScene(new Scene(root)); 56 primaryStage.show(); 57 58 appStartedLatch.countDown(); 59 } 60 61 public static void waitForStart() throws InterruptedException { 62 appStartedLatch.await(); 63 } 64 65 private static Node createOutputArea() { 66 final TextArea textArea = new TextArea(); 67 textArea.setPrefColumnCount(80); 68 textArea.setPrefRowCount(25); 69 textArea.setEditable(false); 70 textArea.setFont(Font.font("Monospaced")); 71 textArea.setWrapText(true); 72 73 final OutputStream textAreaStream = new TextAreaStream(textArea); 74 System.setOut(new PrintStream(textAreaStream, true)); 75 System.setErr(new PrintStream(textAreaStream, true)); 76 77 return textArea; 78 } 79 80 private static final class TextAreaStream extends OutputStream 81 implements Runnable { 82 private final TextArea textArea; 83 private final ByteArrayOutputStream byteBuffer; 84 private final StringBuilder textToAppend; 85 86 public TextAreaStream(final TextArea textArea) { 87 this.textArea = textArea; 88 this.byteBuffer = new ByteArrayOutputStream(); 89 this.textToAppend = new StringBuilder(); 90 } 91 92 @Override 93 public void write(final int value) { 94 synchronized (byteBuffer) { 95 byteBuffer.write(value); 96 } 97 } 98 99 @Override 100 public void write(final byte buffer[], final int offset, 101 final int length) { 102 synchronized (byteBuffer) { 103 byteBuffer.write(buffer, offset, length); 104 } 105 } 106 107 @Override 108 public synchronized void flush() throws IOException { 109 final byte[] bytes; 110 synchronized (byteBuffer) { 111 byteBuffer.flush(); 112 if (byteBuffer.size() == 0) { 113 return; 114 } 115 116 bytes = byteBuffer.toByteArray(); 117 byteBuffer.reset(); 118 } 119 120 synchronized (textToAppend) { 121 if (textToAppend.length() == 0) { 122 // no flush has been schedulet yet 123 Platform.runLater(this); 124 } 125 126 textToAppend.append(new String(bytes)); 127 } 128 } 129 130 @Override 131 public void run() { 132 final String newText; 133 synchronized (textToAppend) { 134 newText = textToAppend.toString(); 135 textToAppend.setLength(0); 136 } 137 138 textArea.appendText(newText); 139 } 140 } 141 }