[9847] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.tagging;
|
---|
| 3 |
|
---|
| 4 | import javax.swing.ListSelectionModel;
|
---|
| 5 | import javax.swing.table.DefaultTableColumnModel;
|
---|
| 6 | import javax.swing.table.TableCellEditor;
|
---|
| 7 | import javax.swing.table.TableCellRenderer;
|
---|
| 8 | import javax.swing.table.TableColumn;
|
---|
| 9 |
|
---|
| 10 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Builder class allowing to construct customized tag table column models.
|
---|
| 14 | * All columns are resizable and share the same renderer.
|
---|
| 15 | * @since 9847
|
---|
| 16 | */
|
---|
| 17 | public class TagTableColumnModelBuilder {
|
---|
| 18 |
|
---|
| 19 | private final DefaultTableColumnModel model = new DefaultTableColumnModel();
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * Construct a new {@code TagTableColumnModelBuilder}.
|
---|
| 23 | * @param renderer rendered used for all columns
|
---|
| 24 | * @param headerValues header values of each column, determining the number of columns
|
---|
| 25 | * @see TableColumn#setHeaderValue
|
---|
| 26 | * @see TableColumn#setCellRenderer
|
---|
| 27 | */
|
---|
[11747] | 28 | public TagTableColumnModelBuilder(TableCellRenderer renderer, String... headerValues) {
|
---|
[9847] | 29 | CheckParameterUtil.ensureParameterNotNull(headerValues, "headerValues");
|
---|
| 30 | for (int i = 0; i < headerValues.length; i++) {
|
---|
| 31 | TableColumn col = new TableColumn(i);
|
---|
| 32 | col.setHeaderValue(headerValues[i]);
|
---|
| 33 | col.setResizable(true);
|
---|
| 34 | col.setCellRenderer(renderer);
|
---|
| 35 | model.addColumn(col);
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Sets width of specified columns.
|
---|
| 41 | * @param width the new width
|
---|
| 42 | * @param indexes indexes of columns to setup
|
---|
| 43 | * @return {@code this}
|
---|
| 44 | * @see TableColumn#setWidth
|
---|
| 45 | */
|
---|
[11747] | 46 | public TagTableColumnModelBuilder setWidth(int width, int... indexes) {
|
---|
[9847] | 47 | for (int i : indexes) {
|
---|
| 48 | model.getColumn(i).setWidth(width);
|
---|
| 49 | }
|
---|
| 50 | return this;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Sets preferred width of specified columns.
|
---|
| 55 | * @param width the new width
|
---|
| 56 | * @param indexes indexes of columns to setup
|
---|
| 57 | * @return {@code this}
|
---|
| 58 | * @see TableColumn#setPreferredWidth
|
---|
| 59 | */
|
---|
[11747] | 60 | public TagTableColumnModelBuilder setPreferredWidth(int width, int... indexes) {
|
---|
[9847] | 61 | for (int i : indexes) {
|
---|
| 62 | model.getColumn(i).setPreferredWidth(width);
|
---|
| 63 | }
|
---|
| 64 | return this;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Sets max width of specified columns.
|
---|
| 69 | * @param width the new width
|
---|
| 70 | * @param indexes indexes of columns to setup
|
---|
| 71 | * @return {@code this}
|
---|
| 72 | * @see TableColumn#setMaxWidth
|
---|
| 73 | */
|
---|
[11747] | 74 | public TagTableColumnModelBuilder setMaxWidth(int width, int... indexes) {
|
---|
[9847] | 75 | for (int i : indexes) {
|
---|
| 76 | model.getColumn(i).setMaxWidth(width);
|
---|
| 77 | }
|
---|
| 78 | return this;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Sets cell editor of specified columns.
|
---|
| 83 | * @param editor the new cell editor
|
---|
| 84 | * @param indexes indexes of columns to setup
|
---|
| 85 | * @return {@code this}
|
---|
| 86 | * @see TableColumn#setCellEditor
|
---|
| 87 | */
|
---|
[11747] | 88 | public TagTableColumnModelBuilder setCellEditor(TableCellEditor editor, int... indexes) {
|
---|
[9847] | 89 | for (int i : indexes) {
|
---|
| 90 | model.getColumn(i).setCellEditor(editor);
|
---|
| 91 | }
|
---|
| 92 | return this;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * Sets selection model.
|
---|
| 97 | * @param selectionModel new selection model
|
---|
| 98 | * @return {@code this}
|
---|
| 99 | * @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 | }
|
---|