launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/BrowserToolbar.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Oct 2013 11:24:41 +0100
changeset 1391 4cd2c7e22646
parent 1283 1d0e583ac981
child 1787 ea12a3bb4b33
permissions -rw-r--r--
JavaFX from JDK8 does not have this impl method anymore
     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.setScaleX(zoom);
   121             webView.setScaleY(zoom);
   122             webView.setScaleZ(zoom);
   123             return (int)(100*zoom) + "%"; //NOI18N
   124         } catch( NumberFormatException nfe ) {
   125             //ignore
   126         }
   127         return null;
   128     }
   129 
   130     private void resize() {
   131         Toggle selection = resizeGroup.getSelectedToggle();
   132         if( selection instanceof ResizeBtn ) {
   133             ResizeOption ro = ((ResizeBtn)selection).getResizeOption();
   134             if( ro == ResizeOption.SIZE_TO_FIT ) {
   135                 _autofit();
   136             } else {
   137                 _resize( ro.getWidth(), ro.getHeight() );
   138             }
   139         }
   140 
   141     }
   142 
   143     private void _resize( final double width, final double height ) {
   144         ScrollPane scroll;
   145         if( !(container.getChildren().get( 0) instanceof ScrollPane) ) {
   146             scroll = new ScrollPane();
   147             scroll.setContent( webView );
   148             container.getChildren().clear();
   149             container.getChildren().add( scroll );
   150         } else {
   151             scroll = ( ScrollPane ) container.getChildren().get( 0 );
   152         }
   153         scroll.setPrefViewportWidth( width );
   154         scroll.setPrefViewportHeight(height );
   155         webView.setMaxWidth( width );
   156         webView.setMaxHeight( height );
   157         webView.setMinWidth( width );
   158         webView.setMinHeight( height );
   159     }
   160 
   161     private void _autofit() {
   162         if( container.getChildren().get( 0) instanceof ScrollPane ) {
   163             container.getChildren().clear();
   164             container.getChildren().add( webView );
   165         }
   166         webView.setMaxWidth( Integer.MAX_VALUE );
   167         webView.setMaxHeight( Integer.MAX_VALUE );
   168         webView.setMinWidth( -1 );
   169         webView.setMinHeight( -1 );
   170         webView.autosize();
   171     }
   172 
   173     final void toggleFireBug(boolean enable) {
   174         WebEngine eng = webView.getEngine();
   175         Object installed = eng.executeScript("window.Firebug");
   176         if ("undefined".equals(installed)) {
   177             StringBuilder sb = new StringBuilder();
   178             sb.append("var scr = window.document.createElement('script');\n");
   179             sb.append("scr.type = 'text/javascript';\n");
   180             sb.append("scr.src = 'https://getfirebug.com/firebug-lite.js';\n");
   181             sb.append("scr.text = '{ startOpened: true }';\n");
   182             sb.append("var head = window.document.getElementsByTagName('head')[0];");
   183             sb.append("head.appendChild(scr);\n");
   184             sb.append("var html = window.document.getElementsByTagName('html')[0];");
   185             sb.append("html.debug = true;\n");
   186             eng.executeScript(sb.toString());
   187         } else {
   188             if (enable) {
   189                 eng.executeScript("Firebug.chrome.open()");
   190             } else {
   191                 eng.executeScript("Firebug.chrome.close()");
   192             }
   193         }
   194     }
   195 
   196     /**
   197      * Button to resize the browser window.
   198      * Taken from NetBeans. Kept GPLwithCPEx license.
   199      * Portions Copyrighted 2012 Sun Microsystems, Inc.
   200      *
   201      * @author S. Aubrecht
   202      */
   203     static final class ResizeBtn extends ToggleButton {
   204 
   205         private final ResizeOption resizeOption;
   206 
   207         ResizeBtn(ResizeOption resizeOption) {
   208             super(null, new ImageView(toImage(resizeOption)));
   209             this.resizeOption = resizeOption;
   210             setTooltip(new Tooltip(resizeOption.getToolTip()));
   211         }
   212 
   213         ResizeOption getResizeOption() {
   214             return resizeOption;
   215         }
   216 
   217         static Image toImage(ResizeOption ro) {
   218             if (ro == ResizeOption.SIZE_TO_FIT) {
   219                 return ResizeOption.Type.CUSTOM.getImage();
   220             }
   221             return ro.getType().getImage();
   222         }
   223     }
   224 
   225     /**
   226      * Immutable value class describing a single button to resize web browser window.
   227      * Taken from NetBeans. Kept GPLwithCPEx license.
   228      * Portions Copyrighted 2012 Sun Microsystems, Inc.
   229      *
   230      * @author S. Aubrecht
   231      */
   232     static final class ResizeOption {
   233 
   234         private final Type type;
   235         private final String displayName;
   236         private final int width;
   237         private final int height;
   238         private final boolean isDefault;
   239 
   240         enum Type {
   241             DESKTOP("desktop.png"), 
   242             TABLET_PORTRAIT("tabletPortrait.png"), 
   243             TABLET_LANDSCAPE("tabletLandscape.png"), 
   244             SMARTPHONE_PORTRAIT("handheldPortrait.png"), 
   245             SMARTPHONE_LANDSCAPE("handheldLandscape.png"), 
   246             WIDESCREEN("widescreen.png"), 
   247             NETBOOK("netbook.png"), 
   248             CUSTOM("sizeToFit.png");
   249             
   250             
   251             private final String resource;
   252 
   253             private Type(String r) {
   254                 resource = r;
   255             }
   256 
   257             public Image getImage() {
   258                 return new Image(Type.class.getResourceAsStream(resource));
   259             }
   260         }
   261 
   262         private ResizeOption(Type type, String displayName, int width, int height, boolean showInToolbar, boolean isDefault) {
   263             super();
   264             this.type = type;
   265             this.displayName = displayName;
   266             this.width = width;
   267             this.height = height;
   268             this.isDefault = isDefault;
   269         }
   270 
   271         static List<ResizeOption> loadAll() {
   272             List<ResizeOption> res = new ArrayList<ResizeOption>(10);
   273             res.add(ResizeOption.create(ResizeOption.Type.DESKTOP, "Desktop", 1280, 1024, true, true));
   274             res.add(ResizeOption.create(ResizeOption.Type.TABLET_LANDSCAPE, "Tablet Landscape", 1024, 768, true, true));
   275             res.add(ResizeOption.create(ResizeOption.Type.TABLET_PORTRAIT, "Tablet Portrait", 768, 1024, true, true));
   276             res.add(ResizeOption.create(ResizeOption.Type.SMARTPHONE_LANDSCAPE, "Smartphone Landscape", 480, 320, true, true));
   277             res.add(ResizeOption.create(ResizeOption.Type.SMARTPHONE_PORTRAIT, "Smartphone Portrait", 320, 480, true, true));
   278             res.add(ResizeOption.create(ResizeOption.Type.WIDESCREEN, "Widescreen", 1680, 1050, false, true));
   279             res.add(ResizeOption.create(ResizeOption.Type.NETBOOK, "Netbook", 1024, 600, false, true));
   280             return res;
   281         }
   282         
   283         /**
   284          * Creates a new instance.
   285          * @param type
   286          * @param displayName Display name to show in tooltip, cannot be empty.
   287          * @param width Screen width
   288          * @param height Screen height
   289          * @param showInToolbar True to show in web developer toolbar.
   290          * @param isDefault True if this is a predefined option that cannot be removed.
   291          * @return New instance.
   292          */
   293         public static ResizeOption create(Type type, String displayName, int width, int height, boolean showInToolbar, boolean isDefault) {
   294             if (width <= 0 || height <= 0) {
   295                 throw new IllegalArgumentException("Invalid screen dimensions: " + width + " x " + height); //NOI18N
   296             }
   297             return new ResizeOption(type, displayName, width, height, showInToolbar, isDefault);
   298         }
   299         /**
   300          * An extra option to size the browser content to fit its window.
   301          */
   302         public static final ResizeOption SIZE_TO_FIT = new ResizeOption(Type.CUSTOM, "Size To Fit", -1, -1, true, true);
   303 
   304         public String getDisplayName() {
   305             return displayName;
   306         }
   307 
   308         public Type getType() {
   309             return type;
   310         }
   311 
   312         public int getWidth() {
   313             return width;
   314         }
   315 
   316         public int getHeight() {
   317             return height;
   318         }
   319 
   320         public boolean isDefault() {
   321             return isDefault;
   322         }
   323 
   324         @Override
   325         public String toString() {
   326             return displayName;
   327         }
   328 
   329         public String getToolTip() {
   330             if (width < 0 || height < 0) {
   331                 return displayName;
   332             }
   333             StringBuilder sb = new StringBuilder();
   334             sb.append(width);
   335             sb.append(" x "); //NOI18N
   336             sb.append(height);
   337             sb.append(" ("); //NOI18N
   338             sb.append(displayName);
   339             sb.append(')'); //NOI18N
   340             return sb.toString();
   341         }
   342 
   343         @Override
   344         public boolean equals(Object obj) {
   345             if (obj == null) {
   346                 return false;
   347             }
   348             if (getClass() != obj.getClass()) {
   349                 return false;
   350             }
   351             final ResizeOption other = (ResizeOption) obj;
   352             if (this.type != other.type) {
   353                 return false;
   354             }
   355             if ((this.displayName == null) ? (other.displayName != null) : !this.displayName.equals(other.displayName)) {
   356                 return false;
   357             }
   358             if (this.width != other.width) {
   359                 return false;
   360             }
   361             if (this.height != other.height) {
   362                 return false;
   363             }
   364             if (this.isDefault != other.isDefault) {
   365                 return false;
   366             }
   367             return true;
   368         }
   369 
   370         @Override
   371         public int hashCode() {
   372             int hash = 7;
   373             hash = 11 * hash + (this.type != null ? this.type.hashCode() : 0);
   374             hash = 11 * hash + (this.displayName != null ? this.displayName.hashCode() : 0);
   375             hash = 11 * hash + this.width;
   376             hash = 11 * hash + this.height;
   377             hash = 11 * hash + (this.isDefault ? 1 : 0);
   378             return hash;
   379         }
   380     }
   381 }