Example usage for com.intellij.openapi.actionSystem IdeActions ACTION_CLEAR_TEXT

List of usage examples for com.intellij.openapi.actionSystem IdeActions ACTION_CLEAR_TEXT

Introduction

In this page you can find the example usage for com.intellij.openapi.actionSystem IdeActions ACTION_CLEAR_TEXT.

Prototype

String ACTION_CLEAR_TEXT

To view the source code for com.intellij.openapi.actionSystem IdeActions ACTION_CLEAR_TEXT.

Click Source Link

Usage

From source file:com.intellij.ui.SearchTextField.java

License:Apache License

public SearchTextField(boolean historyEnabled) {
    super(new BorderLayout());

    myModel = new MyModel();

    myTextField = new TextFieldWithProcessing() {
        @Override//  www . ja va2  s.  c  o  m
        public void processKeyEvent(final KeyEvent e) {
            if (preprocessEventForTextField(e))
                return;
            super.processKeyEvent(e);
        }

        @Override
        public void setBackground(final Color bg) {
            super.setBackground(bg);
            if (!hasIconsOutsideOfTextField()) {
                if (myClearFieldLabel != null) {
                    myClearFieldLabel.setBackground(bg);
                }
            }
            if (myToggleHistoryLabel != null) {
                myToggleHistoryLabel.setBackground(bg);
            }
        }
    };
    myTextField.setColumns(15);
    myTextField.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            onFocusLost();
            super.focusLost(e);
        }

        @Override
        public void focusGained(FocusEvent e) {
            onFocusGained();
            super.focusGained(e);
        }
    });
    add(myTextField, BorderLayout.CENTER);
    myTextField.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                if (isSearchControlUISupported() && myNativeSearchPopup != null) {
                    myNativeSearchPopup.show(myTextField, 5, myTextField.getHeight());
                } else if (myPopup == null || !myPopup.isVisible()) {
                    showPopup();
                }
            }
        }
    });

    if (isSearchControlUISupported() || UIUtil.isUnderBuildInLaF()) {
        myTextField.putClientProperty("JTextField.variant", "search");
    }
    if (isSearchControlUISupported()) {
        if (historyEnabled) {
            myNativeSearchPopup = new JBPopupMenu();
            myNoItems = new JBMenuItem("No recent searches");
            myNoItems.setEnabled(false);

            updateMenu();
            myTextField.putClientProperty("JTextField.Search.FindPopup", myNativeSearchPopup);
        }
    } else {
        myToggleHistoryLabel = new JLabel(AllIcons.Actions.Search);
        myToggleHistoryLabel.setOpaque(true);
        myToggleHistoryLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                togglePopup();
            }
        });
        if (historyEnabled) {
            add(myToggleHistoryLabel, BorderLayout.WEST);
        }

        myClearFieldLabel = new JLabel(
                UIUtil.isUnderDarcula() ? AllIcons.Actions.Clean : AllIcons.Actions.CleanLight);
        myClearFieldLabel.setOpaque(true);
        add(myClearFieldLabel, BorderLayout.EAST);
        myClearFieldLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                myTextField.setText("");
                onFieldCleared();
            }
        });

        if (!hasIconsOutsideOfTextField()) {
            final Border originalBorder;
            if (SystemInfo.isMac) {
                originalBorder = BorderFactory.createLoweredBevelBorder();
            } else {
                originalBorder = myTextField.getBorder();
            }

            myToggleHistoryLabel.setBackground(myTextField.getBackground());
            myClearFieldLabel.setBackground(myTextField.getBackground());

            setBorder(new CompoundBorder(IdeBorderFactory.createEmptyBorder(2, 0, 2, 0), originalBorder));

            myTextField.setOpaque(true);
            myTextField.setBorder(IdeBorderFactory.createEmptyBorder(0, 5, 0, 5));
        } else {
            setBorder(IdeBorderFactory.createEmptyBorder(2, 0, 2, 0));
        }
    }

    if (ApplicationManager.getApplication() != null) { //tests
        final ActionManager actionManager = ActionManager.getInstance();
        if (actionManager != null) {
            final AnAction clearTextAction = actionManager.getAction(IdeActions.ACTION_CLEAR_TEXT);
            if (clearTextAction.getShortcutSet().getShortcuts().length == 0) {
                clearTextAction.registerCustomShortcutSet(CommonShortcuts.ESCAPE, this);
            }
        }
    }
}