PageRenderTime 122ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/models/MPTableModel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 921 lines | 584 code | 91 blank | 246 comment | 102 complexity | 6c47725df77bb072c14d6bf05509517d 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. * This file is part of YaBS.
  3. *
  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. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.models;
  18. import java.text.DateFormat;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.LinkedList;
  24. import java.util.List;
  25. import java.util.Map.Entry;
  26. import java.util.Set;
  27. import java.util.Vector;
  28. import javax.swing.DefaultCellEditor;
  29. import javax.swing.Icon;
  30. import javax.swing.JFormattedTextField;
  31. import javax.swing.JLabel;
  32. import javax.swing.JTable;
  33. import javax.swing.JTextField;
  34. import javax.swing.event.ChangeEvent;
  35. import javax.swing.event.ListSelectionEvent;
  36. import javax.swing.event.TableColumnModelEvent;
  37. import javax.swing.event.TableColumnModelListener;
  38. import javax.swing.event.TableModelEvent;
  39. import javax.swing.table.DefaultTableCellRenderer;
  40. import javax.swing.table.DefaultTableModel;
  41. import javax.swing.table.TableCellEditor;
  42. import mpv5.db.common.Context;
  43. import mpv5.db.common.DatabaseObject;
  44. import mpv5.db.common.NodataFoundException;
  45. import mpv5.db.objects.ValueProperty;
  46. import mpv5.globals.Headers;
  47. import mpv5.globals.Messages;
  48. import mpv5.logging.Log;
  49. import mpv5.utils.arrays.ArrayUtilities;
  50. import mpv5.utils.numberformat.FormatNumber;
  51. import mpv5.utils.renderer.TableCellEditorForDezimal;
  52. import mpv5.utils.tables.DynamicTableCalculator;
  53. import mpv5.utils.tables.TableCalculator;
  54. /**
  55. *
  56. * A custom table model which implements various convenience methods
  57. */
  58. public class MPTableModel extends DefaultTableModel implements Cloneable, TableColumnModelListener {
  59. private static final long serialVersionUID = 1L;
  60. private Class[] types;
  61. private boolean[] canEdits;
  62. private Context context;
  63. private Object[] predefinedRow;
  64. private Integer autoCountColumn;
  65. private ArrayList<DynamicTableCalculator> calculators = new ArrayList<DynamicTableCalculator>();
  66. private JTable owner;
  67. /**
  68. * Creates an empty, uneditable model
  69. */
  70. public MPTableModel() {
  71. super();
  72. setEditable(false);
  73. Class[] typs = new Class[333];
  74. for (int i = 0; i < 333; i++) {
  75. typs[i] = Object.class;
  76. }
  77. setTypes(typs);
  78. }
  79. /**
  80. * May get slow on lots of data within the given Context
  81. *
  82. * @param c
  83. * @param target
  84. */
  85. public MPTableModel(final Context c, final JTable target) {
  86. super();
  87. try {
  88. setContext(c);
  89. final ArrayList<DatabaseObject> data = DatabaseObject.getObjects(c, true);
  90. final Object[][] dat = new Object[data.size()][];
  91. final Class[] classes = new Class[100];
  92. int j = 0;
  93. LinkedList<String> head = new LinkedList<String>();
  94. Entry<String, Object> e;
  95. for (DatabaseObject o : data) {
  96. final HashMap<String, Object> mod = o.getValues4();
  97. Set<Entry<String, Object>> set = mod.entrySet();
  98. dat[j] = new Object[set.size()];
  99. Iterator<Entry<String, Object>> it = set.iterator();
  100. int i = 0;
  101. while (it.hasNext()) {
  102. e = it.next();
  103. dat[j][i] = e.getValue();
  104. if (j == 0) {
  105. Class class1 = e.getValue().getClass();
  106. classes[i] = class1;
  107. head.add(i, e.getKey().toUpperCase());
  108. }
  109. i++;
  110. }
  111. j++;
  112. }
  113. final Object[] headers = new Object[head.size()];
  114. int x = 0;
  115. for (String hea : head) {
  116. headers[x] = hea;
  117. x++;
  118. }
  119. setDataVector(dat, headers);
  120. setEditable(false);
  121. setTypes(classes);
  122. if (target != null) {
  123. target.setAutoCreateRowSorter(true);
  124. }
  125. } catch (NodataFoundException ex) {
  126. Log.Debug(ex);
  127. }
  128. }
  129. public <T extends DatabaseObject> MPTableModel(final List<T> list, final JTable target) {
  130. super();
  131. final Object[][] dat = new Object[list.size()][];
  132. final Class[] classes = new Class[100];
  133. int j = 0;
  134. LinkedList<String> head = new LinkedList<String>();
  135. Entry<String, Object> e;
  136. for (DatabaseObject object : list) {
  137. if (context == null) {
  138. setContext(object.getContext());
  139. }
  140. final HashMap<String, Object> mod = object.getValues4();
  141. Set<Entry<String, Object>> set = mod.entrySet();
  142. dat[j] = new Object[set.size()];
  143. Iterator<Entry<String, Object>> it = set.iterator();
  144. int i = 0;
  145. while (it.hasNext()) {
  146. e = it.next();
  147. dat[j][i] = e.getValue();
  148. if (j == 0) {
  149. Class class1 = e.getValue().getClass();
  150. classes[i] = class1;
  151. head.add(i, e.getKey().toUpperCase());
  152. }
  153. i++;
  154. }
  155. j++;
  156. }
  157. final Object[] headers = new Object[head.size()];
  158. int x = 0;
  159. for (String hea : head) {
  160. headers[x] = hea;
  161. x++;
  162. }
  163. setEditable(false);
  164. setTypes(classes);
  165. try {
  166. // Log.PrintArray(dat);
  167. Object[] h = getDefaultHeader(15);
  168. setDataVector(dat, h);
  169. } catch (Exception ge) {
  170. }
  171. if (target != null) {
  172. target.setAutoCreateRowSorter(true);
  173. }
  174. target.repaint();
  175. target.getParent().validate();
  176. }
  177. @Override
  178. public MPTableModel clone() {
  179. MPTableModel t = new MPTableModel(types, canEdits, getData(), predefinedRow);
  180. return t;
  181. }
  182. /**
  183. * Creates an uneditable model out of the given data
  184. *
  185. * @param data
  186. */
  187. public MPTableModel(Object[][] data) {
  188. super();
  189. if (data != null && data.length > 0) {
  190. String[] header = new String[data[0].length];
  191. for (int i = 0; i < header.length; i++) {
  192. header[i] = "" + i;
  193. }
  194. setDataVector(data, header);
  195. Class[] typs = new Class[header.length];
  196. for (int i = 0; i < header.length; i++) {
  197. typs[i] = Object.class;
  198. }
  199. setTypes(typs);
  200. }
  201. setEditable(false);
  202. }
  203. // /**
  204. // * Creates an uneditable model out of the given data
  205. // * @param list
  206. // * @param header
  207. // */
  208. // public MPTableModel(ArrayList<DatabaseObject> list, String[] header) {
  209. // super();
  210. // nativeMode = true;
  211. // setDataVector(MPTableModelRow.toRows(list), header);
  212. //
  213. // }
  214. /**
  215. * Creates an uneditable model out of the given data
  216. *
  217. * @param datstr
  218. * @param header
  219. */
  220. public MPTableModel(Object[][] datstr, String[] header) {
  221. super();
  222. setDataVector(datstr, header);
  223. setEditable(false);
  224. Class[] typs = new Class[header.length];
  225. for (int i = 0; i < header.length; i++) {
  226. typs[i] = Object.class;
  227. }
  228. setTypes(typs);
  229. }
  230. /**
  231. * Creates an uneditable model out of the given data
  232. *
  233. * @param data
  234. * @param header
  235. */
  236. public MPTableModel(Object[][] data, Headers header) {
  237. super(data, header.getValue());
  238. setEditable(false);
  239. Class[] typs = new Class[header.getValue().length];
  240. for (int i = 0; i < header.getValue().length; i++) {
  241. typs[i] = Object.class;
  242. }
  243. setTypes(typs);
  244. }
  245. /**
  246. * Creates an uneditable model out of the given data
  247. *
  248. * @param datstr
  249. * @param header
  250. * @param types
  251. */
  252. public MPTableModel(Object[][] datstr, String[] header, Class[] types) {
  253. super(datstr, header);
  254. setEditable(false);
  255. setTypes(types);
  256. }
  257. /**
  258. * Creates an uneditable model out of the given data
  259. *
  260. * @param types
  261. * @param canEdits
  262. * @param data
  263. * @param columnNames
  264. */
  265. public MPTableModel(Class[] types, boolean[] canEdits, Object[][] data, Object[] columnNames) {
  266. super(data, columnNames);
  267. setTypes(types);
  268. setCanEdits(canEdits);
  269. }
  270. /**
  271. * Creates a model out of the given data
  272. *
  273. * @param types
  274. * @param canEdits
  275. * @param columnNames
  276. */
  277. public MPTableModel(Class[] types, boolean[] canEdits, Object[] columnNames) {
  278. super(columnNames, 1);
  279. setTypes(types);
  280. setCanEdits(canEdits);
  281. }
  282. /**
  283. * Creates an uneditable model out of the given data
  284. *
  285. * @param types
  286. * @param data
  287. * @param columnNames
  288. */
  289. public MPTableModel(Class[] types, Object[][] data, Object[] columnNames) {
  290. super(data, columnNames);
  291. setTypes(types);
  292. setEditable(false);
  293. }
  294. /**
  295. * Creates an uneditable model out of the given data
  296. *
  297. * @param types
  298. * @param data
  299. * @param columnNames
  300. */
  301. public MPTableModel(List<Object[]> list, String[] header) {
  302. super();
  303. int width = 0;
  304. for (int i = 0; i < list.size(); i++) {
  305. Object[] objects = list.get(i);
  306. if (objects.length > width) {
  307. width = objects.length;
  308. }
  309. }
  310. if (list.size() > 0) {
  311. if (header == null) {
  312. header = new String[width];
  313. for (int i = 0; i < header.length; i++) {
  314. header[i] = String.valueOf(i);
  315. }
  316. }
  317. Object[][] arr = ArrayUtilities.listToTableArray(list);
  318. setDataVector(arr, header);
  319. }
  320. setEditable(false);
  321. Class[] typs = new Class[header.length];
  322. for (int i = 0; i < header.length; i++) {
  323. typs[i] = Object.class;
  324. }
  325. setTypes(typs);
  326. }
  327. public MPTableModel(List<ValueProperty> properties) {
  328. super();
  329. final Object[][] data = new Object[properties.size() + 10][2];
  330. for (int i = 0; i < properties.size(); i++) {
  331. ValueProperty valueProperty = properties.get(i);
  332. data[i][0] = valueProperty.__getCname();
  333. data[i][1] = valueProperty.getValueObj();
  334. }
  335. setDataVector(data, new Object[]{Messages.PROPERTY, Messages.VALUE});
  336. setEditable(true);
  337. setTypes(new Class<?>[]{String.class, Object.class});
  338. }
  339. /**
  340. * Add a cell calculator for this model
  341. *
  342. * @param cv
  343. */
  344. public void addCalculator(DynamicTableCalculator cv) {
  345. this.calculators.add(cv);
  346. }
  347. @Override
  348. public Class<?> getColumnClass(int columnIndex) {
  349. try {
  350. getTypes()[columnIndex].toString();//Check for non-null
  351. } catch (Exception e) {
  352. return Object.class;
  353. }
  354. //not sortable
  355. if (getTypes()[columnIndex].equals(void.class)) {
  356. return Object.class;
  357. } else {
  358. return getTypes()[columnIndex];
  359. }
  360. }
  361. @Override
  362. public boolean isCellEditable(int rowIndex, int columnIndex) {
  363. new Boolean(getCanEdits()[columnIndex]).toString();//Check for non-null
  364. return getCanEdits()[columnIndex];
  365. }
  366. public void setCellEditable(int rowIndex, int columnIndex, boolean editable) {
  367. new Boolean(getCanEdits()[columnIndex]).toString();//Check for non-null
  368. getCanEdits()[columnIndex] = editable;
  369. }
  370. /**
  371. *
  372. * @return
  373. */
  374. public Class[] getTypes() {
  375. return types;
  376. }
  377. /**
  378. *
  379. * @param types
  380. */
  381. public final void setTypes(Class... types) {
  382. this.types = types;
  383. }
  384. /**
  385. *
  386. * @return
  387. */
  388. public boolean[] getCanEdits() {
  389. return canEdits;
  390. }
  391. // /**
  392. // *
  393. // * @param canEdits
  394. // */
  395. // public void setCanEdits(boolean[] canEdits) {
  396. // this.canEdits = canEdits;
  397. // }
  398. /**
  399. *
  400. * @return
  401. */
  402. public Vector getColumnIdentifiers() {
  403. return columnIdentifiers;
  404. }
  405. /**
  406. * Set the table editable
  407. *
  408. * @param bool
  409. */
  410. public final void setEditable(boolean bool) {
  411. setCanEdits(new boolean[]{bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool,
  412. bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool,
  413. bool, bool, bool, bool, bool, bool, bool, bool, bool});
  414. }
  415. @Override
  416. @SuppressWarnings("unchecked")
  417. public Object getValueAt(int row, int column) {
  418. if (column < getColumnCount()) {
  419. if (row < getRowCount()) {
  420. return super.getValueAt(row, column);
  421. } else {
  422. throw new ArrayIndexOutOfBoundsException("The row " + row + " is not within the models row count of " + getRowCount());
  423. }
  424. } else {
  425. throw new ArrayIndexOutOfBoundsException("The column " + column + " is not within the models column count of " + getColumnCount());
  426. }
  427. }
  428. /**
  429. * Behaves like setValueAt(row, column)
  430. *
  431. * @param aValue
  432. * @param row
  433. * @param column
  434. * @param dontFire If true, does not fire table cell event!
  435. */
  436. @SuppressWarnings("unchecked")
  437. public void setValueAt(Object aValue, int row, int column, boolean dontFire) {
  438. Vector rowVector = (Vector) dataVector.elementAt(row);
  439. rowVector.setElementAt(aValue, column);
  440. if (!dontFire) {
  441. fireTableCellUpdated(row, column);
  442. }
  443. }
  444. private void setDataVector(Object[][] object, Headers head) {
  445. super.setDataVector(object, head.getValue());
  446. }
  447. @Override
  448. public synchronized void fireTableCellUpdated(int row, int column) {
  449. for (int i = 0; i < calculators.size(); i++) {
  450. DynamicTableCalculator calculator = calculators.get(i);
  451. if (calculator != null && !calculator.isTargetCell(row, column)) {
  452. calculator.calculateOnce();
  453. }
  454. }
  455. fireTableChanged(new TableModelEvent(this, row, row, column));
  456. }
  457. /**
  458. * Checks if the model has empty rows
  459. *
  460. * @param columnsToCheck The columns to be checked for emptyness
  461. * @return TRUE if the last row of the model has a NULL value at the
  462. * specified columns
  463. */
  464. public boolean hasEmptyRows(int[] columnsToCheck) {
  465. boolean empty = true;
  466. for (int i = 0; i < columnsToCheck.length; i++) {
  467. if (getValueAt(getRowCount() - 1, columnsToCheck[i]) != null) {
  468. empty = false;
  469. }
  470. }
  471. return empty;
  472. }
  473. /**
  474. * Checks if the model has empty rows
  475. *
  476. * @param columnsToCheck The columns to be checked for emptyness
  477. * @return the count of rows in the model with a NULL or empty "" value at
  478. * the specified columns
  479. */
  480. public int getEmptyRows(int[] columnsToCheck) {
  481. int count = 0;
  482. for (int j = 0; j < getRowCount(); j++) {
  483. boolean found = false;
  484. for (int i = 0; i < columnsToCheck.length; i++) {
  485. if (getValueAt(j, columnsToCheck[i]) == null || String.valueOf(getValueAt(j, columnsToCheck[i])).length() == 0) {
  486. found = true;
  487. }
  488. }
  489. if (found) {
  490. count++;
  491. }
  492. }
  493. return count;
  494. }
  495. /**
  496. * Removes the invalid rows
  497. *
  498. * @param columnsToCheck The columns to be checked for emptyness
  499. */
  500. public void removeEmptyRows(int[] columnsToCheck) {
  501. for (int j = 0; j < getRowCount(); j++) {
  502. for (int i = 0; i < columnsToCheck.length; i++) {
  503. if (getValueAt(j, columnsToCheck[i]) == null || String.valueOf(getValueAt(j, columnsToCheck[i])).length() == 0) {
  504. removeRow(j);
  505. }
  506. }
  507. }
  508. }
  509. /**
  510. * Adds rows to the end of the model. The new rows will contain null
  511. * values.<br/> If this.context is defined, Context specific values may be
  512. * added.
  513. *
  514. * @param count
  515. */
  516. public void addRow(int count) {
  517. for (int i = 0; i < count; i++) {
  518. if (predefinedRow == null) {
  519. addRow((Object[]) null);
  520. } else {
  521. if (autoCountColumn != null) {
  522. predefinedRow[autoCountColumn] = getRowCount() + 1;
  523. }
  524. addRow(predefinedRow);
  525. }
  526. }
  527. }
  528. @Override
  529. public void removeRow(int row) {
  530. super.removeRow(row);
  531. rearrangeAutocountColumn();
  532. }
  533. public void insertRow(int row) {
  534. if (predefinedRow == null) {
  535. super.insertRow(row, (Object[]) null);
  536. } else {
  537. super.insertRow(row, predefinedRow);
  538. }
  539. rearrangeAutocountColumn();
  540. }
  541. @Override
  542. public void moveRow(int start, int end, int to) {
  543. super.moveRow(start, end, to);
  544. rearrangeAutocountColumn();
  545. }
  546. @SuppressWarnings("unchecked")
  547. private void rearrangeAutocountColumn() {
  548. if (autoCountColumn == null) {
  549. return;
  550. }
  551. int index = 0;
  552. for (Object rowVector : dataVector) {
  553. ((Vector) rowVector).setElementAt(index + 1, autoCountColumn);
  554. fireTableCellUpdated(index++, autoCountColumn);
  555. }
  556. }
  557. /**
  558. * @return the context
  559. */
  560. public Context getContext() {
  561. return context;
  562. }
  563. /**
  564. * @param context the context to set
  565. */
  566. public final void setContext(Context context) {
  567. this.context = context;
  568. }
  569. /**
  570. * Define a row which is used in addRow(int)
  571. *
  572. * @param object
  573. */
  574. public void defineRow(Object[] object) {
  575. predefinedRow = object;
  576. }
  577. /**
  578. * Returns all rows where all of the specified columns are not NULL. <br/>
  579. *
  580. * @param columns
  581. * @return
  582. */
  583. public LinkedList<Object[]> getValidRows(int[] columns) {
  584. LinkedList<Object[]> rows = new LinkedList<Object[]>();
  585. for (int ki = 0; ki < getRowCount(); ki++) {
  586. boolean valid = true;
  587. for (int i = 0; i < columns.length; i++) {
  588. int j = columns[i];
  589. if (getValueAt(ki, j) == null || getValueAt(ki, j).toString().length() == 0) {
  590. valid = false;
  591. }
  592. }
  593. if (valid) {
  594. Object[] t = new Object[getColumnCount()];
  595. for (int i = 0; i < getColumnCount(); i++) {
  596. t[i] = getValueAt(ki, i);
  597. }
  598. rows.add(t);
  599. }
  600. }
  601. return rows;
  602. }
  603. /**
  604. * Set/replace the given rows data
  605. *
  606. * @param rowData
  607. * @param row
  608. * @param columnToIgnore
  609. */
  610. public synchronized void setRowAt(Object[] rowData, int row, Integer... columnToIgnore) {
  611. if (getRowCount() <= row) {
  612. addRow(1);
  613. }
  614. List<Integer> ignores = Arrays.asList(columnToIgnore);
  615. for (Integer i = 0; i < rowData.length; i++) {
  616. if (!ignores.contains(i)) {
  617. Object object = rowData[i];
  618. try {
  619. setValueAt(object, row, i);
  620. } catch (Exception e) {
  621. }
  622. }
  623. }
  624. }
  625. /**
  626. * Set the auto increment column for addRows(int)
  627. *
  628. * @param column
  629. */
  630. public void setAutoCountColumn(int column) {
  631. autoCountColumn = column;
  632. }
  633. /**
  634. * Returns the data as array
  635. *
  636. * @return
  637. */
  638. @SuppressWarnings("unchecked")
  639. public Object[][] getData() {
  640. Object[][] k = new Object[getRowCount()][getColumnCount()];
  641. for (int i = 0; i < k.length; i++) {
  642. for (int j = 0; j < k[i].length; j++) {
  643. k[i][j] = getValueAt(i, j);
  644. }
  645. }
  646. return k;
  647. }
  648. /**
  649. * Returns the last valid row in a table whereas "valid" is defined as "all
  650. * columns in columns are not null"
  651. *
  652. * @param columns
  653. * @return
  654. */
  655. public int getLastValidRow(int[] columns) {
  656. int row = 0;
  657. for (int ki = 0; ki < getRowCount(); ki++) {
  658. for (int i = 0; i < columns.length; i++) {
  659. int j = columns[i];
  660. if (getValueAt(ki, j) == null || getValueAt(ki, j).toString().length() == 0) {
  661. } else {
  662. row = ki;
  663. }
  664. }
  665. }
  666. return row;
  667. }
  668. /**
  669. * Add all rows
  670. *
  671. * @param rowsl
  672. */
  673. public void addRows(List<Object[]> rowsl) {
  674. for (Object[] obj : rowsl) {
  675. addRow(obj);
  676. }
  677. }
  678. /**
  679. * Adds all rows using the toArray() method of {@link DatabaseObject}
  680. *
  681. * @param l
  682. */
  683. public void addRows(ArrayList<DatabaseObject> l) {
  684. for (DatabaseObject b : l) {
  685. addRow(b.toArray());
  686. }
  687. }
  688. /**
  689. * Returns an Object where the objects ID is stored in the first column of
  690. * the given row; <b>From database/cache, NOT from the model!</b>
  691. *
  692. * @param <T>
  693. * @param selectedRow
  694. * @param target
  695. * @return
  696. */
  697. @SuppressWarnings("unchecked")
  698. public <T extends DatabaseObject> T getRowAt(int selectedRow, T target) {
  699. try {
  700. return (T) target.getObject(target.getContext(), Integer.valueOf(String.valueOf(getValueAt(selectedRow, 0))));
  701. } catch (Exception ex) {
  702. Log.Debug(this, ex.getMessage());
  703. return null;
  704. }
  705. }
  706. /**
  707. * Set the editable columns
  708. *
  709. * @param editable
  710. */
  711. public final void setCanEdits(boolean... editable) {
  712. this.canEdits = editable;
  713. }
  714. public static TableCellEditor getTablecCellEditorFor(Class<?> clazz) {
  715. if (clazz.isAssignableFrom(Number.class)) {
  716. return new TableCellEditorForDezimal(new JFormattedTextField());
  717. } else {
  718. return new DefaultCellEditor(new JTextField());
  719. }
  720. }
  721. /**
  722. * @return the owner
  723. */
  724. public JTable getOwner() {
  725. return owner;
  726. }
  727. /**
  728. * @param owner the owner to set
  729. */
  730. public void setOwner(JTable owner) {
  731. this.owner = owner;
  732. }
  733. @Override
  734. public void columnAdded(TableColumnModelEvent e) {
  735. }
  736. @Override
  737. public void columnRemoved(TableColumnModelEvent e) {
  738. }
  739. @Override
  740. public void columnMoved(TableColumnModelEvent e) {
  741. resort();
  742. }
  743. @Override
  744. public void columnMarginChanged(ChangeEvent e) {
  745. }
  746. @Override
  747. public void columnSelectionChanged(ListSelectionEvent e) {
  748. }
  749. private void resort() {
  750. }
  751. private Object[] getDefaultHeader(int x) {
  752. Object[] obj = new Object[x];
  753. for (int i = 0; i < x; i++) {
  754. obj[i] = i;
  755. }
  756. return obj;
  757. }
  758. public void removeColumn(int column) {
  759. columnIdentifiers.remove(column);
  760. for (Object row : dataVector) {
  761. if (row instanceof Vector) {
  762. ((Vector) row).remove(column);
  763. }
  764. }
  765. fireTableStructureChanged();
  766. }
  767. /**
  768. * Default renderers
  769. *
  770. */
  771. public static class NumberRenderer extends DefaultTableCellRenderer.UIResource {
  772. public NumberRenderer() {
  773. super();
  774. setHorizontalAlignment(JLabel.RIGHT);
  775. }
  776. }
  777. /**
  778. *
  779. */
  780. public static class DoubleRenderer extends NumberRenderer {
  781. public DoubleRenderer() {
  782. super();
  783. }
  784. @Override
  785. public void setValue(Object value) {
  786. try {
  787. setText((value == null) ? "" : FormatNumber.formatDezimal(Double.valueOf(value.toString())));
  788. } catch (Exception e) {
  789. Log.Debug(MPTableModel.class, "Error caused by: " + value);
  790. }
  791. }
  792. }
  793. /**
  794. *
  795. */
  796. public static class DateRenderer extends DefaultTableCellRenderer.UIResource {
  797. DateFormat formatter;
  798. public DateRenderer() {
  799. super();
  800. }
  801. @Override
  802. public void setValue(Object value) {
  803. if (formatter == null) {
  804. formatter = DateFormat.getDateInstance();
  805. }
  806. try {
  807. setText((value == null) ? "" : formatter.format(value));
  808. } catch (Exception e) {
  809. Log.Debug(e);
  810. }
  811. }
  812. }
  813. /**
  814. *
  815. */
  816. public static class IconRenderer extends DefaultTableCellRenderer.UIResource {
  817. public IconRenderer() {
  818. super();
  819. setHorizontalAlignment(JLabel.CENTER);
  820. }
  821. @Override
  822. public void setValue(Object value) {
  823. try {
  824. setIcon((value instanceof Icon) ? (Icon) value : null);
  825. } catch (Exception e) {
  826. Log.Debug(e);
  827. }
  828. }
  829. }
  830. }