Search code examples
javavisualizationjungprefuse

How to update Prefuse or JUNG graphs when their source GraphML files are modified?


I am rendering a graph from a GraphML source file using either JUNG or Prefuse. Now another program updates this source file periodically with new nodes and edges. I want to update the graph to reflect these changes as well. I cannot find anything in both APIs themselves that let me do this (no appropriate refresh(TIMER) or redraw(TIMER) kind of methods). The one way of doing this might be to terminate the JFrame after some time period and redo the whole data loading, visualization and rendering process again, but that doesn't seem proper to me.

So the question is, what's the best way to do this as simply and as quickly as possible? Thanks :)


Solution

  • Check the source file "periodically". If you hang on to a handle f for the file, you can query f.lastModified() and if the other program has done its dirty work since last you checked, update! Similar solutions apply if the other program were writing only an update file (which would be sensible if you have the power to make that change - less file to read, know exactly what to change in your model, etc).

    "Periodically" is a bit of sticky wicket - your case is probably simple enough that "every X (milli)seconds, check" will do. Periodic tasks in Java can be implemented in a variety of ways - see, e.g. this other question.

    JUNG has re-render/re-paint on update demos, e.g. this one. The tutorial (pdf) linked in their docs covers dynamic updates in section 5 (the section is about interactivity, but the necessaries to do updating when presented with new info are all covered).