xref: /trunk/main/wizards/com/sun/star/wizards/common/ConfigGroup.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package com.sun.star.wizards.common;
28 
29 import java.lang.reflect.Field;
30 
31 /**
32  *
33  * @author  rpiterman
34  */
35 public class ConfigGroup implements ConfigNode
36 {
37 
38     public Object root;
39 
40     public void writeConfiguration(Object configurationView, Object param)
41     {
42         Field[] fields = getClass().getFields();
43         for (int i = 0; i < fields.length; i++)
44         {
45             if (fields[i].getName().startsWith((String) param))
46             {
47                 try
48                 {
49                     writeField(fields[i], configurationView, (String) param);
50                 }
51                 catch (Exception ex)
52                 {
53                     System.out.println("Error writing field: " + fields[i].getName());
54                     ex.printStackTrace();
55                 }
56             }
57         }
58     }
59 
60     private void writeField(Field field, Object configView, String prefix) throws Exception
61     {
62         String propertyName = field.getName().substring(prefix.length());
63         //System.out.println("Going to save:" + propertyName);
64         Class fieldType = field.getType();
65         if (ConfigNode.class.isAssignableFrom(fieldType))
66         {
67             Object childView = Configuration.addConfigNode(configView, propertyName);
68             ConfigNode child = (ConfigNode) field.get(this);
69             child.writeConfiguration(childView, prefix);
70         }
71         else if (fieldType.isPrimitive())
72         {
73             Configuration.set(convertValue(field), propertyName, configView);
74         }
75         else if (fieldType.equals(String.class))
76         {
77             Configuration.set(field.get(this), propertyName, configView);
78         }
79     }
80 
81     /**
82      * convert the primitive type value of the
83      * given Field object to the corresponding
84      * Java Object value.
85      * @param field
86      * @return the value of the field as a Object.
87      * @throws IllegalAccessException
88      */
89     public Object convertValue(Field field) throws IllegalAccessException
90     {
91         if (field.getType().equals(Boolean.TYPE))
92         {
93             return (field.getBoolean(this) ? Boolean.TRUE : Boolean.FALSE);
94         }
95         if (field.getType().equals(Integer.TYPE))
96         {
97             return new Integer(field.getInt(this));
98         }
99         if (field.getType().equals(Short.TYPE))
100         {
101             return new Short(field.getShort(this));
102         }
103         if (field.getType().equals(Float.TYPE))
104         {
105             return new Double(field.getFloat(this));
106         }
107         if (field.getType().equals(Double.TYPE))
108         {
109             return new Double(field.getDouble(this));
110         }
111         //System.out.println("ohoh...");
112         return null; //and good luck with it :-) ...
113     }
114 
115     public void readConfiguration(Object configurationView, Object param)
116     {
117         Field[] fields = getClass().getFields();
118         for (int i = 0; i < fields.length; i++)
119         {
120             if (fields[i].getName().startsWith((String) param))
121             {
122                 try
123                 {
124                     readField(fields[i], configurationView, (String) param);
125                 }
126                 catch (Exception ex)
127                 {
128                     System.out.println("Error reading field: " + fields[i].getName());
129                     ex.printStackTrace();
130                 }
131             }
132         }
133     }
134 
135     private void readField(Field field, Object configView, String prefix) throws Exception
136     {
137         String propertyName = field.getName().substring(prefix.length());
138 
139         Class fieldType = field.getType();
140         if (ConfigNode.class.isAssignableFrom(fieldType))
141         {
142             ConfigNode child = (ConfigNode) field.get(this);
143             child.setRoot(root);
144             child.readConfiguration(Configuration.getNode(propertyName, configView), prefix);
145         }
146         else if (fieldType.isPrimitive())
147         {
148             if (fieldType.equals(Boolean.TYPE))
149             {
150                 field.setBoolean(this, Configuration.getBoolean(propertyName, configView));
151             }
152             else if (fieldType.equals(Integer.TYPE))
153             {
154                 field.setInt(this, Configuration.getInt(propertyName, configView));
155             }
156             else if (fieldType.equals(Short.TYPE))
157             {
158                 field.setShort(this, Configuration.getShort(propertyName, configView));
159             }
160             else if (fieldType.equals(Float.TYPE))
161             {
162                 field.setFloat(this, Configuration.getFloat(propertyName, configView));
163             }
164             else if (fieldType.equals(Double.TYPE))
165             {
166                 field.setDouble(this, Configuration.getDouble(propertyName, configView));
167             }
168         }
169         else if (fieldType.equals(String.class))
170         {
171             field.set(this, Configuration.getString(propertyName, configView));
172         }
173     }
174 
175     public void setRoot(Object newRoot)
176     {
177         root = newRoot;
178     }
179 
180     /* (non-Javadoc)
181      * @see com.sun.star.wizards.common.ConfigNode#writeConfiguration(java.lang.Object, java.lang.Object)
182      */
183 }
184