public void actionPerformed(ActionEvent e) { JPopupMenu popup = new JPopupMenu(); AddLinksAction ala = new AddLinksAction((SelectionInfo<CrawledPackage, CrawledLink>) null); ala.putValue(AbstractAction.NAME, _GUI._.AddOptionsAction_actionPerformed_addlinks()); popup.add(new JMenuItem(ala)); popup.add(new JMenuItem(new AddContainerAction((SelectionInfo<CrawledPackage, CrawledLink>) null))); int[] insets = LookAndFeelController.getInstance().getLAFOptions().getPopupBorderInsets(); Dimension pref = popup.getPreferredSize(); // pref.width = positionComp.getWidth() + ((Component) // e.getSource()).getWidth() + insets[1] + insets[3]; popup.setPreferredSize(pref); popup.show(positionComp, -insets[1] - 1, -popup.getPreferredSize().height + insets[2]); }
private void showCompletion() { try { int caretPosition = editorTextArea.getCaretPosition(); int wordStart = getWordStart(); CompletionCandidate[] words = getFilteredWords(wordStart, editorTextArea.getText(wordStart, caretPosition - wordStart)); if (words.length == 0) { return; } final JPopupMenu w = new JPopupMenu(); final JList jList = new JList(words); jList.setCellRenderer( new DefaultListCellRenderer() { private ImageIcon tableIcon = new ImageIcon( getClass().getResource("/org/jooq/debug/console/resources/Table16.png")); private ImageIcon tableColumnIcon = new ImageIcon( getClass().getResource("/org/jooq/debug/console/resources/TableColumn16.png")); private ImageIcon sqlIcon = new ImageIcon( getClass().getResource("/org/jooq/debug/console/resources/SQL16.png")); @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (c instanceof JLabel) { Icon icon; switch (((CompletionCandidate) value).getKeyWordType()) { case TABLE: icon = tableIcon; break; case TABLE_COlUMN: icon = tableColumnIcon; break; default: icon = sqlIcon; break; } ((JLabel) c).setIcon(icon); } return c; } }); jList.setSelectedIndex(0); jList.addKeyListener( new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_PAGE_UP: case KeyEvent.VK_PAGE_DOWN: case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: case KeyEvent.VK_HOME: case KeyEvent.VK_END: return; case KeyEvent.VK_ESCAPE: w.setVisible(false); return; case KeyEvent.VK_ENTER: editorTextArea.replaceRange( ((CompletionCandidate) jList.getSelectedValue()).toString(), getWordStart(), editorTextArea.getCaretPosition()); w.setVisible(false); return; } editorTextArea.dispatchEvent( new KeyEvent( editorTextArea, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(), e.getKeyLocation())); if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) { adjustListContent(); } } @Override public void keyReleased(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_PAGE_UP: case KeyEvent.VK_PAGE_DOWN: case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: case KeyEvent.VK_HOME: case KeyEvent.VK_END: case KeyEvent.VK_ESCAPE: case KeyEvent.VK_ENTER: return; } editorTextArea.dispatchEvent( new KeyEvent( editorTextArea, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(), e.getKeyLocation())); if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) { adjustListContent(); } } @Override public void keyTyped(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_PAGE_UP: case KeyEvent.VK_PAGE_DOWN: case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: case KeyEvent.VK_HOME: case KeyEvent.VK_END: case KeyEvent.VK_ESCAPE: case KeyEvent.VK_ENTER: return; } editorTextArea.dispatchEvent( new KeyEvent( editorTextArea, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(), e.getKeyLocation())); if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) { adjustListContent(); } } private void adjustListContent() { try { int wordStart = getWordStart(); CompletionCandidate[] words = getFilteredWords( wordStart, editorTextArea.getText( wordStart, editorTextArea.getCaretPosition() - wordStart)); if (words.length == 0) { w.setVisible(false); return; } jList.setListData(words); jList.setSelectedIndex(0); } catch (BadLocationException e) { e.printStackTrace(); } } }); jList.addMouseListener( new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) { editorTextArea.replaceRange( ((CompletionCandidate) jList.getSelectedValue()).toString(), getWordStart(), editorTextArea.getCaretPosition()); w.setVisible(false); } } }); w.add(new JScrollPane(jList), BorderLayout.CENTER); w.setPreferredSize(new Dimension(200, 200)); Rectangle position = editorTextArea.modelToView(caretPosition); w.show(editorTextArea, position.x + position.width, position.y + position.height); jList.requestFocus(); } catch (BadLocationException e) { e.printStackTrace(); } }