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.common;
24 
25 import java.lang.reflect.Field;
26 
27 /**
28  *
29  * @author  rpiterman
30  */
31 public class ConfigGroup implements ConfigNode
32 {
33 
34     public Object root;
35 
writeConfiguration(Object configurationView, Object param)36     public void writeConfiguration(Object configurationView, Object param)
37     {
38         Field[] fields = getClass().getFields();
39         for (int i = 0; i < fields.length; i++)
40         {
41             if (fields[i].getName().startsWith((String) param))
42             {
43                 try
44                 {
45                     writeField(fields[i], configurationView, (String) param);
46                 }
47                 catch (Exception ex)
48                 {
49                     System.out.println("Error writing field: " + fields[i].getName());
50                     ex.printStackTrace();
51                 }
52             }
53         }
54     }
55 
writeField(Field field, Object configView, String prefix)56     private void writeField(Field field, Object configView, String prefix) throws Exception
57     {
58         String propertyName = field.getName().substring(prefix.length());
59         //System.out.println("Going to save:" + propertyName);
60         Class fieldType = field.getType();
61         if (ConfigNode.class.isAssignableFrom(fieldType))
62         {
63             Object childView = Configuration.addConfigNode(configView, propertyName);
64             ConfigNode child = (ConfigNode) field.get(this);
65             child.writeConfiguration(childView, prefix);
66         }
67         else if (fieldType.isPrimitive())
68         {
69             Configuration.set(convertValue(field), propertyName, configView);
70         }
71         else if (fieldType.equals(String.class))
72         {
73             Configuration.set(field.get(this), propertyName, configView);
74         }
75     }
76 
77     /**
78      * convert the primitive type value of the
79      * given Field object to the corresponding
80      * Java Object value.
81      * @param field
82      * @return the value of the field as a Object.
83      * @throws IllegalAccessException
84      */
convertValue(Field field)85     public Object convertValue(Field field) throws IllegalAccessException
86     {
87         if (field.getType().equals(Boolean.TYPE))
88         {
89             return (field.getBoolean(this) ? Boolean.TRUE : Boolean.FALSE);
90         }
91         if (field.getType().equals(Integer.TYPE))
92         {
93             return new Integer(field.getInt(this));
94         }
95         if (field.getType().equals(Short.TYPE))
96         {
97             return new Short(field.getShort(this));
98         }
99         if (field.getType().equals(Float.TYPE))
100         {
101             return new Double(field.getFloat(this));
102         }
103         if (field.getType().equals(Double.TYPE))
104         {
105             return new Double(field.getDouble(this));
106         }
107         //System.out.println("ohoh...");
108         return null; //and good luck with it :-) ...
109     }
110 
readConfiguration(Object configurationView, Object param)111     public void readConfiguration(Object configurationView, Object param)
112     {
113         Field[] fields = getClass().getFields();
114         for (int i = 0; i < fields.length; i++)
115         {
116             if (fields[i].getName().startsWith((String) param))
117             {
118                 try
119                 {
120                     readField(fields[i], configurationView, (String) param);
121                 }
122                 catch (Exception ex)
123                 {
124                     System.out.println("Error reading field: " + fields[i].getName());
125                     ex.printStackTrace();
126                 }
127             }
128         }
129     }
130 
readField(Field field, Object configView, String prefix)131     private void readField(Field field, Object configView, String prefix) throws Exception
132     {
133         String propertyName = field.getName().substring(prefix.length());
134 
135         Class fieldType = field.getType();
136         if (ConfigNode.class.isAssignableFrom(fieldType))
137         {
138             ConfigNode child = (ConfigNode) field.get(this);
139             child.setRoot(root);
140             child.readConfiguration(Configuration.getNode(propertyName, configView), prefix);
141         }
142         else if (fieldType.isPrimitive())
143         {
144             if (fieldType.equals(Boolean.TYPE))
145             {
146                 field.setBoolean(this, Configuration.getBoolean(propertyName, configView));
147             }
148             else if (fieldType.equals(Integer.TYPE))
149             {
150                 field.setInt(this, Configuration.getInt(propertyName, configView));
151             }
152             else if (fieldType.equals(Short.TYPE))
153             {
154                 field.setShort(this, Configuration.getShort(propertyName, configView));
155             }
156             else if (fieldType.equals(Float.TYPE))
157             {
158                 field.setFloat(this, Configuration.getFloat(propertyName, configView));
159             }
160             else if (fieldType.equals(Double.TYPE))
161             {
162                 field.setDouble(this, Configuration.getDouble(propertyName, configView));
163             }
164         }
165         else if (fieldType.equals(String.class))
166         {
167             field.set(this, Configuration.getString(propertyName, configView));
168         }
169     }
170 
setRoot(Object newRoot)171     public void setRoot(Object newRoot)
172     {
173         root = newRoot;
174     }
175 
176     /* (non-Javadoc)
177      * @see com.sun.star.wizards.common.ConfigNode#writeConfiguration(java.lang.Object, java.lang.Object)
178      */
179 }
180