PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 10ms app.codeStats 0ms

/src/mpv5/utils/tables/Selection.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 107 lines | 64 code | 17 blank | 26 comment | 12 complexity | 0435b616d852e424e7cfd429070529dd MD5 | raw file
  1
  2/*
  3 *
  4This file is part of YaBS.
  5
  6YaBS is free software: you can redistribute it and/or modify
  7it under the terms of the GNU General Public License as published by
  8the Free Software Foundation, either version 3 of the License, or
  9(at your option) any later version.
 10
 11YaBS is distributed in the hope that it will be useful,
 12but WITHOUT ANY WARRANTY; without even the implied warranty of
 13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14GNU General Public License for more details.
 15
 16You should have received a copy of the GNU General Public License
 17along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 18
 19 *
 20 */
 21package mpv5.utils.tables;
 22
 23//~--- JDK imports ------------------------------------------------------------
 24import javax.swing.JTable;
 25import javax.swing.table.DefaultTableModel;
 26import javax.swing.table.TableModel;
 27
 28/**
 29 *
 30 *
 31 */
 32public class Selection {
 33
 34    private int id;
 35    private boolean noerror;
 36    private final int row;
 37    private final JTable table;
 38
 39    public Selection(final JTable table) {
 40        this.table = table;
 41        if (table.getCellEditor() != null) {
 42            try {table.getCellEditor().stopCellEditing();} catch (Exception e) {}
 43        }
 44        row = table.getSelectedRow();
 45        try {
 46//            System.err.print("@AAAAAAAAAAAAAAAAAAAAAAAAA@    " + row);
 47            id = Integer.valueOf(String.valueOf(
 48                    table.getValueAt(row,
 49                    table.convertColumnIndexToView(0))));
 50            noerror = true;
 51        } catch (Exception numberFormatException) {
 52            noerror = false;
 53        }
 54    }
 55
 56    public synchronized int getId() {
 57        return id;
 58    }
 59
 60    public Object[] getRowData() {
 61        Object[] data = new Object[table.getModel().getColumnCount()];
 62
 63        for (int idx = 0; idx < data.length; idx++) {
 64            data[idx] = table.getModel().getValueAt(table.getSelectedRow(), idx);
 65        }
 66
 67        return data;
 68    }
 69
 70    public void removeRow() {
 71        TableModel model = table.getModel();
 72        Object[][] data = new Object[model.getRowCount()][model.getColumnCount()];
 73        Object[] columnNames = new Object[model.getColumnCount()];
 74
 75        for (int idx = 0; idx < columnNames.length; idx++) {
 76            columnNames[idx] = model.getColumnName(idx);
 77        }
 78
 79        for (int idx = 0; idx < model.getRowCount(); idx++) {
 80            for (int i = 0; i < model.getColumnCount(); i++) {
 81                if (idx != table.getSelectedRow()) {
 82                    data[idx][i] = model.getValueAt(idx, i);
 83                }
 84            }
 85        }
 86
 87        table.setModel(new DefaultTableModel(data, columnNames));
 88    }
 89
 90    public boolean rowHasData(int testcol) {
 91        if (table.getModel().getRowCount() > 0) {
 92            for (int i = 0; i < table.getModel().getColumnCount(); i++) {
 93                if (table.getModel().getValueAt(table.getSelectedRow(), testcol) != null) {
 94                    return true;
 95                }
 96            }
 97        }
 98
 99        return false;
100    }
101
102    public boolean checkID() {
103        return noerror;
104    }
105}
106//~ Formatted by Jindent --- http://www.jindent.com
107