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



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


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?



Maybe this is where some confusion is creeping in. I think we maybe talking about two different models here. Swing uses a form of MVC. For example a JComboBox has a ComboBoxModel and a ComboBoxUI. The ComboBoxModel is the model and the ComboboxUI is the view. The JComboBox is the controller. It is what keeps the view and the model in sync. The ComboBoxModel is not modeling anything in the underlying application model. It may may contain the same data as the underlying model but it is not modeling anything but a combo box. One might say that this is a micro MVC implementation since it is focused on one UI component. It is just the UI component that is being modeled, viewed and controlled. When we implement the MVC pattern we assemble a group of components into a user interface to present a view of some model object that is representative of some entity, such as an invoice, an oscilloscope or an ICU ward. When a user performs some action intended to either change the model or the user interface, the user interface component then receives that user action (mouse click, text entry, etc.) as an event. It forwards the event to the registered listeners. Ideally, the listeners will invoke a method on some controller object, that controller would either simply send a message to other components in the user interface based on logic relevant to the particular message it received or it would update the model if appropriate and then send messages to the relevant UI components.

Sometimes its desirable to remember several states that the UI was previously in or even to provide the user with a set of default settings. In such cases it seems to make sense to have a UserInetrfaceModel an object which, like a ComboBoxModel keeps track of the state of the entire UI. By doing so a user can easily choose between groups of settings by choosing a particular instance of an UserInterfaceModel. When a user initiates an action which is intended set the UI to some previous state, the listener which receives the event will send a message to the controller to replace its UserInterfaceModel with the new one. The controller will send the appropriate messages to the other UI components to update to the new values and if desired or necessary update the model.

Similarly, If the user chooses to change the underlying model document, a message is sent to the controller to load a new document and create the new model. The controller then notifies the UI to update to the new data.

The whole point is the separation of the entire user interface from the underlying model. If you keep the presentation logic separated from the model logic you won't have to distinguish between user initiated and programmatically initiated events. No doubt presentation logic can get quite complex and it would be nice to an additional method or two to simplify things a bit, but thats what extension and composition are for.

OK I wrote way to much to point that there are models and then again there are models. Sometimes we are talking at the application level and sometimes we are talking at the UI component level.

Now for the secondary issue, as I recall (its been a while since I did this) when programatically changing UI components to stop the cyclic event firing it is a good idea to get the model object from the component and set it such as JComboBox().getModel().setSelectedItem(). The JComboBox updates and no ActionEvent is fired. I'm not sure whether an ItemEvent is generated.


Glen Ezkovich
HardBop Consulting


A Proverb for Paranoids:
"If they can get you asking the wrong questions, they don't have to worry about answers."
- Thomas Pynchon Gravity's Rainbow
_______________________________________________
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.