package com.bar.foo.toolBar;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
public class FooHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
System.out.println("foo");
return null;
}
}
這裏是第二個:
package com.bar.foo.toolBar;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
public class BarHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
System.out.println("bar");
return null;
}
}
因爲我不知道太多關於你的數據,我要創建我自己的模型。在包com.bar.foo.model的模型只是一個類:
package com.bar.foo.model;
public class NetworkConfig {
private String ip = "";
private String mask = "";
private String gateway = "";
public String[] getAvailableHosts() {
return new String[] { "fooHost" };
}
public void scanHost(String host) {
if ("fooHost".equals(host)) {
ip = "192.168.1.2";
mask = "255.255.255.0";
gateway = "192.168.1.1";
} else {
ip = "";
mask = "";
gateway = "";
}
}
public String getIP() {
return ip;
}
public String getMask() {
return mask;
}
public String getGateway() {
return gateway;
}
}
現在對於包含進去定製ToolBar
的ControlContributions
的com.bar.foo.actions包。請注意,這兩個類別都不與型號有關,它們可以在您的產品的其他地方重新使用。
第一堂課只包裝Combo
小部件。通過覆蓋controlCreated(Combo)
方法可以初始定製小部件。我在ToolBarContribution
類中使用它來添加SelectionListener
並設置Combo
的項目。下面是類:
package com.bar.foo.actions;
import org.eclipse.jface.action.ControlContribution;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
public class ComboContributionItem extends ControlContribution {
private Combo combo;
public ComboContributionItem(String id) {
super(id);
}
@Override
protected Control createControl(Composite parent) {
combo = new Combo(parent, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
return combo;
}
@Override
public int computeWidth(Control control) {
// The widget is now 100 pixels. You can new GC gc = new GC(control) and
// use the gc.stringExtent(String) method to help compute a more dynamic
// width.
return 100;
}
public Combo getComboControl() {
return combo;
}
}
此包中的其他類包裝一個Text
部件:
package com.bar.foo.actions;
import org.eclipse.jface.action.ControlContribution;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
public class TextContributionItem extends ControlContribution {
private final int style;
private Text text;
public TextContributionItem(String id) {
this(id, SWT.BORDER | SWT.SINGLE);
}
public TextContributionItem(String id, int style) {
super(id);
this.style = style;
}
@Override
protected Control createControl(Composite parent) {
text = new Text(parent, style);
return text;
}
@Override
public int computeWidth(Control control) {
return 100;
}
public Text getTextControl() {
return text;
}
}
我不這樣做,但如果你需要進一步定製Text
小部件爲您ToolBar
,就像我在初始化ComboContributionItem
時一樣,您可以覆蓋createControl(Composite)
方法。
現在最後一件事:我使用擴展來定製ToolBar
。但是,ToolBarContribution
使用的邏輯適用於您的fillCoolBar(ICoolBarManager)
方法或createControl(Composite)
方法,具體取決於您最終希望修改哪一個ToolBar
。
對我來說,這裏就是我添加到插件的plugin.xml
結束:
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="com.bar.foo.toolbar">
<command
commandId="com.bar.foo.commands.foo"
label="Foo"
style="push">
</command>
<control
class="com.bar.foo.toolBar.ToolBarContribution">
</control>
<command
commandId="com.bar.foo.commands.bar"
label="Bar"
style="push">
</command>
</toolbar>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
id="com.bar.foo.commands.foo"
name="Foo">
</command>
<command
id="com.bar.foo.commands.bar"
name="Bar">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="com.bar.foo.toolBar.FooHandler"
commandId="com.bar.foo.commands.foo">
</handler>
<handler
class="com.bar.foo.toolBar.BarHandler"
commandId="com.bar.foo.commands.bar">
</handler>
</extension>
的命令迷上了,這樣還有的定製ToolBar
和BarHandler
按鈕之前FooHandler
按鈕定製後ToolBar
。這些命令在xml中的指定順序將反映在應用程序中。同樣,項目添加到定製ToolBar
的順序也會反映在您的產品中。
關於展示位置的另一個注意事項:可以通過在locationURI的查詢中設置展示位置,例如toolbar:org.eclipse.ui.main.toolbar?after=additions
,讓menuContributions顯示在不同的位置。 「之前」是另一個放置關鍵字,如「之後」。更多的例子可以在in this Eclipse help doc找到。
歡迎來到SO!如果您在回答之前需要澄清一下,請留下評論(下次,當您擁有足夠的「聲譽」時)現在,您爲什麼不直接展示如何做,並添加一些示例代碼? – GameDroids 2014-10-09 18:19:27