001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * 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, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.util;
017    
018    import java.awt.GridBagConstraints;
019    import java.awt.Insets;
020    
021    import org.springframework.richclient.core.UIConstants;
022    
023    /**
024     * Utility functions to assist using the horridly complex Grid bag layout.
025     * 
026     * @author Keith Donald
027     */
028    public class GridBagCellConstraints {
029    
030        public static Insets RIGHT_INSETS = new Insets(0, 0, 0, UIConstants.ONE_SPACE);
031    
032        public static Insets LEFT_INSETS = new Insets(0, UIConstants.ONE_SPACE, 0, 0);
033    
034        public static Insets TOP_INSETS = new Insets(UIConstants.ONE_SPACE, 0, 0, 0);
035    
036        public static Insets BOTTOM_INSETS = new Insets(0, 0, UIConstants.ONE_SPACE, 0);
037    
038        public static Insets TITLE_LABEL_INSETS = new Insets(0, 0, UIConstants.ONE_SPACE, UIConstants.ONE_SPACE);
039    
040        public static Insets RIGHT_INSETS_TWO_SPACES = new Insets(0, 0, 0, UIConstants.ONE_SPACE);
041    
042        public static Insets EVEN_INSETS = new Insets(UIConstants.ONE_SPACE, UIConstants.ONE_SPACE, UIConstants.ONE_SPACE,
043                UIConstants.ONE_SPACE);
044    
045        public GridBagConstraints xy(int x, int y) {
046            GridBagConstraints result = new GridBagConstraints();
047            result.gridx = x;
048            result.gridy = y;
049            return result;
050        }
051    
052        public GridBagConstraints xywh(int x, int y, int width, int height) {
053            GridBagConstraints result = xy(x, y);
054            result.gridheight = height;
055            result.gridwidth = width;
056            return result;
057        }
058    
059        public GridBagConstraints xyf(int x, int y, int fill) {
060            return xyfi(x, y, fill, null);
061        }
062    
063        public GridBagConstraints xyfi(int x, int y, int fill, Insets insets) {
064            GridBagConstraints result = xy(x, y);
065            result.fill = fill;
066            if (insets != null) {
067                result.insets = insets;
068            }
069            switch (result.fill) {
070            case GridBagConstraints.NONE: {
071            }
072                break;
073            case GridBagConstraints.BOTH: {
074                result.weightx = result.weighty = 1.0;
075            }
076                break;
077            case GridBagConstraints.VERTICAL: {
078                result.weighty = 1.0;
079            }
080                break;
081            case GridBagConstraints.HORIZONTAL: {
082                result.weightx = 1.0;
083            }
084                break;
085            default: {
086                result.fill = GridBagConstraints.NONE;
087            }
088                break;
089            }
090            return result;
091        }
092    
093        public GridBagConstraints xya(int x, int y, int anchor) {
094            GridBagConstraints result = xy(x, y);
095            result.anchor = anchor;
096            return result;
097        }
098    
099        public GridBagConstraints xyaf(int x, int y, int anchor, int fill) {
100            return xyaf(x, y, anchor, fill, null);
101        }
102    
103        public GridBagConstraints xyaf(int x, int y, int anchor, int fill, Insets insets) {
104            GridBagConstraints result = xyfi(x, y, fill, insets);
105            result.anchor = anchor;
106            return result;
107        }
108    
109        public GridBagConstraints title(int x, int y) {
110            GridBagConstraints result = xy(x, y);
111            result.anchor = GridBagConstraints.WEST;
112            result.insets = TITLE_LABEL_INSETS;
113            return result;
114        }
115    
116        public GridBagConstraints label(int x, int y) {
117            return label(x, y, RIGHT_INSETS);
118        }
119    
120        public GridBagConstraints label(int x, int y, Insets insets) {
121            GridBagConstraints result = xy(x, y);
122            result.anchor = GridBagConstraints.WEST;
123            result.insets = insets;
124            return result;
125        }
126    
127        public GridBagConstraints textField(int x, int y) {
128            return xyf(x, y, GridBagConstraints.HORIZONTAL);
129        }
130    
131        public GridBagConstraints textField(int x, int y, Insets insets) {
132            return xyfi(x, y, GridBagConstraints.HORIZONTAL, insets);
133        }
134    
135    }