1 /*
   2  * Copyright (c) 2009, 2012, 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  */
  24 
  25 package test.javaclient.shared;
  26 
  27 import com.sun.javafx.application.PlatformImpl;
  28 import java.util.logging.Level;
  29 import java.util.logging.Logger;
  30 import javafx.scene.Scene;
  31 import javafx.scene.layout.Pane;
  32 import javafx.scene.text.Text;
  33 import javafx.stage.Stage;
  34 import java.util.concurrent.CountDownLatch;
  35 import javafx.application.Platform;
  36 import javafx.scene.Group;
  37 import javafx.scene.Node;
  38 import javafx.scene.control.Button;
  39 import javafx.scene.layout.VBox;
  40 import javafx.scene.paint.Color;
  41 import org.eclipse.swt.widgets.Shell;
  42 import com.sun.javafx.stage.WindowHelper;
  43 
  44 /**
  45  *
  46  * @author shubov
  47  */
  48 public class CombinedTestChooserPresenter implements AbstractTestPresenter, AbstractFailureRegistrator {
  49 
  50     private Pane pageContent;
  51     private Node nodeForScreenshot;
  52     protected final int width;
  53     protected final int height;
  54     private Scene scene = null;
  55     protected final String title;
  56     protected static final int TABS_SPACE = 110;
  57     private final Pane buttons;
  58     private Text getterNotifier;
  59     static final String GETTER_NOTIFIER = "GETTER_NOTIFIER";
  60     static final String FIN_NOTIFIER = "FIN_NOTIFIER";
  61     public static final String PAGE_CONTENT = "AbstractApp.pageContent";
  62     protected boolean mouseTransparent = false;
  63     private boolean setPageContentSize = true;
  64     private boolean showButtonsPane = true;
  65 
  66     public CombinedTestChooserPresenter(int width, int height, String title, Pane buttonsPane, boolean showButtonsPane) {
  67         this.width = width;
  68         this.height = height;
  69         this.title = title;
  70         this.buttons = buttonsPane;
  71         this.showButtonsPane = showButtonsPane;
  72     }
  73 
  74     protected Scene createSceneForChooserPresenter() {
  75         return new Scene(new Group(), width + 50, height + TABS_SPACE + 30);
  76     }
  77 
  78     public void avoidSettingPageContentSize() {
  79         setPageContentSize = false;
  80     }
  81 
  82     public Node getNodeForScreenshot() {
  83         return nodeForScreenshot;
  84     }
  85 
  86     public void setNodeForScreenshot(Node _node) {
  87         nodeForScreenshot = _node;
  88     }
  89 
  90     protected void fillScene() {
  91         //scene = new Scene(new Group(), width + 50, height + TABS_SPACE + 30);
  92         scene = createSceneForChooserPresenter();
  93 
  94         Utils.addBrowser(scene);
  95 
  96         VBox vBox = new VBox();
  97 
  98         pageContent = new Pane();
  99         setNodeForScreenshot(pageContent);
 100 
 101         getterNotifier = new Text("");
 102 
 103         if (showButtonsPane) {
 104             vBox.getChildren().add(buttons);
 105         }else{
 106             if(!TestUtil.isEmbeddedGraphicsProfile()){
 107                 Button button = new Button();
 108                 button.setText("This button need to take a focus");
 109                 vBox.getChildren().add(button);
 110             }
 111 
 112         }
 113 
 114         getterNotifier.setId(GETTER_NOTIFIER);
 115         getterNotifier.setFill(Color.RED);
 116         vBox.getChildren().add(getterNotifier);
 117 
 118         pageContent.setId(PAGE_CONTENT);
 119         if (setPageContentSize) {
 120             pageContent.setMinSize(width, height);
 121             pageContent.setMaxSize(width, height);
 122             pageContent.setPrefSize(width, height);
 123         }
 124         //LayoutInfo li = new LayoutInfo();
 125         //li.setHshrink(Priority.NEVER);
 126         //li.setVshrink(Priority.NEVER);
 127         //pageContent.setLayoutInfo(li);
 128         pageContent.setMouseTransparent(mouseTransparent);
 129         vBox.getChildren().add(pageContent);
 130 
 131         pageContent.setTranslateX(20);
 132         pageContent.setTranslateY(20);
 133 
 134         ((Group) scene.getRoot()).getChildren().add(vBox);
 135     }
 136 
 137     public void showTestNode(TestNode tn) {
 138 
 139         pageContent.getChildren().clear();
 140 
 141         if (setPageContentSize) {
 142             final int _height = tn.getHeight();
 143             final int _width = tn.getWidth();
 144             pageContent.setMinSize(_width, _height);
 145             pageContent.setMaxSize(_width, _height);
 146             pageContent.setPrefSize(_width, _height);
 147         }
 148         //LayoutInfo li = new LayoutInfo();
 149         //li.setHshrink(Priority.NEVER);
 150         //li.setVshrink(Priority.NEVER);
 151         //pageContent.setLayoutInfo(li);
 152 
 153         tn.drawTo(pageContent);
 154     }
 155 
 156     public void registerFailure(final String text) {
 157         StringBuilder sb = new StringBuilder(getterNotifier.getText());
 158         if (sb.length() == 0) {
 159             sb.append("GETTER FAILURES: ");
 160         } else {
 161             sb.append(", ");
 162         }
 163         sb.append(text);
 164         getterNotifier.setText(sb.toString());
 165     }
 166 
 167     public void clearFailures() {
 168         getterNotifier.setText("");
 169     }
 170 
 171     public String getFailures() {
 172         return getterNotifier.getText();
 173     }
 174 
 175     public String getScreenshotPaneName() {
 176         return PAGE_CONTENT;
 177     }
 178 
 179     public void show(Stage stage) {
 180         stage.setX(0);
 181         stage.setY(0);
 182         stage.setTitle(title);
 183         fillScene();
 184         stage.setScene(scene);
 185         stage.show();
 186         stage.toFront();
 187         //stage.setFocused(true);
 188         WindowHelper.setFocused(stage, true);
 189     }
 190 
 191     public void show(final Object frame, final Object panel) {
 192         SwingAWTUtils.setJFrameTitle(frame, title);
 193 
 194         final CountDownLatch sync = new CountDownLatch(1);
 195         PlatformImpl.runAndWait(new Runnable() {
 196 
 197             public void run() {
 198                 fillScene();
 199                 SwingAWTUtils.setJFXPanelScene(panel, scene);
 200                 SwingAWTUtils.setJFXPanelSize(panel, (int)scene.getWidth(), (int)scene.getHeight());
 201                 sync.countDown();
 202             }
 203         });
 204 
 205         try {
 206             sync.await();
 207         } catch (InterruptedException ex) {
 208             Logger.getLogger(CombinedTestChooserPresenter.class.getName()).log(Level.SEVERE, null, ex);
 209         }
 210         SwingAWTUtils.finishShow(frame);
 211     }
 212 
 213     public void show(final Shell shell, final Object panel) {
 214         shell.setText(title);
 215         SwingAWTUtils.setJFXPanelSize(panel, width + 50, height + TABS_SPACE + 30);
 216 
 217         Platform.runLater(new Runnable() {
 218 
 219             public void run() {
 220                 fillScene();
 221                 synchronized (CombinedTestChooserPresenter.this) {
 222                     CombinedTestChooserPresenter.this.notify();
 223                 }
 224                 SwingAWTUtils.setJFXPanelScene(panel, scene);
 225             }
 226         });
 227         shell.setLocation(30, 30);
 228     }
 229 
 230     public Scene getScene() {
 231         return scene;
 232     }
 233 }