JGridPanel.java revision 13978:1993af50385d
1/*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package com.sun.swingset3.demos;
24
25import java.awt.*;
26import javax.swing.*;
27
28/**
29 * JGridPanel
30 *
31 * @author Pavel Porvatov
32 */
33public class JGridPanel extends JPanel {
34
35    public static final int DEFAULT_GAP = 5;
36
37    public enum Layout {
38        FIRST,
39        CENTER,
40        LAST,
41        FILL
42    }
43
44    private enum ComponentType {
45        NO_RESIZABLE,
46        HORIZONTAL_FILL,
47        FULL
48    }
49
50    private final int cols;
51
52    private int bigCol = -1;
53
54    private int bigRow = -1;
55
56    private int vGap = -1;
57
58    private int hGap = -1;
59
60    public JGridPanel(int cols) {
61        this(cols, -1, -1);
62    }
63
64    public JGridPanel(int cols, int bigCol) {
65        this(cols, bigCol, -1);
66    }
67
68    public JGridPanel(int cols, int bigCol, int bigRow) {
69        super(new GridBagLayout());
70
71        assert cols > 0;
72        assert bigCol >= -1 && bigCol < cols;
73        assert bigRow >= -1;
74
75        this.cols = cols;
76        this.bigCol = bigCol;
77        this.bigRow = bigRow;
78    }
79
80    public void setVGap(int vGap) {
81        this.vGap = vGap;
82    }
83
84    public void setHGap(int hGap) {
85        this.hGap = hGap;
86    }
87
88    public JGridPanel cell() {
89        return cell(null, getHLayout(null), getVLayout(null), null);
90    }
91
92    public JGridPanel cell(Component component) {
93        return cell(component, getHLayout(component), getVLayout(component), null);
94    }
95
96    public JGridPanel cell(Component component, Layout hLayout) {
97        return cell(component, hLayout, getVLayout(component), null);
98    }
99
100    public JGridPanel cell(Component component, Layout hLayout, Layout vLayout) {
101        return cell(component, hLayout, vLayout, null);
102    }
103
104    public JGridPanel cell(Component component, Insets insets) {
105        assert insets != null;
106
107        return cell(component, getHLayout(component), getVLayout(component), insets);
108    }
109
110    private JGridPanel cell(Component component, Layout hLayout, Layout vLayout, Insets insets) {
111        int componentCount = getComponentCount();
112        int x = componentCount % cols;
113        int y = componentCount / cols;
114
115        int weightx = x == bigCol || (bigCol < 0 && hLayout == Layout.FILL) ? 1 : 0;
116        int weighty = y == bigRow || (bigRow < 0 && vLayout == Layout.FILL) ? 1 : 0;
117
118        if (insets == null) {
119            int topGap = y == 0 ? 0 : vGap;
120            int leftGap = x == 0 ? 0 : hGap;
121
122            insets = new Insets(topGap < 0 ? DEFAULT_GAP : topGap,
123                    leftGap < 0 ? DEFAULT_GAP : leftGap, 0, 0);
124        }
125
126        add(component == null ? createSeparator() : component,
127                new GridBagConstraints(x, y,
128                        1, 1,
129                        weightx, weighty,
130                        getAnchor(hLayout, vLayout), getFill(hLayout, vLayout),
131                        insets, 0, 0));
132
133        return this;
134    }
135
136    public void setComponent(Component component, int col, int row) {
137        assert col >= 0 && col < cols;
138        assert row >= 0;
139
140        GridBagLayout layout = (GridBagLayout) getLayout();
141
142        for (int i = 0; i < getComponentCount(); i++) {
143            Component oldComponent = getComponent(i);
144
145            GridBagConstraints constraints = layout.getConstraints(oldComponent);
146
147            if (constraints.gridx == col && constraints.gridy == row) {
148                remove(i);
149
150                add(component == null ? createSeparator() : component, constraints);
151
152                validate();
153                repaint();
154
155                return;
156            }
157        }
158
159        // Cell not found
160        assert false;
161    }
162
163    private static JComponent createSeparator() {
164        return new JLabel();
165    }
166
167    private static int getFill(Layout hLayout, Layout vLayout) {
168        if (hLayout == Layout.FILL) {
169            return vLayout == Layout.FILL ? GridBagConstraints.BOTH : GridBagConstraints.HORIZONTAL;
170        }
171
172        return vLayout == Layout.FILL ? GridBagConstraints.VERTICAL : GridBagConstraints.NONE;
173    }
174
175    private static ComponentType getComponentType(Component component) {
176        if (component == null
177                || component instanceof JLabel
178                || component instanceof JRadioButton
179                || component instanceof JCheckBox
180                || component instanceof JButton) {
181            return ComponentType.NO_RESIZABLE;
182        }
183
184        if (component instanceof JComboBox
185                || component instanceof JTextField) {
186            return ComponentType.HORIZONTAL_FILL;
187        }
188
189        return ComponentType.FULL;
190    }
191
192    private static Layout getHLayout(Component component) {
193        if (getComponentType(component) == ComponentType.NO_RESIZABLE) {
194            return Layout.FIRST;
195        } else {
196            return Layout.FILL;
197        }
198    }
199
200    private static Layout getVLayout(Component component) {
201        if (getComponentType(component) == ComponentType.FULL) {
202            return Layout.FILL;
203        } else {
204            return Layout.CENTER;
205        }
206    }
207
208    private static final int[][] ANCHORS = new int[][]{
209        {GridBagConstraints.NORTHWEST, GridBagConstraints.NORTH, GridBagConstraints.NORTHEAST},
210        {GridBagConstraints.WEST, GridBagConstraints.CENTER, GridBagConstraints.EAST},
211        {GridBagConstraints.SOUTHWEST, GridBagConstraints.SOUTH, GridBagConstraints.SOUTHEAST}
212    };
213
214    private static int getAnchorIndex(Layout layout) {
215        if (layout == Layout.CENTER) {
216            return 1;
217        } else if (layout == Layout.LAST) {
218            return 2;
219        } else {
220            return 0;
221        }
222    }
223
224    private static int getAnchor(Layout hLayout, Layout vLayout) {
225        return ANCHORS[getAnchorIndex(vLayout)][getAnchorIndex(hLayout)];
226    }
227
228    public void setBorderEqual(int border) {
229        setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
230    }
231}
232