我正在研究eclipse插件。我在eclipse插件上创建了一个扩展点。我添加了一个下拉列表。所以我实现了一个监听器来知道值何时改变并执行我的逻辑。这是扩展点的片段:
<extension point="org.eclipse.ui.views.properties.tabbed.propertyTabs">
<propertyTabs contributorId="com.ibm.xtools.modeler.ui.properties">
<propertyTab
label="My new Label"
category="Core"
afterTab="modeleditor.property.tab.AdvancedTab"
id="com.test.plugin.tabs.MyNewTab"/>
</propertyTabs>
</extension>
<extension point="org.eclipse.ui.views.properties.tabbed.propertySections">
<propertySections contributorId="com.ibm.xtools.modeler.ui.properties">
<propertySection
class="com.test.plugin.sections.MyNewTabSection"
tab="com.test.plugin.tabs.MyNewTab"
filter="com.test.plugin.sections.MyNewTabSection$Filter"
id="com.bnpp.ism.test.profile.tabs.MyNewTab">
</propertySection>
</propertySections>
</extension>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.NamedElement;
public class MyNewTabSection extends SuperSection{
private ComboViewer comboViewer;
private ISelectionChangedListener comboViewerSelectionListener;
private Label myLabel;
private final static List<String> myValues = new ArrayList<String>(Arrays.asList("FIRST","TWO","THREE"));
public static class Filter extends AbstractFilter{
public Filter() {
super();
}
}
@Override
public void createControls(Composite parent,TabbedPropertySheetPage tabbedPropertySheetPage) {
super.createControls(parent,tabbedPropertySheetPage);
addLabel();
addComboViewer();
@Override
protected void addListeners() {
comboViewer.addSelectionChangedListener(getComboViewerSelectionListener());
}
@Override
protected void removeListeners() {
comboViewer.removeSelectionChangedListener(getComboViewerSelectionListener());
}
@Override
protected boolean acceptElement(EObject element) {
return element instanceof Enumeration;
}
private void addComboViewer() {
FormData data;
comboViewer = new ComboViewer(composite);
data = new FormData();
data.left = new FormAttachment(myLabel,10);
data.top = new FormAttachment(15,110);
comboViewer.getCombo().setText(" ");
comboViewer.getCombo().setLayoutData(data);
comboViewer.setContentProvider(new ArrayContentProvider());
comboViewer.setInput(myValues);
}
private void addReferenceTypeCombo() {
FormData data;
comboViewer = new ComboViewer(composite);
data = new FormData();
data.left = new FormAttachment(myLabel,10);// 10,110
data.width = 100;
data.top = new FormAttachment(15,110);
comboViewer.getCombo().setText(" ");
comboViewer.getCombo().setLayoutData(data);
comboViewer.setContentProvider(new ArrayContentProvider());
NomenclatureTypeView[] values = NomenclatureTypeView.values();
comboViewer.setInput(myValues);
}
private void addLabel() {
FormData data;
nomenclatureLabel = new Label(composite,0);
data = new FormData();
data.left = new FormAttachment(10,110);
data.top = new FormAttachment(15,110);
myLabel.setLayoutData(data);
myLabel.setBackground(white);
myLabel.setText("My Label text");
}
private ISelectionChangedListener getComboViewerSelectionListener() {
if (comboViewerSelectionListener == null) {
comboViewerSelectionListener = new ISelectionChangedListener(){
@Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
Object firstElement = selection.getFirstElement();
final String value = firstElement.toString();
Object firstElementA = comboViewer.getElementAt(0);
if (firstElement != null) {
comboViewer.setSelection(new StructuredSelection(firstElement));
}
// some logic
}
};
}
return comboViewerSelectionListener;
}
@Override
protected void initInternalInput(EObject element) {
this.element = (NamedElement) element;
}
@Override
protected void resetInternalInput() {
this.element = null;
}
}
当我点击标签时,我可以看到带有我的值的下拉列表(ONE,TWO,THREE)。但是当我选择列表的值时,监听器会多次运行。这很奇怪,因为我只选择了一次该值。 它多次运行侦听器代码,直到出现以下异常:
!MESSAGE Unhandled event loop exception
!STACK 0
java.lang.StackOverflowError
at org.eclipse.swt.widgets.Control.windowProc(Control.java:3877)
at org.eclipse.swt.widgets.Combo.windowProc(Combo.java:2058)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4545)
at org.eclipse.swt.internal.win32.OS.SendMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.SendMessage(OS.java:3012)
at org.eclipse.swt.widgets.Combo.select(Combo.java:1386)
at org.eclipse.jface.viewers.ComboViewer.listSetSelection(ComboViewer.java:212)
at org.eclipse.jface.viewers.AbstractListViewer.setSelectionToWidget(AbstractListViewer.java:513)
at org.eclipse.jface.viewers.StructuredViewer.setSelectionToWidget(StructuredViewer.java:1680)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1636)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.myNewTabSection$2.selectionChanged(myNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:880)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2062)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1638)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at com.test.plugin.sections.MyNewTabSection$2.selectionChanged(MyNewTabSection.java:178)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
答案 0 :(得分:1)
肯定会在你的监听器中comboViewer.setSelection
触发一个SelectionChange
事件,该事件将被你的监听器捕获,触发......等等