1 46 package groovy.swing.impl; 47 48 import java.awt.Component ; 49 import java.awt.GridBagConstraints ; 50 import java.util.logging.Level ; 51 import java.util.logging.Logger ; 52 53 59 public class TableLayoutCell implements ContainerFacade { 60 61 protected static final Logger log = Logger.getLogger(TableLayoutCell.class.getName()); 62 63 private TableLayoutRow parent; 64 private Component component; 65 private GridBagConstraints constraints; 66 private String align; 67 private String valign; 68 private int colspan = 1; 69 private int rowspan = 1; 70 private boolean colfill = false; 71 private boolean rowfill = false; 72 73 74 public TableLayoutCell(TableLayoutRow parent) { 75 this.parent = parent; 76 } 77 78 public void addComponent(Component component) { 79 if (this.component != null) { 80 log.log(Level.WARNING, "This td cell already has a component: " + component); 81 } 82 this.component = component; 83 parent.addCell(this); 84 } 85 86 public Component getComponent() { 87 return component; 88 } 89 90 93 public void setAlign(String align) { 94 this.align = align; 95 } 96 97 100 public void setValign(String valign) { 101 this.valign = valign; 102 } 103 104 105 108 public void setColspan(int colspan) { 109 this.colspan = colspan; 110 } 111 112 115 public void setRowspan(int rowspan) { 116 this.rowspan = rowspan; 117 } 118 119 123 public boolean isColfill() { 124 return colfill; 125 } 126 127 131 public boolean isRowfill() { 132 return rowfill; 133 } 134 135 138 public void setColfill(boolean colfill) { 139 this.colfill = colfill; 140 } 141 142 145 public void setRowfill(boolean rowfill) { 146 this.rowfill = rowfill; 147 } 148 149 150 153 public GridBagConstraints getConstraints() { 154 if (constraints == null) { 155 constraints = createConstraints(); 156 } 157 return constraints; 158 } 159 160 163 protected GridBagConstraints createConstraints() { 164 GridBagConstraints answer = new GridBagConstraints (); 165 answer.anchor = getAnchor(); 166 if (colspan < 1) { 167 colspan = 1; 168 } 169 if (rowspan < 1) { 170 rowspan = 1; 171 } 172 if (isColfill()) { 173 answer.fill = isRowfill() 174 ? GridBagConstraints.BOTH 175 : GridBagConstraints.HORIZONTAL; 176 } 177 else { 178 answer.fill = isRowfill() 179 ? GridBagConstraints.VERTICAL 180 : GridBagConstraints.NONE; 181 } 182 answer.weightx = 0.2; 183 answer.weighty = 0; 184 answer.gridwidth = colspan; 185 answer.gridheight = rowspan; 186 return answer; 187 } 188 189 192 protected int getAnchor() { 193 boolean isTop = "top".equalsIgnoreCase(valign); 194 boolean isBottom = "bottom".equalsIgnoreCase(valign); 195 196 if ("center".equalsIgnoreCase(align)) { 197 if (isTop) { 198 return GridBagConstraints.NORTH; 199 } 200 else if (isBottom) { 201 return GridBagConstraints.SOUTH; 202 } 203 else { 204 return GridBagConstraints.CENTER; 205 } 206 } 207 else if ("right".equalsIgnoreCase(align)) { 208 if (isTop) { 209 return GridBagConstraints.NORTHEAST; 210 } 211 else if (isBottom) { 212 return GridBagConstraints.SOUTHEAST; 213 } 214 else { 215 return GridBagConstraints.EAST; 216 } 217 } 218 else { 219 if (isTop) { 221 return GridBagConstraints.NORTHWEST; 222 } 223 else if (isBottom) { 224 return GridBagConstraints.SOUTHWEST; 225 } 226 else { 227 return GridBagConstraints.WEST; 228 } 229 } 230 } 231 } 232 | Popular Tags |