Search in sources :

Example 81 with JTextComponent

use of javax.swing.text.JTextComponent in project zaproxy by zaproxy.

the class PopupFlagCustomPageIndicatorMenu method isEnableForComponent.

@Override
public boolean isEnableForComponent(Component invoker) {
    if (invoker instanceof JTextComponent) {
        // Is it the HttpPanelResponse?
        JTextComponent txtComponent = (JTextComponent) invoker;
        boolean responsePanel = (SwingUtilities.getAncestorOfClass(HttpPanelResponse.class, txtComponent) != null);
        if (!responsePanel) {
            selectedText = null;
            return false;
        }
        // Is anything selected?
        selectedText = txtComponent.getSelectedText();
        this.setEnabled(selectedText != null && selectedText.length() != 0);
        return true;
    } else {
        selectedText = null;
        return false;
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent)

Example 82 with JTextComponent

use of javax.swing.text.JTextComponent in project zaproxy by zaproxy.

the class PopupFlagLoggedInIndicatorMenu method isEnableForComponent.

@Override
public boolean isEnableForComponent(Component invoker) {
    if (invoker instanceof JTextComponent) {
        // Is it the HttpPanelResponse?
        JTextComponent txtComponent = (JTextComponent) invoker;
        boolean responsePanel = (SwingUtilities.getAncestorOfClass(HttpPanelResponse.class, txtComponent) != null);
        if (!responsePanel) {
            selectedText = null;
            return false;
        }
        // Is anything selected?
        selectedText = txtComponent.getSelectedText();
        if (selectedText == null || selectedText.length() == 0) {
            this.setEnabled(false);
        } else {
            this.setEnabled(true);
        }
        return true;
    } else {
        selectedText = null;
        return false;
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent)

Example 83 with JTextComponent

use of javax.swing.text.JTextComponent in project ats-framework by Axway.

the class SwingElementLocator method buildLocator.

private static <T extends Component> GenericTypeMatcher<T> buildLocator(Class<T> componentClass, final UiElementProperties properties, boolean requireVisible, final String[] propertyNamesToUseForMatch) {
    // nested class not to be anonymous in stack traces
    class MyGenericTypeMatcher<Type1 extends Component> extends GenericTypeMatcher<Type1> {

        private int currentIndex = 0;

        public MyGenericTypeMatcher(Class<Type1> componentClass, boolean requireVisible) {
            super(componentClass, requireVisible);
        }

        /**
             * In addition to the type check in constructor adds check by other component properties
             * @param component
             * @return
             */
        @Override
        protected boolean isMatching(Component component) {
            // here we are sure that we do not search by label and
            // also property "visible" and class are tracked additionally by FEST
            // Having several properties means match ALL of them
            int propertiesMatching = 0;
            int currentPropIdxToProcess = 0;
            for (currentPropIdxToProcess = 0; currentPropIdxToProcess < propertyNamesToUseForMatch.length; currentPropIdxToProcess++) {
                String keyName = propertyNamesToUseForMatch[currentPropIdxToProcess];
                if ("visible".equals(keyName) || "class".equals(keyName)) {
                    // already considered as parameter in search
                    propertiesMatching++;
                    continue;
                }
                String propertyValue = properties.getProperty(keyName);
                if (propertyValue != null) {
                    if ("name".equals(keyName)) {
                        if (propertyValue.equals(component.getName())) {
                            propertiesMatching++;
                            log.debug("Found element with 'name' property: " + component);
                            continue;
                        } else {
                            return false;
                        }
                    } else if ("text".equals(keyName)) {
                        // Search by specific component properties
                        if (component instanceof JButton) {
                            JButton button = (JButton) component;
                            if (propertyValue.equals(button.getText())) {
                                propertiesMatching++;
                                log.debug("Found element by 'text' property: " + button);
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JMenuItem) {
                            JMenuItem menuItem = (JMenuItem) component;
                            if (propertyValue.equals(menuItem.getText())) {
                                log.debug("Found element by 'text' property: " + menuItem);
                                propertiesMatching++;
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JPopupMenu) {
                            JPopupMenu popupMenu = (JPopupMenu) component;
                            if (propertyValue.equals(popupMenu.getLabel())) {
                                log.debug("Found element by 'text' property: " + popupMenu);
                                propertiesMatching++;
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JToggleButton) {
                            JToggleButton toggleButton = (JToggleButton) component;
                            if (propertyValue.equals(toggleButton.getText())) {
                                log.debug("Found element by 'text' property: " + toggleButton);
                                propertiesMatching++;
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JTextComponent) {
                            JTextComponent textComponent = (JTextComponent) component;
                            if (propertyValue.equals(textComponent.getText())) {
                                log.debug("Found element by 'text' property: " + textComponent);
                                propertiesMatching++;
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JLabel) {
                            JLabel label = (JLabel) component;
                            if (propertyValue.equals(label.getText())) {
                                log.debug("Found element by 'text' property: " + label);
                                propertiesMatching++;
                                continue;
                            }
                        }
                        // Attempt to search for 'text' for unsupported component type
                        return false;
                    } else if ("tooltip".equals(keyName)) {
                        // Search by specific component properties
                        if (component instanceof JButton) {
                            JButton button = (JButton) component;
                            if (propertyValue.equals(button.getToolTipText())) {
                                propertiesMatching++;
                                log.debug("Found element by 'tooltip' property: " + button);
                                continue;
                            } else {
                                return false;
                            }
                        }
                    } else if ("index".equals(keyName)) {
                        if (Integer.parseInt(propertyValue) == currentIndex++) {
                            propertiesMatching++;
                            continue;
                        }
                    } else {
                        throw new IllegalStateException("Attempt to search for not supported property: " + keyName + ", component: " + component);
                    }
                }
            // if property != null
            }
            if (propertyNamesToUseForMatch.length == propertiesMatching) {
                return true;
            } else {
                if (propertiesMatching > 0 && log.isDebugEnabled()) {
                    log.debug("Not all properties matched. Only " + propertiesMatching + " instead of " + properties.getPropertiesSize());
                }
                return false;
            }
        }
    }
    return new MyGenericTypeMatcher<T>(componentClass, requireVisible);
}
Also used : JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) JTextComponent(javax.swing.text.JTextComponent) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher) JPopupMenu(javax.swing.JPopupMenu) JToggleButton(javax.swing.JToggleButton) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) JMenuItem(javax.swing.JMenuItem)

Example 84 with JTextComponent

use of javax.swing.text.JTextComponent in project ats-framework by Axway.

the class SwingElementLocator method findFixture.

public static ComponentFixture<? extends Component> findFixture(UiElement uiElement) {
    SwingDriverInternal driver = (SwingDriverInternal) uiElement.getUiDriver();
    ContainerFixture<?> containerFixture = (ContainerFixture<?>) driver.getActiveContainerFixture();
    Class<? extends Component> componentClass = componentsMap.get(uiElement.getClass());
    try {
        if (componentClass.equals(JButton.class)) {
            return (ComponentFixture<? extends Component>) new JButtonFixture(containerFixture.robot, (JButton) findElement(uiElement));
        } else if (componentClass.equals(JTextComponent.class)) {
            return (ComponentFixture<? extends Component>) new JTextComponentFixture(containerFixture.robot, (JTextComponent) findElement(uiElement));
        } else if (componentClass.equals(JMenuItem.class)) {
            if (uiElement.getElementProperty("path") != null) {
                return containerFixture.menuItemWithPath(uiElement.getElementProperty("path").split("[\\,\\/]+"));
            } else {
                return (ComponentFixture<? extends Component>) new JMenuItemFixture(containerFixture.robot, (JMenuItem) findElement(uiElement));
            }
        } else if (componentClass.equals(JPopupMenu.class)) {
            return (ComponentFixture<? extends Component>) new JPopupMenuFixture(containerFixture.robot, (JPopupMenu) findElement(uiElement));
        } else if (componentClass.equals(JTree.class)) {
            return (ComponentFixture<? extends Component>) new JTreeFixture(containerFixture.robot, (JTree) findElement(uiElement));
        } else if (componentClass.equals(JList.class)) {
            return (ComponentFixture<? extends Component>) new JListFixture(containerFixture.robot, (JList) findElement(uiElement));
        } else if (componentClass.equals(JCheckBox.class)) {
            return (ComponentFixture<? extends Component>) new JCheckBoxFixture(containerFixture.robot, (JCheckBox) findElement(uiElement));
        } else if (componentClass.equals(JToggleButton.class)) {
            return (ComponentFixture<? extends Component>) new JToggleButtonFixture(containerFixture.robot, (JToggleButton) findElement(uiElement));
        } else if (componentClass.equals(JComboBox.class)) {
            return (ComponentFixture<? extends Component>) new JComboBoxFixture(containerFixture.robot, (JComboBox) findElement(uiElement));
        } else if (componentClass.equals(JRadioButton.class)) {
            return (ComponentFixture<? extends Component>) new JRadioButtonFixture(containerFixture.robot, (JRadioButton) findElement(uiElement));
        } else if (componentClass.equals(JTable.class)) {
            return (ComponentFixture<? extends Component>) new JTableFixture(containerFixture.robot, (JTable) findElement(uiElement));
        } else if (componentClass.equals(JSpinner.class)) {
            return (ComponentFixture<? extends Component>) new JSpinnerFixture(containerFixture.robot, (JSpinner) findElement(uiElement));
        } else if (componentClass.equals(JTabbedPane.class)) {
            return (ComponentFixture<? extends Component>) new JTabbedPaneFixture(containerFixture.robot, (JTabbedPane) findElement(uiElement));
        } else if (componentClass.equals(JOptionPane.class)) {
            return (ComponentFixture<? extends Component>) containerFixture.optionPane();
        } else if (componentClass.equals(JLabel.class)) {
            return (ComponentFixture<? extends Component>) new JLabelFixture(containerFixture.robot, (JLabel) findElement(uiElement));
        } else if (componentClass.equals(Component.class)) {
            return new ComponentFixture<Component>(containerFixture.robot, findElement(uiElement)) {
            };
        } else if (componentClass.equals(JFileChooser.class)) {
            // TODO - might be searched by name too
            return containerFixture.fileChooser(Timeout.timeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()));
        } else {
            throw new ElementNotFoundException(uiElement.toString() + " not found. No such Fixture");
        }
    } catch (ComponentLookupException cle) {
        throw new ElementNotFoundException(uiElement.toString() + " not found.", cle);
    } catch (WaitTimedOutError exc) {
        // thrown for OptionPane search, wait for Window (BasicRobot.waitForWindow), AbstractJTableCellWriter, JTreeDriver.waitForChildrenToShowUp, each Pause wait
        throw new ElementNotFoundException(uiElement.toString() + " not found.", exc);
    }
}
Also used : JTreeFixture(org.fest.swing.fixture.JTreeFixture) JRadioButton(javax.swing.JRadioButton) SwingDriverInternal(com.axway.ats.uiengine.internal.driver.SwingDriverInternal) JTabbedPane(javax.swing.JTabbedPane) JButton(javax.swing.JButton) JTextComponent(javax.swing.text.JTextComponent) JSpinnerFixture(org.fest.swing.fixture.JSpinnerFixture) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) ComponentLookupException(org.fest.swing.exception.ComponentLookupException) WaitTimedOutError(org.fest.swing.exception.WaitTimedOutError) JLabelFixture(org.fest.swing.fixture.JLabelFixture) JTabbedPaneFixture(org.fest.swing.fixture.JTabbedPaneFixture) JButtonFixture(org.fest.swing.fixture.JButtonFixture) JToggleButtonFixture(org.fest.swing.fixture.JToggleButtonFixture) JComboBoxFixture(org.fest.swing.fixture.JComboBoxFixture) JTableFixture(org.fest.swing.fixture.JTableFixture) JToggleButton(javax.swing.JToggleButton) JTextComponentFixture(org.fest.swing.fixture.JTextComponentFixture) ComponentFixture(org.fest.swing.fixture.ComponentFixture) JMenuItemFixture(org.fest.swing.fixture.JMenuItemFixture) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) JMenuItem(javax.swing.JMenuItem) JTextComponentFixture(org.fest.swing.fixture.JTextComponentFixture) JRadioButtonFixture(org.fest.swing.fixture.JRadioButtonFixture) JComboBox(javax.swing.JComboBox) JLabel(javax.swing.JLabel) JOptionPane(javax.swing.JOptionPane) JPopupMenu(javax.swing.JPopupMenu) JCheckBox(javax.swing.JCheckBox) JTree(javax.swing.JTree) JPopupMenuFixture(org.fest.swing.fixture.JPopupMenuFixture) JListFixture(org.fest.swing.fixture.JListFixture) ContainerFixture(org.fest.swing.fixture.ContainerFixture) JTable(javax.swing.JTable) JSpinner(javax.swing.JSpinner) JCheckBoxFixture(org.fest.swing.fixture.JCheckBoxFixture) JList(javax.swing.JList)

Example 85 with JTextComponent

use of javax.swing.text.JTextComponent in project SKMCLauncher by SKCraft.

the class TextFieldPopupMenu method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    JTextComponent textComponent = (JTextComponent) getInvoker();
    textComponent.requestFocus();
    boolean haveSelection = textComponent.getSelectionStart() != textComponent.getSelectionEnd();
    if (e.getSource() == cutItem) {
        if (!haveSelection)
            textComponent.selectAll();
        textComponent.cut();
    } else if (e.getSource() == copyItem) {
        if (!haveSelection)
            textComponent.selectAll();
        textComponent.copy();
    } else if (e.getSource() == pasteItem) {
        textComponent.paste();
    } else if (e.getSource() == deleteItem) {
        if (!haveSelection)
            textComponent.selectAll();
        textComponent.replaceSelection("");
    } else if (e.getSource() == selectAllItem) {
        textComponent.selectAll();
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent)

Aggregations

JTextComponent (javax.swing.text.JTextComponent)181 Component (java.awt.Component)28 JComponent (javax.swing.JComponent)16 BadLocationException (javax.swing.text.BadLocationException)13 NotNull (org.jetbrains.annotations.NotNull)13 DocumentEvent (javax.swing.event.DocumentEvent)11 DocumentAdapter (com.intellij.ui.DocumentAdapter)8 Caret (javax.swing.text.Caret)8 Document (javax.swing.text.Document)8 Point (java.awt.Point)7 ActionEvent (java.awt.event.ActionEvent)7 ActionListener (java.awt.event.ActionListener)7 ArrayList (java.util.ArrayList)7 ComboBoxEditor (javax.swing.ComboBoxEditor)7 FocusEvent (java.awt.event.FocusEvent)6 Color (java.awt.Color)5 KeyEvent (java.awt.event.KeyEvent)5 MouseEvent (java.awt.event.MouseEvent)5 JButton (javax.swing.JButton)5 JComboBox (javax.swing.JComboBox)5