<commit_before>/**
 * $Revision$
 * $Date$
 *
 * Copyright (C) 1999-2005 Jive Software. All rights reserved.
 * This software is the proprietary information of Jive Software. Use is subject to license terms.
 */

package org.jivesoftware.spark.component;

import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.ui.ContactItem;
import org.jivesoftware.spark.util.ModelUtil;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.ListCellRenderer;

/**
 * Implementation of a popup field from a TextField.
 *
 * @author <NAME>
 */
public class JContactItemField extends JPanel {

    private JTextField textField = new JTextField();
    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);
    private JWindow popup;
    private List<ContactItem> items;

    public JContactItemField(List items) {
        setLayout(new BorderLayout());
        this.items = items;

        add(textField, BorderLayout.CENTER);


        textField.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent keyEvent) {
                char ch = keyEvent.getKeyChar();
                if (validateChar(ch)) {
                    showPopupMenu();
                }

                if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
                    int index = list.getSelectedIndex();
                    if (index >= 0) {
                        ContactItem selection = (ContactItem)list.getSelectedValue();
                        textField.setText(selection.getNickname());
                        popup.setVisible(false);
                    }
                }

                if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    popup.setVisible(false);
                }
                dispatchEvent(keyEvent);
            }

            public void keyPressed(KeyEvent e) {
                if (isArrowKey(e)) {
                    list.dispatchEvent(e);
                }

            }
        });


        textField.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
            }

            public void focusLost(FocusEvent e) {
                textField.requestFocusInWindow();
            }
        });


        popup = new JWindow();


        popup.getContentPane().add(new JScrollPane(list));
        popup.setAlwaysOnTop(true);


        list.setCellRenderer(new PopupRenderer());
        list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = list.getSelectedIndex();
                    if (index >= 0) {
                        ContactItem selection = (ContactItem)list.getSelectedValue();
                        textField.setText(selection.getNickname());
                        popup.setVisible(false);
                    }
                }
            }
        });
    }

    public void dispose() {
        popup.dispose();
    }

    public void setItems(List list) {
        this.items = items;
    }

    public JList getList(){
        return list;
    }

    private void showPopupMenu() {
        model.removeAllElements();

        String typedItem = textField.getText();

        final List<ContactItem> validItems = new ArrayList<ContactItem>();
        for (ContactItem contactItem : items) {
            String nickname = contactItem.getNickname().toLowerCase();
            if (nickname.startsWith(typedItem.toLowerCase())) {
                validItems.add(contactItem);
            }
        }


        if (validItems.size() > 0) {
            for (final ContactItem label : validItems) {
                model.addElement(label);
            }
        }

        if (validItems.size() != 0 && !popup.isVisible()) {
            popup.pack();
            popup.setSize(textField.getWidth(), 200);
            Point pt = textField.getLocationOnScreen();
            pt.translate(0, textField.getHeight());
            popup.setLocation(pt);
            popup.toFront();
            popup.setVisible(true);
        }

    }

    /**
     * Validate the given text - to pass it must contain letters, digits, '@', '-', '_', '.', ','
     * or a space character.
     *
     * @param text the text to check
     * @return true if the given text is valid, false otherwise.
     */
    public boolean validateChars(String text) {
        if (!ModelUtil.hasLength(text)) {
            return false;
        }

        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (!Character.isLetterOrDigit(ch) && ch != '@' && ch != '-' && ch != '_'
                && ch != '.' && ch != ',' && ch != ' ') {
                return false;
            }
        }


        return true;
    }

    /**
     * Validate the given text - to pass it must contain letters, digits, '@', '-', '_', '.', ','
     * or a space character.
     *
     * @param ch the character
     * @return true if the given text is valid, false otherwise.
     */
    public boolean validateChar(char ch) {
        if (!Character.isLetterOrDigit(ch) && ch != '@' && ch != '-' && ch != '_'
            && ch != '.' && ch != ',' && ch != ' ' && ch != KeyEvent.VK_BACK_SPACE && ch != KeyEvent.CTRL_DOWN_MASK
            && ch != KeyEvent.CTRL_MASK) {
            return false;
        }

        return true;
    }

    public boolean isArrowKey(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN) {
            return true;
        }
        return false;
    }

    public String getText() {
        return textField.getText();
    }

    public void setText(String text) {
        textField.setText(text);
    }

    public void focus() {
        textField.requestFocus();
    }

    public JTextField getTextField() {
        return textField;
    }

    public JWindow getPopup() {
        return popup;
    }

    class PopupRenderer extends JLabel implements ListCellRenderer {

        /**
         * Construct Default JLabelIconRenderer.
         */
        public PopupRenderer() {
            setOpaque(true);
            this.setHorizontalTextPosition(JLabel.RIGHT);
            this.setHorizontalAlignment(JLabel.LEFT);
        }

        public Component getListCellRendererComponent(JList list,
                                                      Object value,
                                                      int index,
                                                      boolean isSelected,
                                                      boolean cellHasFocus) {
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            }
            else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }


            ContactItem contactItem = (ContactItem)value;
            setText(contactItem.getNickname());
            if (contactItem.getIcon() == null) {
                setIcon(SparkRes.getImageIcon(SparkRes.CLEAR_BALL_ICON));
            }
            else {
                setIcon(contactItem.getIcon());
            }
            setFont(contactItem.getNicknameLabel().getFont());
            setForeground(contactItem.getForeground());

            return this;
        }
    }


}
<commit_msg>Have ctrl+f put initial selection on index 0<commit_after>/**
 * $Revision$
 * $Date$
 *
 * Copyright (C) 1999-2005 Jive Software. All rights reserved.
 * This software is the proprietary information of Jive Software. Use is subject to license terms.
 */

package org.jivesoftware.spark.component;

import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.ui.ContactItem;
import org.jivesoftware.spark.util.ModelUtil;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.ListCellRenderer;

/**
 * Implementation of a popup field from a TextField.
 *
 * @author <NAME>
 */
public class JContactItemField extends JPanel {

    private JTextField textField = new JTextField();
    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);
    private JWindow popup;
    private List<ContactItem> items;

    public JContactItemField(List items) {
        setLayout(new BorderLayout());
        this.items = items;

        add(textField, BorderLayout.CENTER);


        textField.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent keyEvent) {
                char ch = keyEvent.getKeyChar();
                if (validateChar(ch)) {
                    showPopupMenu();
                }

                if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
                    int index = list.getSelectedIndex();
                    if (index >= 0) {
                        ContactItem selection = (ContactItem)list.getSelectedValue();
                        textField.setText(selection.getNickname());
                        popup.setVisible(false);
                    }
                }

                if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    popup.setVisible(false);
                }
                dispatchEvent(keyEvent);
            }

            public void keyPressed(KeyEvent e) {
                if (isArrowKey(e)) {
                    list.dispatchEvent(e);
                }

            }
        });


        textField.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
            }

            public void focusLost(FocusEvent e) {
                textField.requestFocusInWindow();
            }
        });


        popup = new JWindow();


        popup.getContentPane().add(new JScrollPane(list));
        popup.setAlwaysOnTop(true);


        list.setCellRenderer(new PopupRenderer());
        list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = list.getSelectedIndex();
                    if (index >= 0) {
                        ContactItem selection = (ContactItem)list.getSelectedValue();
                        textField.setText(selection.getNickname());
                        popup.setVisible(false);
                    }
                }
            }
        });
    }

    public void dispose() {
        popup.dispose();
    }

    public void setItems(List list) {
        this.items = items;
    }

    public JList getList(){
        return list;
    }

    private void showPopupMenu() {
        model.removeAllElements();

        String typedItem = textField.getText();

        final List<ContactItem> validItems = new ArrayList<ContactItem>();
        for (ContactItem contactItem : items) {
            String nickname = contactItem.getNickname().toLowerCase();
            if (nickname.startsWith(typedItem.toLowerCase())) {
                validItems.add(contactItem);
            }
        }


        if (validItems.size() > 0) {
            for (final ContactItem label : validItems) {
                model.addElement(label);
            }
        }

        if (validItems.size() != 0 && !popup.isVisible()) {
            popup.pack();
            popup.setSize(textField.getWidth(), 200);
            Point pt = textField.getLocationOnScreen();
            pt.translate(0, textField.getHeight());
            popup.setLocation(pt);
            popup.toFront();
            popup.setVisible(true);
        }

        // set initial selection
        if(validItems.size() > 0){
            list.setSelectedIndex(0);
        }

    }

    /**
     * Validate the given text - to pass it must contain letters, digits, '@', '-', '_', '.', ','
     * or a space character.
     *
     * @param text the text to check
     * @return true if the given text is valid, false otherwise.
     */
    public boolean validateChars(String text) {
        if (!ModelUtil.hasLength(text)) {
            return false;
        }

        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (!Character.isLetterOrDigit(ch) && ch != '@' && ch != '-' && ch != '_'
                && ch != '.' && ch != ',' && ch != ' ') {
                return false;
            }
        }


        return true;
    }

    /**
     * Validate the given text - to pass it must contain letters, digits, '@', '-', '_', '.', ','
     * or a space character.
     *
     * @param ch the character
     * @return true if the given text is valid, false otherwise.
     */
    public boolean validateChar(char ch) {
        if (!Character.isLetterOrDigit(ch) && ch != '@' && ch != '-' && ch != '_'
            && ch != '.' && ch != ',' && ch != ' ' && ch != KeyEvent.VK_BACK_SPACE && ch != KeyEvent.CTRL_DOWN_MASK
            && ch != KeyEvent.CTRL_MASK) {
            return false;
        }

        return true;
    }

    public boolean isArrowKey(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN) {
            return true;
        }
        return false;
    }

    public String getText() {
        return textField.getText();
    }

    public void setText(String text) {
        textField.setText(text);
    }

    public void focus() {
        textField.requestFocus();
    }

    public JTextField getTextField() {
        return textField;
    }

    public JWindow getPopup() {
        return popup;
    }

    class PopupRenderer extends JLabel implements ListCellRenderer {

        /**
         * Construct Default JLabelIconRenderer.
         */
        public PopupRenderer() {
            setOpaque(true);
            this.setHorizontalTextPosition(JLabel.RIGHT);
            this.setHorizontalAlignment(JLabel.LEFT);
        }

        public Component getListCellRendererComponent(JList list,
                                                      Object value,
                                                      int index,
                                                      boolean isSelected,
                                                      boolean cellHasFocus) {
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            }
            else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }


            ContactItem contactItem = (ContactItem)value;
            setText(contactItem.getNickname());
            if (contactItem.getIcon() == null) {
                setIcon(SparkRes.getImageIcon(SparkRes.CLEAR_BALL_ICON));
            }
            else {
                setIcon(contactItem.getIcon());
            }
            setFont(contactItem.getNicknameLabel().getFont());
            setForeground(contactItem.getForeground());

            return this;
        }
    }


}

{'fraction_non_alphanumeric': 0.10958136057812111, 'fraction_numerical': 0.0024296037876900076, 'mean_word_length': 1.70525783619818, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '532143', 'n_tokens_mistral': 4368, 'n_tokens_neox': 4122, 'n_words': 915}