2 * Created on 08.02.2007
5 package com.nexuiz.demorecorder.ui.swinggui.utils;
7 import java.awt.Component;
8 import java.beans.DefaultPersistenceDelegate;
9 import java.beans.XMLEncoder;
10 import java.io.Serializable;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Comparator;
14 import java.util.List;
16 import javax.swing.SortOrder;
17 import javax.swing.RowSorter.SortKey;
18 import javax.swing.table.TableColumn;
19 import javax.swing.table.TableColumnModel;
21 import org.jdesktop.swingx.JXTable;
22 import org.jdesktop.swingx.JXTaskPane;
23 import org.jdesktop.swingx.sort.SortUtils;
24 import org.jdesktop.swingx.table.TableColumnExt;
27 * Container class for SwingX specific SessionStorage Properties. Is Factory for
28 * custom PersistanceDelegates
30 public class XProperties {
34 * Registers all custom PersistenceDelegates needed by contained Property
38 * PersistenceDelegates are effectively static properties shared by all
39 * encoders. In other words: Register once on an arbitrary encoder makes
40 * them available for all. Example usage:
44 * new XProperties.registerPersistenceDelegates();
48 * PENDING JW: cleanup for 1.6 sorting/filtering incomplete. Missing storage
49 * - multiple sort keys
51 * PENDING JW: except for comparators: didn't before and is not state that's
52 * configurable by users ... so probably won't, not sure, need to revisit -
53 * comparator (?) - filters (?) - renderers/stringvalues (?) - enhanced
54 * sort-related table state (?)
56 public void registerPersistenceDelegates() {
57 XMLEncoder encoder = new XMLEncoder(System.out);
58 encoder.setPersistenceDelegate(SortKeyState.class, new DefaultPersistenceDelegate(
59 new String[] { "ascending", "modelIndex" }));
60 encoder.setPersistenceDelegate(ColumnState.class, new DefaultPersistenceDelegate(
61 new String[] { "width", "preferredWidth", "modelIndex", "visible", "viewIndex" }));
62 encoder.setPersistenceDelegate(XTableState.class, new DefaultPersistenceDelegate(
63 new String[] { "columnStates", "sortKeyState", "horizontalScrollEnabled" }));
67 * Session storage support for JXTaskPane.
69 public static class XTaskPaneProperty implements Serializable {
71 private static final long serialVersionUID = -4069436038178318216L;
73 public Object getSessionState(Component c) {
75 return new XTaskPaneState(((JXTaskPane) c).isCollapsed());
78 public void setSessionState(Component c, Object state) {
80 if ((state != null) && !(state instanceof XTaskPaneState)) {
81 throw new IllegalArgumentException("invalid state");
83 ((JXTaskPane) c).setCollapsed(((XTaskPaneState) state).isCollapsed());
86 private void checkComponent(Component component) {
87 if (component == null) {
88 throw new IllegalArgumentException("null component");
90 if (!(component instanceof JXTaskPane)) {
91 throw new IllegalArgumentException("invalid component");
97 public static class XTaskPaneState implements Serializable {
98 private static final long serialVersionUID = 3363688961112031969L;
99 private boolean collapsed;
101 public XTaskPaneState() {
108 public XTaskPaneState(boolean collapsed) {
109 this.setCollapsed(collapsed);
114 * the collapsed to set
116 public void setCollapsed(boolean collapsed) {
117 this.collapsed = collapsed;
121 * @return the collapsed
123 public boolean isCollapsed() {
130 * Session storage support for JXTable.
132 public static class XTableProperty implements Serializable {
134 private static final long serialVersionUID = -5064142292091374301L;
136 public Object getSessionState(Component c) {
138 JXTable table = (JXTable) c;
139 List<ColumnState> columnStates = new ArrayList<ColumnState>();
140 List<TableColumn> columns = table.getColumns(true);
141 List<TableColumn> visibleColumns = table.getColumns();
142 for (TableColumn column : columns) {
143 columnStates.add(new ColumnState((TableColumnExt) column, visibleColumns
146 XTableState tableState = new XTableState(columnStates
147 .toArray(new ColumnState[columnStates.size()]));
148 tableState.setHorizontalScrollEnabled(table.isHorizontalScrollEnabled());
149 List<? extends SortKey> sortKeys = null;
150 if (table.getRowSorter() != null) {
151 sortKeys = table.getRowSorter().getSortKeys();
153 // PENDING: store all!
154 if ((sortKeys != null) && (sortKeys.size() > 0)) {
155 tableState.setSortKey(sortKeys.get(0));
160 public void setSessionState(Component c, Object state) {
162 JXTable table = (JXTable) c;
163 XTableState tableState = ((XTableState) state);
164 ColumnState[] columnState = tableState.getColumnStates();
165 List<TableColumn> columns = table.getColumns(true);
166 if (canRestore(columnState, columns)) {
167 for (int i = 0; i < columnState.length; i++) {
168 columnState[i].configureColumn((TableColumnExt) columns.get(i));
170 restoreVisibleSequence(columnState, table.getColumnModel());
172 table.setHorizontalScrollEnabled(tableState.getHorizontalScrollEnabled());
173 if (tableState.getSortKey() != null) {
175 .setSortKeys(Collections.singletonList(tableState.getSortKey()));
179 private void restoreVisibleSequence(ColumnState[] columnStates, TableColumnModel model) {
180 List<ColumnState> visibleStates = getSortedVisibleColumnStates(columnStates);
181 for (int i = 0; i < visibleStates.size(); i++) {
182 TableColumn column = model.getColumn(i);
183 int modelIndex = visibleStates.get(i).getModelIndex();
184 if (modelIndex != column.getModelIndex()) {
185 int currentIndex = -1;
186 for (int j = i + 1; j < model.getColumnCount(); j++) {
187 TableColumn current = model.getColumn(j);
188 if (current.getModelIndex() == modelIndex) {
193 model.moveColumn(currentIndex, i);
199 private List<ColumnState> getSortedVisibleColumnStates(ColumnState[] columnStates) {
200 List<ColumnState> visibleStates = new ArrayList<ColumnState>();
201 for (ColumnState columnState : columnStates) {
202 if (columnState.getVisible()) {
203 visibleStates.add(columnState);
206 Collections.sort(visibleStates, new VisibleColumnIndexComparator());
207 return visibleStates;
211 * Returns a boolean to indicate if it's reasonably safe to restore the
212 * properties of columns in the list from the columnStates. Here:
213 * returns true if the length of both are the same and the modelIndex of
214 * the items at the same position are the same, otherwise returns false.
220 private boolean canRestore(ColumnState[] columnState, List<TableColumn> columns) {
221 if ((columnState == null) || (columnState.length != columns.size()))
223 for (int i = 0; i < columnState.length; i++) {
224 if (columnState[i].getModelIndex() != columns.get(i).getModelIndex()) {
231 private void checkComponent(Component component) {
232 if (component == null) {
233 throw new IllegalArgumentException("null component");
235 if (!(component instanceof JXTable)) {
236 throw new IllegalArgumentException("invalid component - expected JXTable");
242 public static class XTableState implements Serializable {
243 private static final long serialVersionUID = -3566913244872587438L;
244 ColumnState[] columnStates = new ColumnState[0];
245 boolean horizontalScrollEnabled;
246 SortKeyState sortKeyState;
248 public XTableState(ColumnState[] columnStates, SortKeyState sortKeyState,
249 boolean horizontalScrollEnabled) {
250 this.columnStates = copyColumnStates(columnStates);
251 this.sortKeyState = sortKeyState;
252 setHorizontalScrollEnabled(horizontalScrollEnabled);
256 public void setSortKey(SortKey sortKey) {
257 this.sortKeyState = new SortKeyState(sortKey);
261 private SortKey getSortKey() {
262 if (sortKeyState != null) {
263 return sortKeyState.getSortKey();
268 public XTableState(ColumnState[] columnStates) {
269 this.columnStates = copyColumnStates(columnStates);
272 public ColumnState[] getColumnStates() {
273 return copyColumnStates(this.columnStates);
276 public boolean getHorizontalScrollEnabled() {
277 return horizontalScrollEnabled;
280 public void setHorizontalScrollEnabled(boolean horizontalScrollEnabled) {
281 this.horizontalScrollEnabled = horizontalScrollEnabled;
284 private ColumnState[] copyColumnStates(ColumnState[] states) {
285 if (states == null) {
286 throw new IllegalArgumentException("invalid columnWidths");
288 ColumnState[] copy = new ColumnState[states.length];
289 System.arraycopy(states, 0, copy, 0, states.length);
293 public SortKeyState getSortKeyState() {
299 * Quick hack to make SortKey encodable. How to write a PersistenceDelegate
300 * for a SortKey? Boils down to how to write a delegate for the
301 * uninstantiable class (SwingX) SortOrder which does enum-mimickry (defines
302 * privately intantiated constants)
305 public static class SortKeyState implements Serializable {
306 private static final long serialVersionUID = 5819342622261460894L;
313 * Constructor used by the custom PersistenceDelegate.
319 public SortKeyState(boolean ascending, int modelIndex) {
320 this.ascending = ascending;
321 this.modelIndex = modelIndex;
325 * Constructor used by property.
329 public SortKeyState(SortKey sortKey) {
330 this(SortUtils.isAscending(sortKey.getSortOrder()), sortKey.getColumn());
333 protected SortKey getSortKey() {
334 SortOrder sortOrder = getAscending() ? SortOrder.ASCENDING : SortOrder.DESCENDING;
335 return new SortKey(getModelIndex(), sortOrder);
338 public boolean getAscending() {
342 public int getModelIndex() {
347 public static class ColumnState implements Serializable {
348 private static final long serialVersionUID = 6037947151025126049L;
350 private int preferredWidth;
351 private int modelIndex;
352 private boolean visible;
353 private int viewIndex;
356 * Constructor used by the custom PersistenceDelegate.
359 * @param preferredWidth
364 public ColumnState(int width, int preferredWidth, int modelColumn, boolean visible,
367 this.preferredWidth = preferredWidth;
368 this.modelIndex = modelColumn;
369 this.visible = visible;
370 this.viewIndex = viewIndex;
374 * Constructor used by the Property.
379 public ColumnState(TableColumnExt columnExt, int viewIndex) {
380 this(columnExt.getWidth(), columnExt.getPreferredWidth(), columnExt.getModelIndex(),
381 columnExt.isVisible(), viewIndex);
385 * Restores column properties if the model index is the same as the
386 * column's model index. Does nothing otherwise.
389 * Here the properties are: width, preferredWidth, visible.
392 * the column to configure
394 public void configureColumn(TableColumnExt columnExt) {
395 if (modelIndex != columnExt.getModelIndex())
397 columnExt.setPreferredWidth(preferredWidth);
398 columnExt.setWidth(width);
399 columnExt.setVisible(visible);
402 public int getModelIndex() {
406 public int getViewIndex() {
410 public boolean getVisible() {
414 public int getWidth() {
418 public int getPreferredWidth() {
419 return preferredWidth;
424 public static class VisibleColumnIndexComparator implements Comparator<Object> {
426 public int compare(Object o1, Object o2) {
427 return ((ColumnState) o1).getViewIndex() - ((ColumnState) o2).getViewIndex();