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