Results 1 to 6 of 6
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Abusing SwingPropertyChangeSupport

    I'm thinking about misusing SwingPropertyChangeSupport#firePropertyChange(Stri ng propertyName, Object oldValue, Object newValue) to notify my GUI of the status of plugins. The proper thing to do would be to associate a different property name with each plugin class. But I'm planning to use the property name "pluginLoaded", the plugin Class for the oldValue, and a Boolean indicating its loaded status for the newValue.

    How many different ways is this going to blow up on me?
    Last edited by kjkrum; 06-24-2012 at 11:29 PM.
    Get in the habit of using standard Java naming conventions!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Abusing SwingPropertyChangeSupport

    Best would be to look at the source for this class and of course the PropertyChangeSupport class on which it is based, something I haven't done. Having said my concern is that my understanding of how this class works is that the support won't notify the listeners if the newValue isn't changed for that property, so this could potentially mess you up since your property is really being indicated by the oldValue, not the propertyName. I don't know if the listener compares the new with the old value objects or if it does something else, so this might not be a problem.

    Also if it does work for you, I fear what might happen when someone else (or your future self, 6 months hence) tries to modify or debug your code and doesn't understand what's going on. You'll document this heavily of course, but still it might take a bit to fully understand just what you did months or years ago.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Abusing SwingPropertyChangeSupport

    The javadoc for that method states that "No event is fired if old and new [parameters] are equal and non-null." Of course this does not imply that an event will be fired if they are not equal. But the only reason I can see for the old value being passed as a parameter is to eliminate the need for the PropertyChangeSupport to maintain an internal record of property values.

    I'm documenting my misuse of the API in the javadoc for my delegate of the addPropertyChangeListener(...) method. But you're right, I still might forget what I did. Too bad there's not something like SwingPropertyChangeSupport that uses Objects as property keys. (Or is there?)
    Get in the habit of using standard Java naming conventions!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Abusing SwingPropertyChangeSupport

    It could be the fact that I didn't have my shot of espresso yet, but I don't see where the 'abuse' part comes in ... as long as the old and new objects are different, an event is fired and that's exactly what you want ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Abusing SwingPropertyChangeSupport

    The abuse is that the "old value" is not the old value at all, it's information about the event source.

    I looked at the source of PropertyChangeSupport, and indeed, it does not maintain any history of property values. The only test of whether a value has changed is between the two values you pass to the firePropertyChange(...) methods.
    Get in the habit of using standard Java naming conventions!

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Abusing SwingPropertyChangeSupport

    Quote Originally Posted by kjkrum View Post
    The abuse is that the "old value" is not the old value at all, it's information about the event source.

    I looked at the source of PropertyChangeSupport, and indeed, it does not maintain any history of property values. The only test of whether a value has changed is between the two values you pass to the firePropertyChange(...) methods.
    Those 'oldValue' and 'newValue' parameters names are just names; if they were called 'arg0' and 'arg1', would you have even thought about abuse? The only programmatic restriction is that an event is not fired if both the arguments are equal.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •