1*a1b4a26bSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*a1b4a26bSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*a1b4a26bSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*a1b4a26bSAndrew Rist * distributed with this work for additional information 6*a1b4a26bSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*a1b4a26bSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*a1b4a26bSAndrew Rist * "License"); you may not use this file except in compliance 9*a1b4a26bSAndrew Rist * with the License. You may obtain a copy of the License at 10*a1b4a26bSAndrew Rist * 11*a1b4a26bSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*a1b4a26bSAndrew Rist * 13*a1b4a26bSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*a1b4a26bSAndrew Rist * software distributed under the License is distributed on an 15*a1b4a26bSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*a1b4a26bSAndrew Rist * KIND, either express or implied. See the License for the 17*a1b4a26bSAndrew Rist * specific language governing permissions and limitations 18*a1b4a26bSAndrew Rist * under the License. 19*a1b4a26bSAndrew Rist * 20*a1b4a26bSAndrew Rist *************************************************************/ 21*a1b4a26bSAndrew Rist 22*a1b4a26bSAndrew Rist 23cdf0e10cSrcweir package com.sun.star.wizards.common; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import java.lang.reflect.Field; 26cdf0e10cSrcweir 27cdf0e10cSrcweir /** 28cdf0e10cSrcweir * 29cdf0e10cSrcweir * @author rpiterman 30cdf0e10cSrcweir */ 31cdf0e10cSrcweir public class ConfigGroup implements ConfigNode 32cdf0e10cSrcweir { 33cdf0e10cSrcweir 34cdf0e10cSrcweir public Object root; 35cdf0e10cSrcweir writeConfiguration(Object configurationView, Object param)36cdf0e10cSrcweir public void writeConfiguration(Object configurationView, Object param) 37cdf0e10cSrcweir { 38cdf0e10cSrcweir Field[] fields = getClass().getFields(); 39cdf0e10cSrcweir for (int i = 0; i < fields.length; i++) 40cdf0e10cSrcweir { 41cdf0e10cSrcweir if (fields[i].getName().startsWith((String) param)) 42cdf0e10cSrcweir { 43cdf0e10cSrcweir try 44cdf0e10cSrcweir { 45cdf0e10cSrcweir writeField(fields[i], configurationView, (String) param); 46cdf0e10cSrcweir } 47cdf0e10cSrcweir catch (Exception ex) 48cdf0e10cSrcweir { 49cdf0e10cSrcweir System.out.println("Error writing field: " + fields[i].getName()); 50cdf0e10cSrcweir ex.printStackTrace(); 51cdf0e10cSrcweir } 52cdf0e10cSrcweir } 53cdf0e10cSrcweir } 54cdf0e10cSrcweir } 55cdf0e10cSrcweir writeField(Field field, Object configView, String prefix)56cdf0e10cSrcweir private void writeField(Field field, Object configView, String prefix) throws Exception 57cdf0e10cSrcweir { 58cdf0e10cSrcweir String propertyName = field.getName().substring(prefix.length()); 59cdf0e10cSrcweir //System.out.println("Going to save:" + propertyName); 60cdf0e10cSrcweir Class fieldType = field.getType(); 61cdf0e10cSrcweir if (ConfigNode.class.isAssignableFrom(fieldType)) 62cdf0e10cSrcweir { 63cdf0e10cSrcweir Object childView = Configuration.addConfigNode(configView, propertyName); 64cdf0e10cSrcweir ConfigNode child = (ConfigNode) field.get(this); 65cdf0e10cSrcweir child.writeConfiguration(childView, prefix); 66cdf0e10cSrcweir } 67cdf0e10cSrcweir else if (fieldType.isPrimitive()) 68cdf0e10cSrcweir { 69cdf0e10cSrcweir Configuration.set(convertValue(field), propertyName, configView); 70cdf0e10cSrcweir } 71cdf0e10cSrcweir else if (fieldType.equals(String.class)) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir Configuration.set(field.get(this), propertyName, configView); 74cdf0e10cSrcweir } 75cdf0e10cSrcweir } 76cdf0e10cSrcweir 77cdf0e10cSrcweir /** 78cdf0e10cSrcweir * convert the primitive type value of the 79cdf0e10cSrcweir * given Field object to the corresponding 80cdf0e10cSrcweir * Java Object value. 81cdf0e10cSrcweir * @param field 82cdf0e10cSrcweir * @return the value of the field as a Object. 83cdf0e10cSrcweir * @throws IllegalAccessException 84cdf0e10cSrcweir */ convertValue(Field field)85cdf0e10cSrcweir public Object convertValue(Field field) throws IllegalAccessException 86cdf0e10cSrcweir { 87cdf0e10cSrcweir if (field.getType().equals(Boolean.TYPE)) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir return (field.getBoolean(this) ? Boolean.TRUE : Boolean.FALSE); 90cdf0e10cSrcweir } 91cdf0e10cSrcweir if (field.getType().equals(Integer.TYPE)) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir return new Integer(field.getInt(this)); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir if (field.getType().equals(Short.TYPE)) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir return new Short(field.getShort(this)); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir if (field.getType().equals(Float.TYPE)) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir return new Double(field.getFloat(this)); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir if (field.getType().equals(Double.TYPE)) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir return new Double(field.getDouble(this)); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir //System.out.println("ohoh..."); 108cdf0e10cSrcweir return null; //and good luck with it :-) ... 109cdf0e10cSrcweir } 110cdf0e10cSrcweir readConfiguration(Object configurationView, Object param)111cdf0e10cSrcweir public void readConfiguration(Object configurationView, Object param) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir Field[] fields = getClass().getFields(); 114cdf0e10cSrcweir for (int i = 0; i < fields.length; i++) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir if (fields[i].getName().startsWith((String) param)) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir try 119cdf0e10cSrcweir { 120cdf0e10cSrcweir readField(fields[i], configurationView, (String) param); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir catch (Exception ex) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir System.out.println("Error reading field: " + fields[i].getName()); 125cdf0e10cSrcweir ex.printStackTrace(); 126cdf0e10cSrcweir } 127cdf0e10cSrcweir } 128cdf0e10cSrcweir } 129cdf0e10cSrcweir } 130cdf0e10cSrcweir readField(Field field, Object configView, String prefix)131cdf0e10cSrcweir private void readField(Field field, Object configView, String prefix) throws Exception 132cdf0e10cSrcweir { 133cdf0e10cSrcweir String propertyName = field.getName().substring(prefix.length()); 134cdf0e10cSrcweir 135cdf0e10cSrcweir Class fieldType = field.getType(); 136cdf0e10cSrcweir if (ConfigNode.class.isAssignableFrom(fieldType)) 137cdf0e10cSrcweir { 138cdf0e10cSrcweir ConfigNode child = (ConfigNode) field.get(this); 139cdf0e10cSrcweir child.setRoot(root); 140cdf0e10cSrcweir child.readConfiguration(Configuration.getNode(propertyName, configView), prefix); 141cdf0e10cSrcweir } 142cdf0e10cSrcweir else if (fieldType.isPrimitive()) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir if (fieldType.equals(Boolean.TYPE)) 145cdf0e10cSrcweir { 146cdf0e10cSrcweir field.setBoolean(this, Configuration.getBoolean(propertyName, configView)); 147cdf0e10cSrcweir } 148cdf0e10cSrcweir else if (fieldType.equals(Integer.TYPE)) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir field.setInt(this, Configuration.getInt(propertyName, configView)); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir else if (fieldType.equals(Short.TYPE)) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir field.setShort(this, Configuration.getShort(propertyName, configView)); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir else if (fieldType.equals(Float.TYPE)) 157cdf0e10cSrcweir { 158cdf0e10cSrcweir field.setFloat(this, Configuration.getFloat(propertyName, configView)); 159cdf0e10cSrcweir } 160cdf0e10cSrcweir else if (fieldType.equals(Double.TYPE)) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir field.setDouble(this, Configuration.getDouble(propertyName, configView)); 163cdf0e10cSrcweir } 164cdf0e10cSrcweir } 165cdf0e10cSrcweir else if (fieldType.equals(String.class)) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir field.set(this, Configuration.getString(propertyName, configView)); 168cdf0e10cSrcweir } 169cdf0e10cSrcweir } 170cdf0e10cSrcweir setRoot(Object newRoot)171cdf0e10cSrcweir public void setRoot(Object newRoot) 172cdf0e10cSrcweir { 173cdf0e10cSrcweir root = newRoot; 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir /* (non-Javadoc) 177cdf0e10cSrcweir * @see com.sun.star.wizards.common.ConfigNode#writeConfiguration(java.lang.Object, java.lang.Object) 178cdf0e10cSrcweir */ 179cdf0e10cSrcweir } 180