开发者

SWT user control design decision

Hypothetical question:

What about this SWT design decision that to make a custom compound widget I have to subclass Composite? Is that really wise?

Wouldn't it be better if SWT had a UserControl class like in Win Forms or something?

When I subclass Composite my custom widget get the Composite interface, even if it is not intended to be used as a Composite by clients开发者_开发知识库. That's kind of bad. The same is true for some SWT widgets, like Spinner.

Is there a good way around this?

And, most interesting: Do anyone know the motivation for this design decision?


You could look at the source code for org.eclipse.swt.custom.CCombo to see how SWT deals with this problem internally, as CCombo extends from Composite. Here's how they handle setLayout(), for example:

/**
 * Sets the layout which is associated with the receiver to be
 * the argument which may be null.
 * <p>
 * Note: No Layout can be set on this Control because it already
 * manages the size and position of its children.
 * </p>
 *
 * @param layout the receiver's new layout or null
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public void setLayout (Layout layout) {
    checkWidget ();
    return;
}


You could create your own NonCompositeUserControl class:

public class NonCompositeUserControl extends Composite {
    @Override
    public void setLayout(Layout layout) {
        throw new UnsupportedOperationException("This control is not really a composite")
    }
    // similarly for other methods
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜