Search in sources :

Example 31 with FocusEvent

use of java.awt.event.FocusEvent in project knime-core by knime.

the class LimitRowsPanel method getLimitPanel.

private Container getLimitPanel() {
    m_limitRows = new JCheckBox("Read only the first");
    m_maxNumber = new JTextField();
    m_maxNumber.setColumns(6);
    m_maxNumber.setPreferredSize(new Dimension(125, 25));
    m_maxNumber.setMaximumSize(new Dimension(125, 25));
    m_limitRows.setSelected(false);
    m_maxNumber.setEnabled(false);
    // make sure we always have a valid value. Reject invalid characters.
    m_maxNumber.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void removeUpdate(final DocumentEvent e) {
            checkAndFixTextfield();
        }

        @Override
        public void insertUpdate(final DocumentEvent e) {
            checkAndFixTextfield();
        }

        @Override
        public void changedUpdate(final DocumentEvent e) {
            checkAndFixTextfield();
        }
    });
    m_maxNumber.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(final FocusEvent e) {
            if (m_maxNumber.getText().trim().length() == 0) {
                // can't handle no empty strings.
                m_lastValidValue = "0";
                m_maxNumber.setText(m_lastValidValue);
            }
        }

        @Override
        public void focusGained(final FocusEvent e) {
        }
    });
    m_limitRows.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(final ChangeEvent e) {
            // en/disable the textfield
            m_maxNumber.setEnabled(m_limitRows.isSelected());
            // also - if the textfield is enabled and empty set a value
            if (m_maxNumber.isEnabled()) {
                if ((m_maxNumber.getText() == null) || (m_maxNumber.getText().trim().length() == 0)) {
                    // set a valid value, so we can safely assume that if there is a value in there it's always valid.
                    m_maxNumber.setText("1000");
                    m_lastValidValue = "1000";
                }
            }
        }
    });
    Box result = Box.createHorizontalBox();
    result.add(m_limitRows);
    result.add(Box.createHorizontalStrut(3));
    result.add(m_maxNumber);
    result.add(Box.createHorizontalStrut(3));
    result.add(new JLabel("table rows from the file."));
    result.add(Box.createHorizontalGlue());
    return result;
}
Also used : JCheckBox(javax.swing.JCheckBox) DocumentListener(javax.swing.event.DocumentListener) ChangeEvent(javax.swing.event.ChangeEvent) JLabel(javax.swing.JLabel) ChangeListener(javax.swing.event.ChangeListener) Box(javax.swing.Box) JCheckBox(javax.swing.JCheckBox) Dimension(java.awt.Dimension) JTextField(javax.swing.JTextField) DocumentEvent(javax.swing.event.DocumentEvent) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent)

Example 32 with FocusEvent

use of java.awt.event.FocusEvent in project vcell by virtualcell.

the class DisabledTreeCellEditor method getTreeCellEditorComponent.

@Override
public Component getTreeCellEditorComponent(final JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
    Component container = super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
    // Note: System.out.println("Components Type: "+containeractually.getClass().getName());
    // was used to show it was container, javax.swing.tree.DefaultTreeCellEditor$EditorContainer
    // getTreeCellEditorComponent(parameters) is called as soon as editing begins
    // also at this time editingComponent != null (aka initialized)
    // so it's a good place to add in a Focus Listener
    editingComponent.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            tree.stopEditing();
        }
    });
    // (I think the return statement means it's about to be used)
    return container;
}
Also used : Component(java.awt.Component) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent)

Example 33 with FocusEvent

use of java.awt.event.FocusEvent in project processdash by dtuma.

the class HttpAuthenticator method getPasswordAuthentication.

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    // only handle "server" auth requests (no proxy support for now)
    if (getRequestorType() != RequestorType.SERVER)
        return null;
    // only handle Data Bridge requests (no support for non-PDES servers)
    if (getRequestingURL().toString().indexOf("/DataBridge/") == -1 && getRequestingURL().toString().indexOf("/api/v1/") == -1)
        return null;
    // find out what state we are presently in.
    determineCurrentState();
    // if we're in a failure state, return no auth data.
    if (state == State.Failed || state == State.Cancelled)
        return null;
    // possibly supply credentials that were stored in the keyring
    if (state == State.Keyring) {
        char[] password = getPasswordFromKeyring(lastUsername);
        if (password != null)
            return new PasswordAuthentication(lastUsername, password);
        else if (lastUsername != null && lastPassword != null)
            return new PasswordAuthentication(lastUsername, mask(lastPassword));
        else
            state = State.UserEntry1;
    }
    // create user interface components to prompt for username and password
    JTextField username = new JTextField(2);
    JPasswordField password = new JPasswordField(2);
    FocusAdapter selectAll = new FocusAdapter() {

        public void focusGained(FocusEvent e) {
            if (e.getComponent() instanceof JTextField) {
                ((JTextField) e.getComponent()).selectAll();
            }
        }
    };
    username.addFocusListener(selectAll);
    password.addFocusListener(selectAll);
    JComponent focus = username;
    if (StringUtils.hasValue(lastUsername)) {
        username.setText(lastUsername);
        focus = password;
    }
    JLabel usernameLabel = new JLabel(resources.getString("Username"), SwingConstants.RIGHT);
    JLabel passwordLabel = new JLabel(resources.getString("Password"), SwingConstants.RIGHT);
    Dimension d = usernameLabel.getPreferredSize();
    d.width = Math.max(d.width, passwordLabel.getPreferredSize().width);
    usernameLabel.setPreferredSize(d);
    passwordLabel.setPreferredSize(d);
    // if "remember me" support is enabled, create a checkbox
    JCheckBox rememberMe = null;
    if (rememberMeDays > 0) {
        rememberMe = new JCheckBox(resources.getString("Remember_Me.Prompt"));
        rememberMe.setToolTipText(resources.format("Remember_Me.Tooltip_FMT", rememberMeDays));
        Font f = rememberMe.getFont();
        f = f.deriveFont(f.getSize2D() * 0.8f);
        rememberMe.setFont(f);
    }
    // prompt the user for credentials
    final String title = (StringUtils.hasValue(this.title) ? this.title : resources.getString("Title"));
    String[] promptLines = resources.formatStrings("Prompt_FMT", getRequestingPrompt());
    Object[] prompt = new Object[promptLines.length];
    System.arraycopy(promptLines, 0, prompt, 0, prompt.length);
    // add a tooltip to the last line of the prompt, indicating the URL
    JLabel promptLabel = new JLabel(promptLines[prompt.length - 1]);
    promptLabel.setToolTipText(getEffectiveURL());
    prompt[prompt.length - 1] = promptLabel;
    final Object[] message = new Object[] { prompt, BoxUtils.vbox(5), BoxUtils.hbox(15, usernameLabel, 5, username), BoxUtils.hbox(15, passwordLabel, 5, password), BoxUtils.hbox(BoxUtils.GLUE, rememberMe), new JOptionPaneTweaker.GrabFocus(focus) };
    final int[] userChoice = new int[1];
    try {
        Runnable r = new Runnable() {

            public void run() {
                userChoice[0] = JOptionPane.showConfirmDialog(parentComponent, message, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            }
        };
        if (SwingUtilities.isEventDispatchThread())
            r.run();
        else
            SwingUtilities.invokeAndWait(r);
    } catch (Exception e) {
    }
    // record metadata about this password request
    lastUrl = getEffectiveURL();
    lastTimestamp = System.currentTimeMillis();
    lastUsername = username.getText().trim();
    prefs.put(prefsKey(LAST_USERNAME), lastUsername);
    if (userChoice[0] == JOptionPane.OK_OPTION) {
        // if the user entered credentials, return them.
        if (rememberMe != null && rememberMe.isSelected())
            savePasswordToKeyring(lastUsername, password.getPassword());
        lastPassword = mask(password.getPassword());
        return new PasswordAuthentication(lastUsername, password.getPassword());
    } else {
        // if the user cancelled the operation, abort.
        state = State.Cancelled;
        return null;
    }
}
Also used : FocusAdapter(java.awt.event.FocusAdapter) JComponent(javax.swing.JComponent) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) JTextField(javax.swing.JTextField) FocusEvent(java.awt.event.FocusEvent) Font(java.awt.Font) JCheckBox(javax.swing.JCheckBox) JPasswordField(javax.swing.JPasswordField) PasswordAuthentication(java.net.PasswordAuthentication)

Example 34 with FocusEvent

use of java.awt.event.FocusEvent in project jdk8u_jdk by JetBrains.

the class InputContext method dispatchEvent.

/**
     * @see java.awt.im.InputContext#dispatchEvent
     */
@SuppressWarnings("fallthrough")
public void dispatchEvent(AWTEvent event) {
    if (event instanceof InputMethodEvent) {
        return;
    }
    // This is a workaround.  Should be removed after 4452384 is fixed.
    if (event instanceof FocusEvent) {
        Component opposite = ((FocusEvent) event).getOppositeComponent();
        if ((opposite != null) && (getComponentWindow(opposite) instanceof InputMethodWindow) && (opposite.getInputContext() == this)) {
            return;
        }
    }
    InputMethod inputMethod = getInputMethod();
    int id = event.getID();
    switch(id) {
        case FocusEvent.FOCUS_GAINED:
            focusGained((Component) event.getSource());
            break;
        case FocusEvent.FOCUS_LOST:
            focusLost((Component) event.getSource(), ((FocusEvent) event).isTemporary());
            break;
        case KeyEvent.KEY_PRESSED:
            if (checkInputMethodSelectionKey((KeyEvent) event)) {
                // pop up the input method selection menu
                InputMethodManager.getInstance().notifyChangeRequestByHotKey((Component) event.getSource());
                break;
            }
        default:
            if ((inputMethod != null) && (event instanceof InputEvent)) {
                inputMethod.dispatchEvent(event);
            }
    }
}
Also used : InputMethod(java.awt.im.spi.InputMethod) InputMethodEvent(java.awt.event.InputMethodEvent) InputEvent(java.awt.event.InputEvent) Component(java.awt.Component) FocusEvent(java.awt.event.FocusEvent)

Example 35 with FocusEvent

use of java.awt.event.FocusEvent in project jdk8u_jdk by JetBrains.

the class KeyboardFocusManagerPeerImpl method clearGlobalFocusOwner.

@Override
public void clearGlobalFocusOwner(Window activeWindow) {
    if (activeWindow != null) {
        Component focusOwner = activeWindow.getFocusOwner();
        if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
            focusLog.fine("Clearing global focus owner " + focusOwner);
        }
        if (focusOwner != null) {
            FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null, CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
            SunToolkit.postPriorityEvent(fl);
        }
    }
}
Also used : Component(java.awt.Component) FocusEvent(java.awt.event.FocusEvent)

Aggregations

FocusEvent (java.awt.event.FocusEvent)81 FocusListener (java.awt.event.FocusListener)38 FocusAdapter (java.awt.event.FocusAdapter)33 JLabel (javax.swing.JLabel)21 ActionEvent (java.awt.event.ActionEvent)18 ActionListener (java.awt.event.ActionListener)17 JPanel (javax.swing.JPanel)15 JTextField (javax.swing.JTextField)15 Dimension (java.awt.Dimension)14 JButton (javax.swing.JButton)10 KeyEvent (java.awt.event.KeyEvent)9 JCheckBox (javax.swing.JCheckBox)9 BoxLayout (javax.swing.BoxLayout)8 JComboBox (javax.swing.JComboBox)8 Border (javax.swing.border.Border)8 Component (java.awt.Component)7 GridBagConstraints (java.awt.GridBagConstraints)6 GridBagLayout (java.awt.GridBagLayout)6 Insets (java.awt.Insets)6 MouseEvent (java.awt.event.MouseEvent)6