Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

LayoutForm.java

Go to the documentation of this file.
00001 
00066 package com.arcle.rmt.superwaba.ui;
00067 
00068 import superwaba.ext.xplat.ui.MultiEdit;
00069 import waba.ui.Container;
00070 import waba.ui.Label;
00071 import waba.ui.Control;
00072 import waba.ui.Edit;
00073 import waba.fx.Font;
00074 
00082 public abstract class LayoutForm extends Form {
00083     public LayoutForm() {
00084         setBorderStyle(BORDER_SIMPLE);
00085     }
00086 
00093     protected abstract Control[][] getFormControls();
00094 
00095 
00105     protected void layoutControls(Container form, Control[][] controls) {
00106         int maxLabelWidth = 1;
00107         final int xGap = 1;
00108         final int yGap = 1;
00109 
00110         int xWrapTreshold = (form.getSize().width()) / 3;
00111 
00112         Control prevLabel = null;   // placed on the left
00113         Control prevField = null;
00114 
00115         // find maximum size of the label
00116         for(int i=0; i<controls.length; i++) {
00117             Control[] row = controls[i];
00118             Control label = row[0];
00119             Control field = row[1];
00120             int preferred = label.getPreferredWidth();
00121             if(maxLabelWidth < preferred && preferred < xWrapTreshold) {
00122                 maxLabelWidth = preferred;
00123             }
00124         }
00125 
00126         // layout the next controls
00127         int labelX, labelY, labelWidth, labelHeight;
00128         int fieldX, fieldY, fieldWidth, fieldHeight;
00129         boolean wasFull = false;
00130         for(int i=0; i<controls.length; i++) {
00131             Control[] row = controls[i];
00132             Control label = row[0];
00133             Control field = row[1];
00134 
00135             labelX = Control.LEFT + xGap;
00136 
00137             if (prevLabel == null) {
00138                // first row?
00139                 labelY = yGap;
00140             } else {
00141                 labelY = Control.AFTER + yGap;
00142             }
00143 
00144 
00145             Control labelRelative;
00146             Control fieldRelative;
00147 
00148             if (isTextEntry(field) && (
00149                   label.getPreferredWidth() >= xWrapTreshold
00150                   || label.getPreferredWidth() < field.getPreferredHeight()
00151                  ) ||
00152                  isMultilineTextEntry(field)) {
00153                 // place label on its own row
00154                 labelX = Control.LEFT + xGap;
00155                 labelY = Control.AFTER + yGap;
00156                 labelWidth = Control.PREFERRED;
00157                 labelHeight = Control.PREFERRED;
00158                 labelRelative = prevField;
00159 
00160                 // place field below label.
00161                 fieldX = Control.LEFT + xGap;
00162                 fieldY = Control.AFTER + yGap;
00163                 fieldWidth = Control.FILL - xGap;
00164                 fieldHeight = Control.PREFERRED;
00165                 wasFull = true;
00166 
00167             } else {
00168                 labelWidth = maxLabelWidth;
00169                 labelHeight = field.getPreferredHeight();
00170 
00171                 if (wasFull) {
00172                     labelRelative = prevField;
00173                     wasFull = false;
00174 
00175                 } else {
00176                     labelRelative = prevLabel;
00177                 }
00178 
00179                 // place field to the right of label
00180                 fieldX = Control.AFTER + xGap;
00181                 fieldY = Control.SAME;
00182                 fieldWidth = (isTextEntry(field) ? Control.FILL
00183                                 : Control.PREFERRED) - xGap;
00184                 fieldHeight = Control.SAME;
00185 
00186             }
00187 
00188             form.add(label);
00189             label.setRect(labelX, labelY, labelWidth, labelHeight,
00190                 labelRelative);
00191 
00192             form.add(field);
00193             field.setRect(fieldX, fieldY, fieldWidth, fieldHeight, label);
00194 
00195             prevLabel = label;
00196             prevField = field;
00197         }
00198     }
00199 
00200 
00205     protected boolean isTextEntry(Control ctrl) {
00206         return ctrl instanceof Edit || ctrl instanceof MultiEdit;
00207     }
00208 
00209     protected boolean isMultilineTextEntry(Control ctrl) {
00210         return ctrl instanceof MultiEdit;
00211     }
00212 
00213 
00217     protected void layoutChildren() {
00218         Control[][] children = getFormControls();
00219         layoutControls(this, children);
00220     }
00221 
00222 
00226     protected Label createLabel(String caption) {
00227         Label lb = new Label(caption, Control.RIGHT);
00228         Font f = lb.getFont();
00229         lb.setFont(f.asBold());
00230         return lb;
00231     }
00232 
00236     protected MultiEdit createMultiEdit(String text, int rowCount,
00237         int spaceBetweenLines) {
00238         return new MultiEdit(text, rowCount, spaceBetweenLines);
00239     }
00240 
00244     protected Edit createEdit(String text) {
00245         return new Edit(text);
00246     }
00247 
00248 }

Generated on Fri Jun 18 19:51:22 2004 for Arcle Rambutan by doxygen 1.3.5