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

it.tidalwave.swing.PropertyManager Maven / Gradle / Ivy

There is a newer version: 2.13.61
Show newest version
/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2011 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.kenai.com
 * SCM: https://kenai.com/hg/openbluesky~src
 *
 **********************************************************************************************************************/
package it.tidalwave.swing;

import java.awt.Color;
import java.awt.Component;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.ResourceBundle;
import javax.swing.JComponent;
import javax.swing.UIManager;
import org.openide.util.NbBundle;

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public class PropertyManager 
  {
    public static void configure (final JComponent component, final String prefix)
      {
        final Class clazz = component.getClass();
        final ResourceBundle bundle = NbBundle.getBundle(clazz);
        
        for (final Enumeration e = bundle.getKeys(); e.hasMoreElements(); )
          {
            final String key = e.nextElement();
            
            if (key.startsWith(prefix + "."))
              {
                final String[] temp = key.substring(prefix.length() + 1).split("[.]");
                
                if (temp.length == 2)
                  {
                    final String beanName = temp[0].trim();
                    final String property = temp[1].trim();
                    final String valueKey = bundle.getString(key).trim();
                    Object value = UIManager.get(valueKey);
                   
                    if (value == null)
                      {
                        try
                          {
                            value = Color.decode(valueKey);
                          }
                        catch (Exception ex)
                          {
                          }
                      }
                    
//                    if (value == null)
//                      {
//                        value = Font.decode(valueKey);  
//                      }
                    
                    System.err.println("LAF: " + beanName + "." + property + " = " + value);
                    final JComponent bean = findComponent(component, beanName);
                    
                    if (bean == null)
                      {
                        System.err.println("NO bean named: " + beanName);  
                      }
                    
                    else if (value == null)
                      {
                        System.err.println("NO value named: " + valueKey);  
                      }
                    
                    else
                      {
                        for (Class class2 = value.getClass(); class2 != null; class2 = class2.getSuperclass())
                          {
                            try
                              {
                                final Method setter = bean.getClass().getMethod("set" + capitalized(property), class2);
                                setter.invoke(bean, value);
                                break;
                              }
                            catch (Exception ex)
                              {
                                System.err.println(ex);
    //                            ex.printStackTrace();    
                              }
                          }
                      }
                  }
              }
          }
      }
    
    private static JComponent findComponent (final JComponent container, final String name)
      {
        if (name.equals(container.getName()))
          {
            return container;
          }
        
        for (final Component child : container.getComponents())
          {
            if (child instanceof JComponent)
              {
                final JComponent result = findComponent((JComponent)child, name); 
                
                if (result != null)
                  {
                    return result;  
                  }
              }
          }
        
        return null;
      }
    
    private static String capitalized (final String string)
      {
        return string.substring(0, 1).toUpperCase() + string.substring(1);    
      }
  }   




© 2015 - 2024 Weber Informatics LLC | Privacy Policy