View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.validation;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import fr.ifremer.tutti.persistence.ProgressionModel;
27  import fr.ifremer.tutti.persistence.entities.data.Cruise;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import fr.ifremer.tutti.ui.swing.content.validation.actions.EditCruiseInValidationUIAction;
30  import fr.ifremer.tutti.ui.swing.content.validation.actions.EditFishingOperationInValidationUIAction;
31  import fr.ifremer.tutti.ui.swing.content.cruise.EditCruiseUI;
32  import fr.ifremer.tutti.ui.swing.content.operation.FishingOperationsUI;
33  import fr.ifremer.tutti.ui.swing.content.validation.tree.CruiseTreeNode;
34  import fr.ifremer.tutti.ui.swing.content.validation.tree.OperationTreeNode;
35  import fr.ifremer.tutti.ui.swing.content.validation.tree.ValidationTreeCellRenderer;
36  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
37  import fr.ifremer.tutti.ui.swing.util.RemoveablePropertyChangeListener;
38  import jaxx.runtime.SwingUtil;
39  import jaxx.runtime.validator.swing.SwingValidator;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.nuiton.decorator.Decorator;
43  import org.nuiton.jaxx.application.swing.action.ApplicationActionUIModel;
44  import org.nuiton.jaxx.application.swing.util.CloseableUI;
45  import org.nuiton.validator.NuitonValidatorResult;
46  
47  import javax.swing.JComponent;
48  import javax.swing.JPopupMenu;
49  import javax.swing.JTree;
50  import javax.swing.SwingUtilities;
51  import javax.swing.tree.DefaultMutableTreeNode;
52  import javax.swing.tree.DefaultTreeModel;
53  import javax.swing.tree.TreeModel;
54  import javax.swing.tree.TreePath;
55  import java.awt.Point;
56  import java.awt.Rectangle;
57  import java.awt.event.KeyEvent;
58  import java.awt.event.MouseEvent;
59  import java.beans.PropertyChangeEvent;
60  import java.util.LinkedHashMap;
61  import java.util.List;
62  import java.util.Objects;
63  
64  /**
65   * @author Kevin Morin - kmorin@codelutin.com
66   * @since 1.4
67   */
68  public class ValidateCruiseUIHandler extends AbstractTuttiUIHandler<ValidateCruiseUIModel, ValidateCruiseUI> implements CloseableUI {
69  
70      private final static Log log = LogFactory.getLog(ValidateCruiseUIHandler.class);
71  
72      protected EditFishingOperationInValidationUIAction editFishingOperationAction;
73  
74      protected EditCruiseInValidationUIAction editCruiseAction;
75  
76      @Override
77      public void beforeInit(ValidateCruiseUI ui) {
78  
79          super.beforeInit(ui);
80  
81          ValidateCruiseUIModel model = new ValidateCruiseUIModel();
82          model.setCruise(getDataContext().getCruise());
83          ProgressionModel progressionModel = new ProgressionModel();
84          ApplicationActionUIModel actionUIModel = getContext().getActionUI().getModel();
85          actionUIModel.setProgressionModel(progressionModel);
86  
87          Integer cruiseId = getDataContext().getCruiseId();
88          List<Integer> operationIds = getPersistenceService().getAllFishingOperationIds(cruiseId);
89          progressionModel.setTotal(2 * operationIds.size() + 2);
90  
91          NuitonValidatorResult cruiseValidatorResult = getValidateCruiseOperationsService().validateCruise(progressionModel, cruiseId);
92          model.setCruiseValidatorResult(cruiseValidatorResult);
93  
94          LinkedHashMap<FishingOperation, NuitonValidatorResult> validator = getValidateCruiseOperationsService().validateOperations(progressionModel, operationIds);
95          model.addFishingOperationValidatorResults(validator);
96  
97          actionUIModel.setProgressionModel(null);
98  
99          model.addPropertyChangeListener(ValidateCruiseUIModel.PROPERTY_SELECTED_FISHING_OPERATION,
100                                         new RemoveablePropertyChangeListener() {
101                                             @Override
102                                             public void propertyChange(PropertyChangeEvent evt) {
103                                                 FishingOperation operation = (FishingOperation) evt.getNewValue();
104                                                 editFishingOperationAction.setFishingOperation(operation);
105                                                 getContext().getActionEngine().runAction(editFishingOperationAction);
106                                                 getUI().getEditPanelLayout().setSelected("fishingOperation");
107                                             }
108                                         }
109         );
110         model.addPropertyChangeListener(ValidateCruiseUIModel.PROPERTY_SELECTED_CRUISE,
111                                         new RemoveablePropertyChangeListener() {
112                                             @Override
113                                             public void propertyChange(PropertyChangeEvent evt) {
114                                                 Cruise cruise = (Cruise) evt.getNewValue();
115 
116                                                 editCruiseAction.setCruise(cruise);
117                                                 getContext().getActionEngine().runAction(editCruiseAction);
118                                                 getUI().getEditPanelLayout().setSelected("cruise");
119                                             }
120                                         }
121         );
122         this.ui.setContextValue(model);
123     }
124 
125     @Override
126     public void afterInit(ValidateCruiseUI ui) {
127         initUI(ui);
128 
129         editFishingOperationAction = getContext().getActionFactory().createLogicAction(this, EditFishingOperationInValidationUIAction.class);
130         editCruiseAction = getContext().getActionFactory().createLogicAction(this, EditCruiseInValidationUIAction.class);
131 
132         ui.getOperationPanel().getModel().setSelectedFishingOperation(null);
133 
134 
135         ValidateCruiseUIModel uiModel = getModel();
136 
137         DefaultMutableTreeNode root = new DefaultMutableTreeNode();
138 
139         {
140             // add cruise node
141             NuitonValidatorResult result = uiModel.getCruiseValidatorResult();
142             CruiseTreeNode node = new CruiseTreeNode(uiModel.getCruise(), result);
143             root.add(node);
144         }
145         {
146             // add operations node
147             for (FishingOperation operation : uiModel.getFishingOperations()) {
148                 NuitonValidatorResult result = uiModel.getValidatorResult(operation);
149                 OperationTreeNode node = new OperationTreeNode(operation, result);
150                 root.add(node);
151             }
152         }
153         TreeModel model = new DefaultTreeModel(root);
154 
155         JTree navigation = ui.getNavigation();
156         navigation.setModel(model);
157 
158         Decorator<Cruise> cruiseDecorator = getDecorator(Cruise.class, null);
159         Decorator<FishingOperation> fishingOperationDecorator = getDecorator(FishingOperation.class, null);
160         navigation.setCellRenderer(new ValidationTreeCellRenderer(cruiseDecorator, fishingOperationDecorator));
161         SwingUtil.expandTree(navigation);
162         SwingUtil.addExpandOnClickListener(navigation);
163 
164         FishingOperationsUI operationPanel = ui.getOperationPanel();
165         operationPanel.getTopPanel().setVisible(false);
166 
167         EditCruiseUI cruisePanel = ui.getCruisePanel();
168         cruisePanel.remove(cruisePanel.getTopToolBar());
169         cruisePanel.getCloseButton().getParent().remove(cruisePanel.getCloseButton());
170         cruisePanel.getResetButton().setIcon(cruisePanel.getCloseButton().getIcon());
171 
172         getModel().computeReadyToSynch();
173     }
174 
175     @Override
176     public void onCloseUI() {
177         if (log.isDebugEnabled()) {
178             log.debug("closing: " + ui);
179         }
180         clearValidators();
181     }
182 
183     @Override
184     public SwingValidator<ValidateCruiseUIModel> getValidator() {
185         return null;
186     }
187 
188     @Override
189     protected JComponent getComponentToFocus() {
190         return null;
191     }
192 
193     public void autoSelectNode(MouseEvent e, JPopupMenu popup) {
194         JTree navigation = (JTree) e.getSource();
195 
196         boolean rightClick = SwingUtilities.isRightMouseButton(e);
197 
198         if (rightClick) {
199 
200             // get the path at this point
201             TreePath path = navigation.getPathForLocation(e.getX(), e.getY());
202 
203             // select row (could empty selection)
204             if (path == null) {
205                 navigation.clearSelection();
206 
207             } else {
208                 navigation.setSelectionPath(path);
209             }
210 
211             // on right click show popup
212             popup.show(navigation, e.getX(), e.getY());
213 
214         }
215 
216         TreePath selectionPath = navigation.getSelectionPath();
217 
218         if (selectionPath != null && selectionPath.getPathCount() > 1) {
219             Object o = selectionPath.getPathComponent(1);
220 
221             if (o instanceof OperationTreeNode) {
222                 FishingOperation operation = ((OperationTreeNode) o).getUserObject();
223 
224                 if (!Objects.equals(operation, getModel().getLastSelectedObject())) {
225 
226                     getModel().setSelectedFishingOperation(operation);
227                 }
228             }
229             if (o instanceof CruiseTreeNode) {
230                 Cruise cruise = ((CruiseTreeNode) o).getUserObject();
231                 if (!Objects.equals(cruise, getModel().getLastSelectedObject())) {
232 
233                     getModel().setSelectedCruise(cruise);
234                 }
235 
236             }
237 
238         }
239 
240     }
241 
242     public void openNodeMenu(KeyEvent e, JPopupMenu popup) {
243 
244         if (e.getKeyCode() == KeyEvent.VK_CONTEXT_MENU) {
245 
246             JTree navigation = (JTree) e.getSource();
247 
248             TreePath path = navigation.getSelectionPath();
249 
250             if (path != null) {
251                 Rectangle r = navigation.getPathBounds(path);
252 
253                 // get the point in the middle lower of the node
254                 Point p = new Point(r.x + r.width / 2, r.y + r.height);
255 
256                 popup.show(navigation, p.x, p.y);
257             }
258         }
259     }
260 
261     public void updateCurrentOperationNode(NuitonValidatorResult validationResult) {
262         JTree navigation = ui.getNavigation();
263         TreePath selectionPath = navigation.getSelectionPath();
264         Preconditions.checkArgument(selectionPath.getPathCount() > 1);
265 
266         OperationTreeNode node = (OperationTreeNode) selectionPath.getPathComponent(1);
267         node.removeAllChildren();
268         node.createChildren(validationResult);
269 
270         DefaultTreeModel treeModel = (DefaultTreeModel) navigation.getModel();
271         treeModel.reload(node);
272         navigation.setSelectionPath(selectionPath);
273     }
274 
275     public void updateCurrentCruiseNode(NuitonValidatorResult validationResult) {
276         JTree navigation = ui.getNavigation();
277         TreePath selectionPath = navigation.getSelectionPath();
278         Preconditions.checkArgument(selectionPath.getPathCount() > 1);
279 
280         CruiseTreeNode node = (CruiseTreeNode) selectionPath.getPathComponent(1);
281         node.removeAllChildren();
282         node.createChildren(validationResult);
283 
284         DefaultTreeModel treeModel = (DefaultTreeModel) navigation.getModel();
285         treeModel.reload(node);
286         navigation.setSelectionPath(selectionPath);
287     }
288 
289     @Override
290     public boolean quitUI() {
291         editFishingOperationAction.removeListener();
292         editCruiseAction.removeListener();
293         return true;
294     }
295 }