英文:
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:
<?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>
<extension
point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension
targetID="org.eclipse.jdt.ui.JavaPerspective">
<view
id="asher.views.id.SampleView"
relative="org.eclipse.ui.views.ProblemView"
relationship="right"
ratio="0.5">
</view>
</perspectiveExtension>
</extension>
<extension
point="org.eclipse.help.contexts">
<contexts
file="contexts.xml">
</contexts>
</extension>
<extension
point="org.eclipse.ui.commands">
<category
id="asher.commands.unparse.category"
name="Unparse Category">
</category>
<command
categoryId="asher.commands.unparse.category"
name="UnParse"
id="asher.commands.unparseCommand">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="asher.handlers.UnparseHandler"
commandId="asher.commands.unparseCommand">
</handler>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
commandId="asher.commands.unparseCommand"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
contextId="org.eclipse.ui.contexts.window"
sequence="M1+7">
</key>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="true"
locationURI="toolbar:asher.views.id.SampleView?after=additions">
<menu
id="asher.menus.unparseMenu"
label="Unparse"
mnemonic="M">
<command
commandId="asher.commands.unparseCommand"
icon="icons/daffodil.png"
id="asher.menus.unparseCommand"
mnemonic="S"
tooltip="Unparse">
</command>
</menu>
</menuContribution>
</extension>
</plugin>
ViewPart Class:
public class SampleView extends ViewPart {
/**
* The ID of the view as specified by the extension.
*/
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) {
// 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:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论