001 package jagafa.util.view;
002
003 import java.awt.GridLayout;
004 import java.util.LinkedList;
005 import java.util.List;
006
007 import javax.swing.JComponent;
008 import javax.swing.JPanel;
009
010 public class RowPanel extends JPanel {
011
012 /**
013 *
014 */
015 private static final long serialVersionUID = -3015307949913335671L;
016
017 private int columns_;
018
019 private List<JComponent> components_;
020
021 public RowPanel(int columns) {
022 super();
023 this.columns_ = columns;
024 this.components_ = new LinkedList<JComponent>();
025
026 for (int i = 0; i < columns; i++) {
027 this.components_.add(null);
028 }
029 }
030
031 /**
032 * Get the number of columns in the row
033 * @return
034 */
035 public int columnCount() {
036 return this.columns_;
037 }
038
039 /**
040 * Get the component at the n-th column of the row
041 * @return The component at column col.
042 * If it is not set (=null), return an empty JPanel object
043 */
044 public JComponent getComponent(int col) {
045 if (col >= this.columns_) {
046 return null;
047 }
048 JComponent comp = this.components_.get(col);
049 if (comp == null) {
050 return new JPanel();
051 }
052 return comp;
053 }
054
055 /**
056 * Set the component at column i to the component specified
057 * @param column
058 * @param component
059 */
060 public void setComponent(int column, JComponent component) {
061 if (column >= this.columns_) {
062 return;
063 }
064 this.components_.set(column, component);
065 update();
066 }
067
068 /**
069 * Adds a component to the next free column in the row
070 * @param component
071 */
072 public void addComponent(JComponent component) {
073
074 for (int i = 0; i < this.columns_; i++) {
075 if (this.components_.get(i) == null) {
076 this.setComponent(i, component);
077
078 return;
079 }
080 }
081
082 }
083
084 /**
085 * Remove the component at the column specified
086 * @param column
087 */
088 public void removeComponent(int column) {
089 if (column >= this.columns_) {
090 return;
091 }
092 this.components_.set(column, null);
093 update();
094 }
095
096 /**
097 * Internally update the RowPanel. Called after every change
098 *
099 */
100 private void update() {
101
102 this.removeAll();
103
104 this.setLayout(new GridLayout(1, this.columnCount()));
105
106 for (int j = 0; j < this.columnCount(); j++) {
107 JComponent comp = this.getComponent(j);
108 this.add(comp);
109 }
110
111 }
112 }