Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/cybertivity/powerjournal/gridpanel/GridPanelBuilder.java


1   package com.cybertivity.powerjournal.gridpanel;
2   import java.awt.BorderLayout;
3   import java.awt.GridBagConstraints;
4   import java.awt.GridBagLayout;
5   import java.awt.Insets;
6   import java.util.ArrayList;
7   import javax.swing.JComponent;
8   import javax.swing.JPanel;
9   
10  /**
11   * Title:        PowerJournal
12   * Description:  $Id: GridPanelBuilder.java,v 1.4 2001/12/07 03:00:54 arrowood Exp $
13   * Takes an array of GridPanelColumns and lays them out on a JPanel from left to
14   * right. Each JComponent in the column is a row.
15   * Copyright:    Copyright (c) 2001
16   * Company:      <A HREF="http://www.cybertivity.com">Cybertivity</A>
17   *
18   * @author <A HREF="mailto:chris.arrowood@cybertivity.com">Chris Arrowood</A>
19   * @created November 24, 2001
20   * @version 1.0
21   */
22  
23  public class GridPanelBuilder {
24  
25    private static void assertEqualSizes(ArrayList columnArray) throws GridPanelException {
26      if (columnArray.size() > 0) {
27        int sizeOfFirstColumn = 0;
28        for (int i = 0; i < columnArray.size(); i++) {
29          GridPanelColumn gridPanelColumn = (GridPanelColumn) columnArray.get(i);
30          if (i == 0) {
31            sizeOfFirstColumn = gridPanelColumn.size();
32          }
33          if (gridPanelColumn.size() != sizeOfFirstColumn) {
34            throw new GridPanelException("All GridPanelColumns must be the same size");
35          }
36        }
37      }
38    }
39  
40    public static JPanel createGridPanel(Insets insets, ArrayList columnArray,
41        JPanel buttonPanel, String buttonPanelPosition,JPanel labelPanel, String labelPanelPosition) throws GridPanelException {
42      JPanel outerPanel = new JPanel(new BorderLayout());
43      JPanel centerPanel = createCenterPanel(insets, columnArray);
44      outerPanel.add(centerPanel, BorderLayout.CENTER);
45      if (buttonPanel != null) {
46        outerPanel.add(buttonPanel, buttonPanelPosition);
47      }
48      if (labelPanel != null) {
49        outerPanel.add(labelPanel, labelPanelPosition);
50      }
51      return outerPanel;
52    }
53  
54    public static JPanel createGridPanel(Insets insets, ArrayList columnArray,
55        JPanel buttonPanel, String buttonPanelPosition) throws GridPanelException {
56      JPanel outerPanel = new JPanel(new BorderLayout());
57      JPanel centerPanel = createCenterPanel(insets, columnArray);
58      outerPanel.add(centerPanel, BorderLayout.CENTER);
59      if (buttonPanel != null) {
60        outerPanel.add(buttonPanel, buttonPanelPosition);
61      }
62      return outerPanel;
63    }
64  
65  
66    public static JPanel createGridPanel(Insets insets, ArrayList columnArray) throws GridPanelException {
67      JPanel outerPanel = new JPanel(new BorderLayout());
68      JPanel centerPanel = createCenterPanel(insets, columnArray);
69      outerPanel.add(centerPanel, BorderLayout.CENTER);
70      return createGridPanel(insets, columnArray, null, null);
71    }
72  
73  
74    private static JPanel createCenterPanel(Insets insets, ArrayList columnArray) throws GridPanelException {
75      GridBagLayout gbl = new GridBagLayout();
76      JPanel contents = new JPanel(gbl);
77      GridBagConstraints gbc = null;
78      gbc = new GridBagConstraints();
79      gbc.insets = insets;
80      assertEqualSizes(columnArray);
81      for (int i = 0; i < columnArray.size(); i++) {
82        GridPanelColumn gridPanelColumn = (GridPanelColumn) columnArray.get(i);
83        contents = addGridPanel(gridPanelColumn, i, contents, gbc, gbl);
84      }
85      return contents;
86    }
87  
88  
89    private static JPanel addGridPanel(GridPanelColumn gridPanelColumn, int horizontalPosition, JPanel panel, GridBagConstraints gbc, GridBagLayout gbl) {
90      for (int i = 0; i < gridPanelColumn.size(); i++) {
91        GridComponent gridComponent = gridPanelColumn.get(i);
92        gbc.gridx = horizontalPosition;
93        gbc.gridy = i;
94        if (gridComponent.getHorizontalResize()) {
95          gbc.weightx = 1.0;
96        } else {
97          gbc.weightx = 0.0;
98        }
99        if (gridComponent.getVerticalResize()) {
100         gbc.weighty = 1.0;
101       } else {
102         gbc.weighty = 0.0;
103       }
104       JComponent component = gridComponent.getJComponent();
105       component.setAlignmentX(gridComponent.getHorizontalAlignment());
106       component.setAlignmentY(gridComponent.getVerticalAlignment());
107       gbc.anchor = gridComponent.getGridBagConstraintsAnchor();
108       gbc.fill = gridComponent.getGridBagConstraintsFill();
109       gbl.setConstraints(component, gbc);
110       panel.add(component);
111     }
112     return panel;
113   }
114 }