All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.netbeans.util.EnhancedNbPreferences Maven / Gradle / Ivy

There is a newer version: 2.13.61
Show newest version
/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://openbluesky.java.net
 * SCM: https://bitbucket.org/tidalwave/openbluesky-src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.util;

import java.lang.reflect.Method;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.openide.util.NbPreferences;

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public final class EnhancedNbPreferences
  {
    private static final String CLASS = EnhancedNbPreferences.class.getName();
    
    private static final Logger logger = Logger.getLogger(CLASS);
    
    private EnhancedNbPreferences()
      {
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    public static Preferences forModule (Class clazz) // FIXME: can be dropped
      {
        return NbPreferences.forModule(clazz);
      }
     
    /***************************************************************************
     *
     *
     **************************************************************************/
    public static Preferences root()  // FIXME: can be dropped
      {
        return NbPreferences.root(); 
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    public static void bind (final Object bean, 
                             final String propertyName, 
                             final Class  clazz, 
                             final String preferenceName, 
                             final Object defaultValue)
      {
        final Preferences preferences = forModule(clazz);
        Object value = null;
        
        if (defaultValue instanceof String)
          {
            value = preferences.get(preferenceName, (String)defaultValue);
          }

        else if (defaultValue instanceof Integer)
          {
            value = preferences.getInt(preferenceName, (Integer)defaultValue);
          }

        else if (defaultValue instanceof Double)
          {
            value = preferences.getDouble(preferenceName, (Double)defaultValue);
          }

        else if (defaultValue instanceof Boolean)
          {
            value = preferences.getBoolean(preferenceName, (Boolean)defaultValue);
          }
 
        try
          {
            String methodName = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
            Method getter;
            
            try 
              {
                getter = bean.getClass().getMethod("get" + methodName);
              }  
            catch (NoSuchMethodException e) 
              {
                getter = bean.getClass().getMethod("is" + methodName);
              }
            
            Method setter = bean.getClass().getMethod("set" + methodName, getter.getReturnType());
            setter.invoke(bean, value);
          }
        catch (Exception e)
          {
            System.err.println("XXX " + value + "  //// " + value.getClass());
            throw new RuntimeException(e);  
          }
                    
        if (bean instanceof JComponent)
          {
            ((JComponent)bean).addPropertyChangeListener(propertyName, new PropertyChangeListener() 
              {
                public void propertyChange (PropertyChangeEvent event) 
                  {
                    try 
                      {
                        Object newValue = event.getNewValue();
                        logger.info("Storing preference " + clazz + "." + preferenceName + " = " + newValue);

                        if (newValue instanceof String)
                          {
                            preferences.put(preferenceName, (String)newValue);
                          }

                        else if (newValue instanceof Integer)
                          {
                            preferences.putInt(preferenceName, (Integer)newValue);
                          }

                        else if (newValue instanceof Double)
                          {
                            preferences.putDouble(preferenceName, (Double)newValue);
                          }

                        else if (newValue instanceof Boolean)
                          {
                            preferences.putBoolean(preferenceName, (Boolean)newValue);
                          }

                        preferences.flush();
                      } 
                    catch (BackingStoreException e)
                      {
                        e.printStackTrace(); // FIXME
                      }
                  } 
              }); 
          }
        
        else if (bean instanceof SpinnerModel)
          {
            if (!"value".equals(propertyName))
              {
                throw new IllegalArgumentException("Property must be 'value'");
              }
            
            ((SpinnerModel)bean).addChangeListener(new ChangeListener() 
              {
                public void stateChanged (ChangeEvent event) 
                  {
                    try
                      {
                        preferences.putLong(preferenceName, (Integer)((SpinnerModel)bean).getValue());
                        preferences.flush();
                      } 
                    catch (BackingStoreException e)
                      {
                        e.printStackTrace(); // FIXME
                      }
                  }
              });
          }
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    public static void bind (final JTextField textField, 
                             final String propertyName, 
                             final Class clazz, 
                             final String preferenceName, 
                             final String defaultValue)
      {
        if (!"text".equals(propertyName))
          {
            throw new IllegalArgumentException("Property must be 'text'");
          }
            
        final Preferences preferences = forModule(clazz);
        final String text = preferences.get(preferenceName, defaultValue);
        textField.setText(text);
        textField.getDocument().addDocumentListener(new DocumentListener() 
          {
            public void changedUpdate (DocumentEvent e) 
              {
                update();
              }
            
            public void insertUpdate (DocumentEvent e) 
              {
                update();
              }
            
            public void removeUpdate (DocumentEvent e) 
              {
                update();
              }
            
            private void update()
              {
                try 
                  {
                    preferences.put(preferenceName, textField.getText());
                    preferences.flush();
                  } 
                catch (BackingStoreException e)
                  {
                    e.printStackTrace(); // FIXME
                  }
              }
          });
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy