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

net.anwiba.spatial.swing.ckan.search.SelectTableContentPane Maven / Gradle / Ivy

There is a newer version: 1.2.50
Show newest version
/*
 * #%L
 * *
 * %%
 * Copyright (C) 2007 - 2018 Andreas W. Bartels ([email protected])
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
package net.anwiba.spatial.swing.ckan.search;

import java.util.List;

import javax.swing.JComponent;

import net.anwiba.commons.lang.functional.IConverter;
import net.anwiba.commons.lang.optional.Optional;
import net.anwiba.commons.model.ISelectionListener;
import net.anwiba.commons.model.SelectionEvent;
import net.anwiba.commons.swing.dialog.DataState;
import net.anwiba.commons.swing.dialog.pane.AbstractContentPane;
import net.anwiba.commons.swing.table.IObjectTableModel;
import net.anwiba.commons.swing.table.ObjectListTable;
import net.anwiba.commons.swing.table.ObjectTableBuilder;
import net.anwiba.commons.swing.table.filter.IColumToStringConverter;
import net.anwiba.commons.utilities.collection.IterableUtilities;
import net.anwiba.spatial.swing.ckan.search.message.Messages;

public final class SelectTableContentPane extends AbstractContentPane {
  private final List selections;
  private final IObjectTableModel tableModel;
  private final List values;
  private final IConverter converter;

  public SelectTableContentPane(
      final List selections,
      final IObjectTableModel tableModel,
      final List values,
      final IConverter converter) {
    this.selections = selections;
    this.tableModel = tableModel;
    this.values = values;
    this.converter = converter;
  }

  @Override
  public JComponent getComponent() {
    final ObjectListTable table = new ObjectTableBuilder()
        .setFilterToStringConverter(new IColumToStringConverter() {

          @Override
          public int[] getFilterableColumnIndicies() {
            return new int[]{ 0 };
          }

          @Override
          public String convert(final int index, final Object value) {
            return Optional.of(value).convert(o -> o.toString()).get();
          }
        })
        .setValues(this.values)
        .addSortableStringColumn(Messages.name, value -> this.converter.convert(value), 10)
        .build();
    table.getSelectionModel().setSelectedObjects(IterableUtilities.asList(this.tableModel.values()));
    table.getSelectionModel().addSelectionListener(new ISelectionListener() {

      @Override
      public void selectionChanged(final SelectionEvent event) {
        getDataStateModel().set(DataState.MODIFIED);
        if (event.getSource().isEmpty()) {
          SelectTableContentPane.this.selections.clear();
        }
        SelectTableContentPane.this.selections.clear();
        event.getSource().getSelectedObjects().forEach(s -> SelectTableContentPane.this.selections.add(s));
      }
    });
    return table.getComponent();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy