View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.context.contextslist;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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.dali.dto.configuration.context.ContextDTO;
27  import fr.ifremer.dali.ui.swing.content.manage.context.ManageContextsUI;
28  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableModel;
29  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableUIHandler;
30  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
31  import org.jdesktop.swingx.table.TableColumnExt;
32  
33  import java.util.List;
34  
35  /**
36   * <p>ManageContextsListTableUIHandler class.</p>
37   *
38   */
39  public class ManageContextsListTableUIHandler extends
40          AbstractDaliTableUIHandler<ManageContextsListTableUIRowModel, ManageContextsListTableUIModel, ManageContextsListTableUI> {
41  
42      /**
43       * {@inheritDoc}
44       *
45       * Logger.
46       */
47  //    private static final Log LOG = LogFactory.getLog(ManageContextsListTableUIHandler.class);
48      @Override
49      public void beforeInit(final ManageContextsListTableUI ui) {
50          super.beforeInit(ui);
51  
52          // create model and register to the JAXX context
53          final ManageContextsListTableUIModel model = new ManageContextsListTableUIModel();
54          ui.setContextValue(model);
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public void afterInit(final ManageContextsListTableUI ui) {
60  
61          // Initialize the screen
62          initUI(ui);
63  
64          // Initialize the table
65          initTable();
66  
67          // Initialize the listeners
68          initListeners();
69  
70          getUI().getDuplicateButton().setEnabled(false);
71          getUI().getDeleteButton().setEnabled(false);
72          getUI().getActivateButton().setEnabled(false);
73          getUI().getExportButton().setEnabled(false);
74          
75          // TODO remove button ? hide Activate button
76          getUI().getActivateButton().setVisible(false);
77      }
78  
79      /**
80       * Initialisation du tableau.
81       */
82      private void initTable() {
83  
84          // the table
85          final SwingTable table = getTable();
86  
87          // label
88          TableColumnExt libelleCol = addColumn(ManageContextsListTableUITableModel.LIBELLE);
89          libelleCol.setSortable(true);
90          libelleCol.setEditable(true);
91  
92          // Description
93          TableColumnExt descriptionCol = addColumn(ManageContextsListTableUITableModel.DESCRIPTION);
94          descriptionCol.setSortable(true);
95          descriptionCol.setEditable(true);
96  
97          ManageContextsListTableUITableModel tableModel = new ManageContextsListTableUITableModel(getTable().getColumnModel());
98          table.setModel(tableModel);
99  
100         // Initialize du tableau
101         initTable(table);
102 
103         table.setVisibleRowCount(5);
104     }
105 
106     private void initListeners() {
107 
108         // Listener sur le tableau
109         getModel().addPropertyChangeListener(ManageContextsListTableUIModel.PROPERTY_SINGLE_ROW_SELECTED, evt -> {
110             if (getModel().getSingleSelectedRow() != null) {
111                 final ContextDTO localContext = getModel().getSingleSelectedRow();
112                 getUI().getParentContainer(ManageContextsUI.class).getHandler().loadLocalContext(localContext);
113             }
114         });
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public AbstractDaliTableModel<ManageContextsListTableUIRowModel> getTableModel() {
120         return (ManageContextsListTableUITableModel) getTable().getModel();
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public SwingTable getTable() {
126         return ui.getManageContextsListTable();
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     protected void onRowsAdded(List<ManageContextsListTableUIRowModel> addedRows) {
132         super.onRowsAdded(addedRows);
133 
134         // should be only one row
135         if (addedRows.size() == 1) {
136             ManageContextsListTableUIRowModel row = addedRows.get(0);
137 
138             getModel().setModify(true);
139 
140             // add focus on Code column
141             setFocusOnCell(row);
142         }
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     protected void onRowModified(int rowIndex, ManageContextsListTableUIRowModel row, String propertyName, Integer propertyIndex, Object oldValue, Object newValue) {
148         super.onRowModified(rowIndex, row, propertyName, propertyIndex, oldValue, newValue);
149         row.setDirty(true);
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     protected String[] getRowPropertiesToIgnore() {
155         return new String[] {ContextDTO.PROPERTY_DIRTY};
156     }
157 }