Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to distinguish between user selection and programatic selection with ListSelectionListener



I can't believe this thread is still lingering, but maybe it'll help if I explain the approach I've settled on after constructing many different data-editing UIs.

1) have a data object. It's best if all of the various bits of data can be gathered into a single object, though that mostly helps conceptually.

2) do NOT have actionListeners do anything other than set the related component's values ( maybe checking for correctness, i.e. limiting or reverting the possible state ). They should do as little as possible- using them to mark the document as modified, as Scott suggests, means that even if you set everything *back* to it's original state, your document is modified. Write as few action/focus listeners as possible. Most importantly, in most data-editors, you don't want them to modify the original data object. Even in places where they have to do complex calculations, have the results stored either in UI components or some separate variable associated with that component ( it doesn't have to be in a separate model object, but if you have a component like this you probably should at least subclass ).

3) have a single method which compares the contents/state of the UI widgets to check to see if it's different from the ( unchanged ) data object. For large editors with lots of data, this is a somewhat ugly bit of code with lots of 'if ( myComponent.getState()!=data.somefield ) { isChanged=true }' business, but it's simple, not involving special models or lots of listener objects.

4) changing a bit of actual 'document data' via the UI should not by itself start any intensive processing ( at most it should be a quick check to see if the new state is valid ) if at all possible. There should be a "save" or "calculate" button which does that. This is a usability measure as much as a measure to save programming effort. Sometimes, like when a number field edit causes other numbers to adjust, you have to break this, but then see the last part of (2).

5) on window close, 'save', or document-selection change, you check the "is data changed" method and take the appropriate action.

It's not elegant in that I'm not creating a full-on model for a view, and my code often ends up being a 'view is controller' model, where the 'view' extends JPanel or JFrame. But I'm writing a minimum of action/focus listeners, and I've never had to handle 'user' actions differently from 'programatic' actions, probably because the effect of any action is pretty limited.

It would probably make more sense to use the more elegant ( and less UI-library-specific ) solution of creating a 'DataModel' with lots of set/get methods, a 'DataView' object ( Panel or Frame ) with a bunch of set/get methods, and a 'Controller' object which on creation sets the view from the data, then carefully controls actions on the 'save' and 'calculate'-variety button presses. In the long run it'd definitely be easier to debug and maintain. But when I'm writing the code, that seems like a whole lot of extra get/set methods, and I'm not going to switch out the graphic library I'm using, and typically I don't have multiple editors for the same data object. Where taking the trouble to do this would really make sense is if you were going to have several different-looking view objects for the same data object, or a data model which handles varieties of related objects... but even then, you'd likely be writing more controller objects anyway.

The short answer, though, is that needing to know the difference between 'user-initiated' and 'program-initiated' changes to a UI object's state should raise a red flag that your UI object is doing something a little 'too much' in it's action method, or doing something ( as in the original case that started this thread ) which should be handled differently, most likely elsewhere in the code. JTextComponent.setText() does not fire an action method, and neither do most programatic component-setting methods, so if you're in a situation where you're firing an unwanted action by setting up a UI component, you *know* you're doing something special. Scott-- we need the specific case if we're going to hear about this any further. The post that started this thread was about starting a timer thread in a method which caused the *same* method to be called ( he'd set up a recursion ) and was clearly a case where he'd confused 'selection' for 'mouse event'. Either you have the same confusion somewhere, or you have a specific issue with a specific type of component or method which I'd like for you to explain in a bit more concrete detail than needing to 'initialize the GUI'.

On Feb 5, 2004, at 6:57 PM, Scott Palmer wrote:

On Feb 5, 2004, G.G. wrote:

The model itself should manage whether the document is modified or not.

This is entirely unreasonable. You are effectively saying that I must replace the default model of almost every single GUI widget in my application

Your views, all of them, should set or query that state from the model.

The default views of Swing components already do this, don't they?

If you have a document model, then you should be able to represent a new
document with that model. Or a modified one, or whatever else the
conceptual model needs.

What does that have to do with what I wrote above?

Maybe I'm missing something, but I still can't see why, with a proper
model/view relationship, you have to make the distinction between
user-initiated and program-initiated events. I *CAN* understand why this
would be convenient if one doesn't have the proper model/view relationship.

Ideally my document would be the model for many of the GUI components. But implementing things that way would be a lot more work than simply using the default models for swing controls and differentiating between initializing the GUI from a newly loaded document and user events intended to modify the document.

But adding a feature to correct avoidable design problems strikes me as a
fundamentally bad idea.

I agree - but it depends on how easily avoidable the design problems are. I still haven't seen a reasonable suggestion as to how to avoid the 'design problem'.

Since this discussion is now entirely theoretical,

Hardly, it relates directly to almost every swing application I have written.

maybe a good example
where the feature's necessity is obvious would be worth seeing. I admit I
can't think of one, so may be someone else can.

If you load a document/project and need to initialize the GUI without the Swing components calling back to listeners that end up marking the document as modified or triggering intensive processing unless you have additional logic in the listeners to ignore the 'programmatic changes' to the model..

Scott
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: How to distinguish between user selection and programatic selection with ListSelectionListener (From: "John St. Ledger" <email@hidden>)
 >Re: How to distinguish between user selection and programatic selection with ListSelectionListener (From: Scott Palmer <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.