launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/BrowserToolbar.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.launcher.fximpl;
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;
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>();
48 BrowserToolbar(WebView webView, Pane container, boolean useFirebug) {
49 this.webView = webView;
50 this.container = container;
52 List<ResizeOption> options = ResizeOption.loadAll();
53 options.add( 0, ResizeOption.SIZE_TO_FIT );
54 resizeButtons = new ArrayList<ResizeBtn>( options.size() );
56 for( ResizeOption ro : options ) {
57 ResizeBtn button = new ResizeBtn(ro);
58 resizeButtons.add( button );
59 resizeGroup.getToggles().add( button );
60 getItems().add( button );
62 resizeButtons.get( 0 ).setSelected( true );
63 resizeGroup.selectedToggleProperty().addListener( new InvalidationListener() {
66 public void invalidated( Observable o ) {
71 getItems().add( new Separator() );
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>() {
86 public void changed( ObservableValue<? extends String> ov, String t, String t1 ) {
87 String newZoom = zoom( t1 );
88 comboZoom.setValue( newZoom );
93 getItems().add(new Separator());
95 final ToggleButton firebug = new ToggleButton(null, new ImageView(
96 new Image(BrowserToolbar.class.getResourceAsStream("firebug.png"))
98 firebug.setTooltip(new Tooltip("Show/Hide firebug"));
99 firebug.selectedProperty().addListener(new InvalidationListener() {
101 public void invalidated(Observable o) {
102 toggleFireBug(firebug.isSelected());
105 getItems().add(firebug);
109 private String zoom( String zoomFactor ) {
110 if( zoomFactor.trim().isEmpty() )
114 zoomFactor = zoomFactor.replaceAll( "\\%", ""); //NOI18N
115 zoomFactor = zoomFactor.trim();
116 double zoom = Double.parseDouble( zoomFactor );
117 zoom = Math.abs( zoom )/100;
120 webView.impl_setScale( zoom );
121 return (int)(100*zoom) + "%"; //NOI18N
122 } catch( NumberFormatException nfe ) {
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 ) {
135 _resize( ro.getWidth(), ro.getHeight() );
141 private void _resize( final double width, final double height ) {
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 );
149 scroll = ( ScrollPane ) container.getChildren().get( 0 );
151 scroll.setPrefViewportWidth( width );
152 scroll.setPrefViewportHeight(height );
153 webView.setMaxWidth( width );
154 webView.setMaxHeight( height );
155 webView.setMinWidth( width );
156 webView.setMinHeight( height );
159 private void _autofit() {
160 if( container.getChildren().get( 0) instanceof ScrollPane ) {
161 container.getChildren().clear();
162 container.getChildren().add( webView );
164 webView.setMaxWidth( Integer.MAX_VALUE );
165 webView.setMaxHeight( Integer.MAX_VALUE );
166 webView.setMinWidth( -1 );
167 webView.setMinHeight( -1 );
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());
187 eng.executeScript("Firebug.chrome.open()");
189 eng.executeScript("Firebug.chrome.close()");
195 * Button to resize the browser window.
196 * Taken from NetBeans. Kept GPLwithCPEx license.
197 * Portions Copyrighted 2012 Sun Microsystems, Inc.
199 * @author S. Aubrecht
201 static final class ResizeBtn extends ToggleButton {
203 private final ResizeOption resizeOption;
205 ResizeBtn(ResizeOption resizeOption) {
206 super(null, new ImageView(toImage(resizeOption)));
207 this.resizeOption = resizeOption;
208 setTooltip(new Tooltip(resizeOption.getToolTip()));
211 ResizeOption getResizeOption() {
215 static Image toImage(ResizeOption ro) {
216 if (ro == ResizeOption.SIZE_TO_FIT) {
217 return ResizeOption.Type.CUSTOM.getImage();
219 return ro.getType().getImage();
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.
228 * @author S. Aubrecht
230 static final class ResizeOption {
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;
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");
249 private final String resource;
251 private Type(String r) {
255 public Image getImage() {
256 return new Image(Type.class.getResourceAsStream(resource));
260 private ResizeOption(Type type, String displayName, int width, int height, boolean showInToolbar, boolean isDefault) {
263 this.displayName = displayName;
265 this.height = height;
266 this.isDefault = isDefault;
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));
282 * Creates a new instance.
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.
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
295 return new ResizeOption(type, displayName, width, height, showInToolbar, isDefault);
298 * An extra option to size the browser content to fit its window.
300 public static final ResizeOption SIZE_TO_FIT = new ResizeOption(Type.CUSTOM, "Size To Fit", -1, -1, true, true);
302 public String getDisplayName() {
306 public Type getType() {
310 public int getWidth() {
314 public int getHeight() {
318 public boolean isDefault() {
323 public String toString() {
327 public String getToolTip() {
328 if (width < 0 || height < 0) {
331 StringBuilder sb = new StringBuilder();
333 sb.append(" x "); //NOI18N
335 sb.append(" ("); //NOI18N
336 sb.append(displayName);
337 sb.append(')'); //NOI18N
338 return sb.toString();
342 public boolean equals(Object obj) {
346 if (getClass() != obj.getClass()) {
349 final ResizeOption other = (ResizeOption) obj;
350 if (this.type != other.type) {
353 if ((this.displayName == null) ? (other.displayName != null) : !this.displayName.equals(other.displayName)) {
356 if (this.width != other.width) {
359 if (this.height != other.height) {
362 if (this.isDefault != other.isDefault) {
369 public int hashCode() {
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);