001    /**
002     * Java Gui Builder - A library to build GUIs using an XML file.
003     * Copyright 2002, 2003 (C) François Beausoleil
004     *
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     *
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018     */
019    
020    package jgb.handlers.swing;
021    
022    import org.xml.sax.SAXException;
023    
024    import javax.swing.*;
025    import java.awt.*;
026    import java.lang.reflect.Field;
027    import java.lang.reflect.Method;
028    import java.util.Map;
029    
030    public class PanelTagHandler extends AbstractControlTagHandler {
031        /**
032         * The name of the layout attribute.
033         */
034        protected static final String ATTR_LAYOUT = "layout";
035    
036        /**
037         * The name of the rows attribute.
038         */
039        protected static final String ATTR_ROWS = "rows";
040    
041        /**
042         * The name of the columns attribute.
043         */
044        protected static final String ATTR_COLUMNS = "columns";
045    
046        /**
047         * The name of the hgap attribute.
048         */
049        protected static final String ATTR_HGAP = "hgap";
050    
051        /**
052         * The name of the vgap attribute.
053         */
054        protected static final String ATTR_VGAP = "vgap";
055    
056        /**
057         * The name of the alignment attribute.
058         */
059        protected static final String ATTR_ALIGNMENT = "alignment";
060    
061        protected void enterElement(Map atts) throws SAXException {
062            final JPanel panel = new JPanel();
063            final String layoutClassName = (String)atts.get(ATTR_LAYOUT);
064            if (layoutClassName != null) {
065                try {
066                    final Class layoutClass = Class.forName(layoutClassName);
067                    final LayoutManager layoutManager = (LayoutManager)layoutClass.newInstance();
068    
069                    panel.setLayout(layoutManager);
070                } catch (ClassNotFoundException e) {
071                    try {
072                        final Class layoutClass = Class.forName("java.awt." + layoutClassName);
073                        final LayoutManager layoutManager = (LayoutManager)layoutClass.newInstance();
074    
075                        panel.setLayout(layoutManager);
076                    } catch (ClassNotFoundException e1) {
077                        throwParsingException("Could not find layout manager named " + layoutClassName, e1);
078                    } catch (Exception e1) {
079                        throwParsingException(e1);
080                    }
081                } catch (Exception e) {
082                    throwParsingException(e);
083                }
084            }
085    
086            final LayoutManager layoutManager = panel.getLayout();
087            setLayoutManagerProperty(layoutManager, ATTR_ROWS, atts);
088            setLayoutManagerProperty(layoutManager, ATTR_COLUMNS, atts);
089            setLayoutManagerProperty(layoutManager, ATTR_HGAP, atts);
090            setLayoutManagerProperty(layoutManager, ATTR_VGAP, atts);
091            setLayoutManagerProperty(layoutManager, ATTR_ALIGNMENT, atts);
092    
093            if (atts.containsKey(ATTR_REFLABEL)) {
094                JLabel referrent = (JLabel)getObject(atts.get(ATTR_REFLABEL).toString());
095                referrent.setLabelFor(panel);
096            }
097    
098            pushCurrentControl(panel, (String)atts.get(ATTR_ID));
099            if (atts.containsKey(ATTR_ID)) {
100                putObject((String)atts.get(ATTR_ID), panel);
101            }
102        }
103    
104        protected void exitElement() throws SAXException {
105            popCurrentControl();
106        }
107    
108        protected String getDefaultPackagePrefix() {
109            throw new IllegalStateException();
110        }
111    
112        private void setLayoutManagerProperty(final LayoutManager layoutManager, final String attrKey, Map atts) throws SAXException {
113            if (atts.containsKey(attrKey)) {
114                final String methodName = "set" + Character.toUpperCase(attrKey.charAt(0)) + attrKey.substring(1);
115                try {
116                    Method setRowsMethod = layoutManager.getClass().getMethod(methodName, new Class[]{Integer.TYPE});
117                    final String attrValue = atts.get(attrKey).toString();
118                    Integer integerValue = null;
119                    try {
120                        integerValue = Integer.valueOf(attrValue);
121                    } catch (NumberFormatException e) {
122                        Field constantValue = layoutManager.getClass().getField(attrValue.toUpperCase());
123                        integerValue = new Integer(constantValue.getInt(null));
124                    }
125    
126                    setRowsMethod.invoke(layoutManager, new Object[]{
127                        integerValue
128                    });
129                } catch (Exception e) {
130                    throwParsingException("Could not set number of " + attrKey
131                            + " - LayoutManager is probably not of the right class - "
132                            + layoutManager.getClass().getName(), e);
133                }
134            }
135        }
136    }