Re: Which pattern for Settings/Preferences/Options scenario?




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

.



Relevant Pages

  • Problem with custom Panel class
    ... I'm extending the Panel control to allow selection of controls on the Panel ... selection rectangle is drawn behind the controls on the Panel. ... private void OnMouseDown ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Problem with custom Panel class
    ... I'm extending the Panel control to allow selection of controls on the Panel ... selection rectangle is drawn behind the controls on the Panel. ... private void OnMouseDown ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Asp.net Dropdownlist selected index in changed
    ... i am having three dropdownlists when i am selected first item from list ... related to second item selection. ... dropdown list item text i will be display result. ... private void BindSubSectionDropDownlist() ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: JList not behaving properly
    ... PanelB contains the JList which has the Listener. ... Now when the listener is invoked for the first time, the proper panel ... Then I click again on the JList to display my first panel. ... there is NO selection in the JList. ...
    (comp.lang.java.gui)
  • Re: Introducing CBM-Command
    ... and that brings up the list of drives. ... either the left panel or right panel (depending on which menu was ... So there's no white> pointing at the current selection to the left of ... The function keys are not going to be used. ...
    (comp.sys.cbm)