e4数据绑定转换器异常无法捕获

时间:2017-08-18 13:31:59

标签: data-binding eclipse-rcp jface e4

我创建了一个自定义转换器,将String转换回Date。

public Object convert(Object fromObject){
   if (fromObject != null && fromObject.toString().trim().length() == 0){
       return null;
   }

   for (DateFormat f : formats){
       try{
            return f.parse(fromObject.toString());
       }catch (ParseException e){
           // Ignore
       }
   }

   throw new RuntimeException(message);
}

基本上,如果字符串不可解析,则会抛出RuntimeException。

我已将转换器添加到数据绑定中的更新策略中,并且正在调用它。

问题是抛出异常的时间。 (例如,当我开始在TextFiled中键入日期时)。

表示异常不是catch,而是出现装饰器字段以指示输入中的错误

控制台日志中出现异常(日志中的错误位于问题的最后),因为似乎没有人抓住它。

我缺少什么?转换器中的异常应该是在updateStrategy中捕获并显示错误,不应该吗?

  

!ENTRY org.eclipse.core.databinding 4 0 2017-08-18 15:16:27.816   !MESSAGE无效的时间格式   !堆栈0   java.lang.RuntimeException:无效的时间格式       at com.lsespace.earthcare.tds.gui.util.databinding.conversion.StringToJavaTimeTagConverter.convert(StringToJavaTimeTagConverter.java:21)       在org.eclipse.core.databinding.UpdateStrategy.convert(UpdateStrategy.java:715)       在org.eclipse.core.databinding.UpdateValueStrategy.convert(UpdateValueStrategy.java:1)       在org.eclipse.core.databinding.ValueBinding $ 3.run(ValueBinding.java:175)       在org.eclipse.core.databinding.observable.Realm $ 1.run(Realm.java:149)       在org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)       在org.eclipse.core.databinding.observable.Realm.safeRun(Realm.java:153)       在org.eclipse.core.databinding.observable.Realm.exec(Realm.java:171)       在org.eclipse.core.databinding.ValueBinding.doUpdate(ValueBinding.java:158)       在org.eclipse.core.databinding.ValueBinding.access $ 4(ValueBinding.java:147)       在org.eclipse.core.databinding.ValueBinding $ 1.handleValueChange(ValueBinding.java:46)       在org.eclipse.core.databinding.observable.value.ValueChangeEvent.dispatch(ValueChangeEvent.java:70)       在org.eclipse.core.databinding.observable.ChangeManager.fireEvent(ChangeManager.java:117)       在org.eclipse.core.databinding.observable.value.DecoratingObservableValue.fireValueChange(DecoratingObservableValue.java:61)       在org.eclipse.core.databinding.observable.value.DecoratingObservableValue.handleValueChange(DecoratingObservableValue.java:103)       在org.eclipse.core.databinding.observable.value.DecoratingObservableValue $ 1.handleValueChange(DecoratingObservableValue.java:76)       在org.eclipse.core.databinding.observable.value.ValueChangeEvent.dispatch(ValueChangeEvent.java:70)       在org.eclipse.core.databinding.observable.ChangeManager.fireEvent(ChangeManager.java:117)       at org.eclipse.core.databinding.observable.value.AbstractObservableValue.fireValueChange(AbstractObservableValue.java:82)       at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.notifyIfChanged(SimplePropertyObservableValue.java:126)       at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.access $ 3(SimplePropertyObservableValue.java:118)       at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue $ 1 $ 1.run(SimplePropertyObservableValue.java:70)       在org.eclipse.core.databinding.observable.Realm $ 1.run(Realm.java:149)       在org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)       在org.eclipse.core.databinding.observable.Realm.safeRun(Realm.java:153)       在org.eclipse.core.databinding.observable.Realm.exec(Realm.java:171)       at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue $ 1.handleEvent(SimplePropertyObservableValue.java:66)       在org.eclipse.core.databinding.property.NativePropertyListener.fireChange(NativePropertyListener.java:69)       在org.eclipse.jface.internal.databinding.swt.WidgetListener.handleEvent(WidgetListener.java:56)       在org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)       在org.eclipse.swt.widgets.Display.sendEvent(Display.java:4410)       在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)       在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1103)       在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1084)       在org.eclipse.swt.widgets.Text.wmCommandChild(Text.java:3117)       在org.eclipse.swt.widgets.Control.WM_COMMAND(Control.java:4939)       在org.eclipse.swt.widgets.Control.windowProc(Control.java:4794)       在org.eclipse.swt.widgets.Display.windowProc(Display.java:5115)       在org.eclipse.swt.internal.win32.OS.CallWindowProcW(本机方法)       在org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2446)       在org.eclipse.swt.widgets.Text.callWindowProc(Text.java:262)       在org.eclipse.swt.widgets.Control.windowProc(Control.java:4889)       在org.eclipse.swt.widgets.Text.windowProc(Text.java:2704)       在org.eclipse.swt.widgets.Display.windowProc(Display.java:5102)       at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)       在org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2552)       在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3814)       在org.eclipse.jface.window.Window.runEventLoop(Window.java:818)       在org.eclipse.jface.window.Window.open(Window.java:794)       在com.lsespace.earthcare.tds.gui.jface.actions.EditConfigAction.run(EditConfigAction.java:39)       在org.eclipse.jface.action.Action.runWithEvent(Action.java:473)       at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:565)       at org.eclipse.jface.action.ActionContributionItem.lambda $ 5(ActionContributionItem.java:436)       at org.eclipse.jface.action.ActionContributionItem $$ Lambda $ 57 / 765702264.handleEvent(Unknown Source)       在org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)       在org.eclipse.swt.widgets.Display.sendEvent(Display.java:4410)       在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)       在org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4228)       在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3816)       at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine $ 4.run(PartRenderingEngine.java:1121)       在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)       在org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1022)       在org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:150)       在org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:161)       在org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)       在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)       在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)       在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)       在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:497)       在org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:673)       在org.eclipse.equinox.launcher.Main.basicRun(Main.java:610)       在org.eclipse.equinox.launcher.Main.run(Main.java:1519)       在org.eclipse.equinox.launcher.Main.main(Main.java:1492)

1 个答案:

答案 0 :(得分:0)

这是我使用的UpdateStrategy的实现,因此转换器中的异常被视为验证异常。

/**
 * This implementation of UpdateValueStrategy does not catch the exceptions thrown by the converter,
 * thus letting the normal mechanism of ValueBinding deal with the exception as it will do with a
 * validation exception.
 *
 */
public class AlternativeUpdateValueStrategy extends UpdateValueStrategy {

 public AlternativeUpdateValueStrategy() {
    this(UpdateValueStrategy.POLICY_UPDATE);
 }

 public AlternativeUpdateValueStrategy(int updateStrategy) {
    super(updateStrategy);
 }

 @Override
 public Object convert(Object value) {
    if (converter != null) {
        return converter.convert(value);
    }
    return value;
 }

}