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

Java tutorial

Introduction

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

Source

// Description: Java 7 Swing Picker of Obj JPanel implementation for TSecGroupMember.

/*
 *   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.util.List;
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.*;

/**
 *   CFAccSwingTSecGroupMemberPickerJPanel Swing Pick Obj JPanel implementation
 *   for TSecGroupMember.
 */
public class CFAccSwingTSecGroupMemberPickerJPanel extends CFJPanel
        implements ICFAccSwingTSecGroupMemberJPanelList {
    protected ICFAccSwingSchema swingSchema = null;
    protected Collection<ICFAccTSecGroupMemberObj> swingDataCollection = null;
    protected ICFAccTSecGroupMemberObj arrayOfTSecGroupMember[] = null;
    public final static int COLID_ROW_HEADER = -1;
    public final static int COLID_OBJQUALIFIEDNAME = 0;
    public final static int COLID_TSECGROUPMEMBERID = 1;
    public final static int COLID_PARENT_USER = 2;
    public final static int NUM_COLS = 3;
    protected JTable swingJTable = null;

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

        public int getRowCount() {
            int retval;
            if (arrayOfTSecGroupMember != null) {
                retval = arrayOfTSecGroupMember.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 = "Tenant Security Group Member";
                break;
            case COLID_OBJQUALIFIEDNAME:
                retval = "Qualified Name";
                break;
            case COLID_TSECGROUPMEMBERID:
                retval = "TSecurity Group Member Id";
                break;
            case COLID_PARENT_USER:
                retval = "User";
                break;
            default:
                retval = null;
                break;
            }
            return (retval);
        }

        public Object getValueAt(int row, int column) {
            if ((row < 0) || (column < -1)) {
                return (null);
            }
            if (arrayOfTSecGroupMember == null) {
                return (null);
            }
            int len = arrayOfTSecGroupMember.length;
            if (row >= len) {
                return (null);
            }
            ICFAccTSecGroupMemberObj obj = arrayOfTSecGroupMember[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_TSECGROUPMEMBERID:
                retval = new Long(obj.getRequiredTSecGroupMemberId());
                break;
            case COLID_PARENT_USER:
                retval = obj.getRequiredParentUser();
                break;
            default:
                retval = null;
                break;
            }
            return (retval);
        }

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

    public class PickerTableColumnModel 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 TSecGroupMemberIdColumn extends TableColumn {
            protected class ColumnCellRenderer extends CFInt64ColumnCellRenderer {
                public ColumnCellRenderer() {
                    super();
                }
            }

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

            public Object getHeaderValue() {
                return ("TSecurity Group Member Id");
            }

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

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

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

            public Object getHeaderValue() {
                return ("User");
            }

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

        public PickerTableColumnModel() {
            super();
            addColumn(new QualifiedNameColumn());
            addColumn(new TSecGroupMemberIdColumn());
            addColumn(new UserColumn());
        }
    }

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

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

        public void valueChanged(ListSelectionEvent lse) {
            final String S_ProcName = "valueChanged";
            ICFAccTSecGroupMemberObj selectedObj;
            if (lse.getValueIsAdjusting()) {
                return;
            }
            if (dataTable == null) {
                return;
            }
            int dataRow = dataTable.getSelectedRow();
            int modelIndex = dataTable.convertRowIndexToModel(dataRow);
            PickerTableModel tblDataModel = getDataModel();
            Object selectedRowData = tblDataModel.getValueAt(modelIndex, COLID_ROW_HEADER);
            if (selectedRowData != null) {
                if (selectedRowData instanceof ICFAccTSecGroupMemberObj) {
                    int selectedCount = dataTable.getSelectedRowCount();
                    if (selectedCount <= 0) {
                        selectedObj = null;
                    } else if (selectedCount == 1) {
                        selectedObj = (ICFAccTSecGroupMemberObj) selectedRowData;
                    } else {
                        selectedObj = null;
                    }
                } else {
                    selectedObj = null;
                    throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                            "selectedRowData", selectedRowData, "ICFAccTSecGroupMemberObj");
                }
            } else {
                selectedObj = null;
            }
            setSwingFocus(selectedObj);
            adjustFeedback();
        }
    }

    public class PickerListMouseAdapter extends MouseAdapter {
        public PickerListMouseAdapter() {
            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;
            }
            PickerTableModel model = getDataModel();
            ICFAccTSecGroupMemberObj o = (ICFAccTSecGroupMemberObj) model.getValueAt(row, COLID_ROW_HEADER);
            invokeWhenChosen.choseTSecGroupMember(o);
            try {
                Container cont = getParent();
                while ((cont != null) && (!(cont instanceof JInternalFrame))) {
                    cont = cont.getParent();
                }
                if (cont != null) {
                    ((JInternalFrame) cont).setClosed(true);
                }
            } catch (Exception x) {
            }
        }
    }

    protected PickerTableModel dataModel = null;
    protected PickerTableColumnModel dataColumnModel = null;
    protected PickerListSelectionModel dataListSelectionModel = null;
    protected PickerListMouseAdapter dataListMouseAdapter = null;
    protected PickerListSelectionListener dataListSelectionListener = null;
    protected JTable dataTable = null;
    protected JTableHeader dataTableHeader = null;
    protected JScrollPane dataScrollPane = null;
    public final String S_ColumnNames[] = { "Name" };
    protected ICFAccSwingTSecGroupMemberChosen invokeWhenChosen = null;
    protected ICFAccTSecGroupObj swingContainer = null;
    protected Action actionCancel = null;
    protected Action actionChooseNone = null;
    protected Action actionChooseSelected = null;
    protected JButton buttonCancel = null;
    protected JButton buttonChooseNone = null;
    protected JButton buttonChooseSelected = null;

    protected class ActionCancel extends AbstractAction {
        public ActionCancel() {
            super();
            putValue(Action.NAME, "Cancel");
            setEnabled(true);
        }

        public void actionPerformed(ActionEvent e) {
            try {
                Container cont = getParent();
                while ((cont != null) && (!(cont instanceof JInternalFrame))) {
                    cont = cont.getParent();
                }
                if (cont != null) {
                    ((JInternalFrame) cont).setClosed(true);
                }
            } catch (Exception x) {
            }
        }
    }

    protected class ActionChooseNone extends AbstractAction {
        public ActionChooseNone() {
            super();
            putValue(Action.NAME, "Choose None");
            setEnabled(true);
        }

        public void actionPerformed(ActionEvent e) {
            final String S_ProcName = "actionPerformed";
            ICFAccSchemaObj schemaObj = swingSchema.getSchema();
            if (schemaObj == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "schemaObj");
            }
            invokeWhenChosen.choseTSecGroupMember(null);
            try {
                Container cont = getParent();
                while ((cont != null) && (!(cont instanceof JInternalFrame))) {
                    cont = cont.getParent();
                }
                if (cont != null) {
                    ((JInternalFrame) cont).setClosed(true);
                }
            } catch (Exception x) {
            }
        }
    }

    protected class ActionChooseSelectedTSecGroupMember extends AbstractAction {
        public ActionChooseSelectedTSecGroupMember() {
            super();
            putValue(Action.NAME, "Choose Selected Tenant Security Group Member");
            setEnabled(true);
        }

        public void actionPerformed(ActionEvent e) {
            final String S_ProcName = "actionPerformed";
            ICFAccSchemaObj schemaObj = swingSchema.getSchema();
            if (schemaObj == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "schemaObj");
            }
            ICFAccTSecGroupMemberObj selectedInstance = getSwingFocusAsTSecGroupMember();
            invokeWhenChosen.choseTSecGroupMember(selectedInstance);
            try {
                Container cont = getParent();
                while ((cont != null) && (!(cont instanceof JInternalFrame))) {
                    cont = cont.getParent();
                }
                if (cont != null) {
                    ((JInternalFrame) cont).setClosed(true);
                }
            } catch (Exception x) {
            }
        }
    }

    public CFAccSwingTSecGroupMemberPickerJPanel(ICFAccSwingSchema argSchema, ICFAccTSecGroupMemberObj argFocus,
            ICFAccTSecGroupObj argContainer, Collection<ICFAccTSecGroupMemberObj> argDataCollection,
            ICFAccSwingTSecGroupMemberChosen whenChosen) {
        super();
        final String S_ProcName = "construct-schema-focus";
        if (argSchema == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1,
                    "argSchema");
        }
        if (whenChosen == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 5,
                    "whenChosen");
        }
        invokeWhenChosen = whenChosen;
        // argFocus is optional; focus may be set later during execution as
        // conditions of the runtime change.
        swingSchema = argSchema;
        swingFocus = argFocus;
        swingContainer = argContainer;
        setSwingDataCollection(argDataCollection);
        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);
        actionCancel = new ActionCancel();
        buttonCancel = new JButton(actionCancel);
        actionChooseNone = new ActionChooseNone();
        buttonChooseNone = new JButton(actionChooseNone);
        actionChooseSelected = new ActionChooseSelectedTSecGroupMember();
        buttonChooseSelected = new JButton(actionChooseSelected);
        // Do initial layout
        setSize(1024, 480);
        add(buttonChooseNone);
        add(buttonChooseSelected);
        add(buttonCancel);
        add(dataScrollPane);
        dataScrollPane.setBounds(0, 35, 1024, 455);
        doLayout();
        setSwingFocusAsTSecGroupMember(argFocus);
    }

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

    public void setSwingFocus(ICFLibAnyObj2 value) {
        final String S_ProcName = "setSwingFocus";
        if ((value == null) || (value instanceof ICFAccTSecGroupMemberObj)) {
            super.setSwingFocus(value);
        } else {
            throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "value",
                    value, "ICFAccTSecGroupMemberObj");
        }
        if (dataTable == null) {
            return;
        }
        if (value == null) {
            dataTable.clearSelection();
        } else {
            ICFAccTSecGroupMemberObj curSelected;
            PickerTableModel tblDataModel = getDataModel();
            int selectedRow = dataTable.getSelectedRow();
            int modelIndex = dataTable.convertRowIndexToModel(selectedRow);
            if (selectedRow >= 0) {
                Object selectedRowData = tblDataModel.getValueAt(modelIndex, COLID_ROW_HEADER);
                curSelected = (ICFAccTSecGroupMemberObj) selectedRowData;
            } else {
                curSelected = null;
            }
            if (curSelected != value) {
                int len = tblDataModel.getRowCount();
                int idx = 0;
                while ((idx < len) && (tblDataModel.getValueAt(idx, COLID_ROW_HEADER) != value)) {
                    idx++;
                }
                if (idx < len) {
                    int viewRow = dataTable.convertRowIndexToView(idx);
                    dataTable.clearSelection();
                    dataTable.addRowSelectionInterval(viewRow, viewRow);
                }
            }
        }
    }

    public ICFAccTSecGroupMemberObj getSwingFocusAsTSecGroupMember() {
        return ((ICFAccTSecGroupMemberObj) getSwingFocus());
    }

    public void setSwingFocusAsTSecGroupMember(ICFAccTSecGroupMemberObj value) {
        setSwingFocus(value);
    }

    public class TSecGroupMemberByQualNameComparator implements Comparator<ICFAccTSecGroupMemberObj> {
        public TSecGroupMemberByQualNameComparator() {
        }

        public int compare(ICFAccTSecGroupMemberObj lhs, ICFAccTSecGroupMemberObj 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 TSecGroupMemberByQualNameComparator compareTSecGroupMemberByQualName = new TSecGroupMemberByQualNameComparator();

    public Collection<ICFAccTSecGroupMemberObj> getSwingDataCollection() {
        return (swingDataCollection);
    }

    public void setSwingDataCollection(Collection<ICFAccTSecGroupMemberObj> value) {
        final String S_ProcName = "setSwingDataCollection";
        swingDataCollection = value;
        if (swingDataCollection == null) {
            arrayOfTSecGroupMember = new ICFAccTSecGroupMemberObj[0];
        } else {
            int len = value.size();
            arrayOfTSecGroupMember = new ICFAccTSecGroupMemberObj[len];
            Iterator<ICFAccTSecGroupMemberObj> iter = swingDataCollection.iterator();
            int idx = 0;
            while (iter.hasNext() && (idx < len)) {
                arrayOfTSecGroupMember[idx++] = iter.next();
            }
            if (idx < len) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Collection iterator did not fully populate the array copy");
            }
            if (iter.hasNext()) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Collection iterator had left over items when done populating array copy");
            }
            Arrays.sort(arrayOfTSecGroupMember, compareTSecGroupMemberByQualName);
        }
        PickerTableModel tblDataModel = getDataModel();
        if (tblDataModel != null) {
            tblDataModel.fireTableDataChanged();
        }
    }

    public void doLayout() {
        Dimension sz = getSize();
        final int buttonWidth = 100;
        final int buttonHeight = 25;
        final int buttonSpacing = 10;
        final int spTop = buttonSpacing + buttonHeight + buttonSpacing;
        int buttonStart = (sz.width - ((3 * buttonWidth) + (2 * buttonSpacing))) / 2;
        if (buttonStart < 0) {
            buttonStart = 0;
        }
        int spHeight = sz.height - spTop;
        if (spHeight < 0) {
            spHeight = 0;
        }
        buttonChooseNone.setBounds(buttonStart, buttonSpacing, buttonWidth, buttonHeight);
        buttonChooseSelected.setBounds(buttonStart + buttonWidth + buttonSpacing, buttonSpacing, buttonWidth,
                buttonHeight);
        buttonCancel.setBounds(buttonStart + buttonWidth + buttonSpacing + buttonWidth + buttonSpacing,
                buttonSpacing, buttonWidth, buttonHeight);
        dataScrollPane.setBounds(0, spTop, sz.width, spHeight);
    }

    public void setPanelMode(CFJPanel.PanelMode value) {
        super.setPanelMode(value);
    }

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

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

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

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

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

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

    public ICFAccTSecGroupObj getSwingContainer() {
        return (swingContainer);
    }

    public void setSwingContainer(ICFAccTSecGroupObj value) {
        swingContainer = value;
    }

    public void adjustFeedback() {
        actionCancel.setEnabled(true);
        actionChooseNone.setEnabled(true);
        if (getSwingFocus() == null) {
            actionChooseSelected.setEnabled(false);
        } else {
            actionChooseSelected.setEnabled(true);
        }
    }
}