001 package org.shiftone.jrat.desktop.util; 002 003 import org.jdesktop.swingx.JXTable; 004 import org.jdesktop.swingx.table.TableColumnExt; 005 import org.shiftone.jrat.util.log.Logger; 006 007 import java.beans.PropertyChangeEvent; 008 import java.beans.PropertyChangeListener; 009 import java.util.List; 010 import java.util.prefs.Preferences; 011 012 /** 013 * @author jeff@shiftone.org (Jeff Drost) 014 */ 015 public class JXTableWatcher { 016 017 private static final Logger LOG = Logger.getLogger(JXTableWatcher.class); 018 019 public static void initialize(JXTable table, Preferences preferences, List tableColumns) { 020 021 List columns = table.getColumns(true); 022 023 for (int i = 0; i < columns.size(); i++) { 024 TableColumnExt columnExt = (TableColumnExt) columns.get(i); 025 String key = "visible." + i; 026 String visible = preferences.get(key, null); 027 columnExt.addPropertyChangeListener(new VisibleListener(preferences, key)); // watch for changes 028 029 if (visible != null) { 030 031 columnExt.setVisible(Boolean.parseBoolean(visible)); // set saved visible 032 033 } else if (tableColumns.size() > i) { 034 035 Table.Column tableColumn = (Table.Column) tableColumns.get(i); 036 columnExt.setVisible(tableColumn.isDefaultVisible()); 037 038 } 039 040 } 041 } 042 043 044 /** 045 * when visibility changes.. record that to the map 046 */ 047 private static class VisibleListener implements PropertyChangeListener { 048 private final Preferences preferences; 049 private final String key; 050 051 public VisibleListener(Preferences preferences, String key) { 052 this.preferences = preferences; 053 this.key = key; 054 } 055 056 public void propertyChange(PropertyChangeEvent evt) { 057 if ("visible".equals(evt.getPropertyName())) { 058 //LOG.info("propertyChange " + key + " " + evt.getNewValue()); 059 preferences.put(key, evt.getNewValue().toString()); 060 } 061 } 062 } 063 }