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