Package com.nexirius.framework.layout

Source Code of com.nexirius.framework.layout.TableColumnLayoutItem$LayoutTableCellRenderer

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.layout;

import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.ModelFlag;
import com.nexirius.framework.datamodel.SimpleModel;
import com.nexirius.framework.dataviewer.DataViewer;
import com.nexirius.framework.dataviewer.LayoutItem;
import com.nexirius.framework.dataviewer.ViewerFactory;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;

public class TableColumnLayoutItem implements LayoutItem {



    String fieldIdentifier; // a ';' separated list of fieldnames naming a substructure element in a structure
    String columnLabel;
    int columnWidth;
    PropertyEntry properties = null;
    ViewerFactory factory = null;

    String modelFieldName[] = null;

    public TableColumnLayoutItem() {
        super();
    }

    /**
     * @param field       The associated structure field name (the name is direct field member name or a ';' separated list of substructure field names)
     * @param columnLabel this name is used to create the table header string for the specified column
     * @param columnWidth the initial width of the specified table column (when <= 0 a default width which depends on the total table width is assigned)
     */
    public TableColumnLayoutItem(String field, String columnLabel, int columnWidth) {
        super();
        this.fieldIdentifier = field;
        this.columnLabel = columnLabel;
        this.columnWidth = columnWidth;
    }

    /**
     * @param field       The associated structure field name (the name is direct field member name or a ';' separated list of substructure field names)
     * @param columnLabel this name is used to create the table header string for the specified column
     * @param columnWidth the initial width of the specified table column (when <= 0 a default width which depends on the total table width is assigned)
     * @param properties  the properties are changing the DefaultTableCellRenderer (which is a JLabel)
     */
    public TableColumnLayoutItem(String field, String columnLabel, int columnWidth, PropertyEntry properties) {
        this(field, columnLabel, columnWidth);
        this.properties = properties;
    }

    public void doLayout(DataViewer parent_viewer) {
        factory = parent_viewer.getFactory();
        JTable table = (JTable) ((JScrollPane) parent_viewer.getJComponent()).getViewport().getView();

        SwingUtilities.invokeLater(new SetColumnRenderer(table));
    }

    private class SetColumnRenderer implements Runnable {
        JTable table;

        SetColumnRenderer(JTable t) {
            table = t;
        }

        public void run() {
            TableColumn column = table.getColumn(factory.getText(columnLabel));

            column.setCellRenderer(getTableCellRenderer());

            if (columnWidth > 0) {
//                column.setMinWidth(columnWidth); // FIX
//                column.setMaxWidth(columnWidth); // FIX
                column.setPreferredWidth(columnWidth);
            }
        }
    }

    public TableCellRenderer getTableCellRenderer() {
        return new LayoutTableCellRenderer();
    }

    public String getColumnLabel() {
        return columnLabel;
    }

    public String getModelFieldName() {
        return fieldIdentifier;
    }

    public String[] getModelFieldNameArray() {
        if (modelFieldName == null) {
            modelFieldName = DataModel.getFieldNameArray(getModelFieldName());
        }

        return modelFieldName;
    }

    class LayoutTableCellRenderer extends JLabel implements TableCellRenderer {
        public int prevRow = -1;
        public int prevColumn = -1;
        public boolean prevIsSelected = false;

        public LayoutTableCellRenderer() {
            setOpaque(true);
            setBorder(new EmptyBorder(0, 3, 0, 3));
        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            try {
                prevRow = row;

                if (value instanceof DataModel) {
                    String iconId = ((DataModel) value).getIconId();

                    if (iconId != null) {
                        setIcon(factory.getClientResource().getIcon(iconId));
                    } else {
                        setIcon(null);
                    }
                } else {
                    setIcon(null);
                }

                if (value instanceof SimpleModel) {
                    SimpleModel model = (SimpleModel) value;

                    if (model.getFlag(ModelFlag.TRANSLATE_VALUE)) {

                        setText(factory.getText(model.getText()));
                    } else {
                        setText(value.toString());
                    }
                } else {
                    setText(value.toString());
                }

                prevColumn = column;
                prevIsSelected = isSelected;

                if (!isSelected) {
                    setBackground(SystemColor.text);
                    setForeground(SystemColor.textText);
                }

                if (properties != null) {
                    properties.change(this, factory);
                }

                if (!isSelected) {
                    Color background = null;

                    if (value instanceof DataModel) {
                        DataModel model = (DataModel) value;

                        String c = model.getBackgroundColorId();

                        if (c != null) {
                            background = factory.getClientResource().getColor(c);
                        } else {
                            Object rowModel = table.getModel().getValueAt(row, -1);

                            if (rowModel instanceof DataModel) {
                                c = ((DataModel)rowModel).getBackgroundColorId();

                                if (c != null) {
                                    background = factory.getClientResource().getColor(c);
                                }
                            }
                        }
                    }

                    if (background != null) {
                        setBackground(background);
                    }
                }

                if (isSelected) {
                    setBackground(SystemColor.textHighlight);
                    setForeground(SystemColor.textHighlightText);
                }
            } catch (Exception ex) {
//ignore
            }

            return this;
        }
    }
}
TOP

Related Classes of com.nexirius.framework.layout.TableColumnLayoutItem$LayoutTableCellRenderer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.