Connecting an editor to a visualizer
"winder" <[email protected]>
| Newsgroups | gmane.comp.java.netbeans.modules.openide.devel |
|---|---|
| Message-ID | <[email protected]> |
Thanks Mark. I spent some time looking at the Savable interface, something like that looks like just what I need. But it looks like implementing a similar "Highlightable" interface would require quite a bit of code.
There must still be gaps in my NBP understanding, so I'd like to go through this selection tutorial sometime soon:
https://platform.netbeans.org/tutorials/nbm-selection-1.html
For now, I ended up finding a much simpler solution using a Guava EventBus (https://github.com/google/guava/wiki/EventBusExplained) to provides the same level of decoupling (minus the contextual selection information).
In my interface module I have two small classes to provide the interface and the bus:
Code:
@ServiceProvider(service=HighlightEventBus.class)
public class HighlightEventBus extends EventBus {
}
Code:
public class HighlightEvent {
Collection<Integer> lines;
public HighlightEvent(Collection<Integer> lines) {
this.lines = lines;
}
public Collection<Integer> getLines() {
return lines;
}
}
Now in my "Editor" class I can publish highlight events:
Code:
public class EditorListener implements CaretListener {
@Override
public void caretUpdate(CaretEvent e) {
...
EventBus eb = Lookup.getDefault().lookup(HighlightEventBus.class);
if (eb != null) {
eb.post(new HighlightEvent(selectedLines));
}
}
}
}
And in my Visualizer I register my class:
Code:
EventBus eb = Lookup.getDefault().lookup(HighlightEventBus.class);
if (eb != null) {
eb.register(listenerObject);
}
And hook up the "listener" method:
Code:
@Subscribe
public void highlightEventListener(HighlightEvent he) {
gcodeRenderer.setHighlightedLines(he.getLines());
gcodeRenderer.forceRedraw();
}