سؤال

in SWT I try to create a composite that is compound of a couple of already existing (instantiated) composites. Sadly the SWT API does not allow that, as the parent (where the composite gets drawn) has to be given to the constructor.

I've created a little example that will (hopefully) show what I try to accomplish, and my problem:

Composite

public class Composite {
  public Composite(Composite parent, ...) {
    // this composite will be drawn on parent

    // ...
  }

  // ...
}

Composed Composite

public class ComposedComposite extends Composite {

  // Note that there are composed composites with more than one child
  public ComposedComposite(Composite parent, Composite child) {
    super(parent);
    // child is used as content for some control

    // ...
  }

  // ...
}

Where things get composed

// ...
// This is how I would prefer to compose things
ChildComposite child = new ChildComposite(...); // zonk parent is not available yet
ComposedComposite composed = new ComposedComposite(..., child); // again parent is not available yet

MainComposite main = new MainComposite(parent, composed); // The overall parent is set from outside
// ...

Greetings Ben

-- Edit add more details about the problem

Here is what I really want to accomplish:

I have a main window that hosts same TabItems. Each of the TabItems has a layout and represents different data from a model. Now I've created a couple of controls which I want to compound in a separate control (container). The container has the following layout.

+-------------+---------------------------+
|             |            B              |
|             |                           |
|      A      +---------------------------+
|             |            C              |
|             |                           |
+-------------+---------------------------+

Three TabItems have the same layout (the container above). And all three of them share one control (as this one is needed on all tabs).

So what I want to do at least is this:

SharedComposite shared = new SharedComposite(...);
shared.registerListener(this);

SomeOtherComposite comp1 = new SomeOtherComposite(...);
comp1.registerListener(this);
// ... couple of them

// know compose the controls
Container container = new Container(...);
container.setA(shared); // instead of this setters the composites may be given in the ctor
container.setB(comp1);
container.setC(comp2);
addTabItem(container);

Container container2 = new Container(shared, comp3, comp4); // other way
addTabItem(container2);

So using the given answer (setParent) I can do something similar. Sadly I still can't reuse a composite in multiple tabs. But that seems impossible with SWT, so using setParent seems the best I can get.

Thank you all for the help!

هل كانت مفيدة؟

المحلول

SWT does have setParent, but this isn't supported by all operating systems and according to http://www.eclipsezone.com/eclipse/forums/t23411.html,

has negative side effects even on Windows where it is supported by the OS

(unfortunately, I don't know what these effects are). However, given this limitation something like this should work:

public class ComposedComposite extends Composite {
    public ComposedComposite(Composite parent, Control... children) {
        super(parent, SWT.NONE);
        for (Control child : children) {
            child.setParent(this);
        }
    }

    public void addChild(Control c) {
        c.setParent(this);
    }
}

You'll likely need to call layout as well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top