<commit_before>/*
 * Copyright 2013 <NAME>.
 *
 * 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 simplejpa.swing.glazed

import ca.odell.glazedlists.EventList
import ca.odell.glazedlists.SortedList
import ca.odell.glazedlists.impl.gui.SortingStrategy
import ca.odell.glazedlists.swing.AdvancedTableModel
import ca.odell.glazedlists.swing.GlazedListsSwing
import ca.odell.glazedlists.swing.TableComparatorChooser
import simplejpa.swing.glazed.renderer.DefaultTableHeaderRenderer
import javax.swing.*
import javax.swing.table.JTableHeader
import javax.swing.table.TableColumnModel
import java.awt.Color

class GlazedTable extends JTable {

    EventList eventList
    List<GlazedColumn> eventColumns = []
    SortingStrategy sortingStrategy
    TableComparatorChooser tableComparatorChooser
    Closure onValueChanged

    public GlazedTable() {
        this(null)
    }

    public GlazedTable(EventList eventList) {
        super()
        this.eventList = eventList
        getTableHeader().setDefaultRenderer(new JTableHeader().getDefaultRenderer())
        setRowSorter(null)
        setAutoCreateRowSorter(false)
        setAutoCreateColumnsFromModel(false)
    }

    public void addEventColumn(GlazedColumn eventColumn) {
        if (!eventColumn.modelIndex) {
            eventColumn.modelIndex = eventColumns.size()
        }
        eventColumn.identifier = eventColumn.modelIndex
        eventColumns << eventColumn
    }

    public void build() {

        // Build table model
        eventColumns.sort { it.modelIndex }
        GlazedTableFormat tableFormat = new GlazedTableFormat(eventColumns)
        if (sortingStrategy && !(eventList instanceof SortedList)) {
            eventList = new SortedList(eventList, new Comparator() {
                @Override
                int compare(Object o1, Object o2) {
                    0
                }
            })
        }
        AdvancedTableModel tableModel = GlazedListsSwing.eventTableModelWithThreadProxyList(eventList, tableFormat)
        setModel(tableModel)

        // Remove and add columns
        TableColumnModel cm = getColumnModel()
        while (cm.getColumnCount() > 0) {
            cm.removeColumn(cm.getColumn(0))
        }
        eventColumns.each{ GlazedColumn c ->
            if (!c.headerRenderer) c.headerRenderer = new DefaultTableHeaderRenderer()
            if (isInstanceOfNumber(c.columnClass)) {
                c.headerRenderer.addPropertyValue('horizontalAlignment', SwingConstants.RIGHT)
            } else {
                c.headerRenderer.addPropertyValue('horizontalAlignment', SwingConstants.LEFT)
            }
            addColumn(c)
        }

        // Add sorting
        if (sortingStrategy) {
            tableComparatorChooser = TableComparatorChooser.install(this, eventList, sortingStrategy)
        }

        // Event selection
        selectionModel = GlazedListsSwing.eventSelectionModelWithThreadProxyList(eventList)
        if (onValueChanged) {
            onValueChanged.resolveStrategy = Closure.DELEGATE_FIRST
            onValueChanged.delegate = this
            selectionModel.valueChanged = onValueChanged
        }<commit_msg>Add 'isRowSelected' property to GlazedTable.<commit_after>/*
 * Copyright 2013 <NAME>.
 *
 * 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 simplejpa.swing.glazed

import ca.odell.glazedlists.EventList
import ca.odell.glazedlists.SortedList
import ca.odell.glazedlists.impl.gui.SortingStrategy
import ca.odell.glazedlists.swing.AdvancedTableModel
import ca.odell.glazedlists.swing.GlazedListsSwing
import ca.odell.glazedlists.swing.TableComparatorChooser
import groovy.beans.Bindable
import simplejpa.swing.glazed.renderer.DefaultTableHeaderRenderer
import javax.swing.*
import javax.swing.table.JTableHeader
import javax.swing.table.TableColumnModel
import java.awt.Color

class GlazedTable extends JTable {

    EventList eventList
    List<GlazedColumn> eventColumns = []
    SortingStrategy sortingStrategy
    TableComparatorChooser tableComparatorChooser
    Closure onValueChanged
    @Bindable Boolean isRowSelected

    public GlazedTable() {
        this(null)
    }

    public GlazedTable(EventList eventList) {
        super()
        this.eventList = eventList
        getTableHeader().setDefaultRenderer(new JTableHeader().getDefaultRenderer())
        setRowSorter(null)
        setAutoCreateRowSorter(false)
        setAutoCreateColumnsFromModel(false)
    }

    public void addEventColumn(GlazedColumn eventColumn) {
        if (!eventColumn.modelIndex) {
            eventColumn.modelIndex = eventColumns.size()
        }
        eventColumn.identifier = eventColumn.modelIndex
        eventColumns << eventColumn
    }

    public void build() {

        // Build table model
        eventColumns.sort { it.modelIndex }
        GlazedTableFormat tableFormat = new GlazedTableFormat(eventColumns)
        if (sortingStrategy && !(eventList instanceof SortedList)) {
            eventList = new SortedList(eventList, new Comparator() {
                @Override
                int compare(Object o1, Object o2) {
                    0
                }
            })
        }
        AdvancedTableModel tableModel = GlazedListsSwing.eventTableModelWithThreadProxyList(eventList, tableFormat)
        setModel(tableModel)

        // Remove and add columns
        TableColumnModel cm = getColumnModel()
        while (cm.getColumnCount() > 0) {
            cm.removeColumn(cm.getColumn(0))
        }
        eventColumns.each{ GlazedColumn c ->
            if (!c.headerRenderer) c.headerRenderer = new DefaultTableHeaderRenderer()
            if (isInstanceOfNumber(c.columnClass)) {
                c.headerRenderer.addPropertyValue('horizontalAlignment', SwingConstants.RIGHT)
            } else {
                c.headerRenderer.addPropertyValue('horizontalAlignment', SwingConstants.LEFT)
            }
            addColumn(c)
        }

        // Add sorting
        if (sortingStrategy) {
            tableComparatorChooser = TableComparatorChooser.install(this, eventList, sortingStrategy)
        }

        // Event selection
        selectionModel = GlazedListsSwing.eventSelectionModelWithThreadProxyList(eventList)
        if (onValueChanged) {
            onValueChanged.delegate = this
            selectionModel.valueChanged = onValueChanged
            selectionModel.valueChanged = {
                isRowSelected = !selectionModel.isSelectionEmpty()
                firePropertyChange("isRowSelected", !isRowSelected, isRowSelected)
            }
            isRowSelected = false
        }
{'fraction_non_alphanumeric': 0.07412257726558408, 'fraction_numerical': 0.0034049240440020955, 'mean_word_length': 2.782565626547796, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3626101', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1945, 'n_words': 469}