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.Component;
018    import java.awt.Container;
019    import java.awt.Dimension;
020    import java.awt.LayoutManager2;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import com.jgoodies.forms.layout.CellConstraints;
025    import com.jgoodies.forms.layout.FormLayout;
026    
027    /**
028     * This class is a simplified version of FormLayoutMaker's equivalent class, in a dream to avoid need for Java 1.4 at
029     * runtime.
030     */
031    public class SimpleContainerLayout
032        implements LayoutManager2
033    {
034        private final Map componentConstraints = new HashMap();
035    
036        private final Map componentsToNames = new HashMap();
037    
038        private final FormLayout formLayout;
039    
040        private final String name;
041    
042        public SimpleContainerLayout( String name, String columnSpecs, String rowSpecs )
043        {
044            this.name = name;
045            formLayout = new FormLayout( columnSpecs, rowSpecs );
046        }
047    
048        public String getName()
049        {
050            return this.name;
051        }
052    
053        public void addCellConstraints( String name, CellConstraints constraints )
054        {
055            componentConstraints.put( name, constraints );
056        }
057    
058        // interface for LayoutManager2
059        public void addLayoutComponent( String name, Component comp )
060        {
061            throw new RuntimeException(
062                                        "This method should not be called.  Call addLayoutComponent(Component, Object) instead" );
063        }
064    
065        public float getLayoutAlignmentX( Container target )
066        {
067            return formLayout.getLayoutAlignmentX( target );
068        }
069    
070        public float getLayoutAlignmentY( Container target )
071        {
072            return formLayout.getLayoutAlignmentY( target );
073        }
074    
075        public void invalidateLayout( Container target )
076        {
077            formLayout.invalidateLayout( target );
078        }
079    
080        public void layoutContainer( Container parent )
081        {
082            formLayout.layoutContainer( parent );
083        }
084    
085        public Dimension maximumLayoutSize( Container target )
086        {
087            return formLayout.maximumLayoutSize( target );
088        }
089    
090        public Dimension minimumLayoutSize( Container parent )
091        {
092            return formLayout.minimumLayoutSize( parent );
093        }
094    
095        public Dimension preferredLayoutSize( Container parent )
096        {
097            return formLayout.preferredLayoutSize( parent );
098        }
099    
100        public void removeLayoutComponent( Component comp )
101        {
102            String componentName = (String) componentsToNames.get( comp );
103            componentsToNames.remove( comp );
104            componentConstraints.remove( componentName );
105            formLayout.removeLayoutComponent( comp );
106        }
107    
108        public void addLayoutComponent( Component comp, Object constraints )
109        {
110            String componentName = (String) constraints;
111            CellConstraints cellConstraints = (CellConstraints) componentConstraints.get( componentName );
112            if ( cellConstraints == null )
113            {
114                System.err.println( "Warning: " + componentName + " was added without constraints" );
115                cellConstraints = new CellConstraints();
116                componentConstraints.put( componentName, cellConstraints );
117                comp.setVisible( false );
118            }
119            componentsToNames.put( comp, componentName );
120            formLayout.addLayoutComponent( comp, cellConstraints );
121        }
122    }