001    package net.sourceforge.mp3dings.preferences;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: BeanPrefModule.java,v 1.5 2006/09/27 19:36:12 ringler Exp $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.awt.BorderLayout;
023    import java.lang.reflect.Constructor;
024    import java.util.logging.Level;
025    import java.util.logging.Logger;
026    import java.util.prefs.Preferences;
027    import net.sourceforge.mp3dings.Globals;
028    import net.sourceforge.mp3dings.utils.AbstractBeanCustomizer;
029    import net.sourceforge.mp3dings.utils.Bean;
030    
031    /**
032     * Abstract Preference Dialogue Field.
033     * @author Moritz Ringler
034     * @version $Revision: 1.5 $ ($Date: 2006/09/27 19:36:12 $)
035     */
036    public class BeanPrefModule extends AbstractPrefModule{
037        private static final Logger logger = Logger.getLogger(BeanPrefModule.class.getPackage().getName());
038        final Preferences pref;
039        final String key;
040        private Bean bean;
041    
042    
043    
044        private BeanPrefModule(String name, Preferences pref, String key, Bean bean, Class beanClass){
045            super(name);
046            this.pref = pref;
047            this.key = key;
048            setLayout(new BorderLayout());
049            add(getTitle(),BorderLayout.NORTH);
050            this.bean = (bean == null) ? loadBean(pref, key, beanClass) : bean;
051        }
052    
053    
054        public static Bean loadBean(Preferences pref, String key, Class beanClass){
055            Bean xbean = Bean.load(pref, key);
056            if(xbean != null && beanClass != null){
057                if(!beanClass.equals(xbean.getClass())){
058                    logger.severe("The bean at " + pref.toString() + ":" + key + " is not of type " + beanClass.getName());
059                    logger.severe("Will construct a new bean of the correct type.");
060                    xbean = null;
061                }
062            }
063            if(xbean == null && beanClass != null){
064                try{
065                    xbean = (Bean) beanClass.newInstance();
066                    xbean.save(pref, key, Globals.getDir("BEANS"));
067                } catch (Exception ex){
068                    logger.severe("Error initializing BeanPrefModule");
069                    Globals.logStackTrace(logger, Level.SEVERE, ex);
070                }
071            }
072            return xbean;
073        }
074    
075        private static BeanPrefModule configure(BeanPrefModule bpm){
076            if(bpm.bean == null){
077                bpm = null;
078            } else {
079                Class beanClass = bpm.bean.getClass();
080                String customizerClass = beanClass.getName().replaceFirst("Bean$", "") + "Customizer";
081                try {
082                    Class customizerCl = Class.forName(customizerClass);
083                    Constructor cc = customizerCl.getConstructor(new Class[]{beanClass});
084                    AbstractBeanCustomizer abc = (AbstractBeanCustomizer) cc.newInstance(new Object[]{bpm.bean});
085                    bpm.add(abc.getUI(), BorderLayout.CENTER);
086                } catch (Exception ex){
087                    logger.severe("Error initializing BeanPrefModule");
088                    Globals.logStackTrace(logger, Level.SEVERE, ex);
089                    bpm = null;
090                }
091            }
092            return bpm;
093        }
094    
095        public static BeanPrefModule getInstance(String name, Preferences pref, String key, Bean bean){
096            return configure(new BeanPrefModule(name, pref, key, bean, null));
097        }
098    
099        public static BeanPrefModule getInstance(String name, Preferences pref, String key, Class cl){
100            return configure(new BeanPrefModule(name, pref, key, null, cl));
101        }
102    
103        /** Does nothing **/
104        public void remove(PrefPanel field){
105        }
106    
107        /** Does nothing **/
108        public void addPanel(PrefPanel field){
109        }
110    
111        public void save(){
112            bean.save(pref, key, Globals.getDir("BEANS"));
113        }
114    }