001package net.sf.logdistiller.gui; 002 003/* 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017import java.awt.Insets; 018import java.io.InputStream; 019import java.util.*; 020import javax.xml.parsers.DocumentBuilder; 021import javax.xml.parsers.DocumentBuilderFactory; 022import org.w3c.dom.Document; 023import org.w3c.dom.Element; 024 025import com.jgoodies.forms.layout.CellConstraints; 026 027import net.sf.logdistiller.xml.DOMUtils; 028 029/** 030 * This class is a simplified version of FormLayoutMaker's equivalent class, in a dream to avoid need for Java 1.4 at 031 * runtime. 032 */ 033public class SimpleLayoutConstraintsManager 034{ 035 private final Map<String, SimpleContainerLayout> layouts = new HashMap<String, SimpleContainerLayout>(); 036 037 private SimpleLayoutConstraintsManager() 038 { 039 } 040 041 public SimpleContainerLayout getLayout( String name ) 042 { 043 return layouts.get( name ); 044 } 045 046 public void addLayout( SimpleContainerLayout containerLayout ) 047 { 048 layouts.put( containerLayout.getName(), containerLayout ); 049 } 050 051 public static final String DEFAULT = "default"; 052 053 private final static Map<String, CellConstraints.Alignment> CELL_CONSTRAINTS = 054 new HashMap<String, CellConstraints.Alignment>() 055 { 056 private static final long serialVersionUID = -3816092941389065583L; 057 058 { 059 put( "default", CellConstraints.DEFAULT ); 060 put( "fill", CellConstraints.FILL ); 061 put( "center", CellConstraints.CENTER ); 062 put( "left", CellConstraints.LEFT ); 063 put( "right", CellConstraints.RIGHT ); 064 put( "top", CellConstraints.TOP ); 065 put( "bottom", CellConstraints.BOTTOM ); 066 } 067 }; 068 069 public static CellConstraints.Alignment getAlignment( String value ) 070 { 071 CellConstraints.Alignment alignment = CELL_CONSTRAINTS.get( ( value == null ) ? DEFAULT : value ); 072 if ( alignment == null ) 073 { 074 throw new RuntimeException( "Invalid alignment '" + value + "'" ); 075 } 076 return alignment; 077 } 078 079 public static SimpleLayoutConstraintsManager getLayoutConstraintsManager( InputStream stream ) 080 { 081 try 082 { 083 DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 084 Document dataDocument = documentBuilder.parse( stream ); 085 Element root = (Element) dataDocument.getDocumentElement(); 086 return getLayoutConstraintsManager( root ); 087 } 088 catch ( Exception e ) 089 { 090 throw new RuntimeException( "Unable to create DocumentBuilder", e ); 091 } 092 } 093 094 public static SimpleLayoutConstraintsManager getLayoutConstraintsManager( Element containersNode ) 095 { 096 if ( !containersNode.getNodeName().equals( "containers" ) ) 097 { 098 throw new RuntimeException( "Expected a node named 'containers', found '" + containersNode.getNodeName() 099 + "'" ); 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}