PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/ui/misc/TableViewPersistenceHandler.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 209 lines | 120 code | 23 blank | 66 comment | 19 complexity | f51c18bd91795a6f1c3691ee64558942 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.ui.misc;
  18. //~--- non-JDK imports --------------------------------------------------------
  19. import com.l2fprod.common.demo.Main;
  20. import java.awt.Component;
  21. import java.awt.Rectangle;
  22. import java.awt.event.MouseAdapter;
  23. import java.awt.event.MouseEvent;
  24. import java.beans.PropertyChangeEvent;
  25. import java.beans.PropertyChangeListener;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.Enumeration;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31. import mpv5.db.objects.User;
  32. import mpv5.ui.misc.MPTable;
  33. //~--- JDK imports ------------------------------------------------------------
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import javax.swing.JComponent;
  37. import javax.swing.JTable;
  38. import javax.swing.event.ChangeEvent;
  39. import javax.swing.event.ListSelectionEvent;
  40. import javax.swing.event.TableColumnModelEvent;
  41. import javax.swing.event.TableColumnModelListener;
  42. import javax.swing.table.JTableHeader;
  43. import javax.swing.table.TableColumn;
  44. import javax.swing.table.TableColumnModel;
  45. import mpv5.logging.Log;
  46. import mpv5.utils.files.FileDirectoryHandler;
  47. import org.jdesktop.application.SessionStorage;
  48. /**
  49. *
  50. * @author anti
  51. */
  52. public final class TableViewPersistenceHandler {
  53. private List<TableColumnModelListener> listeners = new ArrayList<TableColumnModelListener>();
  54. private final Component identifier;
  55. private final JTable target;
  56. private final SessionStorage storage;
  57. private final TableColumnModelListener cListener;
  58. private final String saveFile;
  59. private final PropertyChangeListener wListener;
  60. private final MouseAdapter mListener;
  61. /**
  62. *
  63. * @param target
  64. * @param identifier
  65. */
  66. public TableViewPersistenceHandler(final MPTable target, final Component identifier) {
  67. this(target, identifier, false);
  68. }
  69. /**
  70. *
  71. * @param target
  72. * @param identifier
  73. * @param passive
  74. */
  75. public TableViewPersistenceHandler(final MPTable target, final Component identifier, final boolean passive) {
  76. if (User.getCurrentUser().__getCname() == null || User.getCurrentUser().__getCname().length() == 0) {
  77. throw new IllegalStateException("The username is not set.");
  78. }
  79. if (target.getName() == null || target.getName().length() == 0) {
  80. throw new IllegalStateException("The table name is not set.");
  81. }
  82. if (identifier == null ) {
  83. throw new IllegalStateException("The identifier is not set: " + identifier);
  84. }
  85. if (identifier.getName() == null || identifier.getName().length() == 0) {
  86. throw new IllegalStateException("The identifier name is not set: " + identifier);
  87. }
  88. saveFile = User.getCurrentUser().__getCname() + "_" + target.getName() + "_" + identifier.getName() + ".xml";
  89. storage = mpv5.YabsApplication.getApplication().getContext().getSessionStorage();
  90. mListener = new MouseAdapter() {
  91. @Override
  92. public void mouseReleased(MouseEvent evt) {
  93. persist();
  94. }
  95. };
  96. wListener = new PropertyChangeListener() {
  97. public void propertyChange(PropertyChangeEvent evt) {
  98. // if (evt.getPropertyName().equals("model")) {
  99. // // Log.Debug(this, evt);
  100. // setObserveColumnState(true);
  101. // }
  102. //
  103. //// Log.Debug(this, evt.getPropertyName());
  104. //
  105. // if (!passive && isObserveColumnState() && evt.getPropertyName().equals("preferredWidth")) {
  106. // // Log.Debug(this, evt);
  107. // persist();
  108. // }
  109. }
  110. };
  111. cListener = new TableColumnModelListener() {
  112. public void columnAdded(TableColumnModelEvent e) {
  113. }
  114. public void columnRemoved(TableColumnModelEvent e) {
  115. }
  116. public void columnMoved(TableColumnModelEvent e) {
  117. }
  118. public void columnMarginChanged(ChangeEvent e) {
  119. }
  120. public void columnSelectionChanged(ListSelectionEvent e) {
  121. }
  122. };
  123. this.target = target;
  124. this.identifier = identifier;
  125. // target.setColumnModel(new MPColumnModel());
  126. //
  127. // try {
  128. // restore();
  129. // } catch (IOException ex) {
  130. // Log.Debug(ex);
  131. // persist();
  132. // }
  133. //
  134. set();
  135. }
  136. /**
  137. * Set a listener
  138. */
  139. protected boolean set() {
  140. // try {
  141. // Log.Debug(this, "Setting restore listeners for table: " + target.getName());
  142. // res = restore();
  143. // } catch (Exception ex) {
  144. // Log.Debug(this, ex);
  145. // res = false;
  146. //// User.getCurrentUser().getLayoutProperties().remove(target.getName() + "@" + identifier.getName());
  147. //// target.createDefaultColumnsFromModel();
  148. //// persist();
  149. // }
  150. target.getColumnModel().addColumnModelListener(cListener);
  151. target.getTableHeader().addMouseListener(mListener);
  152. target.addPropertyChangeListener(wListener);
  153. Enumeration<TableColumn> cols = target.getColumnModel().getColumns();
  154. while (cols.hasMoreElements()) {
  155. cols.nextElement().addPropertyChangeListener(wListener);
  156. }
  157. return true;
  158. }
  159. protected boolean restore() {
  160. if (hasData()) {
  161. Log.Debug(this, "Trying to restore table: " + target.getName());
  162. try {
  163. storage.restoreSingle(target, saveFile);
  164. } catch (IOException ex) {
  165. Log.Debug(ex);
  166. return false;
  167. }
  168. return true;
  169. } else {
  170. Log.Debug(this, "Cannot restore table: " + target.getName());
  171. return false;
  172. }
  173. }
  174. private void persist() {
  175. try {
  176. storage.saveSingle(target, saveFile);
  177. Log.Debug(this, "Persisting table state: " + target);
  178. } catch (IOException ex) {
  179. Log.Debug(ex);
  180. }
  181. }
  182. protected boolean hasData() {
  183. File x = new File(FileDirectoryHandler.getTempDir() + saveFile);
  184. return x.exists() && x.canWrite() && x.canRead();
  185. }
  186. }