Search in sources :

Example 56 with DocumentListener

use of javax.swing.event.DocumentListener in project ChatGameFontificator by GlitchCog.

the class ListInput method build.

private void build(String label, String description, int size) {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = ControlPanelBase.getGbc();
    gbc.insets = new Insets(0, 0, 0, 0);
    this.label = label == null ? null : new JLabel(label);
    if (description != null) {
        this.description = new JLabel(description);
        Font defFont = this.description.getFont();
        this.description.setFont(new Font(defFont.getName(), defFont.getStyle(), defFont.getSize() - 2));
    }
    this.input = new JTextField(12);
    this.list = new List(size);
    this.list.setMultipleMode(true);
    this.add = new JButton("✚");
    this.add.setBackground(Color.WHITE);
    this.add.setMargin(new Insets(0, 0, 0, 0));
    this.remove = new JButton("✘");
    this.remove.setBackground(Color.WHITE);
    this.remove.setMargin(new Insets(0, 0, 0, 0));
    // Backup in case the fancy unicode characters for the buttons aren't supported by the font
    if (!this.add.getFont().canDisplay(add.getText().charAt(0))) {
        this.add.setText("+");
    }
    if (!this.remove.getFont().canDisplay(remove.getText().charAt(0))) {
        this.add.setText("-");
    }
    // Listener to enable remove button when at least one item on the list is selected
    this.list.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            enableRemoveButton();
        }
    });
    // Listener to enable add button when a valid value has been entered in the input
    this.input.getDocument().addDocumentListener(new DocumentListener() {

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

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

        @Override
        public void changedUpdate(DocumentEvent e) {
            change(e);
        }

        private void change(DocumentEvent e) {
            enableAddButton();
        }
    });
    this.input.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            enableAddButton();
            if (add.isEnabled()) {
                addInput();
            }
        }
    });
    // Listener to handle add and remove button clicks
    ActionListener al = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();
            if (add.equals(source)) {
                addInput();
            } else if (remove.equals(source)) {
                String[] selectedItems = list.getSelectedItems();
                input.setText(selectedItems.length == 1 ? selectedItems[0] : "");
                for (String item : selectedItems) {
                    list.remove(item);
                }
                remove.setEnabled(false);
                censor.recheckCensorship();
            }
        }
    };
    this.add.addActionListener(al);
    this.remove.addActionListener(al);
    // Start off the add and remove buttons disabled
    this.add.setEnabled(false);
    this.remove.setEnabled(false);
    JPanel inputPanel = new JPanel(new GridBagLayout());
    GridBagConstraints ipGbc = ControlPanelBase.getGbc();
    ipGbc.insets = new Insets(0, 0, 0, 0);
    ipGbc.weightx = 1.0;
    ipGbc.fill = GridBagConstraints.BOTH;
    inputPanel.add(this.input, ipGbc);
    ipGbc.gridx++;
    ipGbc.weightx = 0.0;
    ipGbc.fill = GridBagConstraints.VERTICAL;
    inputPanel.add(this.add, ipGbc);
    ipGbc.gridx++;
    inputPanel.add(this.remove, ipGbc);
    ipGbc.gridx++;
    gbc.anchor = GridBagConstraints.CENTER;
    if (this.label != null) {
        add(this.label, gbc);
        gbc.gridy++;
    }
    if (this.description != null) {
        add(this.description, gbc);
        gbc.gridy++;
    }
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(inputPanel, gbc);
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridy++;
    add(this.list, gbc);
    gbc.gridy++;
}
Also used : DocumentListener(javax.swing.event.DocumentListener) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) ItemEvent(java.awt.event.ItemEvent) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) DocumentEvent(javax.swing.event.DocumentEvent) Font(java.awt.Font) ActionListener(java.awt.event.ActionListener) List(java.awt.List) ItemListener(java.awt.event.ItemListener)

Example 57 with DocumentListener

use of javax.swing.event.DocumentListener in project ChatGameFontificator by GlitchCog.

the class ControlPanelMessage method build.

@Override
protected void build() {
    usernamesBox = new JCheckBox("Show Usernames");
    joinMessagesBox = new JCheckBox("Show Joins");
    timestampsBox = new JCheckBox("Show Timestamps");
    timeFormatInput = new LabeledInput(null, 9);
    timeFormatUpdateButton = new JButton("Update Time Format");
    queueSizeSlider = new LabeledSlider("Message Queue Size", "messages", ConfigMessage.MIN_QUEUE_SIZE, ConfigMessage.MAX_QUEUE_SIZE);
    final String maxSpeedLabel = "MAX";
    messageSpeedSlider = new LabeledSlider("Message Speed", "char/sec", ConfigMessage.MIN_MESSAGE_SPEED, ConfigMessage.MAX_MESSAGE_SPEED, maxSpeedLabel.length()) {

        private static final long serialVersionUID = 1L;

        @Override
        public String getValueString() {
            if (getValue() == slider.getMaximum()) {
                return maxSpeedLabel;
            } else {
                return super.getValueString();
            }
        }
    };
    final String minExpirationLabel = "NEVER";
    expirationTimeSlider = new LabeledSlider("Hide Messages After ", "sec", ConfigMessage.MIN_MESSAGE_EXPIRATION, ConfigMessage.MAX_MESSAGE_EXPIRATION, minExpirationLabel.length()) {

        private static final long serialVersionUID = 1L;

        @Override
        protected String getUnitLabelStr() {
            if (getValue() == slider.getMinimum()) {
                return padValue("", super.getUnitLabelStr().length());
            } else {
                return super.getUnitLabelStr();
            }
        }

        @Override
        public String getValueString() {
            if (getValue() == slider.getMinimum()) {
                return minExpirationLabel;
            } else {
                return super.getValueString();
            }
        }
    };
    JLabel hideLabel = new JLabel("When No Messages Are Displayed: ");
    hideEmptyBorder = new JCheckBox("Hide Border");
    hideEmptyBackground = new JCheckBox("Hide Background");
    caseTypeDropdown = new JComboBox<UsernameCaseResolutionType>(UsernameCaseResolutionType.values());
    specifyCaseBox = new JCheckBox("Permit users to specify their own username case in posts");
    messageCasingDropdown = new JComboBox<MessageCasing>(MessageCasing.values());
    DocumentListener docListener = new DocumentListener() {

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

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

        @Override
        public void changedUpdate(DocumentEvent e) {
            toggleEnableds();
        }
    };
    timeFormatInput.addDocumentListener(docListener);
    caseTypeDropdown.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            UsernameCaseResolutionType type = (UsernameCaseResolutionType) caseTypeDropdown.getSelectedItem();
            boolean changed = config.getCaseResolutionType() != type;
            config.setCaseResolutionType(type);
            if (changed) {
                chatWindow.clearUsernameCases();
            }
        }
    });
    messageCasingDropdown.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            config.setMessageCasing((MessageCasing) messageCasingDropdown.getSelectedItem());
            chat.repaint();
        }
    });
    ActionListener boxListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JCheckBox source = (JCheckBox) e.getSource();
            if (usernamesBox.equals(source)) {
                config.setShowUsernames(source.isSelected());
            } else if (joinMessagesBox.equals(source)) {
                config.setJoinMessages(source.isSelected());
            } else if (timestampsBox.equals(source)) {
                config.setShowTimestamps(source.isSelected());
                toggleEnableds();
            } else if (specifyCaseBox.equals(source)) {
                config.setSpecifyCaseAllowed(specifyCaseBox.isSelected());
                chatWindow.clearUsernameCases();
            } else if (hideEmptyBorder.equals(source)) {
                config.setHideEmptyBorder(hideEmptyBorder.isSelected());
            } else if (hideEmptyBackground.equals(source)) {
                config.setHideEmptyBackground(hideEmptyBackground.isSelected());
            }
            chat.repaint();
        }
    };
    usernamesBox.addActionListener(boxListener);
    joinMessagesBox.addActionListener(boxListener);
    timestampsBox.addActionListener(boxListener);
    specifyCaseBox.addActionListener(boxListener);
    hideEmptyBorder.addActionListener(boxListener);
    hideEmptyBackground.addActionListener(boxListener);
    timeFormatUpdateButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            LoadConfigReport report = new LoadConfigReport();
            config.validateTimeFormat(report, timeFormatInput.getText());
            if (report.isErrorFree()) {
                config.setTimeFormat(timeFormatInput.getText());
                toggleEnableds();
                chat.repaint();
            } else {
                ChatWindow.popup.handleProblem(report);
            }
        }
    });
    ChangeListener cl = new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            if (!source.getValueIsAdjusting()) {
                if (queueSizeSlider.getSlider().equals(source)) {
                    config.setQueueSize(queueSizeSlider.getValue());
                } else if (messageSpeedSlider.getSlider().equals(source)) {
                    config.setMessageSpeed(messageSpeedSlider.getValue(), chat.getMessageProgressor());
                } else if (expirationTimeSlider.getSlider().equals(source)) {
                    config.setExpirationTime(expirationTimeSlider.getValue(), chat.getMessageExpirer());
                    chat.repaint();
                }
            }
        }
    };
    messageSpeedSlider.addChangeListener(cl);
    expirationTimeSlider.addChangeListener(cl);
    queueSizeSlider.addChangeListener(cl);
    gbc.weightx = 0.0;
    gbc.weighty = 0.0;
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.fill = GridBagConstraints.NONE;
    JPanel topOptions = new JPanel(new GridBagLayout());
    topOptions.setBorder(new TitledBorder(baseBorder, "Message Format Options", TitledBorder.CENTER, TitledBorder.TOP));
    JPanel optionsA = new JPanel(new GridBagLayout());
    JPanel optionsB = new JPanel(new GridBagLayout());
    GridBagConstraints tfGbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, DEFAULT_INSETS, 0, 0);
    JPanel timeFormatPanel = new JPanel(new GridBagLayout());
    timeFormatPanel.add(timeFormatInput, tfGbc);
    tfGbc.gridx++;
    timeFormatPanel.add(timeFormatUpdateButton, tfGbc);
    GridBagConstraints aGbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, DEFAULT_INSETS, 0, 0);
    optionsA.add(usernamesBox, aGbc);
    aGbc.gridx++;
    aGbc.anchor = GridBagConstraints.NORTH;
    optionsA.add(timestampsBox, aGbc);
    aGbc.gridy++;
    aGbc.gridx = 0;
    aGbc.anchor = GridBagConstraints.WEST;
    optionsA.add(joinMessagesBox, aGbc);
    aGbc.gridx++;
    aGbc.anchor = GridBagConstraints.EAST;
    optionsA.add(timeFormatPanel, aGbc);
    GridBagConstraints bGbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, DEFAULT_INSETS, 0, 0);
    bGbc.gridwidth = 3;
    optionsB.add(queueSizeSlider, bGbc);
    bGbc.gridy++;
    optionsB.add(messageSpeedSlider, bGbc);
    bGbc.gridy++;
    bGbc.fill = GridBagConstraints.HORIZONTAL;
    optionsB.add(expirationTimeSlider, bGbc);
    bGbc.gridy++;
    bGbc.fill = GridBagConstraints.NONE;
    bGbc.weightx = 0.333;
    bGbc.gridwidth = 1;
    bGbc.anchor = GridBagConstraints.EAST;
    optionsB.add(hideLabel, bGbc);
    bGbc.gridx++;
    bGbc.anchor = GridBagConstraints.CENTER;
    optionsB.add(hideEmptyBorder, bGbc);
    bGbc.gridx++;
    bGbc.anchor = GridBagConstraints.WEST;
    optionsB.add(hideEmptyBackground, bGbc);
    bGbc.gridx = 0;
    bGbc.gridwidth = 3;
    bGbc.gridy++;
    bGbc.anchor = GridBagConstraints.CENTER;
    bGbc.weightx = 1.0;
    GridBagConstraints topOpGbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, DEFAULT_INSETS, 0, 0);
    topOpGbc.anchor = GridBagConstraints.SOUTH;
    topOptions.add(optionsA, topOpGbc);
    topOpGbc.gridy++;
    topOpGbc.anchor = GridBagConstraints.NORTH;
    topOptions.add(optionsB, topOpGbc);
    topOpGbc.gridy++;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 1.0;
    add(topOptions, gbc);
    gbc.gridy++;
    JPanel usernameOptions = new JPanel(new GridLayout(3, 1));
    usernameOptions.setBorder(new TitledBorder(baseBorder, "Username Options", TitledBorder.CENTER, TitledBorder.TOP));
    usernameOptions.add(new JLabel("Default Method for Handling Username Casing"));
    usernameOptions.add(caseTypeDropdown);
    usernameOptions.add(specifyCaseBox);
    JPanel casingOptions = new JPanel();
    casingOptions.setBorder(new TitledBorder(baseBorder, "Message Casing Options", TitledBorder.CENTER, TitledBorder.TOP));
    casingOptions.add(new JLabel("Force uppercase or lowercase: "));
    casingOptions.add(messageCasingDropdown);
    add(usernameOptions, gbc);
    gbc.gridy++;
    add(casingOptions, gbc);
    gbc.gridy++;
    // Filler panel
    gbc.gridy++;
    gbc.anchor = GridBagConstraints.SOUTH;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    add(new JPanel(), gbc);
    gbc.gridy++;
}
Also used : DocumentListener(javax.swing.event.DocumentListener) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) MessageCasing(com.glitchcog.fontificator.config.MessageCasing) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) TitledBorder(javax.swing.border.TitledBorder) LabeledSlider(com.glitchcog.fontificator.gui.component.LabeledSlider) GridLayout(java.awt.GridLayout) JSlider(javax.swing.JSlider) ChangeListener(javax.swing.event.ChangeListener) LabeledInput(com.glitchcog.fontificator.gui.component.LabeledInput) UsernameCaseResolutionType(com.glitchcog.fontificator.config.UsernameCaseResolutionType) JLabel(javax.swing.JLabel) DocumentEvent(javax.swing.event.DocumentEvent) LoadConfigReport(com.glitchcog.fontificator.config.loadreport.LoadConfigReport) JCheckBox(javax.swing.JCheckBox) ActionListener(java.awt.event.ActionListener) ChangeEvent(javax.swing.event.ChangeEvent)

Example 58 with DocumentListener

use of javax.swing.event.DocumentListener in project JMRI by JMRI.

the class SystemConsole method createFrame.

/**
     * Layout the console frame
     */
private void createFrame() {
    // Use a JmriJFrame to ensure that we fit on the screen
    frame = new JmriJFrame(Bundle.getMessage("TitleConsole"));
    pref = jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class);
    // Add Help menu (Windows menu automaitically added)
    // NOI18N
    frame.addHelpMenu("package.apps.SystemConsole", true);
    // Grab a reference to the system clipboard
    final Clipboard clipboard = frame.getToolkit().getSystemClipboard();
    // Setup the scroll pane
    JScrollPane scroll = new JScrollPane(console);
    frame.add(scroll, BorderLayout.CENTER);
    // Add button to allow copy to clipboard
    JPanel p = new JPanel();
    JButton copy = new JButton(Bundle.getMessage("ButtonCopyClip"));
    copy.addActionListener((ActionEvent event) -> {
        StringSelection text = new StringSelection(console.getText());
        clipboard.setContents(text, text);
    });
    p.add(copy);
    // Add button to allow console window to be closed
    JButton close = new JButton(Bundle.getMessage("ButtonClose"));
    close.addActionListener((ActionEvent event) -> {
        frame.setVisible(false);
        frame.dispose();
    });
    p.add(close);
    JButton stackTrace = new JButton(Bundle.getMessage("ButtonStackTrace"));
    stackTrace.addActionListener((ActionEvent event) -> {
        performStackTrace();
    });
    p.add(stackTrace);
    // Add checkbox to enable/disable auto-scrolling
    // Use the inverted SimplePreferenceState to default as enabled
    p.add(autoScroll = new JCheckBox(Bundle.getMessage("CheckBoxAutoScroll"), !pref.getSimplePreferenceState(alwaysScrollCheck)));
    autoScroll.addActionListener((ActionEvent event) -> {
        doAutoScroll(console, autoScroll.isSelected());
        pref.setSimplePreferenceState(alwaysScrollCheck, !autoScroll.isSelected());
    });
    // Add checkbox to enable/disable always on top
    p.add(alwaysOnTop = new JCheckBox(Bundle.getMessage("CheckBoxOnTop"), pref.getSimplePreferenceState(alwaysOnTopCheck)));
    alwaysOnTop.setVisible(true);
    alwaysOnTop.setToolTipText(Bundle.getMessage("ToolTipOnTop"));
    alwaysOnTop.addActionListener((ActionEvent event) -> {
        frame.setAlwaysOnTop(alwaysOnTop.isSelected());
        pref.setSimplePreferenceState(alwaysOnTopCheck, alwaysOnTop.isSelected());
    });
    frame.setAlwaysOnTop(alwaysOnTop.isSelected());
    // Define the pop-up menu
    copySelection = new JMenuItem(Bundle.getMessage("MenuItemCopy"));
    copySelection.addActionListener((ActionEvent event) -> {
        StringSelection text = new StringSelection(console.getSelectedText());
        clipboard.setContents(text, text);
    });
    popup.add(copySelection);
    JMenuItem menuItem = new JMenuItem(Bundle.getMessage("ButtonCopyClip"));
    menuItem.addActionListener((ActionEvent event) -> {
        StringSelection text = new StringSelection(console.getText());
        clipboard.setContents(text, text);
    });
    popup.add(menuItem);
    popup.add(new JSeparator());
    JRadioButtonMenuItem rbMenuItem;
    // Define the colour scheme sub-menu
    schemeMenu = new JMenu(rbc.getString("ConsoleSchemeMenu"));
    schemeGroup = new ButtonGroup();
    for (final Scheme s : schemes) {
        rbMenuItem = new JRadioButtonMenuItem(s.description);
        rbMenuItem.addActionListener((ActionEvent event) -> {
            setScheme(schemes.indexOf(s));
        });
        rbMenuItem.setSelected(getScheme() == schemes.indexOf(s));
        schemeMenu.add(rbMenuItem);
        schemeGroup.add(rbMenuItem);
    }
    popup.add(schemeMenu);
    // Define the wrap style sub-menu
    wrapMenu = new JMenu(rbc.getString("ConsoleWrapStyleMenu"));
    wrapGroup = new ButtonGroup();
    rbMenuItem = new JRadioButtonMenuItem(rbc.getString("ConsoleWrapStyleNone"));
    rbMenuItem.addActionListener((ActionEvent event) -> {
        setWrapStyle(WRAP_STYLE_NONE);
    });
    rbMenuItem.setSelected(getWrapStyle() == WRAP_STYLE_NONE);
    wrapMenu.add(rbMenuItem);
    wrapGroup.add(rbMenuItem);
    rbMenuItem = new JRadioButtonMenuItem(rbc.getString("ConsoleWrapStyleLine"));
    rbMenuItem.addActionListener((ActionEvent event) -> {
        setWrapStyle(WRAP_STYLE_LINE);
    });
    rbMenuItem.setSelected(getWrapStyle() == WRAP_STYLE_LINE);
    wrapMenu.add(rbMenuItem);
    wrapGroup.add(rbMenuItem);
    rbMenuItem = new JRadioButtonMenuItem(rbc.getString("ConsoleWrapStyleWord"));
    rbMenuItem.addActionListener((ActionEvent event) -> {
        setWrapStyle(WRAP_STYLE_WORD);
    });
    rbMenuItem.setSelected(getWrapStyle() == WRAP_STYLE_WORD);
    wrapMenu.add(rbMenuItem);
    wrapGroup.add(rbMenuItem);
    popup.add(wrapMenu);
    // Bind pop-up to objects
    MouseListener popupListener = new PopupListener();
    console.addMouseListener(popupListener);
    frame.addMouseListener(popupListener);
    // Add document listener to scroll to end when modified if required
    console.getDocument().addDocumentListener(new DocumentListener() {

        // References to the JTextArea and JCheckBox
        // of this instantiation
        JTextArea ta = console;

        JCheckBox chk = autoScroll;

        @Override
        public void insertUpdate(DocumentEvent e) {
            doAutoScroll(ta, chk.isSelected());
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            doAutoScroll(ta, chk.isSelected());
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            doAutoScroll(ta, chk.isSelected());
        }
    });
    // Add the button panel to the frame & then arrange everything
    frame.add(p, BorderLayout.SOUTH);
    frame.pack();
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) DocumentListener(javax.swing.event.DocumentListener) JTextArea(javax.swing.JTextArea) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem) DocumentEvent(javax.swing.event.DocumentEvent) UserPreferencesManager(jmri.UserPreferencesManager) JSeparator(javax.swing.JSeparator) StringSelection(java.awt.datatransfer.StringSelection) JCheckBox(javax.swing.JCheckBox) MouseListener(java.awt.event.MouseListener) JmriJFrame(jmri.util.JmriJFrame) ButtonGroup(javax.swing.ButtonGroup) Clipboard(java.awt.datatransfer.Clipboard) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu)

Example 59 with DocumentListener

use of javax.swing.event.DocumentListener in project jabref by JabRef.

the class AutoCompleteSupport method install.

/**
     * Inits the autocompletion popup. After this method is called, further input in the specified textbox will be
     * autocompleted.
     */
public void install() {
    // ActionListeners for navigating the suggested autocomplete items with the arrow keys
    final ActionListener upAction = new MoveAction(-1);
    final ActionListener downAction = new MoveAction(1);
    // ActionListener hiding the autocomplete popup
    final ActionListener hidePopupAction = e -> popup.setVisible(false);
    // ActionListener accepting the currently selected item as the autocompletion
    final ActionListener acceptAction = e -> {
        E itemToInsert = renderer.getSelectedItem();
        if (itemToInsert == null) {
            return;
        }
        String toInsert = autoCompleter.getAutoCompleteText(itemToInsert);
        if (!autoCompleter.isSingleUnitField()) {
            int priv = textComp.getText().length() - 1;
            while ((priv >= 0) && !Character.isWhitespace(textComp.getText().charAt(priv)) && (textComp.getText().charAt(priv) != ',')) {
                priv--;
            }
            textComp.setText(textComp.getText().substring(0, priv + 1) + toInsert);
        } else {
            textComp.setText(toInsert);
        }
        textComp.setCaretPosition(textComp.getText().length());
        popup.setVisible(false);
    };
    // Create popup
    popup.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY));
    popup.setPopupSize(textComp.getWidth(), 200);
    popup.setLayout(new BorderLayout());
    popup.setFocusable(false);
    popup.setRequestFocusEnabled(false);
    popup.add(renderer.init(acceptAction));
    // Listen for changes to the text -> update autocomplete suggestions
    textComp.getDocument().addDocumentListener(new DocumentListener() {

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

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

        @Override
        public void changedUpdate(DocumentEvent e) {
        // Do nothing
        }
    });
    // Listen for up/down arrow keys -> move currently selected item up or down
    // We have to reimplement this function here since we cannot be sure that a simple list will be used to display the items
    // So better let the renderer decide what to do.
    // (Moreover, the list does not have the focus so probably would not recognize the keystrokes in the first place.)
    textComp.registerKeyboardAction(downAction, KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), JComponent.WHEN_FOCUSED);
    textComp.registerKeyboardAction(upAction, KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), JComponent.WHEN_FOCUSED);
    // Listen for ESC key -> hide popup
    textComp.registerKeyboardAction(hidePopupAction, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
    // Listen to focus events -> select all the text on gaining the focus
    this.textComp.addFocusListener(new ComboBoxEditorFocusHandler());
    // Listen for ENTER key if popup is visible -> accept current autocomplete suggestion
    popup.addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            textComp.registerKeyboardAction(acceptAction, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_FOCUSED);
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            textComp.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
        // Do nothing
        }
    });
}
Also used : Color(java.awt.Color) JComponent(javax.swing.JComponent) KeyStroke(javax.swing.KeyStroke) ActionListener(java.awt.event.ActionListener) DocumentListener(javax.swing.event.DocumentListener) AutoCompleter(org.jabref.logic.autocompleter.AutoCompleter) JPopupMenu(javax.swing.JPopupMenu) BorderFactory(javax.swing.BorderFactory) PopupMenuListener(javax.swing.event.PopupMenuListener) KeyEvent(java.awt.event.KeyEvent) ActionEvent(java.awt.event.ActionEvent) JTextComponent(javax.swing.text.JTextComponent) FocusAdapter(java.awt.event.FocusAdapter) List(java.util.List) AbstractAction(javax.swing.AbstractAction) PopupMenuEvent(javax.swing.event.PopupMenuEvent) FocusEvent(java.awt.event.FocusEvent) BorderLayout(java.awt.BorderLayout) DocumentEvent(javax.swing.event.DocumentEvent) DocumentListener(javax.swing.event.DocumentListener) ActionListener(java.awt.event.ActionListener) BorderLayout(java.awt.BorderLayout) PopupMenuListener(javax.swing.event.PopupMenuListener) DocumentEvent(javax.swing.event.DocumentEvent) PopupMenuEvent(javax.swing.event.PopupMenuEvent)

Example 60 with DocumentListener

use of javax.swing.event.DocumentListener in project JMRI by JMRI.

the class BackupDialog method initComponents.

private void initComponents() {
    setModalityType(ModalityType.DOCUMENT_MODAL);
    setModal(true);
    setTitle(Bundle.getMessage("BackupDialog.this.title"));
    setBounds(100, 100, 395, 199);
    getContentPane().setLayout(new BorderLayout());
    {
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        GridBagLayout gbl = new GridBagLayout();
        gbl.columnWidths = new int[] { 0, 0 };
        gbl.rowHeights = new int[] { 0, 0, 0, 0, 0 };
        gbl.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gbl.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
        contentPanel.setLayout(gbl);
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            captionLabel = new JLabel(Bundle.getMessage("BackupDialog.nameLabel.text"));
            GridBagConstraints gbc_captionLabel = new GridBagConstraints();
            gbc_captionLabel.anchor = GridBagConstraints.WEST;
            gbc_captionLabel.insets = new Insets(0, 0, 5, 0);
            gbc_captionLabel.gridx = 0;
            gbc_captionLabel.gridy = 0;
            contentPanel.add(captionLabel, gbc_captionLabel);
        }
        {
            setNameTextField = new JTextField();
            setNameTextField.setText(backup.suggestBackupSetName());
            setNameTextField.getDocument().addDocumentListener(new DocumentListener() {

                // These should probably pass the document to
                // enableBackupButton....
                @Override
                public void removeUpdate(DocumentEvent arg0) {
                    enableBackupButton();
                }

                @Override
                public void insertUpdate(DocumentEvent arg0) {
                    enableBackupButton();
                }

                @Override
                public void changedUpdate(DocumentEvent arg0) {
                    enableBackupButton();
                }
            });
            GridBagConstraints gbc_setNameTextField = new GridBagConstraints();
            gbc_setNameTextField.insets = new Insets(0, 0, 5, 0);
            gbc_setNameTextField.fill = GridBagConstraints.HORIZONTAL;
            gbc_setNameTextField.gridx = 0;
            gbc_setNameTextField.gridy = 1;
            contentPanel.add(setNameTextField, gbc_setNameTextField);
            setNameTextField.setColumns(10);
        }
        {
            infoLabel1 = new JLabel(Bundle.getMessage("BackupDialog.notesLabel1.text"));
            GridBagConstraints gbc_infoLabel1 = new GridBagConstraints();
            gbc_infoLabel1.insets = new Insets(0, 0, 5, 0);
            gbc_infoLabel1.anchor = GridBagConstraints.NORTHWEST;
            gbc_infoLabel1.gridx = 0;
            gbc_infoLabel1.gridy = 2;
            contentPanel.add(infoLabel1, gbc_infoLabel1);
        }
        {
            infoLabel2 = new JLabel(Bundle.getMessage("BackupDialog.notesLabel2.text"));
            GridBagConstraints gbc_infoLabel2 = new GridBagConstraints();
            gbc_infoLabel2.anchor = GridBagConstraints.WEST;
            gbc_infoLabel2.gridx = 0;
            gbc_infoLabel2.gridy = 3;
            contentPanel.add(infoLabel2, gbc_infoLabel2);
        }
    }
    {
        JPanel buttonPane = new JPanel();
        FlowLayout fl_buttonPane = new FlowLayout(FlowLayout.CENTER);
        buttonPane.setLayout(fl_buttonPane);
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            backupButton = new JButton(Bundle.getMessage("BackupDialog.backupButton.text"));
            backupButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    do_backupButton_actionPerformed(e);
                }
            });
            buttonPane.add(backupButton);
            getRootPane().setDefaultButton(backupButton);
        }
        {
            JButton cancelButton = new JButton(Bundle.getMessage("ButtonCancel"));
            cancelButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    do_cancelButton_actionPerformed(arg0);
                }
            });
            // NOI18N
            cancelButton.setActionCommand("Cancel");
            buttonPane.add(cancelButton);
        }
    // Help button isn't used yet
    //   {
    //    helpButton = new JButton(Bundle.getMessage("BackupDialog.helpButton.text"));
    //    helpButton.addActionListener(new ActionListener() {
    //     public void actionPerformed(ActionEvent e) {
    //      do_helpButton_actionPerformed(e);
    //     }
    //    });
    //    helpButton.setEnabled(false);
    //    buttonPane.add(helpButton);
    //   }
    }
}
Also used : DocumentListener(javax.swing.event.DocumentListener) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) FlowLayout(java.awt.FlowLayout) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) DocumentEvent(javax.swing.event.DocumentEvent) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) EmptyBorder(javax.swing.border.EmptyBorder)

Aggregations

DocumentListener (javax.swing.event.DocumentListener)73 DocumentEvent (javax.swing.event.DocumentEvent)70 JTextField (javax.swing.JTextField)24 ActionEvent (java.awt.event.ActionEvent)21 ActionListener (java.awt.event.ActionListener)19 JLabel (javax.swing.JLabel)19 JPanel (javax.swing.JPanel)18 Dimension (java.awt.Dimension)16 JButton (javax.swing.JButton)13 JCheckBox (javax.swing.JCheckBox)13 GridBagLayout (java.awt.GridBagLayout)9 ChangeEvent (javax.swing.event.ChangeEvent)9 GridBagConstraints (java.awt.GridBagConstraints)8 Box (javax.swing.Box)8 ChangeListener (javax.swing.event.ChangeListener)8 BoxLayout (javax.swing.BoxLayout)7 ZapTextField (org.zaproxy.zap.utils.ZapTextField)7 BorderLayout (java.awt.BorderLayout)6 Insets (java.awt.Insets)6 FocusEvent (java.awt.event.FocusEvent)6