Refreshing a view from another class
Hi all,
I am doing my first RCP application development using Lotus Expeditor.
Anyway, so far, here is what I have:
- A View (ViewPart) that displays a list of buttons depending on the content of a table in a database
- A class that is aware of the network status: when it detects that the application is online again, it will replicate the database (from a remote server, I am using DB2 Everyplace here)... that's working fine, but I would like also to refresh my View with the new data after replicating (without clicking on a "refresh" button on the view... I have tried this, and it's working fine, but I would like to have an automatic refresh).
So here are my questions:
- First, a design question: how should I do if I want a class to have access to my View? Indeed, my NetworkAwareImpl class needs to have access to it, but I can not pass the View object to the constructor the NetworkAwareImpl, since I create this object before creating the view (in the bundle activator class, so that the application is listening from the very start to the network status). So, what I did, is implement my View as a kind of singleton, so that my NetworkAwareImpl can call DesktopView.getDefault().updateView() for instance...
- Please take a look to my DesktopView class... for some reason, when I call the updateView method from my NetworkAwareImpl class, the method stops on the container.dispose() call... without any errors but it stops!
My DesktopView:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
public class DesktopView extends ViewPart implements SelectionListener{
private MessageGeneratorSupport msgGenService;
private DatabaseSupport dsService;
private DatabaseSyncSupport dssService;
private PreferencesService prefService;
private static DesktopView view;
private UI ui;
private Composite parent;
private Composite container;
private boolean disposed = false;
public DesktopView()
{
view = this;
BundleContext context = Activator.getDefault().getContext();
ServiceReference refMsgGen = context.getServiceReference(MessageGeneratorSupport.class.getName());
msgGenService = (MessageGeneratorSupport) context.getService(refMsgGen);
ServiceReference refDS = context.getServiceReference(DatabaseSupport.class.getName());
dsService = (DatabaseSupport) context.getService(refDS);
ServiceReference refDSS = context.getServiceReference(DatabaseSyncSupport.class.getName());
dssService = (DatabaseSyncSupport) context.getService(refDSS);
ServiceReference refPref = context.getServiceReference(PreferencesService.class.getName());
prefService = (PreferencesService) context.getService(refPref);
}
public void createPartControl(Composite parent) {
this.parent = parent;
parent.setLayout(new FillLayout());
Vector activities = new Vector();
try
{
activities = dsService.getActivitiesList();
}
catch(Exception e)
{
System.err.println(e.toString());
}
createContainer(parent, activities);
}
public void createContainer(Composite parent, Vector activities)
{
System.out.println("createContainer");
if(parent.isDisposed())
{
System.out.println("HERE");
return;
}
container = new Composite(parent, SWT.NONE);
container.setBounds(parent.getBounds());
GridLayout gl = new GridLayout();
gl.numColumns = 1;
container.setLayout(gl);
for(int i=0; i<activities.size(); i++)
{
System.out.println(i);
Button btn = new Button(container, SWT.NONE);
btn.setText((String)activities.elementAt(i));
}
Button btn = new Button(container, SWT.NONE);
btn.setText("refresh");
btn.setData("button");
btn.addSelectionListener(this);
container.layout();
}
public void setFocus() {
// TODO Auto-generated method stub
}
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
public void updateView()
{
System.out.println("UpdateView");
if(!isDisposed()) {
if(!container.isDisposed())
{
System.out.println("about to dispose");
container.dispose();
disposed = true;
System.out.println("Disposed");
}
}
Vector activities = new Vector();
System.out.println("new Vector");
try
{
activities = dsService.getActivitiesList();
}
catch(Exception e)
{
System.err.println(e.toString());
}
createContainer(parent, activities);
disposed = false;
}
public void widgetSelected(SelectionEvent e) {
if(((String) e.widget.getData()).equals("button")) {
if(!isDisposed()) {
updateView();
}
}
}
public boolean isDisposed()
{
return disposed;
}
public void dispose() {
disposed = true;
super.dispose();
}
public static DesktopView getDefault()
{
return view;
}
} |
Note: for now, I added a refresh button, and this is working fine.
And my NetworkAwareImpl :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
public class NetworkAwareImpl implements INetworkAware {
IOfflineServiceHelper _helper;
private DatabaseSyncSupport dssService;
private DatabaseSupport dsService;
private PreferencesService prefService;
public NetworkAwareImpl()
{
BundleContext context = Activator.getDefault().getContext();
ServiceReference refDS = context.getServiceReference(DatabaseSupport.class.getName());
dsService = (DatabaseSupport) context.getService(refDS);
ServiceReference refDSS = context.getServiceReference(DatabaseSyncSupport.class.getName());
dssService = (DatabaseSyncSupport) context.getService(refDSS);
ServiceReference refPref = context.getServiceReference(PreferencesService.class.getName());
prefService = (PreferencesService) context.getService(refPref);
}
public INetworkAware init() {
_helper = IOfflineServiceHelperFactory.INSTANCE.create(this);
return this;
}
public IOfflineServiceHelper getOfflineServiceHelper() {
return _helper;
}
public void disconnected(){
System.out.println("DISCONNECTED");
//do things accordingly if the system state changed to offline. e.g if the network connectivity is lost.
//e.g if application calls IOfflineManager.disconnected(), IOfflineManager will
// fire an DISCONNECTED event, which directly call INetworkAware.disconnected() method here.
// no intermediate states.
}
public void reconnected(){
System.out.println("RECONNECTED");
//do things accordingly if the system state changed to online.e.g. if the network connectivity is back.
//e.g if application calls IOfflineManager.reconnected(), IOfflineManager will
// fire an RECONNECTED event, which directly call INetworkAware.reconnected() method here.
// no intermediate states.
dssService.synchronizeDatabase(prefService.getUsername(), prefService.getPassword(), prefService.getHost());
System.out.println("replicated");
DesktopView.getDefault().updateView();
}
... |
Last question: is it ok to invoke other plugins in the DesktopView, as I did it? Since it's a view, it doesn't look nice to me... can you advice me also on this?
Thanks a LOT ! :)