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<String, SimpleContainerLayout> layouts = new HashMap<String, SimpleContainerLayout>();
36
37 private SimpleLayoutConstraintsManager()
38 {
39 }
40
41 public SimpleContainerLayout getLayout( String name )
42 {
43 return 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<String, CellConstraints.Alignment> CELL_CONSTRAINTS =
54 new HashMap<String, CellConstraints.Alignment>()
55 {
56 private static final long serialVersionUID = -3816092941389065583L;
57
58 {
59 put( "default", CellConstraints.DEFAULT );
60 put( "fill", CellConstraints.FILL );
61 put( "center", CellConstraints.CENTER );
62 put( "left", CellConstraints.LEFT );
63 put( "right", CellConstraints.RIGHT );
64 put( "top", CellConstraints.TOP );
65 put( "bottom", CellConstraints.BOTTOM );
66 }
67 };
68
69 public static CellConstraints.Alignment getAlignment( String value )
70 {
71 CellConstraints.Alignment alignment = CELL_CONSTRAINTS.get( ( value == null ) ? DEFAULT : value );
72 if ( alignment == null )
73 {
74 throw new RuntimeException( "Invalid alignment '" + value + "'" );
75 }
76 return alignment;
77 }
78
79 public static SimpleLayoutConstraintsManager getLayoutConstraintsManager( InputStream stream )
80 {
81 try
82 {
83 DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
84 Document dataDocument = documentBuilder.parse( stream );
85 Element root = (Element) dataDocument.getDocumentElement();
86 return getLayoutConstraintsManager( root );
87 }
88 catch ( Exception e )
89 {
90 throw new RuntimeException( "Unable to create DocumentBuilder", e );
91 }
92 }
93
94 public static SimpleLayoutConstraintsManager getLayoutConstraintsManager( Element containersNode )
95 {
96 if ( !containersNode.getNodeName().equals( "containers" ) )
97 {
98 throw new RuntimeException( "Expected a node named 'containers', found '" + containersNode.getNodeName()
99 + "'" );
100 }
101 SimpleLayoutConstraintsManager layoutConstraintsManager = new SimpleLayoutConstraintsManager();
102 Iterator<Element> iter = DOMUtils.getChildElementsByTagName( containersNode, "container" );
103 while ( iter.hasNext() )
104 {
105 Element containerNode = (Element) iter.next();
106 String containerName = containerNode.getAttribute( "name" );
107 String columnSpecs = containerNode.getAttribute( "columnSpecs" );
108 String rowSpecs = containerNode.getAttribute( "rowSpecs" );
109 SimpleContainerLayout containerLayout = new SimpleContainerLayout( containerName, columnSpecs, rowSpecs );
110
111 Iterator<Element> iterConstraints = DOMUtils.getChildElementsByTagName( containerNode, "cellconstraints" );
112 while ( iterConstraints.hasNext() )
113 {
114 Element constraintsNode = (Element) iterConstraints.next();
115 String name = constraintsNode.getAttribute( "name" );
116 CellConstraints.Alignment horizontalAlignment =
117 getAlignment( constraintsNode.getAttribute( "horizontalAlignment" ) );
118 CellConstraints.Alignment verticalAlignment =
119 getAlignment( constraintsNode.getAttribute( "verticalAlignment" ) );
120 int gridX = getIntValue( constraintsNode, "gridX", 1 );
121 int gridY = getIntValue( constraintsNode, "gridY", 1 );
122 int gridWidth = getIntValue( constraintsNode, "gridWidth", 1 );
123 int gridHeight = getIntValue( constraintsNode, "gridHeight", 1 );
124 int topInset = getIntValue( constraintsNode, "topInset", 0 );
125 int bottomInset = getIntValue( constraintsNode, "bottomInset", 0 );
126 int rightInset = getIntValue( constraintsNode, "rightInset", 0 );
127 int leftInset = getIntValue( constraintsNode, "leftInset", 0 );
128
129 CellConstraints constraints =
130 new CellConstraints( gridX, gridY, gridWidth, gridHeight, horizontalAlignment, verticalAlignment,
131 new Insets( topInset, leftInset, bottomInset, rightInset ) );
132 containerLayout.addCellConstraints( name, constraints );
133 }
134 layoutConstraintsManager.addLayout( containerLayout );
135 }
136 return layoutConstraintsManager;
137 }
138
139 private static int getIntValue( Element elmt, String name, int defaultValue )
140 {
141 String value = elmt.getAttribute( name );
142 return ( value == null ) ? defaultValue : Integer.parseInt( value );
143 }
144 }