Example usage for com.intellij.openapi.ui.popup PopupChooserBuilder registerKeyboardAction

List of usage examples for com.intellij.openapi.ui.popup PopupChooserBuilder registerKeyboardAction

Introduction

In this page you can find the example usage for com.intellij.openapi.ui.popup PopupChooserBuilder registerKeyboardAction.

Prototype

@Override
    public PopupChooserBuilder<T> registerKeyboardAction(KeyStroke keyStroke, ActionListener actionListener) 

Source Link

Usage

From source file:com.favorite.FavoriteBaseShowRecentFilesAction.java

License:Apache License

protected void show(final Project project) {
    final DefaultListModel model = new DefaultListModel();

    FileEditorManagerEx fem = FileEditorManagerEx.getInstanceEx(project);
    VirtualFile[] selectedFiles = fem.getSelectedFiles();
    VirtualFile currentFile = fem.getCurrentFile();
    VirtualFile[] files = filesToShow(project);
    FileEditorProviderManager editorProviderManager = FileEditorProviderManager.getInstance();

    for (int i = files.length - 1; i >= 0; i--) { // reverse order of files
        VirtualFile file = files[i];//from   w  w w .j a va  2s.co m
        boolean isSelected = ArrayUtil.find(selectedFiles, file) >= 0;
        boolean equals = file.equals(currentFile);
        FileEditorProvider[] providers = editorProviderManager.getProviders(project, file);
        boolean length = true;//providers.length > 0;
        if ((!isSelected || !equals) && length) {
            // 1. do not put currently selected file
            // 2. do not include file with no corresponding editor providers
            model.addElement(file);
        }
    }

    final JLabel pathLabel = new JLabel(" ");
    pathLabel.setHorizontalAlignment(SwingConstants.RIGHT);

    if (true /*SystemInfo.isMac*/) {
        final Font font = pathLabel.getFont();
        pathLabel.setFont(font.deriveFont((float) 10));
    }

    final JList list = new JBList(model);
    list.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                int index = list.getSelectedIndex();
                if (index == -1 || index >= list.getModel().getSize()) {
                    return;
                }
                Object[] values = list.getSelectedValues();
                for (Object value : values) {
                    VirtualFile file = (VirtualFile) value;
                    model.removeElement(file);
                    if (model.getSize() > 0) {
                        if (model.getSize() == index) {
                            list.setSelectedIndex(model.getSize() - 1);
                        } else if (model.getSize() > index) {
                            list.setSelectedIndex(index);
                        }
                    } else {
                        list.clearSelection();
                    }
                    EditorHistoryManager.getInstance(project).removeFile(file);
                }
            }
        }
    });

    final MyListSelectionListener listSelectionListener = new MyListSelectionListener(pathLabel, list);
    list.getSelectionModel().addListSelectionListener(listSelectionListener);

    Runnable runnable = new Runnable() {
        public void run() {
            Object[] values = list.getSelectedValues();
            for (Object value : values) {
                VirtualFile file = (VirtualFile) value;
                if (file.isDirectory()) {
                    String path = file.getPath();
                    ProjectUtil.openOrImport(path, project, false);
                } else {
                    FileEditorManager.getInstance(project).openFile(file, true, true);
                }
                //String path = "/Volumes/SHARE/MacSystem/Home/Users/djzhang/NetBeansProjects/intellijPlugDemo";
                //String path = file.getPath();
                //final Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());

                //FileEditorManager.getInstance(project).openFile(file, true, true);

                //OpenFileAction.openFile(file, project);

            }
        }
    };

    if (list.getModel().getSize() == 0) {
        list.clearSelection();
    }

    list.setCellRenderer(new RecentFilesRenderer(project));

    /*
    TODO:
    if (model.getSize() > 0) {
      Dimension listPreferredSize = list.getPreferredSize();
      list.setVisibleRowCount(0);
      Dimension viewPreferredSize = new Dimension(listPreferredSize.width, Math.min(listPreferredSize.height, r.height - 20));
      ((JViewport)list.getParent()).setPreferredSize(viewPreferredSize);
    }
    */

    JPanel footerPanel = new JPanel(new BorderLayout()) {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(BORDER_COLOR);
            g.drawLine(0, 0, getWidth(), 0);
        }
    };

    footerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    footerPanel.add(pathLabel);

    final PopupChooserBuilder builder = new PopupChooserBuilder(list).setTitle(getTitle()).setAdText(" ")
            .setMovable(true).setItemChoosenCallback(runnable).setModalContext(false)
            .addAdditionalChooseKeystroke(getAdditionalSelectKeystroke())
            .setFilteringEnabled(new Function<Object, String>() {
                public String fun(Object o) {
                    return o instanceof VirtualFile ? ((VirtualFile) o).getName() : "";
                }
            });
    final Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap().getShortcuts(getPeerActionId());
    final PeerListener listener = new PeerListener(project, getPeerActionId());
    for (Shortcut shortcut : shortcuts) {
        if (shortcut instanceof KeyboardShortcut
                && ((KeyboardShortcut) shortcut).getSecondKeyStroke() == null) {
            final KeyStroke keyStroke = ((KeyboardShortcut) shortcut).getFirstKeyStroke();
            builder.registerKeyboardAction(keyStroke, listener);
        }
    }
    JBPopup popup = builder.createPopup();
    listener.setPopup(popup);
    listSelectionListener.setPopup(popup);
    popup.showCenteredInCurrentWindow(project);
}

From source file:com.intellij.ide.actions.BaseShowRecentFilesAction.java

License:Apache License

protected void show(final Project project) {
    final DefaultListModel model = new DefaultListModel();

    FileEditorManagerEx fem = FileEditorManagerEx.getInstanceEx(project);
    VirtualFile[] selectedFiles = fem.getSelectedFiles();
    VirtualFile currentFile = fem.getCurrentFile();
    VirtualFile[] files = filesToShow(project);
    FileEditorProviderManager editorProviderManager = FileEditorProviderManager.getInstance();

    for (int i = files.length - 1; i >= 0; i--) { // reverse order of files
        VirtualFile file = files[i];//from  www  .j a  v  a2s  . c o  m
        boolean isSelected = ArrayUtil.find(selectedFiles, file) >= 0;
        if ((!isSelected || !file.equals(currentFile))
                && editorProviderManager.getProviders(project, file).length > 0) {
            // 1. do not put currently selected file
            // 2. do not include file with no corresponding editor providers
            model.addElement(file);
        }
    }

    final JLabel pathLabel = new JLabel(" ");
    pathLabel.setHorizontalAlignment(SwingConstants.RIGHT);

    if (true /*SystemInfo.isMac*/) {
        final Font font = pathLabel.getFont();
        pathLabel.setFont(font.deriveFont((float) 10));
    }

    final JList list = new JBList(model);
    list.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                int index = list.getSelectedIndex();
                if (index == -1 || index >= list.getModel().getSize()) {
                    return;
                }
                Object[] values = list.getSelectedValues();
                for (Object value : values) {
                    VirtualFile file = (VirtualFile) value;
                    model.removeElement(file);
                    if (model.getSize() > 0) {
                        if (model.getSize() == index) {
                            list.setSelectedIndex(model.getSize() - 1);
                        } else if (model.getSize() > index) {
                            list.setSelectedIndex(index);
                        }
                    } else {
                        list.clearSelection();
                    }
                    EditorHistoryManager.getInstance(project).removeFile(file);
                }
            }
        }
    });

    final MyListSelectionListener listSelectionListener = new MyListSelectionListener(pathLabel, list);
    list.getSelectionModel().addListSelectionListener(listSelectionListener);

    Runnable runnable = new Runnable() {
        public void run() {
            Object[] values = list.getSelectedValues();
            for (Object value : values) {
                VirtualFile file = (VirtualFile) value;
                FileEditorManager.getInstance(project).openFile(file, true, true);
            }
        }
    };

    if (list.getModel().getSize() == 0) {
        list.clearSelection();
    }

    list.setCellRenderer(new RecentFilesRenderer(project));

    /*
    TODO:
    if (model.getSize() > 0) {
      Dimension listPreferredSize = list.getPreferredSize();
      list.setVisibleRowCount(0);
      Dimension viewPreferredSize = new Dimension(listPreferredSize.width, Math.min(listPreferredSize.height, r.height - 20));
      ((JViewport)list.getParent()).setPreferredSize(viewPreferredSize);
    }
    */

    JPanel footerPanel = new JPanel(new BorderLayout()) {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(BORDER_COLOR);
            g.drawLine(0, 0, getWidth(), 0);
        }
    };

    footerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    footerPanel.add(pathLabel);

    final PopupChooserBuilder builder = new PopupChooserBuilder(list).setTitle(getTitle()).setAdText(" ")
            .setMovable(true).setItemChoosenCallback(runnable).setModalContext(false)
            .addAdditionalChooseKeystroke(getAdditionalSelectKeystroke())
            .setFilteringEnabled(new Function<Object, String>() {
                public String fun(Object o) {
                    return o instanceof VirtualFile ? ((VirtualFile) o).getName() : "";
                }
            });
    final Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap().getShortcuts(getPeerActionId());
    final PeerListener listener = new PeerListener(project, getPeerActionId());
    for (Shortcut shortcut : shortcuts) {
        if (shortcut instanceof KeyboardShortcut
                && ((KeyboardShortcut) shortcut).getSecondKeyStroke() == null) {
            final KeyStroke keyStroke = ((KeyboardShortcut) shortcut).getFirstKeyStroke();
            builder.registerKeyboardAction(keyStroke, listener);
        }
    }
    JBPopup popup = builder.createPopup();
    listener.setPopup(popup);
    listSelectionListener.setPopup(popup);
    popup.showCenteredInCurrentWindow(project);
}