Package clips.administrator.expenditure

Source Code of clips.administrator.expenditure.TableModelExpenditureEdit$Cell

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.administrator.expenditure;

import clips.delegate.expenditure.ExpenditureItemData;
import cli_fmw.delegate.directory.DirectoryItem;
import cli_fmw.main.ClipsException;
import cli_fmw.utils.SelectorEditable;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.table.AbstractTableModel;

/**
* Модель таблицы редактирования расхода материалов
* @author petr
*/
public class TableModelExpenditureEdit extends AbstractTableModel {

    public static int COL_EXPEN_TYPE = 0;   //колонка "тип"
    public static int COL_QUANTITY = 1;       //колонка "количество"
    public static int COL_METRIC = 2;       //колонка "метрика"
    public static int COL_COUNT = 3;      
   
    private boolean canBeChanged;            //флаг: истина если таблица может быть изменена
    private SelectorEditable<ExpenditureItemData> items;
   
    private Cell lastChanged;//хранит предыдущее знасчение ячейки
    class Cell {
        public int row;
        public int col;
        public Object value;
    }

    /**
     * Конструктор
     * @param iie - итератор, взятый из услуги
     * @param CAN_BE_EDIT - флаг: истина разрешает редактирование
     */
    public TableModelExpenditureEdit(SelectorEditable<ExpenditureItemData> items, boolean canBeChanged) {
        this.canBeChanged = canBeChanged;
        this.items = items;
        lastChanged = new Cell();
    }

    /**
     * Восстанавливает последнее изменение объёма
     */
    public void recoverChangedValue() {
        ExpenditureItemData eil  = null;       
        if (lastChanged != null
                && lastChanged.row < items.size()
                && lastChanged.row >= 0){
            eil = items.get(lastChanged.row);
        }
        if (eil != null && lastChanged.value != null){
            eil.setQuantity((Float) lastChanged.value);
        }
    }
   
    @Override
    public int getRowCount() {
        return items.size();
    }

    @Override
    public int getColumnCount() {
        return COL_COUNT;
    }

    @Override
    public Object getValueAt(int row, int col) {//загрузка
        String s = null;
        try {
            ExpenditureItemData item = items.get(row);
            if (col == COL_EXPEN_TYPE) {
                s = item.getType().toString();
               
            } else if (col == COL_QUANTITY) {
                String volume = String.format("%.2f", (item.getQuantity()));
                System.out.println(volume);
                while (volume.charAt(volume.length() - 1) == '0') {
                    volume = volume.substring(0, volume.length() - 1);
                }
                char ch = volume.charAt(volume.length() - 1);
                if (ch == ',' || ch == '.') {
                    volume = volume.substring(0, volume.length() - 1);
                }
                return volume;
               
            } else if (col == COL_METRIC) {
                return item.getMetric();
               
            } else {
                assert false;
            }
        } catch (ClipsException ex) {
            ex.printStackTrace();
        }
        return s;
    }

    @Override
    public String getColumnName(int col) {
        String s = null;
        if (col == COL_EXPEN_TYPE) {
            s = "материал";
        } else if (col == COL_QUANTITY) {
            s = "количество";
        } else if (col == COL_METRIC) {
            s = "ед. измерения";
        } else {
            assert false;
        }
        return s;
    }

    @Override
    public Class<?> getColumnClass(int col) {
        if (col == COL_METRIC) {
            return DirectoryItem.class;
        }
        return super.getColumnClass(col);
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (col == COL_EXPEN_TYPE || col == COL_METRIC) {
            //Название и метрику ...вообще нельзя редактировать
            return false;
        }
        return canBeChanged; // объём только по разрешению
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        try {
            ExpenditureItemData eil = items.get(row);
            if (col == COL_QUANTITY) {
                //сохраняем старое значение величины
                lastChanged.value = eil.getQuantity();
                lastChanged.row = row;
                lastChanged.col = col;
               
                Number v = NumberFormat.getInstance().parse(value.toString());
                eil.setQuantity(v.floatValue());
                fireTableDataChanged();
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
    }
}
TOP

Related Classes of clips.administrator.expenditure.TableModelExpenditureEdit$Cell

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.