This might be ‘user’ error but it took me some time to figure this out. This is one of the ‘rare’ times I miss Java Swing but I’m thankfully I have a ‘tiny’ glimpse of what is behind the Vaadin curtain.
Here is the code that was failing and look at Line item #3 below:
[code]
_updateStates.addClickListener(new ClickListener() {
StringBuilder selectedStates = new StringBuilder();
@Override
public void buttonClick(ClickEvent event) {
@SuppressWarnings("unchecked")
Set<String> selections = (Set<String>) _states.getValue();
Object states = selections.toArray();
LOG.info("TwinCol getValue list=" + selections);
final int size = selections.size();
final int nextToLast = size - 1;
for (int i = 0; i < size; i++) {
selectedStates.append(states[i]
);
if (i < nextToLast) {
selectedStates.append(", ");
}
}
LOG.info("selected list=" + selectedStates.toString());
_parentView._states.setImmediate(true);
_parentView._states.setReadOnly(false);
_parentView._states.setValue(selectedStates.toString());
_parentView._states.setReadOnly(true);
}
});
[/i]
[/code]I have a TwinColumn list of states where the State name is shown and after selection I put the State Postal code in a read-only TextField.
So during testing when I logged this I got the following:
2015-06-30 10:41:56 INFO StatesWindow.buttonClick:[109]
- TwinCol getValue list=[AZ, AR]
2015-06-30 10:41:56 INFO StatesWindow.buttonClick:[121]
- selected list=AZ, AR
2015-06-30 10:42:00 INFO StatesWindow.buttonClick:[109] - TwinCol getValue list=
2015-06-30 10:42:00 INFO StatesWindow.buttonClick:[121] - selected list=AZ, AR
Notice how the StringBuilder even though it is ‘newed’ is GHOSTING content!?!?! How does it STILL have data? The only thing I can think of is ClickListener and ClickEvent BOTH have a copy and since the ‘inner’ version is null my ‘outer’ version falls through? Not sure and I don’t have time to debug it but I did want to share this diamon in the rough.
The fix is simply MOVING where StringBuilder is constructed! See below and notice the last log item below is NULL as compared to the last log item shown above.
[code]
_updateStates.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
@SuppressWarnings("unchecked")
Set<String> selections = (Set<String>) _states.getValue();
Object[] states = selections.toArray();
LOG.info("TwinCol getValue list=" + selections);
StringBuilder selectedStates = new StringBuilder();
final int size = selections.size();
final int nextToLast = size - 1;
for (int i = 0; i < size; i++) {
selectedStates.append(states[i]
);
if (i < nextToLast) {
selectedStates.append(", ");
}
}
LOG.info("selected list=" + selectedStates.toString());
_parentView._states.setImmediate(true);
_parentView._states.setReadOnly(false);
_parentView._states.setValue(selectedStates.toString());
_parentView._states.setReadOnly(true);
}
});
[/code]That little change now gives me the following:
2015-06-30 10:46:57 INFO StatesWindow.buttonClick:[107]
- TwinCol getValue list=[AZ, AR]
2015-06-30 10:46:57 INFO StatesWindow.buttonClick:[120]
- selected list=AZ, AR
2015-06-30 10:47:04 INFO StatesWindow.buttonClick:[107] - TwinCol getValue list=
2015-06-30 10:47:04 INFO StatesWindow.buttonClick:[120] - selected list=
The test case was selecting two states, adding to the list and then selecting NULL to clear them out.
Notice by moving the StringBuilder local data variable it changes EVERYTHING.
Bug? Feature?
You tell me, but I know which one I am sticking with so I can meet a code deadline.
Cheers