1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.wizards.ui.event;
24 
25 import com.sun.star.awt.XItemListener;
26 import com.sun.star.awt.XRadioButton;
27 import com.sun.star.uno.UnoRuntime;
28 
29 /**
30  * @author rpiterman
31  *
32  * To change the template for this generated type comment go to
33  * Window>Preferences>Java>Code Generation>Code and Comments
34  */
35 public class RadioDataAware extends DataAware
36 {
37 
38     protected XRadioButton[] radioButtons;
39 
RadioDataAware(Object data, Value value, Object[] radioButs)40     public RadioDataAware(Object data, Value value, Object[] radioButs)
41     {
42         super(data, value);
43         radioButtons = new XRadioButton[radioButs.length];
44         for (int i = 0; i < radioButs.length; i++)
45         {
46             radioButtons[i] = UnoRuntime.queryInterface(XRadioButton.class, radioButs[i]);
47         }
48     }
49 
50     /* (non-Javadoc)
51      * @see com.sun.star.wizards.ui.DataAware#setToUI(java.lang.Object)
52      */
setToUI(Object value)53     protected void setToUI(Object value)
54     {
55         int selected = ((Number) value).intValue();
56         if (selected == -1)
57         {
58             for (int i = 0; i < radioButtons.length; i++)
59             {
60                 radioButtons[i].setState(false);
61             }
62         }
63         else
64         {
65             radioButtons[selected].setState(true);
66         }
67     }
68 
69     /* (non-Javadoc)
70      * @see com.sun.star.wizards.ui.DataAware#getFromUI()
71      */
getFromUI()72     protected Object getFromUI()
73     {
74         for (int i = 0; i < radioButtons.length; i++)
75         {
76             if (radioButtons[i].getState())
77             {
78                 return new Integer(i);
79             }
80         }
81         return new Integer(-1);
82     }
83 
attachRadioButtons(Object data, String dataProp, Object[] buttons, final Listener listener, boolean field)84     public static DataAware attachRadioButtons(Object data, String dataProp, Object[] buttons, final Listener listener, boolean field)
85     {
86         final RadioDataAware da = new RadioDataAware(data,
87                 field
88                 ? DataAwareFields.getFieldValueFor(data, dataProp, 0)
89                 : new DataAware.PropertyValue(dataProp, data), buttons);
90         XItemListener xil = UnoDataAware.itemListener(da, listener);
91         for (int i = 0; i < da.radioButtons.length; i++)
92         {
93             da.radioButtons[i].addItemListener(xil);
94         }
95         return da;
96     }
97 }
98