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