View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.program.strategiesByLocation.locations;
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.programStrategy.ProgramDTO;
27  import fr.ifremer.dali.dto.referential.LocationDTO;
28  import fr.ifremer.dali.ui.swing.content.manage.program.locations.LocationsTableUIModel;
29  import fr.ifremer.dali.ui.swing.content.manage.program.strategiesByLocation.StrategiesLieuxUI;
30  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableModel;
31  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableUIHandler;
32  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
33  import org.jdesktop.swingx.table.TableColumnExt;
34  
35  import javax.swing.BorderFactory;
36  import javax.swing.SortOrder;
37  import java.util.List;
38  
39  import static org.nuiton.i18n.I18n.t;
40  
41  /**
42   * Controleur pour la zone des programmes.
43   */
44  public class LieuxProgrammeTableUIHandler extends AbstractDaliTableUIHandler<LieuxProgrammeTableRowModel, LieuxProgrammeTableUIModel, LieuxProgrammeTableUI> {
45  
46  	/** {@inheritDoc} */
47  	@Override
48  	public AbstractDaliTableModel<LieuxProgrammeTableRowModel> getTableModel() {
49  		return (LieuxProgrammeTableModel) getTable().getModel();
50  	}
51  
52  	/** {@inheritDoc} */
53  	@Override
54  	public SwingTable getTable() {
55  		return getUI().getTableau();
56  	}
57  
58      /** {@inheritDoc} */
59      @Override
60      public void beforeInit(final LieuxProgrammeTableUI ui) {
61          super.beforeInit(ui);
62          
63          // create model and register to the JAXX context
64          final LieuxProgrammeTableUIModel model = new LieuxProgrammeTableUIModel();
65          ui.setContextValue(model);
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public void afterInit(final LieuxProgrammeTableUI ui) {
71      	
72      	// Initialiser l UI
73          initUI(ui);
74  
75          // Initialiser le tableau
76          initialiserTableau();
77          
78          // Initialiser listeners
79          initialiserListeners();
80      }
81  
82      /**
83       * Chargement des lieux.
84       *
85       * @param programme Le programme selectionne
86       */
87      public void load(final ProgramDTO programme) {
88      	
89      	// Sauvegarde du programme
90      	getModel().setProgramme(programme);
91      	
92      	// Les lieux
93      	final List<LocationDTO> lieux = getContext().getReferentialService().getLocations(null, programme.getCode());
94      	
95      	// Chargement des lieux dans le model
96      	getModel().setBeans(lieux);
97          
98          // Initialiser le titre du panel
99          getUI().setBorder(BorderFactory.createTitledBorder(
100         		t("dali.program.strategies.location.title", programme.getCode())));
101         
102         // Selectionner la premiere ligne du tableau
103         selectCell(0, null);
104         
105         // Load les autres tableaux avec le premier lieu
106         if (getContext().getSelectedLocationId() != null) {
107             selectRowById(getContext().getSelectedLocationId());
108             setFocusOnCell(getModel().getSingleSelectedRow());
109         }
110     }
111 
112     /**
113      * Initialisation des listeners.
114      */
115     private void initialiserListeners() {
116 
117 		// Listener sur le tableau
118 		getModel().addPropertyChangeListener(LocationsTableUIModel.PROPERTY_SINGLE_ROW_SELECTED, evt -> loadAutresTableaux(getModel().getSingleSelectedRow()));
119     }
120     
121     /**
122      * Load les autres tableaux.
123      * @param lieu Le lieu selectionne
124      */
125     private void loadAutresTableaux(final LocationDTO lieu) {
126 		if (lieu != null) {
127 			
128 			// Chargement du tableau des strategies pour le lieu selectionne
129 			getUI().getParentContainer(StrategiesLieuxUI.class).getStrategiesLieuTableUI().getHandler().load(lieu);
130 			
131 			// Chargement du tableau des strategies pour un programme et un lieu
132 			getUI().getParentContainer(StrategiesLieuxUI.class).getStrategiesProgrammeTableUI().getHandler().load(
133 					getModel().getProgramme(), lieu);
134 		}
135     }
136 
137     /**
138      * Initialisation de le tableau.
139      */
140     private void initialiserTableau() {
141 
142         // La table des prelevements
143         final SwingTable table = getTable();
144 
145         // Colonne label
146         final TableColumnExt colonneLabel = addColumn(
147                 LieuxProgrammeTableModel.CODE);
148         colonneLabel.setSortable(true);
149         colonneLabel.setMinWidth(80);
150         colonneLabel.setCellRenderer(newNumberCellRenderer(0));
151 
152         // Colonne name
153         final TableColumnExt colonneName = addColumn(
154                 LieuxProgrammeTableModel.LABEL);
155         colonneName.setSortable(true);
156         colonneName.setMinWidth(80);
157 
158         // Colonne comment
159         final TableColumnExt colonneComment = addColumn(
160                 LieuxProgrammeTableModel.NAME);
161         colonneComment.setSortable(true);
162         colonneComment.setMinWidth(100);
163         
164         // Modele de la table
165         final LieuxProgrammeTableModel tableModel = new LieuxProgrammeTableModel(getTable().getColumnModel());
166         table.setModel(tableModel);
167 
168         // Initialisation de la table
169         initTable(table, true);
170 
171         // Tri par defaut
172         table.setSortOrder(LieuxProgrammeTableModel.CODE, SortOrder.ASCENDING);
173 
174 		// Colonnes non editable
175 		tableModel.setNoneEditableCols();
176 
177 		// Les colonnes obligatoire sont toujours presentes
178 		colonneLabel.setHideable(false);
179 		colonneLabel.setEditable(false);
180 		colonneName.setHideable(false);
181 		colonneName.setEditable(false);
182 		colonneComment.setHideable(false);
183 		colonneComment.setEditable(false);
184     }
185 }