|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PanelTagHandler.java | 100% | 95% | 100% | 96,2% |
|
1 |
/**
|
|
2 |
* Java Gui Builder - A library to build GUIs using an XML file.
|
|
3 |
* Copyright 2002, 2003 (C) François Beausoleil
|
|
4 |
*
|
|
5 |
* This library is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU Lesser General Public
|
|
7 |
* License as published by the Free Software Foundation; either
|
|
8 |
* version 2.1 of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This library is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 |
* Lesser General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU Lesser General Public
|
|
16 |
* License along with this library; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 |
*/
|
|
19 |
|
|
20 |
package jgb.handlers.swing;
|
|
21 |
|
|
22 |
import org.xml.sax.SAXException;
|
|
23 |
|
|
24 |
import javax.swing.*;
|
|
25 |
import java.awt.*;
|
|
26 |
import java.lang.reflect.Field;
|
|
27 |
import java.lang.reflect.Method;
|
|
28 |
import java.util.Map;
|
|
29 |
|
|
30 |
public class PanelTagHandler extends AbstractControlTagHandler { |
|
31 |
/**
|
|
32 |
* The name of the layout attribute.
|
|
33 |
*/
|
|
34 |
protected static final String ATTR_LAYOUT = "layout"; |
|
35 |
|
|
36 |
/**
|
|
37 |
* The name of the rows attribute.
|
|
38 |
*/
|
|
39 |
protected static final String ATTR_ROWS = "rows"; |
|
40 |
|
|
41 |
/**
|
|
42 |
* The name of the columns attribute.
|
|
43 |
*/
|
|
44 |
protected static final String ATTR_COLUMNS = "columns"; |
|
45 |
|
|
46 |
/**
|
|
47 |
* The name of the hgap attribute.
|
|
48 |
*/
|
|
49 |
protected static final String ATTR_HGAP = "hgap"; |
|
50 |
|
|
51 |
/**
|
|
52 |
* The name of the vgap attribute.
|
|
53 |
*/
|
|
54 |
protected static final String ATTR_VGAP = "vgap"; |
|
55 |
|
|
56 |
/**
|
|
57 |
* The name of the alignment attribute.
|
|
58 |
*/
|
|
59 |
protected static final String ATTR_ALIGNMENT = "alignment"; |
|
60 |
|
|
61 | 18 |
protected void enterElement(Map atts) throws SAXException { |
62 | 18 |
final JPanel panel = new JPanel();
|
63 | 18 |
final String layoutClassName = (String)atts.get(ATTR_LAYOUT); |
64 | 18 |
if (layoutClassName != null) { |
65 | 7 |
try {
|
66 | 7 |
final Class layoutClass = Class.forName(layoutClassName); |
67 | 1 |
final LayoutManager layoutManager = (LayoutManager)layoutClass.newInstance(); |
68 |
|
|
69 | 1 |
panel.setLayout(layoutManager); |
70 |
} catch (ClassNotFoundException e) {
|
|
71 | 6 |
try {
|
72 | 6 |
final Class layoutClass = Class.forName("java.awt." + layoutClassName);
|
73 | 5 |
final LayoutManager layoutManager = (LayoutManager)layoutClass.newInstance(); |
74 |
|
|
75 | 5 |
panel.setLayout(layoutManager); |
76 |
} catch (ClassNotFoundException e1) {
|
|
77 | 1 |
throwParsingException("Could not find layout manager named " + layoutClassName, e1);
|
78 |
} catch (Exception e1) {
|
|
79 | 0 |
throwParsingException(e1); |
80 |
} |
|
81 |
} catch (Exception e) {
|
|
82 | 0 |
throwParsingException(e); |
83 |
} |
|
84 |
} |
|
85 |
|
|
86 | 17 |
final LayoutManager layoutManager = panel.getLayout(); |
87 | 17 |
setLayoutManagerProperty(layoutManager, ATTR_ROWS, atts); |
88 | 16 |
setLayoutManagerProperty(layoutManager, ATTR_COLUMNS, atts); |
89 | 16 |
setLayoutManagerProperty(layoutManager, ATTR_HGAP, atts); |
90 | 16 |
setLayoutManagerProperty(layoutManager, ATTR_VGAP, atts); |
91 | 16 |
setLayoutManagerProperty(layoutManager, ATTR_ALIGNMENT, atts); |
92 |
|
|
93 | 16 |
if (atts.containsKey(ATTR_REFLABEL)) {
|
94 | 1 |
JLabel referrent = (JLabel)getObject(atts.get(ATTR_REFLABEL).toString()); |
95 | 1 |
referrent.setLabelFor(panel); |
96 |
} |
|
97 |
|
|
98 | 16 |
pushCurrentControl(panel, (String)atts.get(ATTR_ID)); |
99 | 16 |
if (atts.containsKey(ATTR_ID)) {
|
100 | 11 |
putObject((String)atts.get(ATTR_ID), panel); |
101 |
} |
|
102 |
} |
|
103 |
|
|
104 | 4 |
protected void exitElement() throws SAXException { |
105 | 4 |
popCurrentControl(); |
106 |
} |
|
107 |
|
|
108 | 1 |
protected String getDefaultPackagePrefix() {
|
109 | 1 |
throw new IllegalStateException(); |
110 |
} |
|
111 |
|
|
112 | 81 |
private void setLayoutManagerProperty(final LayoutManager layoutManager, final String attrKey, Map atts) throws SAXException { |
113 | 81 |
if (atts.containsKey(attrKey)) {
|
114 | 8 |
final String methodName = "set" + Character.toUpperCase(attrKey.charAt(0)) + attrKey.substring(1);
|
115 | 8 |
try {
|
116 | 8 |
Method setRowsMethod = layoutManager.getClass().getMethod(methodName, new Class[]{Integer.TYPE});
|
117 | 7 |
final String attrValue = atts.get(attrKey).toString(); |
118 | 7 |
Integer integerValue = null;
|
119 | 7 |
try {
|
120 | 7 |
integerValue = Integer.valueOf(attrValue); |
121 |
} catch (NumberFormatException e) {
|
|
122 | 3 |
Field constantValue = layoutManager.getClass().getField(attrValue.toUpperCase()); |
123 | 3 |
integerValue = new Integer(constantValue.getInt(null)); |
124 |
} |
|
125 |
|
|
126 | 7 |
setRowsMethod.invoke(layoutManager, new Object[]{
|
127 |
integerValue |
|
128 |
}); |
|
129 |
} catch (Exception e) {
|
|
130 | 1 |
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 |
} |
|
137 |
|
|