这是在混合 Swing/FX并将两个部分绑定到同一模型时看到线程规则违规的一种后续行动。
同时我做了一些实验:使用一个自定义属性,它的唯一任务是分别负责在 EDT/fx 线程上访问/通知。这个想法是自定义属性
- 由需要在 EDT 上访问的属性支持
- 用于fx端,即它的fx api是从FX-AT调用的
- 它的任务是酌情调用/runLater
摆脱违反线程规则的问题……以一定的代价:在 fx 文本字段中键入时,插入符号设置为文本的开头,从而在每个字符之前添加。在继续之前,问题是
- 像下面这样的包装器是否可以工作?
- 它做错了吗?(作为游戏的新手,我可能会做一些非常愚蠢的事情 ;-)
- 插入符号设置的原因是什么?
代码(可以在上一个问题的 SSCCE 中使用,单个更改是取消注释包装器创建并使用它来代替直接绑定到字段的文本)
/**
* Wrapper that switches to FX-AT/EDT as appropriate. The assumption is
* that the delegate needs to be accessed on the EDT while this property
* allows client access on the FX-AT.
*
* @author Jeanette Winzenburg, Berlin
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public class PropertyWrapper<T> extends ObjectPropertyBase<T> {
// the delegate we are keeping synched to
private Property<T> delegate;
// the value which is kept in synch (on being notified) with the delegate's value
// JW: does this make sense at all?
private volatile T value;
// keeping a copy of the bean ... ? better not allow accessing at all?
// private Object delegateBean;
private String delegateName;
private ChangeListener<T> changeListener;
public PropertyWrapper(Property<T> delegate) {
this.delegate = delegate;
bindDelegate();
}
/**
* Returns the value which is kept synched to the delegate's value.
*/
@Override
public T get() {
return value;
}
/**
* Implemented to update the delegate on the EDT
*/
@Override
public void set(T value) {
// PENDING: think about uni-directional binding
updateToDelegate(value);
}
/**
* Updates the delegate's value to the given value.
* Guarantees to do the update on the EDT.
*
* @param value
*/
protected void updateToDelegate(final T value) {
if (SwingUtilities.isEventDispatchThread()) {
doUpdateToDelegate(value);
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
doUpdateToDelegate(value);
}
});
}
}
/**
* Updates the delegate's value to the given value
* This methods runs on the thread that it is called from.
*
* @param the value to set.
*
*/
private void doUpdateToDelegate(T value) {
delegate.setValue(value);
}
/**
* Adds a ChangeListener to the delegate and synchs the value
* to the delegate's value.
*
* This is called once from the constructor, assuming that the thread it is
* called on is compatible with the delegates threading rules.
*/
private void bindDelegate() {
if (changeListener != null) throw new IllegalStateException("cannot bind twice");
value = delegate.getValue();
delegateName = delegate.getName();
changeListener = createChangeListener();
delegate.addListener(
changeListener);
}
/**
* Creates and returns the ChangeLister that's registered to the delegate.
* @return
*/
private ChangeListener<T> createChangeListener() {
ChangeListener<T> l = new ChangeListener<T>() {
@Override
public void changed(ObservableValue<? extends T> observable,
T oldValue, T newValue) {
updateFromDelegate(newValue);
}
};
// weakchangelistener doesn't work ... for some reason
// we seem to need a strong reference to the wrapped listener
// return new WeakChangeListener<T>(l);
return l;
}
/**
* Updates the internal value and notifies its listeners. Schedules the
* activity for execution on the fx-thread, if not already called on it.
*
* @param newValue
*/
protected void updateFromDelegate(final T newValue) {
if (Platform.isFxApplicationThread()) {
doUpdateFromDelegate(newValue);
} else {
Platform.runLater(new Runnable() {
@Override
public void run() {
doUpdateFromDelegate(newValue);
}});
}
}
/**
* Updates the internal value and notifies its listeners. It
* runs on the thread it is called from.
*
* @param newValue the new value.
*/
protected void doUpdateFromDelegate(T newValue) {
value = newValue;
fireValueChangedEvent();
}
/**
* Overridden to guarantee calling super on the fx-thread.
*/
@Override
protected void fireValueChangedEvent() {
if (Platform.isFxApplicationThread()) {
superFireChangedEvent();
} else {
Platform.runLater(new Runnable() {
@Override
public void run() {
superFireChangedEvent();
}});
}
}
protected void superFireChangedEvent() {
super.fireValueChangedEvent();
}
/**
* Implemented to return null.<p>
* PENDING: allow access to delegate's bean? It's risky, as this method
* most probably will be called on the fx-thread: even if we keep a copy
* around, clients might poke around the bean without switching to the EDT.
*/
@Override
public Object getBean() {
return null; //delegate != null ? delegate.getBean() : null;
}
@Override
public String getName() {
return delegateName; //delegate != null ? delegate.getName() : null;
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger.getLogger(PropertyWrapper.class
.getName());
}