001 package jagafa.util.view;
002
003 import java.awt.Component;
004 import java.awt.GridLayout;
005 import java.util.LinkedList;
006 import java.util.List;
007
008 import javax.swing.JComponent;
009 import javax.swing.JPanel;
010
011 public class GridPanel extends JPanel {
012 private List<RowPanel> rows_;
013
014 /**
015 *
016 */
017 private static final long serialVersionUID = -56984613549427868L;
018
019 public GridPanel() {
020 super();
021 this.rows_ = new LinkedList<RowPanel>();
022 }
023
024 /**
025 * Update the GridPanel. Called after every insert or delete.
026 *
027 */
028 private void update() {
029 if (this.rowCount() == 0) {
030 return;
031 }
032 this.removeAll();
033
034 this.setLayout(new GridLayout(this.rowCount(), 1));
035
036 for (int i = 0; i < this.rowCount(); i++) {
037 RowPanel row = this.getRowInternal(i);
038 this.add(row);
039 }
040 }
041
042 /**
043 * Returns the component at the n-th row
044 */
045 public JPanel getComponent(int row) {
046 if (row >= this.rows_.size()) {
047 return null;
048 }
049 return this.rows_.get(row);
050 }
051
052 /**
053 * Add a new row with n columns
054 * @param columns
055 */
056 public void addRow(int columns) {
057 RowPanel row = new RowPanel(columns);
058 this.rows_.add(row);
059 update();
060 }
061
062 /**
063 * Add a row with a single column
064 *
065 */
066 public void addRow() {
067 this.addRow(1);
068
069 }
070
071 /**
072 * Add the RowPanel specified
073 * @param row
074 */
075 public void addRow(RowPanel row) {
076 this.rows_.add(row);
077
078 }
079
080
081 /**
082 * Remove the row at index i
083 * @param index
084 */
085 public void removeRow(int index) {
086 if (index >= this.rows_.size()) {
087 return;
088 }
089 this.rows_.remove(index);
090 update();
091 }
092
093 /**
094 * Get the number of rows in the panel
095 */
096 public int rowCount() {
097 return this.rows_.size();
098 }
099
100 /**
101 * Set the component at (column, row) to the component specified
102 * @param column
103 * @param row
104 * @param component
105 */
106 public void setComponent(int column, int row, JComponent component) {
107 if (row >= this.rows_.size()) {
108 return;
109 }
110 this.getRowInternal(row).setComponent(column, component);
111 update();
112 }
113
114 private RowPanel getRowInternal(int row) {
115 if (row >= this.rows_.size()) {
116 return null;
117 }
118 return this.rows_.get(row);
119 }
120
121
122 /**
123 * Override super methods for safety reasons
124 *
125 */
126 @Override
127 public Component add(Component arg0, int arg1) {
128 // TODO Auto-generated method stub
129 return arg0;
130 }
131
132 @Override
133 public void add(Component arg0, Object arg1, int arg2) {
134
135 }
136
137 @Override
138 public void add(Component arg0, Object arg1) {
139
140 }
141
142 @Override
143 public Component add(Component arg0) {
144 return super.add(arg0);
145 }
146
147 @Override
148 public Component add(String arg0, Component arg1) {
149 return arg1;
150 }
151
152 @Override
153 public int getComponentCount() {
154 return this.componentCount();
155 }
156
157 @Override
158 public Component[] getComponents() {
159 Component comps[] = new Component[this.componentCount()];
160 this.rows_.toArray(comps);
161 return comps;
162 }
163
164 private int componentCount() {
165 return this.rowCount();
166 }
167
168
169 }