Java tutorial
/* * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.kamhon.ieagle.swing.jgoodies; import java.awt.Component; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.swing.JLabel; import javax.swing.JPanel; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.jgoodies.forms.builder.DefaultFormBuilder; import com.jgoodies.forms.layout.CellConstraints; import com.jgoodies.forms.layout.CellConstraints.Alignment; import com.jgoodies.forms.layout.FormLayout; public class KFormBuilder implements FormBuilder { private static final Log log = LogFactory.getLog(KFormBuilder.class); public static final String DEFAULT_ENCODED_LABEL_COLUMN_SPEC = "left:pref"; public static final String DEFAULT_ENCODED_INPUT_COLUMN_SPEC = "pref:grow"; // public static final String DEFAULT_ENCODED_ROW_SPEC = "top:pref"; public static final String DEFAULT_ENCODED_ROW_SPEC = "pref"; public static final String DEFAULT_ENCODED_COLUMN_SPACING_SPEC = "3dlu"; public static final String DEFAULT_ENCODED_ROW_SPACING_SPEC = "2dlu"; private String[] encodedColSpec; private String defaultLabelCol; private String defaultInputCol; private String defaultRowSpec; private String encodedColSpacing; private String encodedRowSpacing; private List<RowDescriptor> rowDescriptors; private ColumnType columnType; /** * default is 2 columns */ public KFormBuilder() { encodedColSpec = new String[ColumnType.column6.columnCount]; encodedColSpacing = DEFAULT_ENCODED_COLUMN_SPACING_SPEC; encodedRowSpacing = DEFAULT_ENCODED_ROW_SPACING_SPEC; defaultRowSpec = DEFAULT_ENCODED_ROW_SPEC; defaultLabelCol = DEFAULT_ENCODED_LABEL_COLUMN_SPEC; defaultInputCol = DEFAULT_ENCODED_INPUT_COLUMN_SPEC; columnType = ColumnType.column2; rowDescriptors = new ArrayList<RowDescriptor>(); } @Override public JPanel getPanel() { String colSpec = ""; String rowSpec = ""; if (columnType.columnCount >= ColumnType.column2.columnCount) { if (StringUtils.isNotBlank(encodedColSpec[0])) { colSpec += encodedColSpec[0]; } else { colSpec += defaultLabelCol; } colSpec += ", " + DEFAULT_ENCODED_COLUMN_SPACING_SPEC + ", "; if (StringUtils.isNotBlank(encodedColSpec[1])) { colSpec += encodedColSpec[1]; } else { colSpec += defaultLabelCol; } } if (columnType.columnCount >= ColumnType.column4.columnCount) { colSpec += ", 6dlu, "; if (StringUtils.isNotBlank(encodedColSpec[2])) { colSpec += encodedColSpec[2]; } else { colSpec += defaultLabelCol; } colSpec += ", " + DEFAULT_ENCODED_COLUMN_SPACING_SPEC + ", "; if (StringUtils.isNotBlank(encodedColSpec[3])) { colSpec += encodedColSpec[3]; } else { colSpec += defaultLabelCol; } } if (columnType.columnCount >= ColumnType.column6.columnCount) { colSpec += ", 6dlu, "; if (StringUtils.isNotBlank(encodedColSpec[4])) { colSpec += encodedColSpec[4]; } else { colSpec += defaultLabelCol; } colSpec += ", " + DEFAULT_ENCODED_COLUMN_SPACING_SPEC + ", "; if (StringUtils.isNotBlank(encodedColSpec[5])) { colSpec += encodedColSpec[5]; } else { colSpec += defaultInputCol; } } log.debug("row size = " + rowDescriptors.size()); for (Iterator<RowDescriptor> iterator = rowDescriptors.iterator(); iterator.hasNext();) { RowDescriptor rowDescriptor = iterator.next(); if (StringUtils.isNotBlank(rowDescriptor.encodedRow)) rowSpec += rowDescriptor.encodedRow; else rowSpec += defaultRowSpec; if (iterator.hasNext()) { rowSpec += ", " + encodedRowSpacing + ", "; } } /*log.debug("colSpec = " + colSpec); log.debug("rowSpec = " + rowSpec);*/ FormLayout layout = new FormLayout(colSpec, rowSpec); DefaultFormBuilder builder = new DefaultFormBuilder(layout); builder.setDefaultDialogBorder(); CellConstraints cc = new CellConstraints(); for (int row = 0; row < rowDescriptors.size(); row++) { RowDescriptor rowDescriptor = rowDescriptors.get(row); int colCount = 0; int colSpan = 0; for (int col = 0; col < rowDescriptor.size(); col++) { ComponentWrapper compWrapper = rowDescriptor.get(col); colCount += col; if (compWrapper != null) { colSpan = compWrapper.colspan; Component comp = compWrapper.component; /*log.debug("comp = " + comp); log.debug("colSpan = " + colSpan); log.debug("getCorrespondingColumn(colCount) = " + getCorrespondingColumn(colCount)); log.debug("getCorrespondingColumnSpan(colSpan) = " + getCorrespondingColumnSpan(colSpan));*/ if (comp instanceof SeparatorComponent) { builder.addSeparator(((SeparatorComponent) comp).getText(), cc.xyw(getCorrespondingColumn(col), getCorrespondingRow(row), getCorrespondingColumnSpan(colSpan))); } else { if (compWrapper.colAlign != null && compWrapper.rowAlign != null) builder.add(comp, cc.xyw(getCorrespondingColumn(col), getCorrespondingRow(row), getCorrespondingColumnSpan(colSpan), compWrapper.colAlign, compWrapper.rowAlign)); else builder.add(comp, cc.xyw(getCorrespondingColumn(col), getCorrespondingRow(row), getCorrespondingColumnSpan(colSpan))); } colCount += colSpan - 1; } } } return builder.getPanel(); } @Override public void setEncodedColSpec(int columnIndex, String encodedColSpec) { this.encodedColSpec[columnIndex] = encodedColSpec; } @Override public void add(Component component) { add(component, 0); } @Override public void add(Component component, int columnIndex) { add(component, columnIndex, 1); } @Override public void add(Component component, int columnIndex, int colSpan) { add(component, columnIndex, colSpan, null, null); } @Override public void add(Component component, int columnIndex, int colSpan, Alignment colAlign, Alignment rowAlign) { if (colSpan <= 0) throw new IllegalStateException("colSpan must greater than 0"); /*log.debug("component = " + component); log.debug("columnIndex = " + columnIndex); log.debug("columnSpan = " + colSpan);*/ RowDescriptor rowDescriptor = null; if (rowDescriptors.size() > 0) rowDescriptor = rowDescriptors.get(rowDescriptors.size() - 1); if (rowDescriptor == null) { rowDescriptor = createNewRowDescriptior(null); rowDescriptor.add(columnIndex, new ComponentWrapper(component, colSpan, colAlign, rowAlign)); rowDescriptors.add(rowDescriptor); } else { // if the columnIndex already exists, the new component will put at new row if (rowDescriptor.size() > columnIndex && rowDescriptor.get(columnIndex) != null) { // create a new row rowDescriptor = createNewRowDescriptior(null); rowDescriptor.add(columnIndex, new ComponentWrapper(component, colSpan, colAlign, rowAlign)); rowDescriptors.add(rowDescriptor); } else { // if add index = 1 and size is 0 while (rowDescriptor.size() < columnIndex) { // add a empty label rowDescriptor.add(new ComponentWrapper(new JLabel(), 1, null, null)); } rowDescriptor.add(columnIndex, new ComponentWrapper(component, colSpan, colAlign, rowAlign)); } } } @Override public void newRow() { newRow(null); } @Override public void newRow(int noOfRow) { if (noOfRow > 0) { for (int i = 0; i < noOfRow; i++) { newRow(); } } } @Override public void newRow(String encodedRowSpec) { rowDescriptors.add(createNewRowDescriptior(encodedRowSpec)); } private RowDescriptor createNewRowDescriptior(String encodedRowSpec) { RowDescriptor rowDescriptor = new RowDescriptor(); rowDescriptor.encodedRow = encodedRowSpec; return rowDescriptor; } private int getCorrespondingRow(int row) { return (row + 1) * 2 - 1; } private int getCorrespondingColumn(int col) { return (col + 1) * 2 - 1; } private int getCorrespondingColumnSpan(int colSpan) { return colSpan * 2 - 1; } @Override public void addSeparator(String textWithMnemonic) { addSeparator(textWithMnemonic, 0, columnType.columnCount); } @Override public void addSeparator(String textWithMnemonic, int columnIndex, int colSpan) { add(new SeparatorComponent(textWithMnemonic), columnIndex, colSpan); } @Override public void addButtonPanel(JPanel buttonPanel) { add(buttonPanel, 0, columnType.columnCount, CellConstraints.CENTER, CellConstraints.TOP); } @Override public void addButtonPanel(JPanel buttonPanel, int columnIndex, int colSpan) { add(buttonPanel, columnIndex, colSpan, CellConstraints.CENTER, CellConstraints.TOP); } // ---------------- GETTER & SETTER (START) ------------- @Override public void setColumnType(ColumnType columnType) { this.columnType = columnType; } @Override public void setDefaultLabelCol(String defaultLabelCol) { this.defaultLabelCol = defaultLabelCol; } @Override public void setDefaultInputCol(String defaultInputCol) { this.defaultInputCol = defaultInputCol; } @Override public void setDefaultColSpacing(String encodedColumnSpacingSpec) { this.encodedColSpacing = encodedColumnSpacingSpec; } @Override public void setDefaultRowSpacing(String encodedRowSpacingSpec) { this.encodedRowSpacing = encodedRowSpacingSpec; } public String getEncodedColumnSpacingSpec() { return encodedColSpacing; } public void setEncodedColumnSpacingSpec(String encodedColumnSpacingSpec) { this.encodedColSpacing = encodedColumnSpacingSpec; } public String getEncodedRowSpacingSpec() { return encodedRowSpacing; } public void setEncodedRowSpacingSpec(String encodedRowSpacingSpec) { this.encodedRowSpacing = encodedRowSpacingSpec; } // ---------------- GETTER & SETTER (END) --------------- private class SeparatorComponent extends JLabel { private static final long serialVersionUID = 1L; private SeparatorComponent(String text) { super(text); } } private class RowDescriptor extends ArrayList<ComponentWrapper> { private static final long serialVersionUID = 1L; private String encodedRow; public RowDescriptor() { } } private class ComponentWrapper { private int colspan = 1; private Component component; private Alignment colAlign; private Alignment rowAlign; public ComponentWrapper(Component component, int colspan, Alignment colAlign, Alignment rowAlign) { this.component = component; this.colspan = colspan; this.colAlign = colAlign; this.rowAlign = rowAlign; } } }