/src/mpv5/utils/tables/TableFormat.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 371 lines · 229 code · 51 blank · 91 comment · 33 complexity · 99db998260f8f68d6c85bf52e714bde2 MD5 · raw file

  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.tables;
  18. //~--- non-JDK imports --------------------------------------------------------
  19. import com.l2fprod.common.swing.renderer.ColorCellRenderer;
  20. import java.util.Enumeration;
  21. import javax.swing.table.TableColumn;
  22. import javax.swing.table.TableColumnModel;
  23. import mpv5.db.common.DatabaseObject;
  24. import mpv5.db.common.DatabaseObject.Entity;
  25. import mpv5.globals.LocalSettings;
  26. import mpv5.logging.Log;
  27. import mpv5.utils.models.MPTableModel;
  28. //~--- JDK imports ------------------------------------------------------------
  29. import java.awt.Color;
  30. import java.awt.Component;
  31. import java.awt.Dimension;
  32. import java.awt.Font;
  33. import javax.swing.BorderFactory;
  34. import javax.swing.JComponent;
  35. import javax.swing.JLabel;
  36. import javax.swing.JTable;
  37. import javax.swing.event.ChangeEvent;
  38. import javax.swing.event.ListSelectionEvent;
  39. import javax.swing.event.TableColumnModelEvent;
  40. import javax.swing.event.TableColumnModelListener;
  41. import javax.swing.table.DefaultTableCellRenderer;
  42. import javax.swing.table.DefaultTableModel;
  43. import javax.swing.table.TableCellEditor;
  44. import javax.swing.table.TableCellRenderer;
  45. /**
  46. *
  47. *
  48. */
  49. public class TableFormat {
  50. /**
  51. * Changes table values from text to class values,<br/>
  52. * currently supported classes:<br/>
  53. * - <code>Boolean.class</code>: "1" and "true" will be true, false otherwise
  54. * @param values The original values
  55. * @param aClass The class to be changed
  56. * @param cols The column to change
  57. * @return
  58. */
  59. public static Object[][] changeToClassValue(Object[][] values, Class aClass, int[] cols) {
  60. try {
  61. Object[][] data = new Object[values.length][values[0].length];
  62. for (int idx = 0; idx < values.length; idx++) {
  63. for (int k = 0; k < cols.length; k++) {
  64. if (aClass.getName().equalsIgnoreCase("java.lang.Boolean")) {
  65. if (String.valueOf(values[idx][cols[k]]).equalsIgnoreCase("1")
  66. || String.valueOf(values[idx][cols[k]]).equalsIgnoreCase("true")) {
  67. data[idx][cols[k]] = true;
  68. } else {
  69. data[idx][cols[k]] = false;
  70. }
  71. }
  72. }
  73. for (int h = 0; h < values[0].length; h++) {
  74. if (data[idx][h] == null) {
  75. data[idx][h] = values[idx][h];
  76. }
  77. }
  78. }
  79. return data;
  80. } catch (Exception e) {
  81. // Log.Debug(this,e);
  82. return new Object[0][0];
  83. }
  84. }
  85. public static void makeUneditable(JTable table) {
  86. try {
  87. ((MPTableModel) table.getModel()).setCanEdits(new boolean[]{
  88. false, false, false, false, false, false, false, false, false, false, false, false, false
  89. });
  90. } catch (Exception e) {
  91. Log.Debug(TableFormat.class, "Can not change this table to uneditable.");
  92. }
  93. }
  94. public static void makeUneditableColumns(JTable table, Integer[] desiredCol) {
  95. boolean[] unedits;
  96. try {
  97. unedits = new boolean[desiredCol.length];
  98. for (int i = 0; i < desiredCol.length; i++) {
  99. if (desiredCol[i] != null) {
  100. unedits[i] = false;
  101. } else {
  102. unedits[i] = true;
  103. }
  104. }
  105. ((MPTableModel) table.getModel()).setCanEdits(unedits);
  106. } catch (Exception e) {
  107. Log.Debug(TableFormat.class, "Can not change this table to uneditable.");
  108. }
  109. }
  110. /**
  111. * Stops the tables' cell editor
  112. * @param jTable1
  113. */
  114. public static void stopEditing(JTable jTable1) {
  115. TableCellEditor editor = jTable1.getCellEditor();
  116. if (editor != null) {
  117. try {
  118. editor.stopCellEditing();
  119. } catch (Exception e) {
  120. }
  121. }
  122. }
  123. /**
  124. * Resizes a tables cols
  125. * @param table
  126. * @param desiredColSizes
  127. * @param fixed Should the cols be non-resizable
  128. */
  129. public static void resizeCols(JTable table, Integer[] desiredColSizes, boolean fixed) {
  130. for (int i = 0; i < desiredColSizes.length; i++) {
  131. if (desiredColSizes[i] != null) {
  132. try {
  133. table.getColumnModel().getColumn(i).setMinWidth(desiredColSizes[i]);
  134. table.getColumnModel().getColumn(i).setPreferredWidth(desiredColSizes[i]);
  135. if (fixed) {
  136. table.getColumnModel().getColumn(i).setMaxWidth(desiredColSizes[i]);
  137. } else {
  138. table.getColumnModel().getColumn(i).setMaxWidth(1000);
  139. }
  140. } catch (Exception e) {
  141. Log.Debug(TableFormat.class, e.getMessage());
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Resizes a tables cols
  148. * @param table
  149. * @param columnIndex
  150. * @param desiredColSize
  151. * @param fixed Should the cols be non-resizable
  152. */
  153. public static void resizeCol(JTable table, int columnIndex, int desiredColSize, boolean fixed) {
  154. try {
  155. table.getColumnModel().getColumn(columnIndex).setMinWidth(desiredColSize);
  156. table.getColumnModel().getColumn(columnIndex).setPreferredWidth(desiredColSize);
  157. if (fixed) {
  158. table.getColumnModel().getColumn(columnIndex).setMaxWidth(desiredColSize);
  159. } else {
  160. table.getColumnModel().getColumn(columnIndex).setMaxWidth(1000);
  161. }
  162. } catch (Exception e) {
  163. Log.Debug(TableFormat.class, e.getMessage());
  164. }
  165. }
  166. public static DefaultTableModel getUneditableTable(String[][] data, String[] header) {
  167. return new javax.swing.table.DefaultTableModel(data, header) {
  168. boolean[] canEdits = new boolean[]{
  169. false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
  170. false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
  171. };
  172. @Override
  173. public boolean isCellEditable(int rowIndex, int columnIndex) {
  174. return canEdits[columnIndex];
  175. }
  176. };
  177. }
  178. /**
  179. * Hide a column of a table
  180. * @param table
  181. * @param columnToHide from model
  182. */
  183. public static void stripColumn(JTable table, int columnToHide) {
  184. try {
  185. table.getColumnModel().getColumn(columnToHide).setWidth(0);
  186. table.getColumnModel().getColumn(columnToHide).setPreferredWidth(0);
  187. table.getColumnModel().getColumn(columnToHide).setMinWidth(0);
  188. table.getColumnModel().getColumn(columnToHide).setMaxWidth(0);
  189. table.doLayout();
  190. } catch (Exception e) {
  191. }
  192. }
  193. /**
  194. * Hide the columns
  195. * @param resulttable
  196. * @param i
  197. */
  198. public static void stripColumns(JTable resulttable, int[] i) {
  199. for (int j = 0; j < i.length; j++) {
  200. int k = i[j];
  201. try {
  202. stripColumn(resulttable, k);
  203. } catch (Exception e) {
  204. }
  205. }
  206. }
  207. /**
  208. * Hide columns with a specific column class
  209. * @param jTable1
  210. * @param aClass
  211. */
  212. public static void stripColumn(JTable table, Class<Entity> aClass) {
  213. for (int i = 0; i < table.getColumnCount(); i++) {
  214. if (table.getColumnClass(i).equals(aClass)) {
  215. stripColumn(table, table.convertColumnIndexToModel(i));
  216. }
  217. }
  218. }
  219. /**
  220. * Hide the first column of a table
  221. * @param table
  222. */
  223. public static void stripFirstColumn(JTable table) {
  224. stripColumn(table, 0);
  225. }
  226. /**
  227. *
  228. * @param table
  229. * @param column
  230. * @param width
  231. */
  232. public static void format(JTable table, int column, int width) {
  233. try {
  234. table.getColumnModel().getColumn(column).setWidth(width);
  235. table.getColumnModel().getColumn(column).setPreferredWidth(width);
  236. table.getColumnModel().getColumn(column).setMinWidth(width);
  237. table.getColumnModel().getColumn(column).setMaxWidth(width);
  238. } catch (Exception e) {
  239. }
  240. }
  241. /**
  242. *
  243. * @param table
  244. * @param desiredColSizes
  245. * @param fixedCols
  246. */
  247. public static void resizeCols(JTable table, Integer[] desiredColSizes, Boolean[] fixedCols) {
  248. for (int i = 0; i < desiredColSizes.length; i++) {
  249. if (desiredColSizes[i] != null) {
  250. table.getColumnModel().getColumn(i).setPreferredWidth(desiredColSizes[i]);
  251. if ((fixedCols.length > 0) && (fixedCols[i] != null) && fixedCols[i]) {
  252. table.getColumnModel().getColumn(i).setMinWidth(desiredColSizes[i]);
  253. table.getColumnModel().getColumn(i).setMaxWidth(desiredColSizes[i]);
  254. } else {
  255. table.getColumnModel().getColumn(i).setMinWidth(0);
  256. table.getColumnModel().getColumn(i).setMaxWidth(1000);
  257. }
  258. }
  259. }
  260. }
  261. /**
  262. * Overwrites any other renderer for this column..
  263. * @param table
  264. * @param column
  265. * @param color
  266. */
  267. public static void changeBackground(final JTable table, final int column, final Color color) {
  268. class ColorRenderer extends JLabel implements TableCellRenderer {
  269. public ColorRenderer() {
  270. this.setOpaque(true);
  271. this.setHorizontalAlignment(JLabel.CENTER);
  272. }
  273. @Override
  274. public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
  275. boolean hasFocus, int row, int col) {
  276. try {
  277. this.setText(value.toString());
  278. } catch (Exception e) {
  279. }
  280. this.setBackground(color);
  281. return this;
  282. }
  283. }
  284. table.getColumnModel().getColumn(column).setCellRenderer(new ColorRenderer());
  285. }
  286. /**
  287. * Hides the header of a table by setting it's header height to 0
  288. * @param table
  289. */
  290. public static void hideHeader(JTable table) {
  291. table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(), 15));
  292. table.getTableHeader().setFont(new Font(LocalSettings.getProperty(LocalSettings.DEFAULT_FONT), Font.PLAIN, 1));
  293. class ColumnListener implements TableColumnModelListener {
  294. private final JTable table;
  295. private ColumnListener(JTable target) {
  296. this.table = target;
  297. }
  298. public void columnAdded(TableColumnModelEvent e) {
  299. table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(),
  300. 15));
  301. }
  302. public void columnRemoved(TableColumnModelEvent e) {
  303. table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(),
  304. 15));
  305. }
  306. public void columnMoved(TableColumnModelEvent e) {
  307. table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(),
  308. 15));
  309. }
  310. public void columnMarginChanged(ChangeEvent e) {
  311. table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(),
  312. 15));
  313. }
  314. public void columnSelectionChanged(ListSelectionEvent e) {
  315. }
  316. }
  317. table.getColumnModel().addColumnModelListener(new ColumnListener(table));
  318. }
  319. }
  320. //~ Formatted by Jindent --- http://www.jindent.com