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