View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.table.state;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * State used for a FixedSwingTable with a AbstractTableModel
42   * Only width property is handled
43   * <p/>
44   * Created by Ludovic on 12/03/2020.
45   */
46  public class FixedSwingTableSessionState implements ExtendedState {
47  
48      private static final Log LOG = LogFactory.getLog(FixedSwingTableSessionState.class);
49  
50      // session states map by context
51      private Map<String, FixedSwingTableState> sessionStates;
52  
53      /**
54       * <p>Constructor for SwingTableSessionState.</p>
55       */
56      public FixedSwingTableSessionState() {
57          sessionStates = new HashMap<>();
58      }
59  
60      /**
61       * <p>checkComponent.</p>
62       *
63       * @param object a {@link Object} object.
64       * @return a {@link FixedSwingTable} object.
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       * {@inheritDoc}
75       */
76      @Override
77      public State getState(Object o) {
78  
79          return getState(o, null, null);
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      @SuppressWarnings("unchecked")
87      public State getState(Object o, State previousState, String stateContextToSave) {
88  
89          FixedSwingTable table = checkComponent(o);
90  
91          // Retrieve table model and its state context
92          String stateContext = stateContextToSave != null ? stateContextToSave : table.getTableModel().getStateContext();
93  
94          // Reuse previous state or create new one
95          FixedSwingTableSessionState tableSessionState;
96          if (previousState instanceof FixedSwingTableSessionState) {
97              tableSessionState = (FixedSwingTableSessionState) previousState;
98          } else {
99              tableSessionState = new FixedSwingTableSessionState();
100         }
101         try {
102             // Get or create the table state for the context
103             FixedSwingTableState tableState = tableSessionState.getSessionStates().computeIfAbsent(stateContext, k -> new FixedSwingTableState());
104 
105             // get tableModel.getIdentifiers() in a new list to prevent ConcurrentModificationException
106             List<ColumnIdentifier<?>> identifiers = new ArrayList<ColumnIdentifier<?>>(table.getTableModel().getIdentifiers());
107 
108             // Iterate over identifiers
109             for (ColumnIdentifier<?> identifier : identifiers) {
110                 TableColumnExt column = table.getColumnExt(identifier);
111                 if (column == null) {
112                     // ignore
113                     continue;
114                 }
115 
116                 //  width property
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      * {@inheritDoc}
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         // Retrieve table model
142         FixedSwingTableSessionState tableSessionState = (FixedSwingTableSessionState) state;
143         FixedSwingTableState tableState = tableSessionState.getSessionStates().get(table.getTableModel().getStateContext());
144         if (tableState == null) {
145             // Ignore if no session state for this context
146             return;
147         }
148 
149         try {
150             // get tableModel.getIdentifiers() in a new list to prevent ConcurrentModificationException
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                     // ignore
157                     continue;
158                 }
159 
160                 // Get the width property
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     // <Dont' delete> these getters ans setters used by SwingSession
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 }