How to know what action is triggering an event? - java

How do I know what action is triggering a particular event?
org.w3c.dom.events.EventListener refreshAnnotationsListener = new org.w3c.dom.events.EventListener() {
#Override
public void handleEvent(org.w3c.dom.events.Event event) {
// how do I know the action which triggered this event?
}
};
I guess there is a way to print stack trace using "throw" which also displays the name of the action that is triggering an event. Am not exactly sure how to do this though. Or, is there another way of doing it ?
Thanks,
Sony

You can create a stacktrace any time you want, by creating an exception:
new Exception().printStackTrace();
You don't have to throw it.

Related

LibGDX logging not showing up in logcat

I cannot get LibGDX logging to work in Android Studio. First i thought I had the same problem as my previous question but made sure my app updated on debug.
playButton.addListener(new ChangeListener()
{
#Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.app.debug("BUTTON", "playButton Pressed");
optionButton.addAction(Actions.moveBy(-200, 0, 2));
}
});
The test action on the option button is carried out but i cannot get the debug log to show up.
The default Log level is LOG_INFO. For the Gdx.app.debug call to work, you must first call Gdx.app.setLogLevel(Application.LOG_DEBUG); once (probably the first line in your Game's constructor so you can easily change it).
Use Gdx.app.log or even System.out.println (write sout (syso in eclipse) and enter );

How to clear libgdx listeners

So in menu i have 3 buttons and all are with the same code. For the topic only important thing is
button2.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
game.setGame();
dispose();
return true;
}
});
Then everything is going fine until:
button3.clearListeners(); // MainMenu.java:174 line in exeption
and then exeption pops out:
Exception in thread "LWJGL Application" java.lang.IllegalStateException: Invalid between begin/end.
at com.badlogic.gdx.utils.DelayedRemovalArray.clear(DelayedRemovalArray.java:125)
at com.badlogic.gdx.scenes.scene2d.Actor.clearListeners(Actor.java:261)
at com.racostyle.avdelux.MainMenu.dispose(MainMenu.java:174)
at com.racostyle.avdelux.MainMenu$3.touchDown(MainMenu.java:123)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:55)
at com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:165)
at com.badlogic.gdx.scenes.scene2d.Actor.fire(Actor.java:136)
at com.badlogic.gdx.scenes.scene2d.Stage.touchDown(Stage.java:277)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:300)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:200)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
EDIT: if i don't remove listeners they are still active and can be clickable, afc with exeptions.
It looks like you're calling MainMenu.dispose within an actor.touchdown listener. MainMenu.dispose clears all listeners on a button. LIBGX won't allow that.
Why? Because LIBGDX is in the process of calling touch events on a lot of actors. You really don't want your game functioning different depending on the order buttons were added to your stage.
So, call clear listeners somewhere else.
Since you're doing this within a dispose method anyway, I'd suggest NOT removing your button listeners at all, as the actor you've registered listeners for should not be used again.
Its not allowed to just call clearListeners() without event;
Will you post your codes in jsfiddle.. i will try to debug it and add some codes..
Thanks ..

How to limit ModifyListener for user interaction only

I have a textbox with attached ModifyListener.
In implemented modifyText(ModifyEvent e) I execute desired functionality.
The problem with that, that this event is triggered on every text change.
I don't want it to trigger if text was altered programmaticly (by setting text via code).
I want it to trigger only when user changes the code (I can't use keylistener because it will be triggered also when user click on arrow buttons and etc, it also won't detect if user copy&paste text)
You could unregister your ModifyListener before calling setText(..) and reregister it afterwards.
How about textBox.addKeyListener(...) and textBox.addMouseListener(...) instead of ModifyListener?
You can try using Focusout listener.... then you will get the value which user has entered only once.
Text text;
text.addListener(SWT.FocusOut, new Listener() {
#Override
public void handleEvent(Event arg0) {
//Your code here.....
}
});

How to stop user from generating more events?

I am developing a java swing desktop application. The dialog form has an ok and cancel button. When the user clicks ok button the application does some processing. How can I stop user from clicking ok again before the event on ok button has finished executing. Also, i dont want the user to able to press the cancel button till ok has finished executed. Any help will be highly appreciated.
Enablement management is an integral part of UI logic. Action helps you doing so:
Action action = new AbstractAction("myAction") {
public void actionPerformed(ActionEvent e) {
setEnabled(false);
doPerform();
setEnabled(true);
}
};
button.setAction(action);
Beware: long running task must not be executed on the EDT, so this is for short-term only, to prevent the second or so click having an effect
Edit
just noticed that you tagged the question with jsr296, then it's even easier: you can tag a method of your presentation model as #Action and bind its enabled property to a property of the model
#Action (enabledProperty == "idle")
public void processOk() {
setIdle(false);
doStuff;
setIdle(true);
}
Plus there is support (much debated, but useable) for Tasks: basically SwingWorker with fine-grained beanified life-cycle support
If you want to disable all the controls, then I'd suggest using a GlassPane. See here for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html
The easiest way is to disable the button just after clicking it , and when the action performed is complete then re-enable it.
You can disable your button like yourButton.setEnabled(false); during your processing method and enable it again when it finishes.
It seems that what you want is something like a waiting cursor.

Any workarounds to lack of PreSelection events in SWT/JFace?

In my application I want the user to save any changes before he leaves a tab (implemented as CTabFolder).
I tried to handle SelectionEvent, but it fires after the tab has been changed (so why does it even have a doit field? Does it fire before change for some other controls?)
Looking on Bugzilla, I've found https://bugs.eclipse.org/bugs/show_bug.cgi?id=193453 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=193064, neither of which is fixed.
Since this requirement is probably common, does anybody have a workaround?
I have a workaround that works with org.eclipse.ui.part.MultiPageEditorPart which is backed by a CTabFolder. I'll adapt it for a straight CTabFolder implementation.
First off use the selection listener:
tabFolder.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
pageChange(tabFolder.indexOf((CTabItem) e.item));
}
});
Then I implement pageChange() like this:
protected void pageChange(int newPageIndex) {
boolean changingPages = this.changingPages;
this.changingPages = true;
int oldPageIndex = tabFolder.getSelectionIndex();
if (isDirty() && !changingPages) {
tabFolder.setSelection(oldPageIndex);
if (canChangePages()) {
tabFolder.setSelection(newPageIndex);
}
}
this.changingPages = false;
}
In canChangePages() I pop up a do you want to save dialog and give the user an opportunity to select yes, no, or cancel. Yes saves the info and returns true. No reverts the info to the last saved state and returns true. Cancel simply returns false. You may simply want to try saving and return false only if the save fails.
It may look weird that I switch back to the old page before calling canChangePages(). This call executes quickly so it gives the illusion the tab never switched. No matter how long canChangePages() takes the user will not see a tab change unless it is approved by that method.

Categories