//    uniCenta oPOS  - Touch Friendly Point Of Sale
//    Copyright (c) 2009-2018 uniCenta & previous Openbravo POS works
//    https://unicenta.com
//
//    This file is part of uniCenta oPOS
//
//    uniCenta oPOS 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.
//
//    uniCenta oPOS 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 uniCenta oPOS.  If not, see <http://www.gnu.org/licenses/>.

package com.unicenta.data.gui;

import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import com.unicenta.basic.BasicException;
import com.unicenta.data.loader.LocalRes;
import com.unicenta.data.user.BrowsableEditableData;
import com.unicenta.data.user.BrowseListener;

/**
 *
 * @author JG uniCenta
 */
public class JListNavigator extends javax.swing.JPanel implements BrowseListener, ListSelectionListener {
    
    /**
     *
     */
    protected BrowsableEditableData m_bd;
    
    /** Creates new form JListBrowse
     * @param bd */
    public JListNavigator(BrowsableEditableData bd) {
        this(bd, false);
    }
    
    /**
     *
     * @param bd
     * @param bTouchable
     */
    public JListNavigator(BrowsableEditableData bd, boolean bTouchable) {
        
        m_bd = bd;
        
        initComponents();
        
//        if (bTouchable) {
            jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));
            jScrollPane1.getHorizontalScrollBar().setPreferredSize(new Dimension(35, 35));            
            jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
//        }


        m_jlist.addListSelectionListener(this);        
        m_jlist.setModel(m_bd.getListModel());
        
        m_bd.addBrowseListener(this);
    }

    /**
     *
     * @param cellRenderer
     */
    public void setCellRenderer(ListCellRenderer cellRenderer) {
        m_jlist.setCellRenderer(cellRenderer);
    }
    
    /**
     *
     * @param iIndex
     * @param iCounter
     */
    @Override
    public void updateIndex(int iIndex, int iCounter) {
        
        if (iIndex >= 0 && iIndex < iCounter) {
            m_jlist.setSelectedIndex(iIndex);
        } else {
            m_jlist.setSelectedIndex(-1);
        }
    } 
    
    @Override
    public void valueChanged(ListSelectionEvent evt) {
        
        if (!evt.getValueIsAdjusting()) {
            int i = m_jlist.getSelectedIndex();
            if (i >= 0) {
                if (!m_bd.isAdjusting()) {

                    try {
                        m_bd.moveTo(i);
                    } catch (BasicException eD) {
                        MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, LocalRes.getIntString("message.nomove"), eD);
                        msg.show(this);
                    }
                }
                Rectangle oRect = m_jlist.getCellBounds(i, i);
                m_jlist.scrollRectToVisible(oRect);       
            }
        }
    }    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        m_jlist = new javax.swing.JList();

        setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        setPreferredSize(new java.awt.Dimension(300, 2));
        setLayout(new java.awt.BorderLayout());

        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        m_jlist.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        m_jlist.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        m_jlist.setFocusable(false);
        m_jlist.setRequestFocusEnabled(false);
        jScrollPane1.setViewportView(m_jlist);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList m_jlist;
    // End of variables declaration//GEN-END:variables
    
}
