View Javadoc
1   package net.sf.logdistiller.gui;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.awt.Component;
18  import java.awt.Container;
19  import java.awt.Dimension;
20  import java.awt.LayoutManager2;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import com.jgoodies.forms.layout.CellConstraints;
25  import com.jgoodies.forms.layout.FormLayout;
26  
27  /**
28   * This class is a simplified version of FormLayoutMaker's equivalent class, in a dream to avoid need for Java 1.4 at
29   * runtime.
30   */
31  public class SimpleContainerLayout
32      implements LayoutManager2
33  {
34      private final Map<String, CellConstraints> componentConstraints = new HashMap<String, CellConstraints>();
35  
36      private final Map<Component, String> componentsToNames = new HashMap<Component, String>();
37  
38      private final FormLayout formLayout;
39  
40      private final String name;
41  
42      public SimpleContainerLayout( String name, String columnSpecs, String rowSpecs )
43      {
44          this.name = name;
45          formLayout = new FormLayout( columnSpecs, rowSpecs );
46      }
47  
48      public String getName()
49      {
50          return this.name;
51      }
52  
53      public void addCellConstraints( String name, CellConstraints constraints )
54      {
55          componentConstraints.put( name, constraints );
56      }
57  
58      // interface for LayoutManager2
59      public void addLayoutComponent( String name, Component comp )
60      {
61          throw new RuntimeException(
62                                      "This method should not be called.  Call addLayoutComponent(Component, Object) instead" );
63      }
64  
65      public float getLayoutAlignmentX( Container target )
66      {
67          return formLayout.getLayoutAlignmentX( target );
68      }
69  
70      public float getLayoutAlignmentY( Container target )
71      {
72          return formLayout.getLayoutAlignmentY( target );
73      }
74  
75      public void invalidateLayout( Container target )
76      {
77          formLayout.invalidateLayout( target );
78      }
79  
80      public void layoutContainer( Container parent )
81      {
82          formLayout.layoutContainer( parent );
83      }
84  
85      public Dimension maximumLayoutSize( Container target )
86      {
87          return formLayout.maximumLayoutSize( target );
88      }
89  
90      public Dimension minimumLayoutSize( Container parent )
91      {
92          return formLayout.minimumLayoutSize( parent );
93      }
94  
95      public Dimension preferredLayoutSize( Container parent )
96      {
97          return formLayout.preferredLayoutSize( parent );
98      }
99  
100     public void removeLayoutComponent( Component comp )
101     {
102         String componentName = 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 }