net.sourceforge.msscodefactory.cfacc.v2_0.CFAccSwing.CFAccSwingAccountConfigFinderJPanel.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfacc.v2_0.CFAccSwing.CFAccSwingAccountConfigFinderJPanel.java

Source

// Description: Java 7 Swing Finder JPanel implementation for AccountConfig.

/*
 *   MSS Code Factory Accounting Business Application Model
 *
 *   Copyright (c) 2014 Mark Sobkow
 *   
 *   This program is available as free software under the GNU GPL v3, or
 *   under a commercial license from Mark Sobkow.  For commercial licensing
 *   details, please contact msobkow@sasktel.net.
 *   
 *   Under the terms of the GPL:
 *   
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 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 Public License for more details.
 *     
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *      This source code incorporates modified modules originally licensed
 *      under the Apache 2.0 license by MSS Code Factory including CFSecurity
 *      (net-sourceforge-msscodefactory-2.0-cfsecurity.xml),
 *      CFInternet (net-sourceforge-msscodefactory-2.0-cfinternet.xml), and
 *      CFCrm 2.0 (net-sourceforge-msscodefactory-2.0-cfcrm.xml), with all of the
 *      required models being available as part of the MSS Code Factory 1.11
 *      distribution source and install zips.
 *   
 *      You can download installations of MSS Code Factory 1.11 from
 *      http://msscodefactory.sourceforge.net/
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 */

package net.sourceforge.msscodefactory.cfacc.v2_0.CFAccSwing;

import java.math.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.*;
import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.*;
import org.apache.commons.codec.binary.Base64;
import net.sourceforge.msscodefactory.cfacc.v2_0.CFAcc.*;
import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccObj.*;

/**
 *   CFAccSwingAccountConfigFinderJPanel Swing Finder JPanel implementation
 *   for AccountConfig.
 */
public class CFAccSwingAccountConfigFinderJPanel extends CFJPanel implements ICFJRefreshCallback {
    protected ICFAccSwingSchema swingSchema = null;
    protected boolean swingIsInitializing = true;
    protected ICFAccTenantObj containingTenant = null;
    protected SortedMap<CFAccAccountConfigPKey, ICFAccAccountConfigObj> mapOfAccountConfig = null;
    protected ICFAccAccountConfigObj arrayOfAccountConfig[] = null;
    public final static int COLID_ROW_HEADER = -1;
    public final static int COLID_OBJQUALIFIEDNAME = 0;
    public final static int COLID_LOOKUP_DEFCCY = 1;
    public final static int COLID_LOOKUP_CUSTCTCLST = 2;
    public final static int COLID_LOOKUP_EMPCTCLST = 3;
    public final static int COLID_LOOKUP_VENDCTCLST = 4;
    public final static int NUM_COLS = 5;

    public class FinderTableModel extends AbstractTableModel {
        public FinderTableModel() {
            super();
        }

        public int getRowCount() {
            int retval;
            if (arrayOfAccountConfig != null) {
                retval = arrayOfAccountConfig.length;
            } else {
                retval = 0;
            }
            return (retval);
        }

        public int getColumnCount() {
            return (NUM_COLS);
        }

        public String getColumnName(int column) {
            String retval;
            switch (column) {
            case COLID_ROW_HEADER:
                retval = "AccountConfig";
                break;
            case COLID_OBJQUALIFIEDNAME:
                retval = "Qualified Name";
                break;
            case COLID_LOOKUP_DEFCCY:
                retval = "Default Currency";
                break;
            case COLID_LOOKUP_CUSTCTCLST:
                retval = "Customer Contact List";
                break;
            case COLID_LOOKUP_EMPCTCLST:
                retval = "Employee Contact List";
                break;
            case COLID_LOOKUP_VENDCTCLST:
                retval = "Vendor Contact List";
                break;
            default:
                retval = null;
                break;
            }
            return (retval);
        }

        public Object getValueAt(int row, int column) {
            final String S_ProcName = "getValueAt";
            if ((row < 0) || (column < -1)) {
                return (null);
            }
            if (arrayOfAccountConfig == null) {
                return (null);
            }
            int len = arrayOfAccountConfig.length;
            if (row >= len) {
                return (null);
            }
            ICFAccAccountConfigObj obj = arrayOfAccountConfig[row];
            if (obj == null) {
                return (null);
            }
            Object retval;
            switch (column) {
            case COLID_ROW_HEADER:
                retval = obj;
                break;
            case COLID_OBJQUALIFIEDNAME:
                retval = obj.getObjQualifiedName();
                break;
            case COLID_LOOKUP_DEFCCY:
                retval = obj.getRequiredLookupDefCcy();
                break;
            case COLID_LOOKUP_CUSTCTCLST:
                retval = obj.getOptionalLookupCustCtcLst();
                break;
            case COLID_LOOKUP_EMPCTCLST:
                retval = obj.getOptionalLookupEmpCtcLst();
                break;
            case COLID_LOOKUP_VENDCTCLST:
                retval = obj.getOptionalLookupVendCtcLst();
                break;
            default:
                retval = null;
                break;
            }
            return (retval);
        }

        public boolean isCellEditable(int row, int column) {
            return (false);
        }
    }

    public class FinderTableColumnModel extends DefaultTableColumnModel {
        protected class QualifiedNameColumn extends TableColumn {
            protected class ColumnCellRenderer extends CFStringColumnCellRenderer {
                public ColumnCellRenderer() {
                    super();
                }
            }

            protected class ColumnHeaderRenderer extends CFHeaderColumnCellRenderer {
                public ColumnHeaderRenderer() {
                    super();
                }
            }

            public Object getHeaderValue() {
                return ("Qualified Name");
            }

            public QualifiedNameColumn() {
                super();
                setCellRenderer(new ColumnCellRenderer());
                setHeaderRenderer(new ColumnHeaderRenderer());
                setModelIndex(COLID_OBJQUALIFIEDNAME);
                setMinWidth(125);
                setResizable(true);
            }
        }

        public class DefCcyColumn extends TableColumn {
            protected class ColumnCellRenderer extends CFReferenceColumnCellRenderer {
                public ColumnCellRenderer() {
                    super();
                }
            }

            protected class ColumnHeaderRenderer extends CFHeaderColumnCellRenderer {
                public ColumnHeaderRenderer() {
                    super();
                }
            }

            public Object getHeaderValue() {
                return ("Default Currency");
            }

            public DefCcyColumn() {
                super();
                setCellRenderer(new ColumnCellRenderer());
                setHeaderRenderer(new ColumnHeaderRenderer());
                setModelIndex(COLID_LOOKUP_DEFCCY);
                setMinWidth(125);
                setResizable(true);
            }
        }

        public class CustCtcLstColumn extends TableColumn {
            protected class ColumnCellRenderer extends CFReferenceColumnCellRenderer {
                public ColumnCellRenderer() {
                    super();
                }
            }

            protected class ColumnHeaderRenderer extends CFHeaderColumnCellRenderer {
                public ColumnHeaderRenderer() {
                    super();
                }
            }

            public Object getHeaderValue() {
                return ("Customer Contact List");
            }

            public CustCtcLstColumn() {
                super();
                setCellRenderer(new ColumnCellRenderer());
                setHeaderRenderer(new ColumnHeaderRenderer());
                setModelIndex(COLID_LOOKUP_CUSTCTCLST);
                setMinWidth(125);
                setResizable(true);
            }
        }

        public class EmpCtcLstColumn extends TableColumn {
            protected class ColumnCellRenderer extends CFReferenceColumnCellRenderer {
                public ColumnCellRenderer() {
                    super();
                }
            }

            protected class ColumnHeaderRenderer extends CFHeaderColumnCellRenderer {
                public ColumnHeaderRenderer() {
                    super();
                }
            }

            public Object getHeaderValue() {
                return ("Employee Contact List");
            }

            public EmpCtcLstColumn() {
                super();
                setCellRenderer(new ColumnCellRenderer());
                setHeaderRenderer(new ColumnHeaderRenderer());
                setModelIndex(COLID_LOOKUP_EMPCTCLST);
                setMinWidth(125);
                setResizable(true);
            }
        }

        public class VendCtcLstColumn extends TableColumn {
            protected class ColumnCellRenderer extends CFReferenceColumnCellRenderer {
                public ColumnCellRenderer() {
                    super();
                }
            }

            protected class ColumnHeaderRenderer extends CFHeaderColumnCellRenderer {
                public ColumnHeaderRenderer() {
                    super();
                }
            }

            public Object getHeaderValue() {
                return ("Vendor Contact List");
            }

            public VendCtcLstColumn() {
                super();
                setCellRenderer(new ColumnCellRenderer());
                setHeaderRenderer(new ColumnHeaderRenderer());
                setModelIndex(COLID_LOOKUP_VENDCTCLST);
                setMinWidth(125);
                setResizable(true);
            }
        }

        public FinderTableColumnModel() {
            super();
            addColumn(new QualifiedNameColumn());
            addColumn(new DefCcyColumn());
            addColumn(new CustCtcLstColumn());
            addColumn(new EmpCtcLstColumn());
            addColumn(new VendCtcLstColumn());
        }
    }

    public class FinderListSelectionModel extends DefaultListSelectionModel {
        public FinderListSelectionModel() {
            super();
            setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        }
    }

    public class FinderListSelectionListener implements ListSelectionListener {
        public FinderListSelectionListener() {
        }

        public void valueChanged(ListSelectionEvent lse) {
            final String S_ProcName = "valueChanged";
            ICFAccAccountConfigObj selectedObj;
            if (lse.getValueIsAdjusting()) {
                return;
            }
            if (dataTable == null) {
                return;
            }
            int dataRow = dataTable.getSelectedRow();
            int modelIndex = dataTable.convertRowIndexToModel(dataRow);
            FinderTableModel tblDataModel = getDataModel();
            Object selectedRowData = tblDataModel.getValueAt(modelIndex, COLID_ROW_HEADER);
            if (selectedRowData != null) {
                if (selectedRowData instanceof ICFAccAccountConfigObj) {
                    int selectedCount = dataTable.getSelectedRowCount();
                    if (selectedCount <= 0) {
                        selectedObj = null;
                    } else if (selectedCount == 1) {
                        selectedObj = (ICFAccAccountConfigObj) selectedRowData;
                    } else {
                        selectedObj = null;
                    }
                } else {
                    selectedObj = null;
                    throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                            "selectedRowData", selectedRowData, "ICFAccAccountConfigObj");
                }
            } else {
                selectedObj = null;
            }
            Container cont = getParent();
            while ((cont != null) && (!(cont instanceof CFJInternalFrame))) {
                cont = cont.getParent();
            }
            if (cont != null) {
                CFJInternalFrame frame = (CFJInternalFrame) cont;
                if (frame instanceof ICFAccSwingAccountConfigJPanelCommon) {
                    ICFAccSwingAccountConfigJPanelCommon jpanelCommon = (ICFAccSwingAccountConfigJPanelCommon) frame;
                    jpanelCommon.setSwingFocus(selectedObj);
                }
            }
        }
    }

    public class FinderListMouseAdapter extends MouseAdapter {
        public FinderListMouseAdapter() {
            super();
        }

        public void mousePressed(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
                return;
            }
            if (e.getClickCount() != 2) {
                return;
            }
            JTable table = (JTable) e.getSource();
            Point p = e.getPoint();
            int row = table.rowAtPoint(p);
            if (row < 0) {
                return;
            }
            FinderTableModel model = getDataModel();
            ICFAccAccountConfigObj o = (ICFAccAccountConfigObj) model.getValueAt(row, COLID_ROW_HEADER);
            if (o == null) {
                return;
            }
            JInternalFrame frame = swingSchema.getAccountConfigFactory().newViewEditJInternalFrame(o);
            ((ICFAccSwingAccountConfigJPanelCommon) frame).setPanelMode(CFJPanel.PanelMode.View);
            if (frame == null) {
                return;
            }
            Container cont = getParent();
            while ((cont != null) && (!(cont instanceof JInternalFrame))) {
                cont = cont.getParent();
            }
            if (cont != null) {
                JInternalFrame myInternalFrame = (JInternalFrame) cont;
                myInternalFrame.getDesktopPane().add(frame);
                frame.setVisible(true);
                frame.show();
            }
        }
    }

    protected FinderTableModel dataModel = null;
    protected FinderTableColumnModel dataColumnModel = null;
    protected FinderListSelectionModel dataListSelectionModel = null;
    protected FinderListMouseAdapter dataListMouseAdapter = null;
    protected FinderListSelectionListener dataListSelectionListener = null;
    protected JTable dataTable = null;
    protected JTableHeader dataTableHeader = null;
    protected JScrollPane dataScrollPane = null;
    protected ViewEditInternalFrameListener viewEditInternalFrameListener = null;

    protected class ViewEditInternalFrameListener implements InternalFrameListener {
        public ViewEditInternalFrameListener() {
        }

        public void internalFrameActivated(InternalFrameEvent e) {
        }

        public void internalFrameClosed(InternalFrameEvent e) {
            refreshMe();
        }

        public void internalFrameClosing(InternalFrameEvent e) {
        }

        public void internalFrameDeactivated(InternalFrameEvent e) {
        }

        public void internalFrameIconified(InternalFrameEvent e) {
        }

        public void internalFrameDeiconified(InternalFrameEvent e) {
        }

        public void internalFrameOpened(InternalFrameEvent e) {
        }
    }

    public CFAccSwingAccountConfigFinderJPanel(ICFAccSwingSchema argSchema) {
        super();
        final String S_ProcName = "construct-schema-focus";
        if (argSchema == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1,
                    "argSchema");
        }
        swingSchema = argSchema;
        dataTable = new JTable(getDataModel(), getDataColumnModel(), getDataListSelectionModel());
        dataTable.addMouseListener(getDataListMouseAdapter());
        dataTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        dataTable.setUpdateSelectionOnSort(true);
        dataTable.setRowHeight(25);
        getDataListSelectionModel().addListSelectionListener(getDataListSelectionListener());
        dataScrollPane = new JScrollPane(dataTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        dataScrollPane.setColumnHeader(new JViewport() {
            @Override
            public Dimension getPreferredSize() {
                Dimension sz = super.getPreferredSize();
                sz.height = 25;
                return (sz);
            }
        });
        dataTable.setFillsViewportHeight(true);
        add(dataScrollPane);
        loadData(true);
        doLayout();
        swingIsInitializing = false;
    }

    public ICFAccSwingSchema getSwingSchema() {
        return (swingSchema);
    }

    public class AccountConfigByQualNameComparator implements Comparator<ICFAccAccountConfigObj> {
        public AccountConfigByQualNameComparator() {
        }

        public int compare(ICFAccAccountConfigObj lhs, ICFAccAccountConfigObj rhs) {
            if (lhs == null) {
                if (rhs == null) {
                    return (0);
                } else {
                    return (-1);
                }
            } else if (rhs == null) {
                return (1);
            } else {
                String lhsValue = lhs.getObjQualifiedName();
                String rhsValue = rhs.getObjQualifiedName();
                if (lhsValue == null) {
                    if (rhsValue == null) {
                        return (0);
                    } else {
                        return (-1);
                    }
                } else if (rhsValue == null) {
                    return (1);
                } else {
                    return (lhsValue.compareTo(rhsValue));
                }
            }
        }
    }

    protected AccountConfigByQualNameComparator compareAccountConfigByQualName = new AccountConfigByQualNameComparator();

    public void loadData(boolean forceReload) {
        ICFAccSchemaObj schemaObj = swingSchema.getSchema();
        if ((containingTenant == null) || forceReload) {
            CFAccAuthorization auth = schemaObj.getAuthorization();
            long containingTenantId = auth.getSecTenantId();
            containingTenant = schemaObj.getTenantTableObj().readTenantByIdIdx(containingTenantId);
        }
        if ((mapOfAccountConfig == null) || forceReload) {
            arrayOfAccountConfig = null;
            mapOfAccountConfig = schemaObj.getAccountConfigTableObj()
                    .readAccountConfigByTenantIdx(containingTenant.getRequiredId(), swingIsInitializing);
            if (mapOfAccountConfig != null) {
                Object objArray[] = mapOfAccountConfig.values().toArray();
                if (objArray != null) {
                    int len = objArray.length;
                    arrayOfAccountConfig = new ICFAccAccountConfigObj[len];
                    for (int i = 0; i < len; i++) {
                        arrayOfAccountConfig[i] = (ICFAccAccountConfigObj) objArray[i];
                    }
                    Arrays.sort(arrayOfAccountConfig, compareAccountConfigByQualName);
                }
            }
        }
    }

    public FinderTableModel getDataModel() {
        if (dataModel == null) {
            dataModel = new FinderTableModel();
        }
        return (dataModel);
    }

    public FinderTableColumnModel getDataColumnModel() {
        if (dataColumnModel == null) {
            dataColumnModel = new FinderTableColumnModel();
        }
        return (dataColumnModel);
    }

    public FinderListSelectionModel getDataListSelectionModel() {
        if (dataListSelectionModel == null) {
            dataListSelectionModel = new FinderListSelectionModel();
        }
        return (dataListSelectionModel);
    }

    public FinderListSelectionListener getDataListSelectionListener() {
        if (dataListSelectionListener == null) {
            dataListSelectionListener = new FinderListSelectionListener();
        }
        return (dataListSelectionListener);
    }

    public FinderListMouseAdapter getDataListMouseAdapter() {
        if (dataListMouseAdapter == null) {
            dataListMouseAdapter = new FinderListMouseAdapter();
        }
        return (dataListMouseAdapter);
    }

    protected JTableHeader createDefaultTableHeader() {
        if (dataTableHeader == null) {
            dataTableHeader = new JTableHeader(getDataColumnModel());
            dataTableHeader.setResizingAllowed(true);
            dataTableHeader.setTable(dataTable);
        }
        return (dataTableHeader);
    }

    public void doLayout() {
        Dimension sz = getSize();
        dataScrollPane.setBounds(0, 0, sz.width, sz.height);
        dataScrollPane.doLayout();
    }

    protected ViewEditInternalFrameListener getViewEditInternalFrameListener() {
        if (viewEditInternalFrameListener == null) {
            viewEditInternalFrameListener = new ViewEditInternalFrameListener();
        }
        return (viewEditInternalFrameListener);
    }

    public void refreshMe() {
        loadData(true);
    }
}