View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.zones;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
28  import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.node.StrataNode;
29  import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.node.SubStrataNode;
30  import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.node.ZoneNode;
31  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
32  import jaxx.runtime.SwingUtil;
33  import jaxx.runtime.validator.swing.SwingValidator;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import javax.swing.JComponent;
38  import javax.swing.JPopupMenu;
39  import javax.swing.JTree;
40  import javax.swing.SwingUtilities;
41  import javax.swing.event.TreeSelectionListener;
42  import javax.swing.tree.TreePath;
43  import java.awt.Point;
44  import java.awt.event.KeyEvent;
45  import java.awt.event.MouseEvent;
46  
47  /**
48   * @author Kevin Morin (Code Lutin)
49   * @since 4.5
50   */
51  public class ZoneEditorUIHandler extends AbstractTuttiUIHandler<EditProtocolUIModel, ZoneEditorUI> {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(ZoneEditorUIHandler.class);
55  
56      @Override
57      public void afterInit(ZoneEditorUI zoneEditorUI) {
58  
59          initUI(zoneEditorUI);
60  
61          // init trees
62  
63          JTree availableStratasTree = ui.getAvailableStratasTree();
64          SwingUtil.addExpandOnClickListener(availableStratasTree);
65  
66          JTree zonesTree = ui.getZonesTree();
67          SwingUtil.addExpandOnClickListener(zonesTree);
68  
69          zonesTree.addTreeSelectionListener(event -> {
70  
71              TreePath[] selectedPaths = zonesTree.getSelectionPaths();
72              boolean allStrataNode = true;
73  
74              if (selectedPaths != null) {
75  
76                  if (selectedPaths.length == 1) {
77                      boolean zoneSelected = event.getPath().getLastPathComponent() instanceof ZoneNode;
78                      getUI().getDeleteZoneMenuItem().setEnabled(zoneSelected);
79                      getUI().getRenameZoneMenuItem().setEnabled(zoneSelected);
80                  }
81  
82                  for (TreePath selectedPath : selectedPaths) {
83                      Object lastPathComponent = selectedPath.getLastPathComponent();
84                      if (!(lastPathComponent instanceof StrataNode
85                              || lastPathComponent instanceof SubStrataNode)) {
86                          allStrataNode = false;
87                          break;
88                      }
89                  }
90  
91              }
92  
93              getUI().getRemoveButton().setEnabled(selectedPaths != null && allStrataNode);
94  
95          });
96  
97          TreeSelectionListener enableAddStrataListener = evt -> {
98  
99              boolean addButtonEnabled = zonesTree.getSelectionCount() == 1
100                     && zonesTree.getSelectionPath().getLastPathComponent() instanceof ZoneNode
101                     && availableStratasTree.getSelectionCount() >= 1;
102             getUI().getAddButton().setEnabled(addButtonEnabled);
103 
104         };
105         zonesTree.addTreeSelectionListener(enableAddStrataListener);
106         availableStratasTree.addTreeSelectionListener(enableAddStrataListener);
107 
108     }
109 
110     @Override
111     public SwingValidator<EditProtocolUIModel> getValidator() {
112         return null;
113     }
114 
115     @Override
116     protected JComponent getComponentToFocus() {
117         return ui.getZonesTree();
118     }
119 
120     @Override
121     public void onCloseUI() {
122 //        getModel().getZone().forEach(zone -> ((ZoneUIModel) zone).removePropertyChangeListener(ZoneUIModel.PROPERTY_STRATA,
123 //                                                                                stratasChangeListener));
124     }
125 
126     public void onKeyPressedOnZones(KeyEvent e) {
127 
128         if (e.getKeyCode() == KeyEvent.VK_ENTER && getUI().getRemoveButton().isEnabled()) {
129             getContext().getActionEngine().runAction(getUI().getRemoveButton());
130         }
131     }
132 
133     public void onKeyPressedOnAvailableStratas(KeyEvent e) {
134 
135         if (e.getKeyCode() == KeyEvent.VK_ENTER && getUI().getAddButton().isEnabled()) {
136             getContext().getActionEngine().runAction(getUI().getAddButton());
137         }
138     }
139 
140 
141     public void onMouseClickedPressedOnZones(MouseEvent e, JPopupMenu popup) {
142 
143         if (SwingUtilities.isRightMouseButton(e)) {
144 
145             // get the coordinates of the mouse click
146             Point p = e.getPoint();
147 
148             JTree source = (JTree) e.getSource();
149 
150             // get the row index at this point
151             int rowIndex = source.getRowForLocation(p.x, p.y);
152 
153             if (log.isDebugEnabled()) {
154                 log.debug("At point [" + p + "] found Row " + rowIndex);
155             }
156 
157             source.setSelectionRow(rowIndex);
158 
159             // on right click show popup
160             popup.show(source, e.getX(), e.getY());
161 
162         } else if (e.getClickCount() == 2 && getUI().getRemoveButton().isEnabled()) {
163             getContext().getActionEngine().runAction(getUI().getRemoveButton());
164         }
165     }
166 
167     public void onMouseClickedOnAvailableStratas(MouseEvent mouseEvent) {
168 
169         if (mouseEvent.getClickCount() == 2 && getUI().getAddButton().isEnabled()) {
170             getContext().getActionEngine().runAction(getUI().getAddButton());
171         }
172     }
173 
174 }