All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.jonestimd.swing.table.TableFactory Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
// The MIT License (MIT)
//
// Copyright (c) 2019 Timothy D. Jones
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package io.github.jonestimd.swing.table;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.RowSorter.SortKey;
import javax.swing.SortOrder;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableRowSorter;

import io.github.jonestimd.swing.component.ComboBoxCellEditor;
import io.github.jonestimd.swing.table.model.BeanListMultimapTableModel;
import io.github.jonestimd.swing.table.model.BeanListTableModel;
import io.github.jonestimd.swing.table.model.BeanTableModel;
import io.github.jonestimd.swing.table.model.ColumnAdapter;
import io.github.jonestimd.swing.table.model.ValidatedBeanListTableModel;
import io.github.jonestimd.swing.validation.ValidatingTextCellEditor;
import io.github.jonestimd.swing.validation.Validator;

/**
 * Provides factory methods for creating sorted and validated tables.
 */
public class TableFactory {
    private final TableInitializer tableInitializer;

    public TableFactory(TableInitializer tableInitializer) {
        this.tableInitializer = tableInitializer;
    }

    public > TableBuilder> tableBuilder(M model) {
        return new TableBuilder<>(model, new DecoratedTable<>(model));
    }

    @SuppressWarnings("unchecked")
    public > TableBuilder> validatedTableBuilder(M model) {
        DecoratedTable table = new DecoratedTable<>(model);
        Validator validationAdapter = value -> table.isEditing() ?
                model.validateAt(table.convertRowIndexToModel(table.getEditingRow()), table.convertColumnIndexToModel(table.getEditingColumn()), value) : null;
        for (int columnIndex = 0; columnIndex < model.getColumnCount(); columnIndex++) {
            Class columnClass = model.getColumnClass(columnIndex);
            ColumnAdapter columnIdentifier = model.getColumnIdentifier(columnIndex);
            if (columnClass == String.class) {
                table.getColumn(columnIdentifier).setCellEditor(new ValidatingTextCellEditor(table, validationAdapter));
            }
            else if (Enum.class.isAssignableFrom(columnClass)) {
                table.getColumn(columnIdentifier).setCellEditor(createEnumCellEditor((Class>) columnClass));
            }
        }
        return new TableBuilder<>(model, table).cellSelectionEnabled();
    }

    public  , T extends DecoratedTable> T initialize(T table) {
        return tableInitializer.initialize(table);
    }

    public RowSorter> newRowSorter(BeanTableModel model, List sortColumns) {
        TableRowSorter> sorter = new TableRowSorter<>(model);
        sorter.setSortsOnUpdates(true);
        if (sortColumns.size() > 0) {
            sorter.setSortKeys(sortColumns);
        }
        return sorter;
    }

    public > TableBuilder> sectionTableBuilder(M model) {
        return new TableBuilder<>(model, new SectionTable<>(model));
    }

    public static TableCellEditor createEnumCellEditor(Class> enumType) { // TODO use TranslatingComboBoxCellEditor
        return new ComboBoxCellEditor(new JComboBox<>(enumType.getEnumConstants()));
    }

    public static void addDoubleClickHandler(JTable table, Consumer handler) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent event) {
                super.mouseClicked(event);
                if (event.getButton() == MouseEvent.BUTTON1 && event.getClickCount() == 2) {
                    handler.accept(event);
                }
            }
        });
    }

    public class TableBuilder, T extends DecoratedTable> {
        protected final M model;
        protected final T table;
        private List sortKeys;

        protected TableBuilder(M model, T table) {
            this.model = model;
            this.table = table;
        }

        public TableBuilder sorted() {
            sortKeys = new ArrayList<>();
            return this;
        }

        public TableBuilder sortedBy(int... columns) {
            return sortedBy(SortOrder.ASCENDING, columns);
        }

        public TableBuilder sortedBy(SortOrder order, int... columns) {
            if (sortKeys == null) sortKeys = new ArrayList<>();
            for (int column : columns) {
                sortKeys.add(new SortKey(column, order));
            }
            return this;
        }

        public TableBuilder cellSelectionEnabled() {
            table.setCellSelectionEnabled(true);
            return this;
        }

        public TableBuilder doubleClickHandler(Consumer handler) {
            addDoubleClickHandler(table, handler);
            return this;
        }

        public T get() {
            if (sortKeys != null) {
                table.setAutoCreateRowSorter(true);  // because reusing the sorter when the model changes doesn't work
                table.setRowSorter(newRowSorter(model, sortKeys));
            }
            return initialize(table);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy