001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging;
003
004import javax.swing.ListSelectionModel;
005import javax.swing.table.DefaultTableColumnModel;
006import javax.swing.table.TableCellEditor;
007import javax.swing.table.TableCellRenderer;
008import javax.swing.table.TableColumn;
009
010import org.openstreetmap.josm.tools.CheckParameterUtil;
011
012/**
013 * Builder class allowing to construct customized tag table column models.
014 * All columns are resizable and share the same renderer.
015 * @since 9847
016 */
017public class TagTableColumnModelBuilder {
018
019    private final DefaultTableColumnModel model = new DefaultTableColumnModel();
020
021    /**
022     * Construct a new {@code TagTableColumnModelBuilder}.
023     * @param renderer rendered used for all columns
024     * @param headerValues header values of each column, determining the number of columns
025     * @see TableColumn#setHeaderValue
026     * @see TableColumn#setCellRenderer
027     */
028    public TagTableColumnModelBuilder(TableCellRenderer renderer, String ... headerValues) {
029        CheckParameterUtil.ensureParameterNotNull(headerValues, "headerValues");
030        for (int i = 0; i < headerValues.length; i++) {
031            TableColumn col = new TableColumn(i);
032            col.setHeaderValue(headerValues[i]);
033            col.setResizable(true);
034            col.setCellRenderer(renderer);
035            model.addColumn(col);
036        }
037    }
038
039    /**
040     * Sets width of specified columns.
041     * @param width the new width
042     * @param indexes indexes of columns to setup
043     * @return {@code this}
044     * @see TableColumn#setWidth
045     */
046    public TagTableColumnModelBuilder setWidth(int width, int ... indexes) {
047        for (int i : indexes) {
048            model.getColumn(i).setWidth(width);
049        }
050        return this;
051    }
052
053    /**
054     * Sets preferred width of specified columns.
055     * @param width the new width
056     * @param indexes indexes of columns to setup
057     * @return {@code this}
058     * @see TableColumn#setPreferredWidth
059     */
060    public TagTableColumnModelBuilder setPreferredWidth(int width, int ... indexes) {
061        for (int i : indexes) {
062            model.getColumn(i).setPreferredWidth(width);
063        }
064        return this;
065    }
066
067    /**
068     * Sets max width of specified columns.
069     * @param width the new width
070     * @param indexes indexes of columns to setup
071     * @return {@code this}
072     * @see TableColumn#setMaxWidth
073     */
074    public TagTableColumnModelBuilder setMaxWidth(int width, int ... indexes) {
075        for (int i : indexes) {
076            model.getColumn(i).setMaxWidth(width);
077        }
078        return this;
079    }
080
081    /**
082     * Sets cell editor of specified columns.
083     * @param editor the new cell editor
084     * @param indexes indexes of columns to setup
085     * @return {@code this}
086     * @see TableColumn#setCellEditor
087     */
088    public TagTableColumnModelBuilder setCellEditor(TableCellEditor editor, int ... indexes) {
089        for (int i : indexes) {
090            model.getColumn(i).setCellEditor(editor);
091        }
092        return this;
093    }
094
095    /**
096     * Sets selection model.
097     * @param selectionModel new selection model
098     * @return {@code this}
099     * @see DefaultTableColumnModel#setSelectionModel
100     */
101    public TagTableColumnModelBuilder setSelectionModel(ListSelectionModel selectionModel) {
102        model.setSelectionModel(selectionModel);
103        return this;
104    }
105
106    /**
107     * Returns the new tag table column model.
108     * @return the new tag table column model
109     */
110    public DefaultTableColumnModel build() {
111        return model;
112    }
113}