001 /* JAPI - (Yet another (hopefully) useful) Java API 002 * 003 * Copyright (C) 2004-2006 Christian Hujer 004 * 005 * This program is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU General Public License as 007 * published by the Free Software Foundation; either version 2 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 * 02111-1307, USA. 019 */ 020 021 package net.sf.japi.swing; 022 023 import static java.awt.GridBagConstraints.REMAINDER; 024 import java.beans.BeanInfo; 025 import java.beans.IntrospectionException; 026 import java.beans.Introspector; 027 import java.beans.PropertyDescriptor; 028 import java.beans.PropertyEditor; 029 import java.beans.PropertyEditorManager; 030 import java.awt.Component; 031 import java.awt.GridBagLayout; 032 import java.awt.GridBagConstraints; 033 import java.util.HashMap; 034 import java.util.Map; 035 import javax.swing.JComponent; 036 import javax.swing.JLabel; 037 import javax.swing.JCheckBox; 038 import javax.swing.JTextField; 039 040 /** User Interface component for editing properties. 041 * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a> 042 * @deprecated don't use this class yet, it's under development and everything is subject of change. 043 */ 044 @Deprecated public class JPropertyEditor extends JComponent { 045 046 private static final Map<Class<?>, Class<? extends Component>> EDITORS = new HashMap<Class<?>, Class<? extends Component>>(); 047 048 static { 049 EDITORS.put(Boolean.TYPE, JCheckBox.class); // TODO 050 EDITORS.put(String.class, JTextField.class); // TODO 051 } 052 053 public JPropertyEditor(final Object object) throws IntrospectionException { 054 this(object, Object.class); 055 } 056 057 public JPropertyEditor(final Object object, final Class<?> stopClass) throws IntrospectionException { 058 setLayout(new GridBagLayout()); 059 final GridBagConstraints labelGbc = new GridBagConstraints(); 060 final GridBagConstraints componentGbc = new GridBagConstraints(); 061 componentGbc.gridwidth = REMAINDER; 062 labelGbc.fill = GridBagConstraints.HORIZONTAL; 063 componentGbc.fill = GridBagConstraints.HORIZONTAL; 064 final BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass(), stopClass); 065 final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 066 for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { 067 final String propertyName = propertyDescriptor.getDisplayName(); 068 final Class<?> propertyType = propertyDescriptor.getPropertyType(); 069 PropertyEditor propertyEditor = propertyDescriptor.createPropertyEditor(object); 070 if (propertyEditor == null) { 071 propertyEditor = PropertyEditorManager.findEditor(propertyType); 072 } 073 if (propertyEditor != null) { 074 final Component editor; 075 if (propertyEditor.supportsCustomEditor()) { 076 editor = propertyEditor.getCustomEditor(); 077 } else { 078 editor = createEditor(propertyType); 079 } 080 if (editor != null) { 081 //noinspection ObjectAllocationInLoop 082 add(new JLabel(propertyName), labelGbc); 083 add(editor, componentGbc); 084 } 085 } 086 } 087 } 088 089 /** Create an editor for a property. 090 * @param propertyType property type 091 */ 092 @SuppressWarnings({"ReturnOfNull"}) 093 private static Component createEditor(final Class<?> propertyType) { 094 // TODO: Attach property editor 095 try { 096 return EDITORS.get(propertyType).newInstance(); 097 } catch (final InstantiationException e) { 098 return null; 099 } catch (IllegalAccessException e) { 100 return null; 101 } 102 } 103 104 } // class JPropertyEditor