[Java-gnome-developer] Enabling and disabling cut/copy/delete actions based on text buffer selectio
Brought to you by:
afcowie
|
From: Richard S. <ri...@r-...> - 2010-08-03 11:12:37
|
I've been experimenting with the Java-Gnome libraries over the past few
days and made good progress. It has been an enjoyable and remarkably
productive few days, and I am impressed with the results.
I set myself an exercise of creating a simple text editor. It is working
well but I've run into a problem enabling and disabling actions based on
whether there is a selection in the text buffer. I want to disable the
cut/copy/delete menus and corresponding toolbar buttons when there is
nothing selected in the edit pane and enable them when there is a selection.
My solution to checking for a selection isn't very elegant. I have a
pair of event handlers, one for button released and one for key
released, that do exactly the same thing:
@Override
public boolean onButtonReleaseEvent(Widget w, EventButton e) {
if (JEditMainWindow.getInstance() != null) {
// Get a reference to the text buffer
TextBuffer b =
JEditMainWindow.getInstance()
.getEditorPane()
.getSourceView()
.getBuffer();
// Use the action group to enable/disable on selection
JEditMainWindow.getInstance()
.getActions()
.getValidOnSelection()
.setSensitive(b.getHasSelection());
}
return false;
}
It does work, apart from one case. If I make a selection and then click
_inside_ the selection with the mouse, the selection clears but in the
handler above the buffer.getHasSelection() returns true. So my
cut/copy/delete buttons remain enabled until I click again or press a key.
(For clarification, the way I have designed this is that JEditMainWindow
is a singleton and getActions() returns a Map that exposes the actions
and action groups for the application).
Is my approach to this wrong? Is there another event I could use?
--
Regards,
Richard
|