EditorExit event handler called recursively - gwt

The below portion of my code caused recursive call on onEditorExit method. If I remove setData call, then no recursion occur.
What can be the workaround?
myGrid.addEditorExitHandler(new EditorExitHandler() {
public void onEditorExit(EditorExitEvent event) {
GWT.log("Hello");
myGrid.setData(new ListGridRecord());
}
});
Now check the console output -
Console Screenshot

It won't work because each time you call setData() the editor will fire an editorExit event in an infinite loop (not recursively). By the way, calling setData() in the way you are doing will replace all your records in the ListGrid with one new empty record. This seems like a disconcerting user experience.
It looks like you want to create and start editing a new record when you tab out of the last one. In order to do that in a ListGrid, you use:
grid.setListEndEditAction(RowEndEditAction.NEXT);
That's all you need to do in order to get it working.

Related

GXT get value of TextArea without focus loss

I have a GXT 3 TextArea on which I catch copy-paste events. On this event, I would like to get the text that is inside the textarea.
Problem : the textarea still has the focus so the value is not updated. Hence, getValue() returns an empty string...
I tried to call getValue() getCurrentValue() flush() validate().
I also tried to extend TextArea to have access to blur() method and call it before getting the value : it makes no difference.
Any solution? (even solution with GWT components would be appreciated).
Without seeing the code you have, it is difficult to say for sure. But at a guess, you are listening to the event, and invoking getCurrentValue() (the correct call in this case) right away.
This is wrong - it is possible for the event handler to call preventDefault(), to cancel the default behavior of that event for most events that can take place. After the event handler returns, only then does the browser actually perform the action (paste or drawing the key that was pressed). The solution to this is to wait a moment before trying to read, to wait until after the action has been completed. The simplest way to achieve this is to schedule a deferred command after the event has occurred, and read the field's value then.
//in the event handler of your choice...
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
String pastedValue = field.getCurrentValue();
//do something with the value now
}
});
Before getting the value you can call
yourTextField.finishEditing();
After it method getValue() should return the value of the field. If you would like to keep this field focused after getting the value you can always call
yourTexField.focus();

QApplication::processEvents never returns

In my application I need to wait until external program (using QProcess) is finished. I want to keep the application responsible so blocking methods are unacceptable.
Also I need to disallow user input. I've tried to make QEventLoop and exec it with QEventLoop::ExcludeUserInputEvents flag, but as documentation says it only delays an event handling:
the events are not discarded; they will be delivered the next time processEvents() is called without the ExcludeUserInputEvents flag.
So I implemented simple event filter and install it on qApp (the idea is took from Qt Application: Simulating modal behaviour (enable/disable user input)). It works well, but sometimes QApplication::processEvents function never returns even if I specify the maximum timeout. Could anyone help me to understand for what reasons it periodically happens?
class UserInputEater : public QObject
{
public:
bool eventFilter(QObject *object, QEvent *event)
{
switch(event->type())
{
case QEvent::UpdateRequest:
case QEvent::UpdateLater:
case QEvent::Paint:
return QObject::eventFilter(object, event);
default:
return true;
}
}
};
-
UserInputEater eventEater;
qApp->installEventFilter(&eventEater);
QProcess prc;
prc.start("...");
while(!prc.waitForFinished(10))
{
if(qApp->hasPendingEvents())
{
// Sometimes it never returns from processEvents
qApp->processEvents(QEventLoop::AllEvents, 100);
}
}
qApp->removeEventFilter(&eventEater);
UPD: Seems like it depends of the timeout value for QProcess::waitForFinished.
I guess you are filtering some useful events (for example, QEvent::SockAct could be involved). Try to add some debug output and find out which event types you're actually filtering. Or it might be better to specify the black list of events you want to block instead of white list of events you want to allow. See this answer.
Also you shouldn't use return QObject::eventFilter(object, event);. You should use return false. All other event filters will be called automatically.
This solution however seems weird and unreasonable to me because you can just call setEnabled(false) for your top level widget to block user input, and then you can use QApplication::processEvents without any flags.

iTextSharp difference between implicit explicit NewPage

I use the onStartPage event handler to write a header, works great, but I need to know whether I issued a NewPage() or it was issued due to a page overflow. Is there an elegant way to tell?
Thanks in advance for any help!
You've written a page event implementation, and you've implemented one or more of its methods. You create an instance of this event like this:
MyPageEvent event = new MyPageEvent();
writer.setPageEvent(event);
Whenever the onStartPage() is called, you want to know if it was called from within iText or from your code using the newPage() method. As iText uses the same newPage() method internally, you'll have to use a trick.
Add a memberVariable to your page event application. Something like:
protected boolean myNewPage = false;
Now add this method to your event:
public void newPage(Document document) {
myNewPage = true;
document.newPage();
myNewPage = false;
}
Now whenever you want to trigger a new page, don't use:
document.newPage();
Use this instead:
event.newPage(document);
The onStartPage() method will be called internally for every new page that is initialized, and at that moment, the value of myNewPage will be true whenever the newPage() was triggered by yourself; otherwise it will be false.
I hope this helps; I didn't test it, I'm just telling you what I would try.
(PS: I'm the original developer of iText.)

Handle Window close event

I'm trying to handle the event when the close button of a Window is clicked:
// View Code
#Override
public void attachWindowListener(WindowListener listener) {
window.addWindowListener(listener);
}
// Presenter code
view.attachWindowListener(new WindowListener(){
public void windowHide(WindowEvent we) {
GWT.log("Window Event - Processing fields");
processFields();
}
});
However, the windowHide function seems to be not executed since I can't see the log I placed there.
How to properly handle that event?
How about
Window.addCloseHandler(
new CloseHandler<Window>()
{
public void onClose( CloseEvent<Window> windowCloseEvent )
{
// Do your worst here
}
} );
I usually put this in onModuleLoad() in my EntryPoint class.
Cheers,
Based on the information provided I would guess that either a.) the events you think are firing do not fire for the Window component (even if it seems like they should) or b.) the events are firing but in a different order than you expect.
For example, it's possible that a BrowserEvent or some other event is firing first as the window is being closed and the Window object's WindowEvent never fires. According to the API docs for GXT 2.x, the WindowEvent will fire on hide and deactivate but it does not specify that it fires on close. The GXT 3.0.x API doc is less clear on this point but I would assume the same behavior. Unfortunately Sencha does not provide good documentation on what events fire for a given component and in what order.
With that said, I have had some luck working through similar issues to this by using a debug class which outputs all the events on a component to which it is attached. This may shed some light on which events are firing and their order of execution, and you may find an optimal event to which you can attach your processFields() method.
For a good example of a debugger class, see this answer from a related post: https://stackoverflow.com/a/2891746/460638. It also includes an example of how to attach the debugger to your component.
API Doc for Window, GXT 2.x: http://dev.sencha.com/deploy/gxt-2.2.5/docs/api/com/extjs/gxt/ui/client/widget/Window.html
API Doc for Window, GXT 3.0.x: http://dev.sencha.com/deploy/gxt-3.0.0/javadoc/gxt/com/sencha/gxt/widget/core/client/Window.html
This worked:
window.addListener(Events.Hide, new Listener<ComponentEvent>() {
#Override
public void handleEvent(ComponentEvent be) {
// Do stuff
}
});

GWT CellList Error: onSelectionChange, index out of bounds

I'm writing a messaging application in GWT, and have a fairly difficult problem to find a solution for. I'm working with a GWT CellList. In my cell list I'm displaying all the recent contacts a user has had recent communication with. But lets say that a user is writing a message to a person not on that list. I temporarily add them to the recentContacts list, and update the CellList so that it shows..
But then let's say that they end up not sending the message. I need to be able to detect that, and remove them from the list. The obvious place to do that is in the selection change handler. It actually turns out though that within a selection change handler, if can modify the list of data objects that represent the cell list, but when you actually push them to the cell list, I get an index out of bounds error.
I've verified that this is the issue. So basically I'm stuck. The obvious place to check this is when your selecting a different contact to view. I can then check if any messages were sent to this other contact, and if not, get rid of the contact, but I need to somehow not do it in the selectionChangeHandler. Does anyone have any solution/ideas? I tried a mouse up event, but that ends up happening before the selection event takes place.
Thanks for any help in advance :-)
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
ContactDO selectedContact = selectionModel.getSelectedObject();
//Check if we want to remove a contact from the list
if ( we want to remove a contact in the list that is not the currently selected contact. ) {
//remove contact
recentContacts.remove(contactThatisNotSelected);
//Refresh the contact cell list
contactCellList.setVisibleRange(0, recentContacts.size());
contactCellList.setRowCount(recentContacts.size(), true);
contactCellList.setRowData(0, recentContacts);
}
}
});
The solution that I implemented was just to use a Timer, and then do the work about 100ms later. Not really a fan of this solution. I'm still looking for another.