001 package 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 017 import java.awt.Insets; 018 import java.io.InputStream; 019 import java.util.*; 020 import javax.xml.parsers.DocumentBuilder; 021 import javax.xml.parsers.DocumentBuilderFactory; 022 import org.w3c.dom.Document; 023 import org.w3c.dom.Element; 024 025 import com.jgoodies.forms.layout.CellConstraints; 026 027 import 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 */ 033 public class SimpleLayoutConstraintsManager 034 { 035 private final Map layouts = new HashMap(); 036 037 private SimpleLayoutConstraintsManager() 038 { 039 } 040 041 public SimpleContainerLayout getLayout( String name ) 042 { 043 return (SimpleContainerLayout) 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 CELL_CONSTRAINTS = new HashMap() 054 { 055 { 056 put( "default", CellConstraints.DEFAULT ); 057 put( "fill", CellConstraints.FILL ); 058 put( "center", CellConstraints.CENTER ); 059 put( "left", CellConstraints.LEFT ); 060 put( "right", CellConstraints.RIGHT ); 061 put( "top", CellConstraints.TOP ); 062 put( "bottom", CellConstraints.BOTTOM ); 063 } 064 }; 065 066 public static CellConstraints.Alignment getAlignment( String value ) 067 { 068 CellConstraints.Alignment alignment = 069 (CellConstraints.Alignment) CELL_CONSTRAINTS.get( ( value == null ) ? DEFAULT : value ); 070 if ( alignment == null ) 071 { 072 throw new RuntimeException( "Invalid alignment '" + value + "'" ); 073 } 074 return alignment; 075 } 076 077 public static SimpleLayoutConstraintsManager getLayoutConstraintsManager( InputStream stream ) 078 { 079 try 080 { 081 DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 082 Document dataDocument = documentBuilder.parse( stream ); 083 Element root = (Element) dataDocument.getDocumentElement(); 084 return getLayoutConstraintsManager( root ); 085 } 086 catch ( Exception e ) 087 { 088 throw new RuntimeException( "Unable to create DocumentBuilder", e ); 089 } 090 } 091 092 public static SimpleLayoutConstraintsManager getLayoutConstraintsManager( Element containersNode ) 093 { 094 if ( !containersNode.getNodeName().equals( "containers" ) ) 095 { 096 throw new RuntimeException( "Expected a node named 'containers', found '" + containersNode.getNodeName() 097 + "'" ); 098 } 099 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 }