在 Eclipse 视图工具栏中添加按钮,而不使用查看器。

huangapple go评论152阅读模式
英文:

Add Button to Eclipse view toolbar without using viewer

问题

我正在尝试创建一个Eclipse PDE插件,其中包含一个带有按钮的视图工具栏的视图。该视图旨在显示文本,该文本在单击工具栏按钮时会更改格式。我一直在尝试绕过使用查看器,因为没有一个查看器符合我的需求。我遵循了这个 StackOverflow帖子的参考,但未能使函数正常工作。**我的plugin.xml文件有什么问题吗?**我将视图的ID添加为菜单贡献的位置URI,但插件未按预期在工具栏中显示按钮。

以下是您提供的插件配置文件和ViewPart类的内容,供您参考:

Plugin.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
    <extension point="org.eclipse.ui.views">
        <category name="Sample Category" id="asher"></category>
        <view id="asher.views.id.SampleView" name="Sample View" icon="icons/daffodil.png"
            class="asher.views.SampleView" category="asher" inject="true">
        </view>
    </extension>
    <!-- 其他扩展部分 -->
</plugin>

ViewPart类:

public class SampleView extends ViewPart {
    public static final String ID = "asher.views.id.SampleView";
    @Inject IWorkbench workbench;

    @Override
    public void createPartControl(Composite parent) {
        Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
        File file = new File("/Users/user/Desktop/install.json");
        Scanner sc;

        try {
            sc = new Scanner(file);
            while (sc.hasNextLine())
                text.setText(text.getText() + "\n" + sc.nextLine());
            sc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setFocus() {
    }
}
英文:

I am trying to create an Eclipse PDE plugin that has a view with a button in the view's tool bar. The view is intended to display text, which will change format when the toolbar button is clicked. I have been trying to work around using a viewer, because none of the viewers match fit my needs. I followed this SO post for reference but have not been able to get the function to work. Is there something wrong with my plugin.xml file? I added the ID of the view as the location URI for the menu contribution, but the plugin is not showing the button in the toolbar as intended.

I have added my plugin.xml and ViewPart class below for your reference:

Plugin.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?eclipse version=&quot;3.4&quot;?&gt;
&lt;plugin&gt;

   &lt;extension
         point=&quot;org.eclipse.ui.views&quot;&gt;
      &lt;category
            name=&quot;Sample Category&quot;
            id=&quot;asher&quot;&gt;
      &lt;/category&gt;
      &lt;view
            id=&quot;asher.views.id.SampleView&quot;
            name=&quot;Sample View&quot;
            icon=&quot;icons/daffodil.png&quot;
            class=&quot;asher.views.SampleView&quot;
            category=&quot;asher&quot;
            inject=&quot;true&quot;&gt;
      &lt;/view&gt;
   &lt;/extension&gt;
   &lt;extension
         point=&quot;org.eclipse.ui.perspectiveExtensions&quot;&gt;
      &lt;perspectiveExtension
            targetID=&quot;org.eclipse.jdt.ui.JavaPerspective&quot;&gt;
         &lt;view
               id=&quot;asher.views.id.SampleView&quot;
               relative=&quot;org.eclipse.ui.views.ProblemView&quot;
               relationship=&quot;right&quot;
               ratio=&quot;0.5&quot;&gt;
         &lt;/view&gt;
      &lt;/perspectiveExtension&gt;
   &lt;/extension&gt;
   &lt;extension
         point=&quot;org.eclipse.help.contexts&quot;&gt;
      &lt;contexts
            file=&quot;contexts.xml&quot;&gt;
      &lt;/contexts&gt;
   &lt;/extension&gt;
      &lt;extension
         point=&quot;org.eclipse.ui.commands&quot;&gt;
     &lt;category
     		id=&quot;asher.commands.unparse.category&quot;
     		name=&quot;Unparse Category&quot;&gt;
     &lt;/category&gt;
     &lt;command
            categoryId=&quot;asher.commands.unparse.category&quot;
            name=&quot;UnParse&quot;
            id=&quot;asher.commands.unparseCommand&quot;&gt;
      &lt;/command&gt;
   &lt;/extension&gt;
   &lt;extension
         point=&quot;org.eclipse.ui.handlers&quot;&gt;
      &lt;handler
      		class=&quot;asher.handlers.UnparseHandler&quot;
      		commandId=&quot;asher.commands.unparseCommand&quot;&gt;
      &lt;/handler&gt;
   &lt;/extension&gt;
    &lt;extension
         point=&quot;org.eclipse.ui.bindings&quot;&gt;
      &lt;key
            commandId=&quot;asher.commands.unparseCommand&quot;
            schemeId=&quot;org.eclipse.ui.defaultAcceleratorConfiguration&quot;
            contextId=&quot;org.eclipse.ui.contexts.window&quot;
            sequence=&quot;M1+7&quot;&gt;
      &lt;/key&gt;
      &lt;/extension&gt;
     &lt;extension
         point=&quot;org.eclipse.ui.menus&quot;&gt;
    &lt;menuContribution
          allPopups=&quot;true&quot;
          locationURI=&quot;toolbar:asher.views.id.SampleView?after=additions&quot;&gt;
         &lt;menu
               id=&quot;asher.menus.unparseMenu&quot;
               label=&quot;Unparse&quot;
               mnemonic=&quot;M&quot;&gt;
            &lt;command
                  commandId=&quot;asher.commands.unparseCommand&quot;
                  icon=&quot;icons/daffodil.png&quot;
                  id=&quot;asher.menus.unparseCommand&quot;
                  mnemonic=&quot;S&quot;
                  tooltip=&quot;Unparse&quot;&gt;
            &lt;/command&gt;
         &lt;/menu&gt;
      &lt;/menuContribution&gt;
      &lt;/extension&gt;
&lt;/plugin&gt;

ViewPart Class:

public class SampleView extends ViewPart {
	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = &quot;asher.views.id.SampleView&quot;;
	@Inject IWorkbench workbench;
	 
	@Override
	public void createPartControl(Composite parent) {
		
		Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
		
		 File file = new File(&quot;/Users/user/Desktop/install.json&quot;);
		 Scanner sc;
		
		try {
			sc = new Scanner(file);
			while (sc.hasNextLine()) 
			      text.setText(text.getText()+&quot;\n&quot;+sc.nextLine()); 
			sc.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void setFocus() {
	}
}

答案1

得分: 1

我找到了解决方法。我所做的更改是从工具栏的<menuContribution></menuContribution>中移除了<menu></menu>标签。

以下是已修复的 plugin.xml 部分:

<menuContribution
      allPopups="true"
      locationURI="toolbar:asher.views.id.SampleView?after=additions">
        <command
              commandId="asher.commands.unparseCommand"
              icon="icons/daffodil.png"
              id="asher.menus.unparseCommand"
              mnemonic="S"
              tooltip="Unparse">
        </command>
 </menuContribution>
英文:

I found a solution to my answer. The change I made was to remove the <menu></menu> tag from the <menuContribution> </menuContribution> for the toolbar.

Here is the fixed plugin.xml section:

&lt;menuContribution
      allPopups=&quot;true&quot;
      locationURI=&quot;toolbar:asher.views.id.SampleView?after=additions&quot;&gt;
            &lt;command
                  commandId=&quot;asher.commands.unparseCommand&quot;
                  icon=&quot;icons/daffodil.png&quot;
                  id=&quot;asher.menus.unparseCommand&quot;
                  mnemonic=&quot;S&quot;
                  tooltip=&quot;Unparse&quot;&gt;
            &lt;/command&gt;
     &lt;/menuContribution&gt;

huangapple
  • 本文由 发表于 2020年7月25日 04:28:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63081008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定