PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/misc/ComponentStateManager.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 155 lines | 91 code | 31 blank | 33 comment | 11 complexity | c0c5ad68b0ba752cdb1ee3123e303c24 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. package mpv5.ui.misc;
  2. //~--- non-JDK imports --------------------------------------------------------
  3. import mpv5.logging.Log;
  4. //~--- JDK imports ------------------------------------------------------------
  5. import java.beans.XMLDecoder;
  6. import java.beans.XMLEncoder;
  7. import java.io.ByteArrayInputStream;
  8. import java.io.ByteArrayOutputStream;
  9. import java.util.Set;
  10. import java.util.TreeSet;
  11. import javax.swing.JTable;
  12. import javax.swing.table.DefaultTableColumnModel;
  13. import javax.swing.table.TableColumn;
  14. import javax.swing.table.TableColumnModel;
  15. /**
  16. * The Component State manager provides functions to store and restore the state of UI Components, yet only {@link JTable}
  17. */
  18. public class ComponentStateManager {
  19. /**
  20. * @author Florian Strienz
  21. * @param table
  22. * @param user
  23. */
  24. public static synchronized String persist(JTable table) {
  25. try {
  26. ByteArrayOutputStream io = new ByteArrayOutputStream();
  27. XMLEncoder encoder = new XMLEncoder(io);
  28. TableColumnModel tableColumnModel = table.getColumnModel();
  29. final Set<TableColumnLayoutInfo> tableColumnLayoutInfos = new TreeSet<TableColumnLayoutInfo>();
  30. for (int currentColumnIndex = 0; currentColumnIndex < tableColumnModel.getColumnCount();
  31. currentColumnIndex++) {
  32. TableColumn tableColumn = tableColumnModel.getColumn(currentColumnIndex);
  33. TableColumnLayoutInfo tableColumnLayoutInfo =
  34. new TableColumnLayoutInfo(tableColumn.getIdentifier().toString(),
  35. tableColumnModel.getColumnIndex(tableColumn.getIdentifier()),
  36. tableColumn.getWidth());
  37. tableColumnLayoutInfos.add(tableColumnLayoutInfo);
  38. }
  39. encoder.writeObject(tableColumnLayoutInfos);
  40. encoder.flush();
  41. encoder.close();
  42. return io.toString("UTF-8");
  43. } catch (Exception e) {
  44. Log.Debug(e);
  45. }
  46. return null;
  47. }
  48. /**
  49. * @author Florian Strienz
  50. *
  51. * @param data
  52. * @param view
  53. * @param table
  54. * @param user
  55. */
  56. public static synchronized void reload(String data, JTable table) throws Exception {
  57. // Log.Debug(ComponentStateManager.class, data);//
  58. if (data != null) {
  59. // Log.Debug(ComponentStateManager.class, "Reloading layout for: " + table.getName() + " data: " + data);
  60. try {
  61. ByteArrayInputStream io =
  62. new ByteArrayInputStream(data.getBytes("UTF-8"));
  63. XMLDecoder decoder = new XMLDecoder(io);
  64. @SuppressWarnings("unchecked")
  65. Set<TableColumnLayoutInfo> tableColumnLayoutInfos =
  66. (Set<TableColumnLayoutInfo>) decoder.readObject();
  67. TableColumnModel tableColumnModel = new DefaultTableColumnModel();
  68. int index = 0;
  69. for (TableColumnLayoutInfo tableColumnLayoutInfo : tableColumnLayoutInfos) {
  70. if ((tableColumnLayoutInfo.getColumnName().length() > 0) && (table.getColumnCount() > 0)) {
  71. try {
  72. TableColumn tableColumn = table.getColumn(tableColumnLayoutInfo.getColumnName());
  73. tableColumn.setPreferredWidth(tableColumnLayoutInfo.getWidth());
  74. tableColumnModel.addColumn(tableColumn);
  75. // Log.Print(tableColumnLayoutInfo.getColumnName() + " : " + tableColumnLayoutInfo.getWidth());
  76. } catch (Exception e) {
  77. try {
  78. Log.Debug(ComponentStateManager.class, e + ": " + tableColumnLayoutInfo.getColumnName());
  79. TableColumn tableColumn = table.getColumn(table.getColumnName(index));
  80. tableColumn.setPreferredWidth(tableColumnLayoutInfo.getWidth());
  81. tableColumnModel.addColumn(tableColumn);
  82. } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
  83. //ignore
  84. //
  85. // Log.Debug(ComponentStateManager.class, "Layout for: " + table.getName() + " data: " + data);
  86. }
  87. // Log.Debug(ComponentStateManager.class, e + " Count: " + table.getColumnCount());
  88. //
  89. // int cols = table.getColumnCount();
  90. //
  91. // for (int i = 0; i < cols; i++) {
  92. // Log.Debug(ComponentStateManager.class, table.getColumnName(i));
  93. // }
  94. // throw e;
  95. }
  96. index++;
  97. }
  98. }
  99. TableColumnModel model = table.getColumnModel();
  100. for (int i = 0; i < model.getColumnCount(); i++) {
  101. boolean found = false;
  102. for (int z = 0; z < tableColumnModel.getColumnCount(); z++) {
  103. if (tableColumnModel.getColumn(z).getHeaderValue().equals(
  104. model.getColumn(i).getHeaderValue())) {
  105. found = true;
  106. break;
  107. }
  108. }
  109. if (!found) {
  110. tableColumnModel.addColumn(model.getColumn(i));
  111. }
  112. }
  113. table.setColumnModel(tableColumnModel);
  114. decoder.close();
  115. } catch (Exception e) {
  116. Log.Debug(e);
  117. throw e;
  118. }
  119. } else {
  120. throw new NullPointerException();
  121. }
  122. }
  123. }
  124. //~ Formatted by Jindent --- http://www.jindent.com