Example usage for java.awt.event KeyEvent getSource

List of usage examples for java.awt.event KeyEvent getSource

Introduction

In this page you can find the example usage for java.awt.event KeyEvent getSource.

Prototype

public Object getSource() 

Source Link

Document

The object on which the Event initially occurred.

Usage

From source file:net.sf.jabref.gui.AutoCompleteListener.java

@Override
public void keyPressed(KeyEvent e) {
    if ((toSetIn != null) && (e.getKeyCode() == KeyEvent.VK_ENTER)) {
        JTextComponent comp = (JTextComponent) e.getSource();

        // replace typed characters by characters from completion
        lastBeginning = lastCompletions[lastShownCompletion];

        int end = comp.getSelectionEnd();
        comp.select(end, end);/*from  w ww  . j av a  2 s . co  m*/
        toSetIn = null;
        if (consumeEnterKey) {
            e.consume();
        }
    }
    // Cycle through alternative completions when user presses PGUP/PGDN:
    else if ((e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) && (toSetIn != null)) {
        cycle((JTextComponent) e.getSource(), 1);
        e.consume();
    } else if ((e.getKeyCode() == KeyEvent.VK_PAGE_UP) && (toSetIn != null)) {
        cycle((JTextComponent) e.getSource(), -1);
        e.consume();
    }
    //        else if ((e.getKeyCode() == KeyEvent.VK_BACK_SPACE)) {
    //           StringBuffer currentword = getCurrentWord((JTextComponent) e.getSource());
    //           // delete last char to obey semantics of back space
    //           currentword.deleteCharAt(currentword.length()-1);
    //           doCompletion(currentword, e);
    //        }
    else if (e.getKeyChar() == KeyEvent.CHAR_UNDEFINED) {
        if (e.getKeyCode() != KeyEvent.VK_SHIFT) {
            // shift is OK, everyhting else leads to a reset
            resetAutoCompletion();
        } else {
            LOGGER.debug("Special case: shift pressed. No action.");
        }
    } else {
        LOGGER.debug("Special case: defined character, but not caught above");
    }
}

From source file:com.github.fritaly.dualcommander.DirectoryBrowser.java

@Override
public void keyReleased(KeyEvent e) {
    if (e.getSource() != table) {
        return;/* w  w  w  .  j a va  2s.  c  om*/
    }

    // Propagate event to our listeners
    processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(),
            e.getKeyLocation()));
}

From source file:com.github.fritaly.dualcommander.DirectoryBrowser.java

@Override
public void keyTyped(KeyEvent e) {
    if (e.getSource() != table) {
        return;//  w ww  . j  a v  a  2s.com
    }

    // Propagate event to our listeners
    processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(),
            e.getKeyLocation()));
}

From source file:org.mbs3.juniuploader.gui.pnlSettings.java

private void tfUserAgentKeyReleased(KeyEvent evt) {
    if (evt.getSource() == tfUserAgent)
        Util.setUserAgent(tfUserAgent.getText());
}

From source file:com.github.fritaly.dualcommander.DirectoryBrowser.java

@Override
public void keyPressed(KeyEvent e) {
    if (e.getSource() != table) {
        return;//w  ww .  j  a  va 2s  . co  m
    }

    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        // What's the current selection ?
        final List<File> selection = getSelection();

        if (selection.size() == 1) {
            final File selectedFile = selection.iterator().next();

            if (selectedFile.isDirectory()) {
                // Change to the selected directory
                setDirectory(selectedFile);
            }
        }
    } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
        // Return to the parent directory (if any)
        final File parentDir = getParentDirectory();

        if ((parentDir != null) && parentDir.exists()) {
            setDirectory(parentDir);
        }
    } else {
        // Propagate event to our listeners
        processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(),
                e.getKeyChar(), e.getKeyLocation()));
    }
}

From source file:org.docx4all.swing.ExternalHyperlinkDialog.java

private JPanel createContentPanel(String sourceFileVFSUrlPath, HyperlinkML hyperlinkML) {
    JPanel thePanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    fillRow1(thePanel, c);/*w w  w  . j a v  a  2 s .  c o m*/
    fillRow2(thePanel, c);
    fillRow3(thePanel, c);
    fillRow4(thePanel, c);
    fillRow5(thePanel, c);

    //Display Text Field
    if (hyperlinkML.getDisplayText() != null) {
        this.displayTextField.setText(hyperlinkML.getDisplayText());
    }

    this.displayTextFieldAutoUpdate = (this.displayTextField.getText().length() == 0);
    //Set DisplayTextFieldAutoUpdate flag
    this.displayTextField.addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent e) {
            JTextField field = (JTextField) e.getSource();
            if (field.getText().length() == 0) {
                ExternalHyperlinkDialog.this.displayTextFieldAutoUpdate = true;
            } else {
                ExternalHyperlinkDialog.this.displayTextFieldAutoUpdate = false;
            }
        }
    });

    //Tooltip Field
    if (hyperlinkML.getTooltip() != null) {
        this.tooltipField.setText(hyperlinkML.getTooltip());
    }

    //Target Field is split into documentNameField and directoryPathField.
    //These two fields are in friendly format; ie: no user credentials
    String target = HyperlinkML.encodeTarget(hyperlinkML, getSourceFileObject(), true);
    if (target != null) {
        int idx = target.lastIndexOf("/");
        this.documentNameField.setText(target.substring(idx + 1));
        if (target.startsWith(FILE_PREFIX)) {
            target = target.substring(FILE_PREFIX.length(), idx);
        } else {
            target = target.substring(0, idx);
        }
        this.directoryPathField.setText(target);
    } else {
        //A brand new HyperlinkML may be ?
        target = VFSUtils.getFriendlyName(this.sourceFileVFSUrlPath);
        int idx = target.lastIndexOf("/");
        //Leave this.documentNameField blank
        this.directoryPathField.setText(target.substring(0, idx));
    }

    //Install DocumentNameFieldListener so that Display Text Field
    //can be automatically updated.
    this.documentNameField.getDocument().addDocumentListener(new DocumentNameFieldListener());

    //We also need to keep a reference to target in complete VFS url format;
    //ie: include user credentials in the reference.
    target = HyperlinkML.encodeTarget(hyperlinkML, getSourceFileObject(), false);
    if (target != null) {
        int idx = target.lastIndexOf("/");
        this.directoryUrlPath = target.substring(0, idx);
    } else {
        //A brand new HyperlinkML may be ?
        int idx = this.sourceFileVFSUrlPath.lastIndexOf("/");
        this.directoryUrlPath = this.sourceFileVFSUrlPath.substring(0, idx);
    }

    return thePanel;
}

From source file:com.willwinder.ugs.nbp.setupwizard.panels.WizardPanelStepCalibration.java

private KeyListener createKeyListenerChangeSetting(Axis axis, JButton buttonUpdateSettings) {
    return new KeyListener() {
        @Override/*from  w  w w .  j a v a 2s. c  o  m*/
        public void keyTyped(KeyEvent event) {

        }

        @Override
        public void keyPressed(KeyEvent event) {

        }

        @Override
        public void keyReleased(KeyEvent event) {
            if (getBackend().getController() != null
                    && getBackend().getController().getFirmwareSettings() != null) {
                try {
                    JTextField source = (JTextField) event.getSource();
                    IFirmwareSettings firmwareSettings = getBackend().getController().getFirmwareSettings();

                    int stepsPerMillimeter = firmwareSettings.getStepsPerMillimeter(axis);
                    if (!StringUtils.isNumeric(source.getText())
                            || source.getText().trim().equalsIgnoreCase(String.valueOf(stepsPerMillimeter))) {
                        buttonUpdateSettings.setEnabled(false);
                    } else if (StringUtils.isNumeric(source.getText())) {
                        buttonUpdateSettings.setEnabled(true);
                    }
                } catch (FirmwareSettingsException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}

From source file:org.openmicroscopy.shoola.agents.dataBrowser.view.SearchPanel.java

/** Initializes the components composing the display. */
private void initComponents() {
    usersBox = new JComboBox();
    groupsBox = createGroupBox();//w  ww.ja v a 2  s.c  o m
    scopes = new HashMap<Integer, JCheckBox>(model.getNodes().size());
    types = new HashMap<Integer, JCheckBox>(model.getTypes().size());
    IconManager icons = IconManager.getInstance();
    fromDate = UIUtilities.createDatePicker(true, EditorUtil.DATE_PICKER_FORMAT);
    fromDate.setBackground(UIUtilities.BACKGROUND_COLOR);
    fromDate.addPropertyChangeListener("date", new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Date d = (Date) evt.getNewValue();
            if (d != null) {
                if (d.after(new Date())) {
                    UserNotifier un = DataBrowserAgent.getRegistry().getUserNotifier();
                    un.notifyWarning("Invalid Date", "Selecting a future 'From' date doesn't make any sense.");
                    fromDate.setDate(null);
                }
                if (toDate.getDate() != null && d.after(toDate.getDate())) {
                    UserNotifier un = DataBrowserAgent.getRegistry().getUserNotifier();
                    un.notifyWarning("Invalid Date",
                            "Cannot set a 'From' date which is more recent than the 'To' date.");
                    fromDate.setDate(null);
                }
            }
        }
    });
    fromDate.setToolTipText(DATE_TOOLTIP);

    toDate = UIUtilities.createDatePicker(true, EditorUtil.DATE_PICKER_FORMAT);
    toDate.setBackground(UIUtilities.BACKGROUND_COLOR);
    toDate.setToolTipText(DATE_TOOLTIP);
    toDate.addPropertyChangeListener("date", new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Date d = (Date) evt.getNewValue();
            if (d != null) {
                if (fromDate.getDate() != null && d.before(fromDate.getDate())) {
                    UserNotifier un = DataBrowserAgent.getRegistry().getUserNotifier();
                    un.notifyWarning("Invalid Date",
                            "Cannot set a 'To' date which is prior to the 'From' date.");
                    toDate.setDate(null);
                }
            }
        }
    });

    clearDate = new JButton(icons.getIcon(IconManager.CLOSE));
    clearDate.setToolTipText("Reset the dates");
    UIUtilities.unifiedButtonLookAndFeel(clearDate);
    clearDate.setBackground(UIUtilities.BACKGROUND_COLOR);
    clearDate.setActionCommand("" + SearchComponent.RESET_DATE);
    clearDate.addActionListener(model);

    fullTextArea = new JTextField(AREA_COLUMNS);
    fullTextArea.addKeyListener(new KeyAdapter() {

        /** Finds the phrase. */
        public void keyPressed(KeyEvent e) {
            Object source = e.getSource();
            if (source != fullTextArea)
                return;
            switch (e.getKeyCode()) {
            case KeyEvent.VK_ENTER:
                model.search();
            }
        }
    });

    helpBasicButton = new JButton(icons.getIcon(IconManager.HELP));
    helpBasicButton.setToolTipText("Search Tips.");
    helpBasicButton.setBackground(UIUtilities.BACKGROUND_COLOR);
    UIUtilities.unifiedButtonLookAndFeel(helpBasicButton);
    helpBasicButton.addActionListener(model);
    helpBasicButton.setActionCommand("" + SearchComponent.HELP);

    SearchContext ctx = model.getSearchContext();
    if (ctx == null)
        return;
}

From source file:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java

@Override
public void keyTyped(KeyEvent e) {
    LOGGER.debug("key typed event caught " + e.getKeyCode());
    char ch = e.getKeyChar();
    if (ch == '\n') {
        // this case is handled at keyPressed(e)
        return;// www  .ja  v a2s .  c o  m
    }

    // don't do auto completion inside words
    if (!atEndOfWord((JTextComponent) e.getSource())) {
        return;
    }

    if ((e.getModifiers() | InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK) {
        // plain key or SHIFT + key is pressed, no handling of CTRL+key,  META+key, ...
        if (Character.isLetter(ch) || Character.isDigit(ch)
                || (Character.isWhitespace(ch) && completer.isSingleUnitField())) {
            JTextComponent comp = (JTextComponent) e.getSource();

            if (toSetIn == null) {
                LOGGER.debug("toSetIn is null");
            } else {
                LOGGER.debug("toSetIn: >" + toSetIn + '<');
            }

            // The case-insensitive system is a bit tricky here
            // If keyword is "TODO" and user types "tO", then this is treated as "continue" as the "O" matches the "O"
            // If keyword is "TODO" and user types "To", then this is treated as "discont" as the "o" does NOT match the "O".

            if ((toSetIn != null) && (toSetIn.length() > 1) && (ch == toSetIn.charAt(1))) {
                // User continues on the word that was suggested.
                LOGGER.debug("cont");

                toSetIn = toSetIn.substring(1);
                if (!toSetIn.isEmpty()) {
                    int cp = comp.getCaretPosition();
                    //comp.setCaretPosition(cp+1-toSetIn.);
                    comp.select((cp + 1) - toSetIn.length(), cp);
                    lastBeginning = lastBeginning + ch;

                    e.consume();
                    lastCaretPosition = comp.getCaretPosition();

                    lastCompletions = findCompletions(lastBeginning);
                    lastShownCompletion = 0;
                    for (int i = 0; i < lastCompletions.size(); i++) {
                        String lastCompletion = lastCompletions.get(i);
                        if (lastCompletion.endsWith(toSetIn)) {
                            lastShownCompletion = i;
                            break;
                        }

                    }
                    if (toSetIn.length() < 2) {
                        // User typed the last character of the autocompleted word
                        // We have to replace the automcompletion word by the typed word.
                        // This helps if the user presses "space" after the completion
                        // "space" indicates that the user does NOT want the autocompletion,
                        // but the typed word
                        String text = comp.getText();
                        comp.setText(text.substring(0, lastCaretPosition - lastBeginning.length())
                                + lastBeginning + text.substring(lastCaretPosition));
                        // there is no selected text, therefore we are not updating the selection
                        toSetIn = null;
                    }
                    return;
                }
            }

            if ((toSetIn != null) && ((toSetIn.length() <= 1) || (ch != toSetIn.charAt(1)))) {
                // User discontinues the word that was suggested.
                lastBeginning = lastBeginning + ch;

                LOGGER.debug("discont toSetIn: >" + toSetIn + "'<' lastBeginning: >" + lastBeginning + '<');

                List<String> completed = findCompletions(lastBeginning);
                if ((completed != null) && (!completed.isEmpty())) {
                    lastShownCompletion = 0;
                    lastCompletions = completed;
                    String sno = completed.get(0);
                    // toSetIn = string used for autocompletion last time
                    // this string has to be removed
                    // lastCaretPosition is the position of the caret after toSetIn.
                    int lastLen = toSetIn.length() - 1;
                    toSetIn = sno.substring(lastBeginning.length() - 1);
                    String text = comp.getText();
                    //we do not use toSetIn as we want to obey the casing of "sno"
                    comp.setText(text.substring(0, (lastCaretPosition - lastLen - lastBeginning.length()) + 1)
                            + sno + text.substring(lastCaretPosition));
                    int startSelect = (lastCaretPosition + 1) - lastLen;
                    int endSelect = (lastCaretPosition + toSetIn.length()) - lastLen;
                    comp.select(startSelect, endSelect);

                    lastCaretPosition = comp.getCaretPosition();
                    e.consume();
                    return;
                } else {
                    setUnmodifiedTypedLetters(comp, true, false);
                    e.consume();
                    toSetIn = null;
                    return;
                }
            }

            LOGGER.debug("case else");

            comp.replaceSelection("");

            StringBuffer currentword = getCurrentWord(comp);

            // only "real characters" end up here
            assert (!Character.isISOControl(ch));
            currentword.append(ch);
            startCompletion(currentword, e);
            return;
        } else {
            if (Character.isWhitespace(ch)) {
                assert (!completer.isSingleUnitField());
                LOGGER.debug("whitespace && !singleUnitField");
                // start a new search if end-of-field is reached

                // replace displayed letters with typed letters
                setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, true);
                resetAutoCompletion();
                return;
            }

            LOGGER.debug("No letter/digit/whitespace or CHAR_UNDEFINED");
            // replace displayed letters with typed letters
            setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, !Character.isISOControl(ch));
            resetAutoCompletion();
            return;
        }
    }
    resetAutoCompletion();
}

From source file:ropes.MainWindow.java

private void jTextField_sliderValueKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_sliderValueKeyReleased
    JTextField txt = (JTextField) evt.getSource();
    if (txt.getText().equals("")) {
        txt.setText("0");
        return;/*w  ww.ja  v  a 2  s.  c o  m*/
    }
    jSlider_size_select.setValue(Integer.valueOf(txt.getText()));
}