2

I need to reference the Part object that created an SWT element. The Part is creating a Label in Part's @PostConstruct like that (e4):

public class SomePart {
    @PostConstruct
    public void postConstruct(Composite parent) {
        ...
        Label someLabel = new Label(parent);
        ...
    }
}

Need to get part that created someLabel like this:

(SomePart) someLabel.getMyParentPartPlease()

Also need to get such reference in RCP 3.x, but I will be realy happy to get help on any RCP version.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
Oroboros102
  • 2,214
  • 1
  • 27
  • 41

1 Answers1

1

Why not set the Part on the Widget in question in it's data field, which allows clients to set arbitary data on the object?

public class SomePart {
    @PostConstruct
    public void postConstruct(Composite parent) {
        ...
        Label someLabel = new Label(parent);
        someLabel.setData(this);
        ...
    }
}

Then later:

SomePart part = (SomePart) someLabel.getData();
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • Thanks for help. But, actualy, I don't manage Part's code. I'm trying to reference Part from my SWT listeners. Do you have any ideas on how to do it? – Oroboros102 Apr 04 '13 at 14:00
  • @Oroboros102 I do not, off the top of my head, unfortunately. I edited your question a bit to clarify that. I will give it further thought, but hopefully somebody else has some idea as well. – Edward Thomson Apr 04 '13 at 14:07
  • Oh, the worst part of all RCP for me is trying to connect SWT object with RCP (pass data, get it and such). Every time that ends with massive usage of `.setData(...)`, which looks ugly. Now I think, that I just don't catch the architecture of the RCP. – Oroboros102 Apr 04 '13 at 14:12
  • setData() method is ugly design. First of all, why do you need to reference Part from Label()? I just want to understand use-case. – sambi reddy Apr 04 '13 at 15:37
  • @sambireddy "Ugly design"? Care to clarify? – Edward Thomson Apr 04 '13 at 15:42
  • setData() method and keeping references to other Objects that are irrelevant for the model of the Label is not a good idea, I think. it could lead to memory leaks. Thats why I am questioning the question!. – sambi reddy Apr 04 '13 at 18:35
  • @sambireddy I bet, my use case is really rare. It is about security. Parts have security contexts, and Listeners on SWT elements must obtain it somehow. AFAIK, SWT elements are destroyed together with parts, so there should be no memleaks. – Oroboros102 Apr 05 '13 at 09:10