The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

ramirogm (1) [Avatar] Offline
#1
Hi
This is my first post in the forum. I'd like to say that I like your book very much and find it really useful, practical, clearly written and full of enthusiastic content and ideas. Thanks and good work!

I'm now re-reading some parts and currently working with chapter 4 code. I'm playing with the "model notifying changes" ideas.

First, I notice that this functionality is not used in the sample code, but provided as an example of a good practice, as it says at the top of page 97. As a suggestion, I think the "real MVC Model" you provide is an interesting idea and it would be useful to see a more complex example using it.

Debugging the code I found that as it's coded, when the user changes a field, the event mechanism redundantly overwrites the field with the sane value. Let's say the user enters a new userName, this triggers a change event on the username ChangeListener, which in turns calls user.setUsername(). This one triggers a change notification that goes back to the username PropertyChangeListener ( "listeners[2]" ), which calls username.setText(). So when the user enters a value in the TextField, it goes to the model and back to the TextField. By looking at the example I couldn't tell why this is necessary. I'd like to know your ideas about this, and if you had some particular use in mind.

Thinking about this, it ocurred to me that this could be useful if the model somehows "processes" the setUsername() parameter. So I came up with a very crude example to try out this model-firing-events functionality:

public void setFirstName(String firstName) {
// User can enter firstname.lastname and completes two fields at once
// Added to test event firing behavior
int dotIndx = firstName.indexOf('.');
if ( dotIndx >= 0) {
setLastName(firstName.substring(dotIndx+1));
firstName = firstName.substring(0, dotIndx);
}
String old = this.firstName;
this.firstName = firstName;
changes.firePropertyChange("firstName", old, firstName);
}



Trying this out, I found there's a bug in User both in the sample code and in p87
public void setLastName(String lastName) {
String old = lastName;
this.lastName = lastName;
changes.firePropertyChange("lastName", old, lastName);
}

it should read

public void setLastName(String lastName) {
String old = this.lastName; // note previous lastName
this.lastName = lastName;
changes.firePropertyChange("lastName", old, lastName);
}

Also on UserEdit where it says
listeners[1] = new PropertyChangeListener(){
public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
firstName.setText( (String) propertyChangeEvent.getNewValue() );
}
};

it should say

listeners[1] = new PropertyChangeListener(){
public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
lastName.setText( (String) propertyChangeEvent.getNewValue() ); // lastName
}
};


This boilerplate code sure is copy-paste error prone smilie
Fixing the two typos above, the code works.

I'd like to know your opinions on this

Thanks,
Ramiro

Message was edited by:
ramirogm