PageRenderTime 224ms CodeModel.GetById 181ms app.highlight 13ms RepoModel.GetById 27ms 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
  1
  2/*
  3 *  This file is part of YaBS.
  4 *
  5 *      YaBS is free software: you can redistribute it and/or modify
  6 *      it under the terms of the GNU General Public License as published by
  7 *      the Free Software Foundation, either version 3 of the License, or
  8 *      (at your option) any later version.
  9 *
 10 *      YaBS is distributed in the hope that it will be useful,
 11 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 *      GNU General Public License for more details.
 14 *
 15 *      You should have received a copy of the GNU General Public License
 16 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 17 */
 18package mpv5.ui.misc;
 19
 20//~--- non-JDK imports --------------------------------------------------------
 21import com.l2fprod.common.demo.Main;
 22import java.awt.Component;
 23import java.awt.Rectangle;
 24import java.awt.event.MouseAdapter;
 25import java.awt.event.MouseEvent;
 26import java.beans.PropertyChangeEvent;
 27import java.beans.PropertyChangeListener;
 28import java.io.File;
 29import java.io.IOException;
 30import java.util.Enumeration;
 31import java.util.logging.Level;
 32import java.util.logging.Logger;
 33import mpv5.db.objects.User;
 34
 35import mpv5.ui.misc.MPTable;
 36
 37//~--- JDK imports ------------------------------------------------------------
 38
 39import java.util.ArrayList;
 40import java.util.List;
 41
 42import javax.swing.JComponent;
 43import javax.swing.JTable;
 44import javax.swing.event.ChangeEvent;
 45import javax.swing.event.ListSelectionEvent;
 46import javax.swing.event.TableColumnModelEvent;
 47import javax.swing.event.TableColumnModelListener;
 48import javax.swing.table.JTableHeader;
 49import javax.swing.table.TableColumn;
 50import javax.swing.table.TableColumnModel;
 51import mpv5.logging.Log;
 52import mpv5.utils.files.FileDirectoryHandler;
 53import org.jdesktop.application.SessionStorage;
 54
 55/**
 56 *
 57 * @author anti
 58 */
 59public final class TableViewPersistenceHandler {
 60
 61    private List<TableColumnModelListener> listeners = new ArrayList<TableColumnModelListener>();
 62    private final Component identifier;
 63    private final JTable target;
 64    private final SessionStorage storage;
 65    private final TableColumnModelListener cListener;
 66    private final String saveFile;
 67    private final PropertyChangeListener wListener;
 68    private final MouseAdapter mListener;
 69
 70    /**
 71     * 
 72     * @param target
 73     * @param identifier
 74     */
 75    public TableViewPersistenceHandler(final MPTable target, final Component identifier) {
 76        this(target, identifier, false);
 77    }
 78
 79    /**
 80     *
 81     * @param target
 82     * @param identifier
 83     * @param passive  
 84     */
 85    public TableViewPersistenceHandler(final MPTable target, final Component identifier, final boolean passive) {
 86        if (User.getCurrentUser().__getCname() == null || User.getCurrentUser().__getCname().length() == 0) {
 87            throw new IllegalStateException("The username is not set.");
 88        }
 89        if (target.getName() == null || target.getName().length() == 0) {
 90            throw new IllegalStateException("The table name is not set.");
 91        }
 92        if (identifier == null  ) {
 93            throw new IllegalStateException("The identifier is not set: " + identifier);
 94        }
 95        if (identifier.getName() == null || identifier.getName().length() == 0) {
 96            throw new IllegalStateException("The identifier name is not set: " + identifier);
 97        }
 98
 99        saveFile = User.getCurrentUser().__getCname() + "_" + target.getName() + "_" + identifier.getName() + ".xml";
100        storage = mpv5.YabsApplication.getApplication().getContext().getSessionStorage();
101        mListener = new MouseAdapter() {
102
103            @Override
104            public void mouseReleased(MouseEvent evt) {
105                persist();
106            }
107        };
108        wListener = new PropertyChangeListener() {
109
110            public void propertyChange(PropertyChangeEvent evt) {
111//                if (evt.getPropertyName().equals("model")) {
112//                    // Log.Debug(this, evt);
113//                    setObserveColumnState(true);
114//                }
115//
116////                Log.Debug(this, evt.getPropertyName());
117//
118//                if (!passive && isObserveColumnState() && evt.getPropertyName().equals("preferredWidth")) {
119//                    // Log.Debug(this, evt);
120//                    persist();
121//                }
122            }
123        };
124        cListener = new TableColumnModelListener() {
125
126            public void columnAdded(TableColumnModelEvent e) {
127            }
128
129            public void columnRemoved(TableColumnModelEvent e) {
130            }
131
132            public void columnMoved(TableColumnModelEvent e) {
133            }
134
135            public void columnMarginChanged(ChangeEvent e) {
136            }
137
138            public void columnSelectionChanged(ListSelectionEvent e) {
139            }
140        };
141        this.target = target;
142        this.identifier = identifier;
143
144        //        target.setColumnModel(new MPColumnModel());
145        //
146        //        try {
147        //            restore();
148        //        } catch (IOException ex) {
149        //            Log.Debug(ex);
150        //            persist();
151        //        }
152        //
153        set();
154    }
155
156    /**
157     * Set a listener
158     */
159    protected boolean set() {
160//        try {
161//            Log.Debug(this, "Setting restore listeners for table: " + target.getName());
162//            res = restore();
163//        } catch (Exception ex) {
164//            Log.Debug(this, ex);
165//            res = false;
166////            User.getCurrentUser().getLayoutProperties().remove(target.getName() + "@" + identifier.getName());
167////            target.createDefaultColumnsFromModel();
168////            persist();
169//        }
170        target.getColumnModel().addColumnModelListener(cListener);
171        target.getTableHeader().addMouseListener(mListener);
172        target.addPropertyChangeListener(wListener);
173        Enumeration<TableColumn> cols = target.getColumnModel().getColumns();
174        while (cols.hasMoreElements()) {
175            cols.nextElement().addPropertyChangeListener(wListener);
176        }
177        return true;
178    }
179
180    protected boolean restore() {
181        if (hasData()) {
182            Log.Debug(this, "Trying to restore table: " + target.getName());
183            try {
184                storage.restoreSingle(target, saveFile);
185            } catch (IOException ex) {
186                Log.Debug(ex);
187                return false;
188            } 
189            return true;
190        } else {
191            Log.Debug(this, "Cannot restore table: " + target.getName());
192            return false;
193        }
194    }
195
196    private void persist() {
197        try {
198            storage.saveSingle(target, saveFile);
199            Log.Debug(this, "Persisting table state: " + target);
200        } catch (IOException ex) {
201            Log.Debug(ex);
202        }
203    }
204
205    protected boolean hasData() {
206        File x = new File(FileDirectoryHandler.getTempDir() + saveFile);
207        return x.exists() && x.canWrite() && x.canRead();
208    }
209}