• Post Reply Bookmark Topic Watch Topic
  • New Topic

ViewPart and Observer

 
Marco Rossi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have created a new view inside Eclipse.
My view is extending ViewPart (org.eclipse.ui.part.ViewPart)

Inside this view there is a TableViewer which displays data that come from a .properties file (containing file name).

To build it up I followed these tutorials:

http://www.vogella.de/articles/EclipseJFaceTable/article.html
http://www.vogella.de/articles/EclipseJFaceTableAdvanced/article.html

So I have built up a ModelProvider which gives me back, in a form of Array the content of the .properties file.

These data are now displayed inside the table, it works fine with sorting and search inside it.

Now one problem arises

This .properties file is filled up by a method called when user chooses different files from a FileDialog (org.eclipse.swt.widgets.FileDialog). Method takes these files and saves filename into .properties file.

If the menu item is called a second time, files change. The .properties file is updated correctly but at the moment the TableViewer is not updated.

I wanted to implement the Observable-Observer model using this tutorial

http://www.java-tips.org/java-se-tips/javax.swing/read-a-data-file-into-a-jtable-and-reload-if-data-file-have-ch.html

but if I do:



I get this error:


The type Observer cannot be a superinterface of MyView; a superinterface must be an interface.


How can I solve this problem?

Or, can you suggest me how to update the table? Probably I am on the wrong way.

Thanks
Marco



 
Marco Rossi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I solved the "superinterface error" simply doing



So, now I have:



How does notifyObserver work?

Thanks again
Marco

 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mostly just like AWT/Swing event listeners. It calls the update method for all Observers that were added to the Observable.
 
Marco Rossi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but I have another question.

How do I update the model when the .properties file changes?
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need a notification mechanism that notices when the file has changed. The easy way for now is to use a thread that regularly checks the file's lastModified() value. Or, if you can wait a short while, Java 7 will come out with its WatchService.
 
Marco Rossi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, unfortunately I can not wait.
I think I will use a "listener" on the "lastModified".

One the model is updated, does the TableViewer update automatically with refresh?
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the model fires the appropriate events, yes.
 
Marco Rossi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I can explain what I have done so far.

I have a TableViewer extending ViewPart and implementing Observable.

Then I have a class called Observer with a timer that every second check if the file has been modified.
If so, it fires notifyObservers().

MyView is registered as observer.

Inside MyView I have:



This now works fine but I have a problem I can not solve:

If the view is closed or the program is exited I got an exception:



And the line where I have error is:



This part of code listens to changes on my file; if timestamp is changed it notifies observers...

The update part of code is:



How can I solve this error?

EDIT: I solved the error adding a condition to my refresh code:



Thanks a lor
Marco
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!