View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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  package org.apache.commons.jelly.tags.swing.impl;
17  
18  import java.awt.GridBagConstraints;
19  import java.awt.Insets;
20  import java.lang.reflect.Field;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /***
26   * This class is a simple "bean-wrapper" for the {@link GridBagConstraints} class
27   * which also tracks wether values are set allowing inheritance
28   *    (using {@link setBasedOn}.
29   *
30   * @author <a href="mailto:paul@activemath.org">Paul Libbrecht</a>
31   * @version $Revision: 155420 $
32   */
33  public class GridBagConstraintBean extends GridBagConstraints {
34  
35      private boolean gridxSet = false;
36      private boolean gridySet = false;
37      private boolean gridwidthSet = false;
38      private boolean gridheightSet = false;
39      private boolean weightxSet = false;
40      private boolean weightySet = false;
41      private boolean ipadxSet = false;
42      private boolean ipadySet = false;
43      private boolean anchorSet = false;
44      private boolean fillSet = false;
45  
46      /*** Logging output */
47      private static final Log LOG = LogFactory.getLog(GridBagConstraintBean.class);
48  
49      /*** error message */
50      private static final String ILLEGAL_ANCHOR_MSG = "Anchor must be one of  the GridBagLayout constants for the current Java version.";
51  
52      public GridBagConstraintBean() {
53      }
54  
55      public int getGridx() {
56          return gridx;
57      }
58      public void setGridx(int gridx) {
59          this.gridx = gridx;
60          this.gridxSet = true;
61      }
62  
63      public int getGridy() {
64          return gridy;
65      }
66      public void setGridy(int gridy) {
67          this.gridy = gridy;
68          this.gridySet = true;
69      }
70  
71      public int getGridwidth() {
72          return gridwidth;
73      }
74      public void setGridwidth(int gridwidth) {
75          this.gridwidth = gridwidth;
76          this.gridwidthSet = true;
77      }
78  
79      public int getGridheight() {
80          return gridheight;
81      }
82      public void setGridheight(int gridheight) {
83          this.gridheight = gridheight;
84          this.gridheightSet = true;
85      }
86  
87      public double getWeightx() {
88          return weightx;
89      }
90      public void setWeightx(double weightx) {
91          this.weightx = weightx;
92          this.weightxSet = true;
93      }
94  
95      public double getWeighty() {
96          return weighty;
97      }
98      public void setWeighty(double weighty) {
99          this.weighty = weighty;
100         this.weightySet = true;
101     }
102 
103     public int getIpadx() {
104         return ipadx;
105     }
106     public void setIpadx(int ipadx) {
107         this.ipadx = ipadx;
108         this.ipadxSet = true;
109     }
110 
111     public int getIpady() {
112         return ipady;
113     }
114     public void setIpady(int ipady) {
115         this.ipady = ipady;
116         this.ipadySet = true;
117     }
118 
119     // TODO: provide better. insetstop, insetsbottom ??
120     public Insets getInsets() {
121         return insets;
122     }
123     public void setInsets(Insets insets) {
124         this.insets = insets;
125     }
126 
127     /*** Returns the lower-case variant of the constant-name
128         *    corresponding to the stored {@link #anchor} attribute.
129         *
130         *    @see    #anchor
131         */
132     public String getAnchor() {
133         switch (this.anchor) {
134             case CENTER :
135                 return "center";
136             case NORTH :
137                 return "north";
138             case NORTHEAST :
139                 return "northeast";
140             case EAST :
141                 return "east";
142             case SOUTHEAST :
143                 return "southeast";
144             case SOUTH :
145                 return "south";
146             case SOUTHWEST :
147                 return "southwest";
148             case WEST :
149                 return "west";
150             case NORTHWEST :
151                 return "northwest";
152         }
153 
154         if (this.anchor == getByReflection("LINE_START"))
155             return "line_start";
156         else if (this.anchor == getByReflection("LINE_END"))
157             return "line_end";
158         else if (this.anchor == getByReflection("PAGE_START"))
159             return "page_start";
160         else if (this.anchor == getByReflection("PAGE_END"))
161             return "page_end";
162         else if (this.anchor == getByReflection("FIRST_LINE_START"))
163             return "first_line_start";
164         else if (this.anchor == getByReflection("FIRST_LINE_END"))
165             return "first_line_end";
166         else if (this.anchor == getByReflection("LAST_LINE_START"))
167             return "last_line_start";
168         else if (this.anchor ==  getByReflection("LAST_LINE_END"))
169             return "last_line_end";
170 
171         throw new IllegalArgumentException(ILLEGAL_ANCHOR_MSG);
172     }
173 
174     /*** Accepts one of the strings with the same name as the constants
175         * and sets the {@link #anchor} value accordingly.
176         *    The accepted strings are case-insensitive.
177         *
178         *    @see #anchor
179         */
180     public void setAnchor(String anchorString) {
181         String lcAnchorString = anchorString.toLowerCase();
182         if (lcAnchorString.equals("center"))
183             this.anchor = CENTER;
184         else if (lcAnchorString.equals("north"))
185             this.anchor = NORTH;
186         else if (lcAnchorString.equals("northeast"))
187             this.anchor = NORTHEAST;
188         else if (lcAnchorString.equals("east"))
189             this.anchor = EAST;
190         else if (lcAnchorString.equals("southeast"))
191             this.anchor = SOUTHEAST;
192         else if (lcAnchorString.equals("south"))
193             this.anchor = SOUTH;
194         else if (lcAnchorString.equals("southwest"))
195             this.anchor = SOUTHWEST;
196         else if (lcAnchorString.equals("west"))
197             this.anchor = WEST;
198         else if (lcAnchorString.equals("northwest"))
199             this.anchor = NORTHWEST;
200         else if (lcAnchorString.equals("page_start"))
201             this.anchor = getByReflection("PAGE_START");
202         else if (lcAnchorString.equals("page_end"))
203             this.anchor = getByReflection("PAGE_END");
204         else if (lcAnchorString.equals("line_start"))
205             this.anchor = getByReflection("LINE_START");
206         else if (lcAnchorString.equals("line_end"))
207             this.anchor = getByReflection("LINE_END");
208         else if (lcAnchorString.equals("first_line_start"))
209             this.anchor = getByReflection("FIRST_LINE_START");
210         else if (lcAnchorString.equals("first_line_end"))
211             this.anchor = getByReflection("FIRST_LINE_END");
212         else if (lcAnchorString.equals("last_line_end"))
213             this.anchor = getByReflection("LAST_LINE_END");
214         else if (lcAnchorString.equals("last_line_start"))
215             this.anchor = getByReflection("LAST_LINE_START");
216         else
217             throw new IllegalArgumentException("Anchor must be the name of one of  the GridBagLayoutConstants (case doesn't matter): center, north, northeast, east, southeast, south, southwest, west, or northwest.");
218         this.anchorSet = true;
219     }
220 
221     /*** Returns the lower-case variant of the constant-name
222         *    corresponding to the stored {@link #fill} attribute.
223         *
224         *    @see    #fill
225         */
226     public String getFill() {
227         switch (fill) {
228             case NONE :
229                 return "none";
230             case HORIZONTAL :
231                 return "horizontal";
232             case VERTICAL :
233                 return "vertical";
234             case BOTH :
235                 return "both";
236             default :
237                 throw new IllegalArgumentException("Fill must be the name of one of  the GridBagLayoutConstants: NONE, HORIZONTAL, VERTICAL, BOTH.");
238         }
239     }
240     /*** Accepts one of the strings with the same name as the constants
241         * and sets the {@link #fill} value accordingly.
242         *    The accepted strings are case-insensitive.
243         *
244         *    @see #fill
245         */
246     public void setFill(String fillString) {
247         String lcFillString = fillString.toLowerCase();
248         if (lcFillString.equals("none"))
249             this.fill = NONE;
250         else if (lcFillString.equals("horizontal"))
251             this.fill = HORIZONTAL;
252         else if (lcFillString.equals("vertical"))
253             this.fill = VERTICAL;
254         else if (lcFillString.equals("both"))
255             this.fill = BOTH;
256         else
257             throw new IllegalArgumentException("Fill must be the name of one of  the GridBagLayoutConstants (case does not matter): NONE, HORIZONTAL, VERTICAL, BOTH.");
258         this.fillSet = true;
259     }
260 
261     /*** Reads the values in the given grid-bag-constraint-bean that are set and sets
262         * them in this object if they have not been set yet.
263         */
264     public void setBasedOn(GridBagConstraintBean from) {
265         if (!gridxSet && from.gridxSet) {
266             gridx = from.gridx;
267             this.gridxSet = true;
268         }
269         if (!gridySet && from.gridySet) {
270             gridy = from.gridy;
271             this.gridySet = true;
272         }
273         if (!gridwidthSet && from.gridwidthSet) {
274             gridwidth = from.gridwidth;
275             this.gridwidthSet = true;
276         }
277         if (!gridheightSet && from.gridheightSet) {
278             gridheight = from.gridheight;
279             this.gridheightSet = true;
280         }
281         if (!weightxSet && from.weightxSet) {
282             weightx = from.weightx;
283             this.weightxSet = true;
284         }
285         if (!weightySet && from.weightySet) {
286             weighty = from.weighty;
287             this.weightySet = true;
288         }
289         if (!ipadxSet && from.ipadxSet) {
290             ipadx = from.ipadx;
291             this.ipadxSet = true;
292         }
293         if (!ipadySet && from.ipadySet) {
294             ipady = from.ipady;
295             this.ipadySet = true;
296         }
297         if (!fillSet && from.fillSet) {
298             fill = from.fill;
299             this.fillSet = true;
300         }
301         if (!anchorSet && from.anchorSet) {
302             anchor = from.anchor;
303             this.anchorSet = true;
304         }
305     }
306 
307     public String toString() {
308         return "GridBagConstraintBean["
309             + "gridx="
310             + gridx
311             + ", gridy="
312             + gridy
313             + ", gridwidth="
314             + gridwidth
315             + ", gridheight="
316             + gridheight
317             + ", weightx="
318             + weightx
319             + ", weighty="
320             + weighty
321             + ", ipadx="
322             + ipadx
323             + ", ipady="
324             + ipady
325             + ", anchor="
326             + getAnchor()
327             + ", fill="
328             + getFill()
329             + ", insets="
330             + insets
331             + "]";
332     }
333 
334     private int getByReflection(String field) {
335         try {
336             Field f = getClass().getField(field);
337             Integer rv = (Integer) f.get(this);
338             return rv.intValue();
339         } catch (SecurityException e) {
340             LOG.debug(e);
341             throw new IllegalArgumentException(ILLEGAL_ANCHOR_MSG);
342         } catch (NoSuchFieldException e) {
343             LOG.debug(e);
344             throw new IllegalArgumentException(ILLEGAL_ANCHOR_MSG);
345         } catch (IllegalArgumentException e) {
346             LOG.debug(e);
347             throw new IllegalArgumentException(ILLEGAL_ANCHOR_MSG);
348         } catch (IllegalAccessException e) {
349             LOG.debug(e);
350             throw new IllegalArgumentException(ILLEGAL_ANCHOR_MSG);
351         }
352     }
353 
354 } // class GridBagConstraintsBean