简体   繁体   English

Eclipse Rcp-如何在ctrl上的文本编辑器中获取自定义弹出菜单+文本上的空格

[英]Eclipse Rcp - How to get custom PopUp Menu in text editor on ctrl + space on text

We have seen that in eclipse Java Editor, when we perform (ctrl + space) key on the objectname, a popup menu appears showing list of methods and variables of that class. 我们已经看到,在Eclipse Java编辑器中,当对对象名执行(ctrl +空格)键时,将出现一个弹出菜单,显示该类的方法和变量的列表。

In my case, it is not a java editor. 就我而言,它不是Java编辑器。 But I want to achieve similar functionality as mentioned above. 但是我想实现如上所述的类似功能。

I have 30 fix objects, when that object appears in the text editor and I do (ctrl + space) key at the end of object name, I need to show popup menu with its member variables. 我有30个修复对象,当该对象出现在文本编辑器中并且在对象名称的末尾使用(ctrl +空格)键时,我需要显示带有其成员变量的弹出菜单。

Could you please guide me how this can be achieved. 您能否指导我如何实现这一目标。

Thanks in advance. 提前致谢。

You need to register a JFace content assistant in your SourceViewerConfiguration, compare this question: How to implement content assist's documentation popup in Eclipse RCP 您需要在SourceViewerConfiguration中注册一个JFace内容助手,然后比较以下问题: 如何在Eclipse RCP中实现内容助手的文档弹出窗口

This page got even more examples: http://www.programcreek.com/java-api-examples/index.php?api=org.eclipse.jface.text.contentassist.IContentAssistant 该页面甚至提供了更多示例: http : //www.programcreek.com/java-api-examples/index.php? api= org.eclipse.jface.text.contentassist.IContentAssistant

And this is the official Eclipse FAQ for this feature: http://wiki.eclipse.org/FAQ_How_do_I_add_Content_Assist_to_my_editor%3F 这是此功能的官方Eclipse FAQ: http : //wiki.eclipse.org/FAQ_How_do_I_add_Content_Assist_to_my_editor%3F

Use KeyStroke controlspace = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.CTRL_MASK); 使用KeyStroke controlspace = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.CTRL_MASK);

Do somthing like this example: 做这样的例子:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

public class KeyTest extends JFrame {

    private JPanel contentPane;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    KeyTest frame = new KeyTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public KeyTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JLabel lblClickHereAnd = new JLabel("Click here and press Ctrl+Space");
        contentPane.add(lblClickHereAnd, BorderLayout.NORTH);

         InputMap inputMap = contentPane.getInputMap();
         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.CTRL_MASK), "foo");
         contentPane.getActionMap().put("foo", new AbstractAction() {
             public void actionPerformed(ActionEvent e) {
                 System.out.println("Key pressed");
             }
         });
         pack();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM