public class CustomComposite extends Composite { @Inject @Translation public Messages messages; /** * not used here for testshowcase, but needed in actual implementation */ @Inject public IEclipseContext context; @Inject public CustomComposite(final Composite parent) { super(parent, SWT.NONE); setLayout(new GridLayout(2, false)); addDisposeListener(e -> destroy()); } @PostConstruct public void postConstruct() { Label label = new Label(this, SWT.NONE); label.setText(messages.myFirstLabel); label = new Label(this, SWT.NONE); label.setText(messages.mySecondLabel); } public void destroy() { context.dispose(); } }
public class SamplePart { List<CustomComposite> customComposites = new ArrayList<>(); @Inject private IEclipseContext context; @PostConstruct public void createComposite(final Composite parent) { parent.setLayout(new GridLayout(2, false)); Button buttonAdd = new Button(parent, SWT.PUSH); buttonAdd.setText("ADD"); buttonAdd.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(final SelectionEvent e) { IEclipseContext childContext = context.createChild("myChildContexts"); childContext.set(Composite.class, parent); CustomComposite make = ContextInjectionFactory.make(CustomComposite.class, childContext); customComposites.add(make); make.requestLayout(); } }); Button buttonRemove = new Button(parent, SWT.PUSH); buttonRemove.setText("REMOVE"); buttonRemove.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(final SelectionEvent e) { if (!customComposites.isEmpty()) { CustomComposite composite = customComposites.get(customComposites.size() - 1); customComposites.remove(composite); composite.dispose(); parent.requestLayout(); } } }); } }
public class Messages { public String myFirstLabel; public String mySecondLabel; }
@Creatable public class MessageRegistry extends BaseMessageRegistry<Messages> { @Override @Inject public void updateMessages(@Translation Messages messages) { super.updateMessages(messages); } }
public class CustomComposite extends Composite { /** * not used here for testshowcase, but needed in actual implementation */ @Inject public IEclipseContext context; @Inject public CustomComposite(final Composite parent) { super(parent, SWT.NONE); setLayout(new GridLayout(2, false)); addDisposeListener(e -> destroy()); } @PostConstruct public void postConstruct(MessageRegistry registry) { Label label = new Label(this, SWT.NONE); registry.register(label::setText, m -> m.myFirstLabel); label = new Label(this, SWT.NONE); registry.register(label::setText, m -> m.mySecondLabel); } public void destroy() { context.dispose(); } }