1 package fr.ifremer.quadrige3.ui.swing.table.state;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import fr.ifremer.quadrige3.core.dao.technical.Assert;
27 import fr.ifremer.quadrige3.ui.swing.table.ColumnIdentifier;
28 import fr.ifremer.quadrige3.ui.swing.table.FixedSwingTable;
29 import jaxx.runtime.swing.session.State;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.jdesktop.swingx.table.TableColumnExt;
33
34 import javax.swing.table.AbstractTableModel;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40
41
42
43
44
45
46 public class FixedSwingTableSessionState implements ExtendedState {
47
48 private static final Log LOG = LogFactory.getLog(FixedSwingTableSessionState.class);
49
50
51 private Map<String, FixedSwingTableState> sessionStates;
52
53
54
55
56 public FixedSwingTableSessionState() {
57 sessionStates = new HashMap<>();
58 }
59
60
61
62
63
64
65
66 protected FixedSwingTable checkComponent(Object object) {
67 Assert.notNull(object);
68 Assert.isInstanceOf(FixedSwingTable.class, object);
69 Assert.isInstanceOf(AbstractTableModel.class, ((FixedSwingTable) object).getModel());
70 return (FixedSwingTable) object;
71 }
72
73
74
75
76 @Override
77 public State getState(Object o) {
78
79 return getState(o, null, null);
80 }
81
82
83
84
85 @Override
86 @SuppressWarnings("unchecked")
87 public State getState(Object o, State previousState, String stateContextToSave) {
88
89 FixedSwingTable table = checkComponent(o);
90
91
92 String stateContext = stateContextToSave != null ? stateContextToSave : table.getTableModel().getStateContext();
93
94
95 FixedSwingTableSessionState tableSessionState;
96 if (previousState instanceof FixedSwingTableSessionState) {
97 tableSessionState = (FixedSwingTableSessionState) previousState;
98 } else {
99 tableSessionState = new FixedSwingTableSessionState();
100 }
101 try {
102
103 FixedSwingTableState tableState = tableSessionState.getSessionStates().computeIfAbsent(stateContext, k -> new FixedSwingTableState());
104
105
106 List<ColumnIdentifier<?>> identifiers = new ArrayList<ColumnIdentifier<?>>(table.getTableModel().getIdentifiers());
107
108
109 for (ColumnIdentifier<?> identifier : identifiers) {
110 TableColumnExt column = table.getColumnExt(identifier);
111 if (column == null) {
112
113 continue;
114 }
115
116
117 tableState.getWidth().put(identifier.getPropertyName(), column.getPreferredWidth());
118
119 }
120
121 } catch (Exception e) {
122 LOG.error("Something goes wrong on getState", e);
123 }
124 return tableSessionState;
125 }
126
127
128
129
130 @Override
131 @SuppressWarnings("unchecked")
132 public void setState(Object o, State state) {
133 if (state == null) {
134 return;
135 }
136 if (!(state instanceof FixedSwingTableSessionState)) {
137 throw new IllegalArgumentException("invalid state");
138 }
139 FixedSwingTable table = checkComponent(o);
140
141
142 FixedSwingTableSessionState tableSessionState = (FixedSwingTableSessionState) state;
143 FixedSwingTableState tableState = tableSessionState.getSessionStates().get(table.getTableModel().getStateContext());
144 if (tableState == null) {
145
146 return;
147 }
148
149 try {
150
151 List<ColumnIdentifier<?>> identifiers = new ArrayList<ColumnIdentifier<?>>(table.getTableModel().getIdentifiers());
152
153 for (ColumnIdentifier<?> identifier : identifiers) {
154 TableColumnExt column = table.getColumnExt(identifier);
155 if (column == null) {
156
157 continue;
158 }
159
160
161 if (column.getResizable()) {
162 Integer width = tableState.getWidth().get(identifier.getPropertyName());
163 if (width != null) {
164 column.setPreferredWidth(width);
165 }
166 }
167
168 }
169
170 } catch (Exception e) {
171 LOG.error("Something goes wrong on setState", e);
172 }
173 }
174
175
176
177 public Map<String, FixedSwingTableState> getSessionStates() {
178 return sessionStates;
179 }
180
181 @SuppressWarnings("unused")
182 public void setSessionStates(Map<String, FixedSwingTableState> sessionStates) {
183 this.sessionStates = sessionStates;
184 }
185
186 }