1 package net.sf.logdistiller.gui;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.awt.Insets;
18 import java.io.InputStream;
19 import java.util.*;
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24
25 import com.jgoodies.forms.layout.CellConstraints;
26
27 import net.sf.logdistiller.xml.DOMUtils;
28
29
30
31
32
33 public class SimpleLayoutConstraintsManager
34 {
35 private final Map layouts = new HashMap();
36
37 private SimpleLayoutConstraintsManager()
38 {
39 }
40
41 public SimpleContainerLayout getLayout( String name )
42 {
43 return (SimpleContainerLayout) layouts.get( name );
44 }
45
46 public void addLayout( SimpleContainerLayout containerLayout )
47 {
48 layouts.put( containerLayout.getName(), containerLayout );
49 }
50
51 public static final String DEFAULT = "default";
52
53 private final static Map CELL_CONSTRAINTS = new HashMap()
54 {
55 {
56 put( "default", CellConstraints.DEFAULT );
57 put( "fill", CellConstraints.FILL );
58 put( "center", CellConstraints.CENTER );
59 put( "left", CellConstraints.LEFT );
60 put( "right", CellConstraints.RIGHT );
61 put( "top", CellConstraints.TOP );
62 put( "bottom", CellConstraints.BOTTOM );
63 }
64 };
65
66 public static CellConstraints.Alignment getAlignment( String value )
67 {
68 CellConstraints.Alignment alignment =
69 (CellConstraints.Alignment) CELL_CONSTRAINTS.get( ( value == null ) ? DEFAULT : value );
70 if ( alignment == null )
71 {
72 throw new RuntimeException( "Invalid alignment '" + value + "'" );
73 }
74 return alignment;
75 }
76
77 public static SimpleLayoutConstraintsManager getLayoutConstraintsManager( InputStream stream )
78 {
79 try
80 {
81 DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
82 Document dataDocument = documentBuilder.parse( stream );
83 Element root = (Element) dataDocument.getDocumentElement();
84 return getLayoutConstraintsManager( root );
85 }
86 catch ( Exception e )
87 {
88 throw new RuntimeException( "Unable to create DocumentBuilder", e );
89 }
90 }
91
92 public static SimpleLayoutConstraintsManager getLayoutConstraintsManager( Element containersNode )
93 {
94 if ( !containersNode.getNodeName().equals( "containers" ) )
95 {
96 throw new RuntimeException( "Expected a node named 'containers', found '" + containersNode.getNodeName()
97 + "'" );
98 }
99 SimpleLayoutConstraintsManager layoutConstraintsManager = new SimpleLayoutConstraintsManager();
100 Iterator iter = DOMUtils.getChildElementsByTagName( containersNode, "container" );
101 while ( iter.hasNext() )
102 {
103 Element containerNode = (Element) iter.next();
104 String containerName = containerNode.getAttribute( "name" );
105 String columnSpecs = containerNode.getAttribute( "columnSpecs" );
106 String rowSpecs = containerNode.getAttribute( "rowSpecs" );
107 SimpleContainerLayout containerLayout = new SimpleContainerLayout( containerName, columnSpecs, rowSpecs );
108
109 Iterator iterConstraints = DOMUtils.getChildElementsByTagName( containerNode, "cellconstraints" );
110 while ( iterConstraints.hasNext() )
111 {
112 Element constraintsNode = (Element) iterConstraints.next();
113 String name = constraintsNode.getAttribute( "name" );
114 CellConstraints.Alignment horizontalAlignment =
115 getAlignment( constraintsNode.getAttribute( "horizontalAlignment" ) );
116 CellConstraints.Alignment verticalAlignment =
117 getAlignment( constraintsNode.getAttribute( "verticalAlignment" ) );
118 int gridX = getIntValue( constraintsNode, "gridX", 1 );
119 int gridY = getIntValue( constraintsNode, "gridY", 1 );
120 int gridWidth = getIntValue( constraintsNode, "gridWidth", 1 );
121 int gridHeight = getIntValue( constraintsNode, "gridHeight", 1 );
122 int topInset = getIntValue( constraintsNode, "topInset", 0 );
123 int bottomInset = getIntValue( constraintsNode, "bottomInset", 0 );
124 int rightInset = getIntValue( constraintsNode, "rightInset", 0 );
125 int leftInset = getIntValue( constraintsNode, "leftInset", 0 );
126
127 CellConstraints constraints =
128 new CellConstraints( gridX, gridY, gridWidth, gridHeight, horizontalAlignment, verticalAlignment,
129 new Insets( topInset, leftInset, bottomInset, rightInset ) );
130 containerLayout.addCellConstraints( name, constraints );
131 }
132 layoutConstraintsManager.addLayout( containerLayout );
133 }
134 return layoutConstraintsManager;
135 }
136
137 private static int getIntValue( Element elmt, String name, int defaultValue )
138 {
139 String value = elmt.getAttribute( name );
140 return ( value == null ) ? defaultValue : Integer.parseInt( value );
141 }
142 }