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  * questions.
  24  */
  25 package org.jemmy.fx.control.caspian;
  26 
  27 import com.sun.javafx.scene.control.skin.ScrollBarSkin;
  28 import com.sun.javafx.scene.control.skin.SliderSkin;
  29 import javafx.scene.Node;
  30 import javafx.scene.control.*;
  31 import org.jemmy.action.FutureAction;
  32 import org.jemmy.JemmyException;
  33 import org.jemmy.control.Wrap;
  34 import org.jemmy.fx.ByStyleClass;
  35 import org.jemmy.fx.NodeWrap;
  36 import org.jemmy.fx.control.ColorPickerWrap;
  37 import org.jemmy.fx.control.ComboBoxWrap;
  38 import org.jemmy.fx.control.MenuBarWrap;
  39 import org.jemmy.fx.control.SplitMenuButtonWrap;
  40 import org.jemmy.fx.control.ThemeDriverFactory;
  41 import org.jemmy.fx.control.TreeNodeWrap;
  42 import org.jemmy.fx.control.TreeTableItemWrap;
  43 import org.jemmy.interfaces.*;
  44 import org.jemmy.interfaces.Editor;
  45 import org.jemmy.interfaces.Keyboard.KeyboardButtons;
  46 
  47 /**
  48  * Defines control behaviour for Caspian theme.
  49  * <p/>
  50  * Ported from the project JemmyFX, original class
  51  * is   org.jemmy.fx.control.caspian.CaspianDriverFactory
  52  *
  53  * @author shura
  54  */
  55 public class CaspianDriverFactory extends ThemeDriverFactory {
  56 
  57     private float dragDelta;
  58 
  59     /**
  60      * @param dragDelta
  61      */
  62     public CaspianDriverFactory(float dragDelta) {
  63         this.dragDelta = dragDelta;
  64     }
  65 
  66     /**
  67      *
  68      */
  69     public CaspianDriverFactory() {
  70         this(1);
  71     }
  72 
  73     /**
  74      * @return
  75      */
  76     public float getDragDelta() {
  77         return dragDelta;
  78     }
  79 
  80     /**
  81      * @param dragDelta
  82      */
  83     public void setDragDelta(float dragDelta) {
  84         this.dragDelta = dragDelta;
  85     }
  86 
  87     /**
  88      * {@inheritDoc}
  89      */
  90     @Override
  91     public Scroller caret(Wrap<? extends Control> wrap, Scroll scroll) {
  92         KnobTrackScrollerImpl res = createScrollerImpl(wrap, scroll);
  93         res.setDragDelta(dragDelta);
  94         return res;
  95     }
  96 
  97     @Override
  98     public Shifter track(final Wrap<? extends Control> wrap, final Scroll scroll) {
  99         KnobTrackScrollerImpl sls = createScrollerImpl(wrap, scroll);
 100         return sls.getTrack();
 101     }
 102 
 103     private KnobTrackScrollerImpl createScrollerImpl(Wrap<? extends Control> wrap, Scroll scroll) {
 104 
 105         if (wrap.getControl() instanceof ScrollBar) {
 106             return new ScrollBarScroller((Wrap<? extends ScrollBar>) wrap, scroll, ScrollBarSkin.class);
 107         }
 108         if (wrap.getControl() instanceof Slider) {
 109             return new SliderScroller((Wrap<? extends Slider>) wrap, scroll, SliderSkin.class);
 110         }
 111         return null;
 112     }
 113 
 114     @Override
 115     public <T> org.jemmy.interfaces.TreeItem treeItem(Wrap<T> wrap, Wrap parentControlWrap) {
 116         if (wrap instanceof TreeNodeWrap) {
 117             return new org.jemmy.fx.control.caspian.TreeItem((TreeNodeWrap) wrap, parentControlWrap);
 118         }
 119         if (wrap instanceof TreeTableItemWrap) {
 120             return new org.jemmy.fx.control.caspian.TreeTableItem((TreeTableItemWrap) wrap, parentControlWrap);
 121         }
 122         throw new JemmyException("Unknown type of parameter.");
 123     }
 124 
 125     @Override
 126     public Focus menuBarFocuser(final MenuBarWrap<? extends MenuBar> menuBarWrap) {
 127         if (isMacOS()) {
 128             return new NodeFocus(menuBarWrap) {
 129                 @Override
 130                 protected void activate() {
 131                     // temporary solution due to bug in MacOS implementation of MenuBar
 132                     new FutureAction(menuBarWrap.getEnvironment(), () -> menuBarWrap.getControl().requestFocus());
 133                 }
 134             };
 135         } else {
 136             return new NodeFocus(menuBarWrap) {
 137                 @Override
 138                 protected void activate() {
 139                     // pressKey()/releaseKey() are used to prevent an attempt to get focus in pushKey()
 140                     menuBarWrap.keyboard().pressKey(KeyboardButtons.F10);
 141                     menuBarWrap.getEnvironment().getTimeout(menuBarWrap.keyboard().PUSH.getName());
 142                     menuBarWrap.keyboard().releaseKey(KeyboardButtons.F10);
 143                 }
 144             };
 145         }
 146     }
 147 
 148     @Override
 149     public Focus comboBoxFocuser(final ComboBoxWrap<? extends ComboBox> comboBoxWrap) {
 150         return new NodeFocus(comboBoxWrap) {
 151             @Override
 152             protected void activate() {
 153                 comboBoxWrap.as(Parent.class, Node.class).lookup(new ByStyleClass<>("arrow-button")).wrap().mouse().click(comboBoxWrap.isShowing() ? 1 : 2);
 154             }
 155         };
 156     }
 157 
 158 
 159     @Override
 160     public void splitMenuButtonExpandCollapseAction(SplitMenuButtonWrap<? extends SplitMenuButton> wrap) {
 161         wrap.asParent().lookup(Node.class, new ByStyleClass<>("arrow-button")).wrap().mouse().click();
 162     }
 163 
 164     @Override
 165     public Editor colorEditor(final ColorPickerWrap<? extends ColorPicker> colorPickerWrap) {
 166         return new ColorEditorImpl(colorPickerWrap);
 167     }
 168 
 169     abstract class NodeFocus implements Focus {
 170 
 171         NodeWrap nodeWrap;
 172 
 173         public NodeFocus(NodeWrap nodeWrap) {
 174             this.nodeWrap = nodeWrap;
 175         }
 176 
 177         @Override
 178         public void focus() {
 179             if (!nodeWrap.isFocused()) {
 180                 activate();
 181             }
 182             nodeWrap.waitState(()->nodeWrap.isFocused(), true);
 183         }
 184 
 185         abstract protected void activate();
 186     }
 187 
 188     private static boolean isMacOS() {
 189         String os = System.getProperty("os.name").toLowerCase();
 190         if (os.indexOf("mac") >= 0) {
 191             return true;
 192         }
 193         return false;
 194     }
 195 }