« GListener interface » : différence entre les versions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
(3 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 1 : | Ligne 1 : | ||
== before() and after() methods == | |||
To manage actions on widgets, a single interface is available: [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GListener.html GListener]. Then, automatically, you must implement these both methods : | To manage actions on widgets, a single interface is available: [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GListener.html GListener]. Then, automatically, you must implement these both methods : | ||
Ligne 24 : | Ligne 24 : | ||
Imagine that we initialized the real value to "1.0", then enter "2.0" at the keyboard. After pushing on the "Enter" key, we will obtain on the console ... | Imagine that we initialized the real value to "1.0", then enter "2.0" at the keyboard. After pushing on the "Enter" key, we will obtain on the console ... | ||
Old value was 1.0 | |||
Old value was 1.0 | New value is 2.0 | ||
New value is 2.0 | |||
== How to know which widget has been selected ? == | |||
Moreover, we see on the previous example that, these both methods will have as an input argument a [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GEvent.html GEvent] objet that will correspond to the event occured. Thus, this objet will contain all the information linked to this event. | Moreover, we see on the previous example that, these both methods will have as an input argument a [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GEvent.html GEvent] objet that will correspond to the event occured. Thus, this objet will contain all the information linked to this event. | ||
Ligne 55 : | Ligne 53 : | ||
But sometimes, we just want to recover the activated object itself without testing all the possibilities ! It can simply done using the <font color=#4169E1>getLocalSource()</font> method : it will return the selected widget known "locally", meaning existing at the current [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GPanel.html GPanel] level. | But sometimes, we just want to recover the activated object itself without testing all the possibilities ! It can simply done using the <font color=#4169E1>getLocalSource()</font> method : it will return the selected widget known "locally", meaning existing at the current [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GPanel.html GPanel] level. | ||
But, imagine that we are inside a [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GPanel.html|GPanel]] level '''P0''', that includes two other [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GPanel.html GPanel] | But, imagine that we are inside a [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GPanel.html|GPanel]] level '''P0''', that includes two other [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GPanel.html GPanel] '''P1''' and '''P2''' with '''P1''' including two buttons, '''B1''' and '''B2''' ... | ||
<syntaxhighlight> | <syntaxhighlight lang="java"> | ||
P0 => P1 => B1 | P0 => P1 => B1 | ||
=> B2 | => B2 | ||
Ligne 79 : | Ligne 77 : | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Example == | |||
In the following example, we will show: | In the following example, we will show: | ||
# how to discriminate which widget has been activated (using the <font color=#4169E1>contains()</font> method) | |||
# how to use the <font color=#4169E1>before()</font> and <font color=#4169E1>after()</font> methods to manage the interdiction to enter negative values for an integer | |||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> |
Dernière version du 10 juillet 2017 à 07:54
before() and after() methods
To manage actions on widgets, a single interface is available: GListener. Then, automatically, you must implement these both methods :
- before()
- after()
Indeed, the GListener interface allows to manage notion as after (something like a call-back) but also before, in a more friendly way than what is proposed by swing with an automatic download/upload management of the pile as described in the diagram just below.
We will go inside the after() method after any action with the left button of the mouse or the "Enter" key of the keyboard.
The before() action may be a bit more difficult to understand. Of course, there is now //before()// action for a button widget (even if the GListener interface implies to get it). It is more clear for a GEntryInt widget as described with the example below: we will enter in the before() method just before the value of the widget will be modified (as after() will be activated after the value will be changed).
public void before(GEvent e) throws GException {
System.out.println("Old value was "+real.getValue());
}
public void before(GEvent e) throws GException {
System.out.println("New value is "+real.getValue());
}
Imagine that we initialized the real value to "1.0", then enter "2.0" at the keyboard. After pushing on the "Enter" key, we will obtain on the console ...
Old value was 1.0 New value is 2.0
How to know which widget has been selected ?
Moreover, we see on the previous example that, these both methods will have as an input argument a GEvent objet that will correspond to the event occured. Thus, this objet will contain all the information linked to this event.
In order to implement correctly the before()/after() methods, we must also know how to discriminate which widget has been activated. Indeed, in case of a GPanel , containing several widgets, it is necessary to know it, as the action to do will surely depend on the selected widget. So, to do it, GENIUS gives us the contains() method associated to the GEvent object given by the before()/after() methods. This method needs as input arguments one or several widgets and will return true if one of these widgets have been activated (else false).
private GButton but1;
private GButton but2;
...
public void after(GEvent e) {
if ( e.contains(but1) ) { // Case of we push on the but1 button ...
...
}
if ( e.contains(but1, but2) ) { // Case of we push on the but1 button or the but2 button ...
...
}
}
But sometimes, we just want to recover the activated object itself without testing all the possibilities ! It can simply done using the getLocalSource() method : it will return the selected widget known "locally", meaning existing at the current GPanel level.
But, imagine that we are inside a [1]] level P0, that includes two other GPanel P1 and P2 with P1 including two buttons, B1 and B2 ...
P0 => P1 => B1
=> B2
=> P2
If we push on the B2 button, it is possible to get the object corresponding to B2 by using the getFinalSource() method that will return it.
So, getLocalSource() will return P1 as getFinalSource() will return B2.
With such a mechanism, it will possible, for example, to discriminate if the action on a GEntryReal widget was coming from the input area or just from the unit menu (as, in that case, the value has not been changed).
public void after(GEvent e) {
if ( e.getFinalSource() instanceof GNumberFieldAbstract ) {
System.out.println("Value changed ...");
}
else {
System.out.println("Only unit change ..."); }
}
Example
In the following example, we will show:
- how to discriminate which widget has been activated (using the contains() method)
- how to use the before() and after() methods to manage the interdiction to enter negative values for an integer
public class myPanel extends GPanel implements GListener {
private GButton but1;
private GButton but2;
private GEntryInt valI;
private int oldInt; // Integer to keep in memory the old value
public myPanel() {
but1 = new GButton("Bouton 1");
but2 = new GButton("Bouton 2");
valI = new GEntryInt("Integer input:", 0);
}
public void generic() throws GException {
put(but1);
put(but2);
put(valI);
}
public void display() throws GException {
generic();
}
public void before(GEvent e) {
if ( e.contains(valI) ) {
// We store the value before to be modified ...
oldInt = valI.getValue();
}
}
public void after(GEvent e) {
if ( e.contains(but1) ) { System.out.println("Click occured on button 1"); }
if ( e.contains(valI) ) {
// Test to keep only positive values
if ( valI.getValue() < 0 ) {
System.out.println("Warning, this value may not be negative ...");
// We recover the old value
valI.setValue(oldInt);
}
}
}