1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.examples.tutorial.viewset;
17
18 import net.sf.jguiraffe.gui.builder.components.model.AbstractRadioButtonHandler;
19
20 /**
21 * <p>
22 * A specialized {@code ComponentHandler} implementation for a radio button
23 * group with only two elements.
24 * </p>
25 * <p>
26 * This handler implementation is used for the radio button group which defines
27 * the sort direction. It can be either <em>ascending</em> or
28 * <em>descending</em>. For demonstration purposes we map these values to a
29 * boolean flag (<b>true</b> means descending, <b>false</b> means ascending). It
30 * would also be possible to use an enumeration type instead. This class mainly
31 * demonstrates how a custom component handler for radio buttons can be created.
32 * </p>
33 *
34 * @author Oliver Heger
35 * @version $Id: BooleanRadioButtonHandler.java 205 2012-01-29 18:29:57Z oheger $
36 */
37 public class BooleanRadioButtonHandler extends
38 AbstractRadioButtonHandler<Boolean>
39 {
40 /** Constant for the index of the DESCENDING button. */
41 private static final int IDX_DESC = 1;
42
43 /** Constant for the index of the ASCENDING button. */
44 private static final int IDX_ASC = 0;
45
46 /**
47 * Creates a new instance of {@code BooleanRadioButtonHandler}
48 */
49 public BooleanRadioButtonHandler()
50 {
51 super(Boolean.TYPE);
52 }
53
54 /**
55 * Returns the index of the button with the specified value. This method
56 * maps data values of the radio group to specific radio buttons. Because in
57 * our example <b>true</b> represents the <em>descending</em> button we
58 * return the corresponding index.
59 *
60 * @param value the data value of the radio group
61 * @return the index of the selected radio button
62 */
63 @Override
64 protected int getButtonIndex(Boolean value)
65 {
66 assert value != null : "Null value passed in!";
67 return value.booleanValue() ? IDX_DESC : IDX_ASC;
68 }
69
70 /**
71 * Returns the value of the radio button with the specified index. This
72 * method determines the data value for the whole radio group based on the
73 * selected radio button. Here we return <b>true</b> if and only if the
74 * <em>descending</em> radio button is selected.
75 *
76 * @param idx the index of the selected radio button
77 * @return the corresponding value for the whole radio group
78 */
79 @Override
80 protected Boolean getDataForButton(int idx)
81 {
82 return idx == IDX_DESC;
83 }
84 }