Re: Which pattern for Settings/Preferences/Options scenario?
- From: "Ed" <iamfractal@xxxxxxxxxxx>
- Date: 23 Oct 2006 08:32:08 -0700
tailorma_de skrev:
Snipski.
How do you implement something like the Settings of an application?
interested in a pattern that applies or dismisses many changes at once.
It doesn't have to be the settings. I generally mean things where the
user has a (modal) dialog with an Accept/Apply button and a
Cancel/Dismiss button where more than one value may be changed.
Thanks,
Robert
Well, first and foremost, you need an option abstraction. Once you have
that, you're almost home. "Option," is - surprise, surprise - a good
name for your abstract class. For a rant about why this is needed (and
to see how some options for a web application are implemented (slightly
different from a GUI application, as the web-page sends back all
options, changed or not)), see:
http://www.edmundkirwan.com/servlet/fractal/cs1/frac-cs110.html
You're options will may, for example, be either binary or not: for the
former, a CheckBox is a useful implementation, for the latter, a
RadioButton is good for multi-choice.
So, if a DialogBox is to be populated with all the system options,
first a list of all available options is retrieved, then the program
will iterate through each one and call addOption() (I'm sure this
indenting will be mangled by the time it hits the newsgroups, so you
may want to cut-'n'-paste to an editor):
/**
* Adds the given option's alternatives to the given panel.
*
* @param panel panel on which option will be displayed
* @param option option whose alternatives will be displayed
*/
private void addOption(JPanel panel, Option option) {
/* If this is a binary option, use a check button, else use a
* radio button */
if (option.isBinaryOption()) {
panel.add(getCheckChoices(option));
} else {
panel.add(getRadioChoices(option));
}
}
For each time the DialogBox is shown, a Map is created to hold the
user's choice as he goes about clicking the options he'd like:
/** Holds mapping of an selection option to the selected
alternative */
private final Map optionToAlternative = new HashMap();
So this answers one of your question: you do have to cache the user's
choices and then activate them all when he presses, "OK."
In the getCheckChoices() method, an actionListener is created so that
each time the user clicks an option, the program records what option
and what alternative he chose:
/**
* Returns the choices for an option whose alternatives are to be
* presented as a check-button selection.
*
* @param option option whose alternatives will be displayed
* @return namespace text display choices
*/
private JPanel getCheckChoices(final Option option) {
JPanel panel = new JPanel();
// Arrange the items vertically
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(getPanelPadding());
final String alternativeName = option.getAlternativeGroupName();
final JCheckBox button = new JCheckBox(alternativeName);
button.setActionCommand(alternativeName);
if (option.isSelected(Boolean.toString(true))) {
button.setSelected(true);
}
// Register a listener
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String value = Boolean.toString(false);
if (button.isSelected()) {
value = Boolean.toString(true);
}
optionToAlternative.put(option, value);
}
});
panel.add(button);
return panel;
}
Then when the user clicks, "OK," all the options recorded as altered
can be scanned through and activated:
/**
* Processes all the users selections.
*/
private void processSelections() {
boolean isPresentationCritical = false;
boolean isProcessingCritical = false;
// Process all generic option selections
for (Iterator iter = optionToAlternative.keySet().iterator();
iter.hasNext();) {
Option option = (Option)iter.next();
String alternativeName = (String)optionToAlternative.get(option);
option.setSelectedAlternative(alternativeName);
if (option.isPresentationCritical()) {
isPresentationCritical = true;
}
if (option.isProcessingCritical()) {
isProcessingCritical = true;
}
}
}
..ed
--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Download Fractality, free Java code analyzer:
www.EdmundKirwan.com/servlet/fractal/frac-page130.html
.
- Follow-Ups:
- Re: Which pattern for Settings/Preferences/Options scenario?
- From: tailorma_de
- Re: Which pattern for Settings/Preferences/Options scenario?
- References:
- Which pattern for Settings/Preferences/Options scenario?
- From: tailorma_de
- Which pattern for Settings/Preferences/Options scenario?
- Prev by Date: Re: HOM: Higher Order Messaging
- Next by Date: Re: Decorator Pattern
- Previous by thread: Which pattern for Settings/Preferences/Options scenario?
- Next by thread: Re: Which pattern for Settings/Preferences/Options scenario?
- Index(es):
Relevant Pages
|