Access objects from multiple parts of an Eclipse RCP Application [message #1695709] |
Mon, 18 May 2015 13:53 |
Bastian Hesselbarth Messages: 2 Registered: May 2015 |
Junior Member |
|
|
Hello together,
I'm quite new with creating RCP-Applications and using "Dependency Injection".
What I plan to do / have done so far:
I have set up an Application.e4mi by using the Eclipse 4 Model Editor. In a Trimmed Window I have organized several Parts.
One of these Parts is meant to be a console for logging, another is a server, which shall use the logger. Now I want to bind the logger to the Application context, to make it accessable for the server, therefore I use
application.getContext().set("MainLogger", logger);
The whole logger-part looks like this.
public class ConsolePart {
private Logger logger = Logger.getLogger("MainLogger");
private Text consoleText;
@Inject
public ConsolePart(MApplication application) {
application.getContext().set("MainLogger", logger);
}
@PostConstruct
public void postConstruct(Composite parent) {
consoleText = new Text(parent, SWT.NORMAL);
logger.addHandler(new Handler() {
@Override
public void close() throws SecurityException {
// TODO Auto-generated method stub
}
@Override
public void flush() {
// TODO Auto-generated method stub
}
@Override
public void publish(LogRecord arg0) {
// TODO Auto-generated method stub
consoleText.append(arg0.getMessage());
}
});
logger.info("Logger ready");
}
}
In the Server-Part I try to inject the logger from the context and use it there
@Inject
private Logger logger;
But I get an Exeption / the logger object equals null.
Here is the whole server class
public class ServerPart {
// User Interface Attributes
private Label serverInformationLabel;
private String serverInformation;
private Server server;
@Inject
private Logger logger;
@Inject
public ServerPart(MApplication application) {
// Functional things
server = new Server(logger);
application.getContext().set("server", server);
}
@PostConstruct
public void postConstruct(Composite parent) {
serverInformationLabel = new Label(parent, SWT.NO_FOCUS);
serverInformationLabel.setAlignment(SWT.CENTER);
serverInformation = server.getServerInformation();
serverInformationLabel.setText(serverInformation);
logger.info("test");
}
}
My target is to share objects under several parts (the ServerPart won't be the only one to use the logger, and there will be other parts, that have to use the server object, provided by the ServerPart)
How is the normal way to do this? What am I doing wrong?
Best regards,
Bastian
|
|
|
|
Powered by
FUDForum. Page generated in 0.02794 seconds