<commit_before>/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.cuba.desktop.gui.components;

import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.desktop.gui.data.TreeTableModelAdapter;
import com.haulmont.cuba.desktop.sys.vcl.JXTreeTableExt;
import com.haulmont.cuba.gui.components.TreeTable;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.HierarchicalDatasource;
import com.haulmont.cuba.gui.data.impl.CollectionDsHelper;
import org.jdesktop.swingx.JXTree;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.decorator.AbstractHighlighter;
import org.jdesktop.swingx.decorator.ComponentAdapter;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.util.*;

/**
 * @author krivopustov
 * @version $Id$
 */
public class DesktopTreeTable<E extends Entity>
        extends DesktopAbstractTable<JXTreeTableExt, E>
        implements TreeTable<E> {

    protected String hierarchyProperty;

    public DesktopTreeTable() {
        impl = new JXTreeTableExt() {
            @Override
            public TableCellRenderer getCellRenderer(int row, int column) {
                TableCellRenderer columnRenderer = DesktopTreeTable.this.getColumnRenderer(column);
                if (columnRenderer != null) {
                    return columnRenderer;
                }

                if (styleProviders != null) {
                    TableCellRenderer defaultRenderer = super.getCellRenderer(row, column);
                    return new StylingCellRenderer(defaultRenderer);
                } else {
                    return super.getCellRenderer(row, column);
                }
            }

            @Override
            public TableCellEditor getCellEditor(int row, int column) {
                TableCellEditor cellEditor = getColumnEditor(column);
                if (cellEditor != null) {
                    return cellEditor;
                }

                return super.getCellEditor(row, column);
            }

            @Override
            public void setFont(Font font) {
                super.setFont(font);
                applyFont(this, font);
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                if (column < 0 || row < 0) {
                    return false;
                }

                if (DesktopTreeTable.this.isCustomCellEditable(row, column)) {
                    return true;
                }

                DesktopTreeTable<E> treeTable = DesktopTreeTable.this;
                Column editColumn = treeTable.getColumns().get(column);
                return (treeTable.isEditable() && editColumn.isEditable())
                        || tableModel.isGeneratedColumn(editColumn);
            }

            /* Copies the JTable standard behaviour, that was overridden in JXTreeTable.
             */
            @Override
            public void setRowSorter(RowSorter<? extends TableModel> sorter) {
                setTableRowSorter(sorter);
            }

            /**
             * Default implementation uses row sorter to return rows count,
             * but there is nonconformity in how RowSorterImpl and tree table wrapper counts rows.
             * Absence of this method will lead to nontrivial NPE with obscure stack trace.
             */
            @Override
            public int getRowCount() {
                if (datasource != null) {
                    CollectionDsHelper.autoRefreshInvalid(datasource, true);
                }

                return getModel().getRowCount();
            }

            @Override
            public void editingStopped(ChangeEvent e) {
                TableCellEditor editor = getCellEditor();
                if (editor != null) {
                    Object value = editor.getCellEditorValue();
                    DesktopTreeTable<E> tableComponent = DesktopTreeTable.this;
                    Column editColumn = tableComponent.getColumns().get(editingColumn);

                    if (!(editor instanceof DesktopAbstractTable.EditableColumnTableCellEditor)) {
                        if (tableComponent.isEditable() && editColumn.isEditable() && !tableModel.isGeneratedColumn(editColumn)) {
                            setValueAt(value, editingRow, editingColumn);
                        }
                    }
                    removeEditor();
                }
            }

//            @Override
//            protected void beforeDelayedStructureChange() {
//                ((com.haulmont.cuba.desktop.gui.data.TreeTableModelAdapter) tableModel).beforeDelayedStructureChange();
//            }
//
//            @Override
//            protected void afterDelayedStructureChange() {
//                ((com.haulmont.cuba.desktop.gui.data.TreeTableModelAdapter) tableModel).afterDelayedStructureChange();
//            }
        };

        impl.addTreeExpansionListener(new TreeExpansionListener() {
            @Override<commit_msg>Standard Edit action doesn't become enabled when used with TreeTable #PL-6527<commit_after>/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.cuba.desktop.gui.components;

import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.desktop.gui.data.TreeTableModelAdapter;
import com.haulmont.cuba.desktop.sys.vcl.JXTreeTableExt;
import com.haulmont.cuba.gui.components.TreeTable;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.HierarchicalDatasource;
import com.haulmont.cuba.gui.data.impl.CollectionDsHelper;
import org.jdesktop.swingx.JXTree;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.decorator.AbstractHighlighter;
import org.jdesktop.swingx.decorator.ComponentAdapter;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.util.*;

/**
 * @author krivopustov
 * @version $Id$
 */
public class DesktopTreeTable<E extends Entity> extends DesktopAbstractTable<JXTreeTableExt, E> implements TreeTable<E> {

    protected String hierarchyProperty;

    public DesktopTreeTable() {
        impl = new JXTreeTableExt() {
            @Override
            public TableCellRenderer getCellRenderer(int row, int column) {
                TableCellRenderer columnRenderer = DesktopTreeTable.this.getColumnRenderer(column);
                if (columnRenderer != null) {
                    return columnRenderer;
                }

                if (styleProviders != null) {
                    TableCellRenderer defaultRenderer = super.getCellRenderer(row, column);
                    return new StylingCellRenderer(defaultRenderer);
                } else {
                    return super.getCellRenderer(row, column);
                }
            }

            @Override
            public TableCellEditor getCellEditor(int row, int column) {
                TableCellEditor cellEditor = getColumnEditor(column);
                if (cellEditor != null) {
                    return cellEditor;
                }

                return super.getCellEditor(row, column);
            }

            @Override
            public void setFont(Font font) {
                super.setFont(font);
                applyFont(this, font);
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                if (column < 0 || row < 0) {
                    return false;
                }

                if (DesktopTreeTable.this.isCustomCellEditable(row, column)) {
                    return true;
                }

                DesktopTreeTable<E> treeTable = DesktopTreeTable.this;
                Column editColumn = treeTable.getColumns().get(column);
                return (treeTable.isEditable() && editColumn.isEditable())
                        || tableModel.isGeneratedColumn(editColumn);
            }

            /* Copies the JTable standard behaviour, that was overridden in JXTreeTable.
             */
            @Override
            public void setRowSorter(RowSorter<? extends TableModel> sorter) {
                setTableRowSorter(sorter);
            }

            /**
             * Default implementation uses row sorter to return rows count,
             * but there is nonconformity in how RowSorterImpl and tree table wrapper counts rows.
             * Absence of this method will lead to nontrivial NPE with obscure stack trace.
             */
            @Override
            public int getRowCount() {
                if (datasource != null) {
                    CollectionDsHelper.autoRefreshInvalid(datasource, true);
                }

                return getModel().getRowCount();
            }

            @Override
            public void editingStopped(ChangeEvent e) {
                TableCellEditor editor = getCellEditor();
                if (editor != null) {
                    Object value = editor.getCellEditorValue();
                    DesktopTreeTable<E> tableComponent = DesktopTreeTable.this;
                    Column editColumn = tableComponent.getColumns().get(editingColumn);

                    if (!(editor instanceof DesktopAbstractTable.EditableColumnTableCellEditor)) {
                        if (tableComponent.isEditable() && editColumn.isEditable() && !tableModel.isGeneratedColumn(editColumn)) {
                            setValueAt(value, editingRow, editingColumn);
                        }
                    }
                    removeEditor();
                }
            }

            @Override
            protected void beforeDelayedStructureChange() {
                ((com.haulmont.cuba.desktop.gui.data.TreeTableModelAdapter) tableModel).beforeDelayedStructureChange();
            }

            @Override
            protected void afterDelayedStructureChange() {
                ((com.haulmont.cuba.desktop.gui.data.TreeTableModelAdapter) tableModel).afterDelayedStructureChange();
            }
        };

        impl.addTreeExpansionListener(new TreeExpansionListener() {
            @Override
{'fraction_non_alphanumeric': 0.08561580026085336, 'fraction_numerical': 0.0022358859698155395, 'mean_word_length': 1.9314582195521572, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, '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': '3113459', 'n_tokens_mistral': 2795, 'n_tokens_neox': 2573, 'n_words': 563}