launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/BrowserToolbar.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 15 Sep 2013 18:24:48 +0200
changeset 1283 1d0e583ac981
parent 1169 c19ac78b940e
child 1391 4cd2c7e22646
permissions -rw-r--r--
Use the new netbeans.inspect property to connect to the IDE
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.launcher.fximpl;
    19 
    20 import java.util.ArrayList;
    21 import java.util.List;
    22 import javafx.beans.InvalidationListener;
    23 import javafx.beans.Observable;
    24 import javafx.beans.value.ChangeListener;
    25 import javafx.beans.value.ObservableValue;
    26 import javafx.collections.FXCollections;
    27 import javafx.scene.control.ComboBox;
    28 import javafx.scene.control.ScrollPane;
    29 import javafx.scene.control.Separator;
    30 import javafx.scene.control.Toggle;
    31 import javafx.scene.control.ToggleButton;
    32 import javafx.scene.control.ToggleGroup;
    33 import javafx.scene.control.ToolBar;
    34 import javafx.scene.control.Tooltip;
    35 import javafx.scene.image.Image;
    36 import javafx.scene.image.ImageView;
    37 import javafx.scene.layout.Pane;
    38 import javafx.scene.web.WebEngine;
    39 import javafx.scene.web.WebView;
    40 
    41 final class BrowserToolbar extends ToolBar {
    42     private final ArrayList<ResizeBtn> resizeButtons;
    43     private final WebView webView;
    44     private final Pane container;
    45     private final ToggleGroup resizeGroup = new ToggleGroup();
    46     private final ComboBox<String> comboZoom = new ComboBox<String>();
    47     
    48     BrowserToolbar(WebView webView, Pane container, boolean useFirebug) {
    49         this.webView = webView;
    50         this.container = container;
    51         
    52         List<ResizeOption> options = ResizeOption.loadAll();
    53         options.add( 0, ResizeOption.SIZE_TO_FIT );
    54         resizeButtons = new ArrayList<ResizeBtn>( options.size() );
    55 
    56         for( ResizeOption ro : options ) {
    57             ResizeBtn button = new ResizeBtn(ro);
    58             resizeButtons.add( button );
    59             resizeGroup.getToggles().add( button );
    60             getItems().add( button );
    61         }
    62         resizeButtons.get( 0 ).setSelected( true );
    63         resizeGroup.selectedToggleProperty().addListener( new InvalidationListener() {
    64 
    65             @Override
    66             public void invalidated( Observable o ) {
    67                 resize();
    68             }
    69         });
    70         
    71         getItems().add( new Separator() );
    72 
    73         getItems().add( comboZoom );
    74         ArrayList<String> zoomModel = new ArrayList<String>( 6 );
    75         zoomModel.add( "200%" ); //NOI18N
    76         zoomModel.add( "150%" ); //NOI18N
    77         zoomModel.add( "100%" ); //NOI18N
    78         zoomModel.add( "75%" ); //NOI18N
    79         zoomModel.add( "50%" ); //NOI18N
    80         comboZoom.setItems( FXCollections.observableList( zoomModel ) );
    81         comboZoom.setEditable( true );
    82         comboZoom.setValue( "100%" ); //NOI18N
    83         comboZoom.valueProperty().addListener( new ChangeListener<String>() {
    84 
    85             @Override
    86             public void changed( ObservableValue<? extends String> ov, String t, String t1 ) {
    87                 String newZoom = zoom( t1 );
    88                 comboZoom.setValue( newZoom );
    89             }
    90         });
    91         
    92         if (useFirebug) {
    93             getItems().add(new Separator());
    94 
    95             final ToggleButton firebug = new ToggleButton(null, new ImageView(
    96                 new Image(BrowserToolbar.class.getResourceAsStream("firebug.png"))
    97             ));
    98             firebug.setTooltip(new Tooltip("Show/Hide firebug"));
    99             firebug.selectedProperty().addListener(new InvalidationListener() {
   100                 @Override
   101                 public void invalidated(Observable o) {
   102                     toggleFireBug(firebug.isSelected());
   103                 }
   104             });
   105             getItems().add(firebug);
   106         }
   107     }
   108 
   109     private String zoom( String zoomFactor ) {
   110         if( zoomFactor.trim().isEmpty() )
   111             return null;
   112 
   113         try {
   114             zoomFactor = zoomFactor.replaceAll( "\\%", ""); //NOI18N
   115             zoomFactor = zoomFactor.trim();
   116             double zoom = Double.parseDouble( zoomFactor );
   117             zoom = Math.abs( zoom )/100;
   118             if( zoom <= 0.0 )
   119                 return null;
   120             webView.impl_setScale( zoom );
   121             return (int)(100*zoom) + "%"; //NOI18N
   122         } catch( NumberFormatException nfe ) {
   123             //ignore
   124         }
   125         return null;
   126     }
   127 
   128     private void resize() {
   129         Toggle selection = resizeGroup.getSelectedToggle();
   130         if( selection instanceof ResizeBtn ) {
   131             ResizeOption ro = ((ResizeBtn)selection).getResizeOption();
   132             if( ro == ResizeOption.SIZE_TO_FIT ) {
   133                 _autofit();
   134             } else {
   135                 _resize( ro.getWidth(), ro.getHeight() );
   136             }
   137         }
   138 
   139     }
   140 
   141     private void _resize( final double width, final double height ) {
   142         ScrollPane scroll;
   143         if( !(container.getChildren().get( 0) instanceof ScrollPane) ) {
   144             scroll = new ScrollPane();
   145             scroll.setContent( webView );
   146             container.getChildren().clear();
   147             container.getChildren().add( scroll );
   148         } else {
   149             scroll = ( ScrollPane ) container.getChildren().get( 0 );
   150         }
   151         scroll.setPrefViewportWidth( width );
   152         scroll.setPrefViewportHeight(height );
   153         webView.setMaxWidth( width );
   154         webView.setMaxHeight( height );
   155         webView.setMinWidth( width );
   156         webView.setMinHeight( height );
   157     }
   158 
   159     private void _autofit() {
   160         if( container.getChildren().get( 0) instanceof ScrollPane ) {
   161             container.getChildren().clear();
   162             container.getChildren().add( webView );
   163         }
   164         webView.setMaxWidth( Integer.MAX_VALUE );
   165         webView.setMaxHeight( Integer.MAX_VALUE );
   166         webView.setMinWidth( -1 );
   167         webView.setMinHeight( -1 );
   168         webView.autosize();
   169     }
   170 
   171     final void toggleFireBug(boolean enable) {
   172         WebEngine eng = webView.getEngine();
   173         Object installed = eng.executeScript("window.Firebug");
   174         if ("undefined".equals(installed)) {
   175             StringBuilder sb = new StringBuilder();
   176             sb.append("var scr = window.document.createElement('script');\n");
   177             sb.append("scr.type = 'text/javascript';\n");
   178             sb.append("scr.src = 'https://getfirebug.com/firebug-lite.js';\n");
   179             sb.append("scr.text = '{ startOpened: true }';\n");
   180             sb.append("var head = window.document.getElementsByTagName('head')[0];");
   181             sb.append("head.appendChild(scr);\n");
   182             sb.append("var html = window.document.getElementsByTagName('html')[0];");
   183             sb.append("html.debug = true;\n");
   184             eng.executeScript(sb.toString());
   185         } else {
   186             if (enable) {
   187                 eng.executeScript("Firebug.chrome.open()");
   188             } else {
   189                 eng.executeScript("Firebug.chrome.close()");
   190             }
   191         }
   192     }
   193 
   194     /**
   195      * Button to resize the browser window.
   196      * Taken from NetBeans. Kept GPLwithCPEx license.
   197      * Portions Copyrighted 2012 Sun Microsystems, Inc.
   198      *
   199      * @author S. Aubrecht
   200      */
   201     static final class ResizeBtn extends ToggleButton {
   202 
   203         private final ResizeOption resizeOption;
   204 
   205         ResizeBtn(ResizeOption resizeOption) {
   206             super(null, new ImageView(toImage(resizeOption)));
   207             this.resizeOption = resizeOption;
   208             setTooltip(new Tooltip(resizeOption.getToolTip()));
   209         }
   210 
   211         ResizeOption getResizeOption() {
   212             return resizeOption;
   213         }
   214 
   215         static Image toImage(ResizeOption ro) {
   216             if (ro == ResizeOption.SIZE_TO_FIT) {
   217                 return ResizeOption.Type.CUSTOM.getImage();
   218             }
   219             return ro.getType().getImage();
   220         }
   221     }
   222 
   223     /**
   224      * Immutable value class describing a single button to resize web browser window.
   225      * Taken from NetBeans. Kept GPLwithCPEx license.
   226      * Portions Copyrighted 2012 Sun Microsystems, Inc.
   227      *
   228      * @author S. Aubrecht
   229      */
   230     static final class ResizeOption {
   231 
   232         private final Type type;
   233         private final String displayName;
   234         private final int width;
   235         private final int height;
   236         private final boolean isDefault;
   237 
   238         enum Type {
   239             DESKTOP("desktop.png"), 
   240             TABLET_PORTRAIT("tabletPortrait.png"), 
   241             TABLET_LANDSCAPE("tabletLandscape.png"), 
   242             SMARTPHONE_PORTRAIT("handheldPortrait.png"), 
   243             SMARTPHONE_LANDSCAPE("handheldLandscape.png"), 
   244             WIDESCREEN("widescreen.png"), 
   245             NETBOOK("netbook.png"), 
   246             CUSTOM("sizeToFit.png");
   247             
   248             
   249             private final String resource;
   250 
   251             private Type(String r) {
   252                 resource = r;
   253             }
   254 
   255             public Image getImage() {
   256                 return new Image(Type.class.getResourceAsStream(resource));
   257             }
   258         }
   259 
   260         private ResizeOption(Type type, String displayName, int width, int height, boolean showInToolbar, boolean isDefault) {
   261             super();
   262             this.type = type;
   263             this.displayName = displayName;
   264             this.width = width;
   265             this.height = height;
   266             this.isDefault = isDefault;
   267         }
   268 
   269         static List<ResizeOption> loadAll() {
   270             List<ResizeOption> res = new ArrayList<ResizeOption>(10);
   271             res.add(ResizeOption.create(ResizeOption.Type.DESKTOP, "Desktop", 1280, 1024, true, true));
   272             res.add(ResizeOption.create(ResizeOption.Type.TABLET_LANDSCAPE, "Tablet Landscape", 1024, 768, true, true));
   273             res.add(ResizeOption.create(ResizeOption.Type.TABLET_PORTRAIT, "Tablet Portrait", 768, 1024, true, true));
   274             res.add(ResizeOption.create(ResizeOption.Type.SMARTPHONE_LANDSCAPE, "Smartphone Landscape", 480, 320, true, true));
   275             res.add(ResizeOption.create(ResizeOption.Type.SMARTPHONE_PORTRAIT, "Smartphone Portrait", 320, 480, true, true));
   276             res.add(ResizeOption.create(ResizeOption.Type.WIDESCREEN, "Widescreen", 1680, 1050, false, true));
   277             res.add(ResizeOption.create(ResizeOption.Type.NETBOOK, "Netbook", 1024, 600, false, true));
   278             return res;
   279         }
   280         
   281         /**
   282          * Creates a new instance.
   283          * @param type
   284          * @param displayName Display name to show in tooltip, cannot be empty.
   285          * @param width Screen width
   286          * @param height Screen height
   287          * @param showInToolbar True to show in web developer toolbar.
   288          * @param isDefault True if this is a predefined option that cannot be removed.
   289          * @return New instance.
   290          */
   291         public static ResizeOption create(Type type, String displayName, int width, int height, boolean showInToolbar, boolean isDefault) {
   292             if (width <= 0 || height <= 0) {
   293                 throw new IllegalArgumentException("Invalid screen dimensions: " + width + " x " + height); //NOI18N
   294             }
   295             return new ResizeOption(type, displayName, width, height, showInToolbar, isDefault);
   296         }
   297         /**
   298          * An extra option to size the browser content to fit its window.
   299          */
   300         public static final ResizeOption SIZE_TO_FIT = new ResizeOption(Type.CUSTOM, "Size To Fit", -1, -1, true, true);
   301 
   302         public String getDisplayName() {
   303             return displayName;
   304         }
   305 
   306         public Type getType() {
   307             return type;
   308         }
   309 
   310         public int getWidth() {
   311             return width;
   312         }
   313 
   314         public int getHeight() {
   315             return height;
   316         }
   317 
   318         public boolean isDefault() {
   319             return isDefault;
   320         }
   321 
   322         @Override
   323         public String toString() {
   324             return displayName;
   325         }
   326 
   327         public String getToolTip() {
   328             if (width < 0 || height < 0) {
   329                 return displayName;
   330             }
   331             StringBuilder sb = new StringBuilder();
   332             sb.append(width);
   333             sb.append(" x "); //NOI18N
   334             sb.append(height);
   335             sb.append(" ("); //NOI18N
   336             sb.append(displayName);
   337             sb.append(')'); //NOI18N
   338             return sb.toString();
   339         }
   340 
   341         @Override
   342         public boolean equals(Object obj) {
   343             if (obj == null) {
   344                 return false;
   345             }
   346             if (getClass() != obj.getClass()) {
   347                 return false;
   348             }
   349             final ResizeOption other = (ResizeOption) obj;
   350             if (this.type != other.type) {
   351                 return false;
   352             }
   353             if ((this.displayName == null) ? (other.displayName != null) : !this.displayName.equals(other.displayName)) {
   354                 return false;
   355             }
   356             if (this.width != other.width) {
   357                 return false;
   358             }
   359             if (this.height != other.height) {
   360                 return false;
   361             }
   362             if (this.isDefault != other.isDefault) {
   363                 return false;
   364             }
   365             return true;
   366         }
   367 
   368         @Override
   369         public int hashCode() {
   370             int hash = 7;
   371             hash = 11 * hash + (this.type != null ? this.type.hashCode() : 0);
   372             hash = 11 * hash + (this.displayName != null ? this.displayName.hashCode() : 0);
   373             hash = 11 * hash + this.width;
   374             hash = 11 * hash + this.height;
   375             hash = 11 * hash + (this.isDefault ? 1 : 0);
   376             return hash;
   377         }
   378     }
   379 }