All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jemmy.swt.ComboWrap Maven / Gradle / Ivy

/*
 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation. Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package org.jemmy.swt;

import java.util.Arrays;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Combo;
import org.jemmy.action.GetAction;
import org.jemmy.control.ControlType;
import org.jemmy.control.Property;
import org.jemmy.control.Wrap;
import org.jemmy.env.Environment;
import org.jemmy.input.KeyboardSelectable;
import org.jemmy.input.KeyboardSelector;
import org.jemmy.input.SelectionText;
import org.jemmy.interfaces.ControlInterface;
import org.jemmy.interfaces.Focusable;
import org.jemmy.interfaces.Keyboard.KeyboardButtons;
import org.jemmy.interfaces.Keyboard.KeyboardModifier;
import org.jemmy.interfaces.Selector;

/**
 *
 * @author shura
 * @author erikgreijus
 */
@ControlType(Combo.class)
public class ComboWrap extends ControlWrap implements
        KeyboardSelectable, Focusable {

    public static final String DISMISSAL_BUTTON_PROP = ComboWrap.class.getName() + ".dismissal.button";
    public static final String DISMISSAL_MODIFIER_PROP = ComboWrap.class.getName() + ".dismissal.modifier";
    private final KeyboardButtons dismissalButton;
    private final KeyboardModifier[] dismissalModifier;

    class ComboKeyboardSelector extends KeyboardSelector {

        private final Wrap comboWrap;

        public ComboKeyboardSelector(Wrap wrap, KeyboardSelectable control) {
            super(wrap, control);
            comboWrap = wrap;
        }

        @Override
        public void select(T state) {
            super.select(state);
            comboWrap.keyboard().pushKey(dismissalButton, dismissalModifier);
        }
    }

    class FocusableSelectionText extends SelectionText implements Focusable {

        protected ClickFocus focuser;
        private final ComboWrap comboWrap;

        public FocusableSelectionText(ComboWrap textWrap) {
            super(textWrap);
            this.comboWrap = textWrap;
        }

        @Override
        public double position() {
            return comboWrap.position();
        }

        @Override
        public String text() {
            return comboWrap.text();
        }

        @Override
        public double anchor() {
            return comboWrap.anchor();
        }

        @Override
        public ClickFocus focuser() {
            if (focuser == null) {
                focuser = new ClickFocus();
            }
            return focuser;
        }
    }

    FocusableSelectionText text = new FocusableSelectionText(this);
    private ComboKeyboardSelector selector = null;

    public ComboWrap(Environment env, T node) {
        super(env, node);
        KeyboardButtons defaultDismissalButton = System.getProperty("os.name").toLowerCase().contains("windows") ? KeyboardButtons.ESCAPE : KeyboardButtons.SPACE;
        KeyboardModifier[] defaultDismissalModifiers = new KeyboardModifier[0];
        dismissalButton = env.getProperty(KeyboardButtons.class,
                DISMISSAL_BUTTON_PROP, defaultDismissalButton);
        dismissalModifier = (KeyboardModifier[]) env.getProperty(KeyboardModifier[].class,
                DISMISSAL_MODIFIER_PROP, defaultDismissalModifiers);
    }

    @Override
    public java.util.List getStates() {
        return new GetAction>() {

            @Override
            public void run(Object... parameters) throws Exception {
                setResult(Arrays.asList(getControl().getItems()));
            }
        }.dispatch(getEnvironment());
    }

    @Override
    public String getState() {
        return new GetAction() {

            @Override
            public void run(Object... parameters) throws Exception {
                if (getControl().getSelectionIndex() >= 0) {
                    setResult(getControl().getItem(getControl().getSelectionIndex()));
                } else {
                    setResult(null);
                }
            }
        }.dispatch(getEnvironment());
    }

    @Override
    public int selection() {
        return new GetAction() {

            @Override
            public void run(Object... parameters) throws Exception {
                setResult(getControl().getSelectionIndex());
            }
        }.dispatch(getEnvironment());
    }

    @Override
    public Selector selector() {
        if (selector == null) {
            selector = new ComboKeyboardSelector<>(this, this);
        }
        return selector;
    }

    @Override
    public Class getType() {
        return String.class;
    }

    @Override
    public boolean isVertical() {
        return true;
    }

    @Override
    public int index(String item) {
        return new GetAction() {

            @Override
            public void run(Object... parameters) throws Exception {
                setResult(getControl().indexOf(item));
            }
        }.dispatch(getEnvironment());
    }

    @Override
    public ClickFocus focuser() {
        if (focuser == null) {
            super.focuser().clickCount = 2;
        }
        return focuser;
    }

    @Override
    public boolean hasFocus() {
        GetAction action;
        action = new GetAction() {

            @Override
            public void run(Object... parameters) {
                setResult(getControl().isFocusControl() || getControl().getListVisible());
            }

            @Override
            public String toString() {
                return "Getting focus state for " + getControl();
            }
        };
        getEnvironment().getExecutor().execute(getEnvironment(), true, action);
        return action.getResult();
    }

    @Property(Wrap.POSITION_PROP_NAME)
    public Integer position() {
        GetAction action = new GetAction() {

            @Override
            public void run(Object... parameters) {
                setResult(getControl().getSelection().x);
            }
        };
        getEnvironment().getExecutor().execute(getEnvironment(), true, action);
        return action.getResult();
    }

    @Property(SelectionText.SELECTION_ANCHOR_PROP_NAME)
    public Integer anchor() {
        GetAction action = new GetAction() {

            @Override
            public void run(Object... parameters) {
                Point selection = getControl().getSelection();
                setResult((selection.x == getControl().getCaretPosition())
                        ? selection.y : selection.x);
            }
        };
        getEnvironment().getExecutor().execute(getEnvironment(), true, action);
        return action.getResult();
    }

    @Property(Wrap.TEXT_PROP_NAME)
    @Override
    public String text() {
        GetAction action = new GetAction() {

            @Override
            public void run(Object... parameters) {
                setResult(getControl().getText());
            }
        };
        getEnvironment().getExecutor().execute(getEnvironment(), true, action);
        return action.getResult();
    }

    @Override
    public  boolean is(Class interfaceClass) {
        if (interfaceClass.isAssignableFrom(SelectionText.class)) {
            return true;
        }
        if (interfaceClass.equals(Focusable.class)) {
            return true;
        }
        return super.is(interfaceClass);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  INTERFACE as(Class interfaceClass) {
        if (interfaceClass.isAssignableFrom(SelectionText.class)) {
            return (INTERFACE) text;
        }
        if (interfaceClass.isAssignableFrom(Focusable.class)) {
            return (INTERFACE) text;
        }
        return super.as(interfaceClass);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy