View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.genericformat.tree;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 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 javax.swing.JTree;
28  import javax.swing.tree.DefaultTreeModel;
29  import javax.swing.tree.TreePath;
30  import java.awt.Rectangle;
31  import java.awt.event.KeyEvent;
32  import java.awt.event.KeyListener;
33  import java.awt.event.MouseEvent;
34  import java.awt.event.MouseListener;
35  
36  /**
37   * Created on 3/31/15.
38   *
39   * @author Tony Chemit - chemit@codelutin.com
40   * @since 3.14.3
41   */
42  public class DataSelectTreeModel extends DefaultTreeModel {
43  
44      private static final long serialVersionUID = 1L;
45  
46      public static void installDataSelectionHandler(JTree tree) {
47          DataSelectTreeNodeListener handler = new DataSelectTreeNodeListener();
48          tree.addKeyListener(handler);
49          tree.addMouseListener(handler);
50      }
51  
52      public DataSelectTreeModel() {
53          super(null);
54      }
55  
56      @Override
57      public ProgramSelectTreeNode getRoot() {
58          return (ProgramSelectTreeNode) super.getRoot();
59      }
60  
61      public boolean isDataSelected() {
62  
63          boolean dataSelected = false;
64  
65          if (getRoot() != null) {
66              for (CruiseSelectTreeNode cruiseNode : getRoot()) {
67                  if (cruiseNode.isSelected() || cruiseNode.isPartialSelected()) {
68                      dataSelected = true;
69                      break;
70                  }
71              }
72          }
73          return dataSelected;
74  
75      }
76  
77      public void setSelected(DataSelectTreeNodeSupport node, boolean selected) {
78  
79          node.setSelected(selected);
80  
81          nodeChanged(node);
82  
83          if (node instanceof CruiseSelectTreeNode) {
84              nodeStructureChanged(node);
85          } else if (node instanceof OperationSelectTreeNode) {
86              nodeChanged(node.getParent());
87          }
88  
89      }
90  
91      public void select(DataSelectTreeNodeSupport node) {
92  
93          boolean selected = !node.isSelected();
94          setSelected(node, selected);
95  
96      }
97  
98      public void selectAll() {
99          for (CruiseSelectTreeNode cruiseNode : getRoot()) {
100             setSelected(cruiseNode, true);
101         }
102     }
103 
104     public void unselectAll() {
105         for (CruiseSelectTreeNode cruiseNode : getRoot()) {
106             setSelected(cruiseNode, false);
107         }
108     }
109 
110     /**
111      * Created on 3/31/15.
112      *
113      * @author Tony Chemit - chemit@codelutin.com
114      * @since 3.14.3
115      */
116     static class DataSelectTreeNodeListener implements KeyListener, MouseListener {
117 
118         @Override
119         public void keyReleased(KeyEvent e) {
120 
121             JTree tree = (JTree) e.getSource();
122             if (!tree.isSelectionEmpty()) {
123 
124                 boolean doEdit = e.getModifiers() == 0
125                                  && (e.getKeyCode() == KeyEvent.VK_ENTER ||
126                                      e.getKeyCode() == KeyEvent.VK_SPACE);
127 
128                 if (doEdit) {
129 
130                     TreePath selectionPath = tree.getSelectionPath();
131                     doSelectNode(tree, selectionPath);
132 
133                 }
134 
135             }
136         }
137 
138         @Override
139         public void mouseReleased(MouseEvent e) {
140 
141             JTree tree = (JTree) e.getSource();
142             if (!tree.isSelectionEmpty()) {
143 
144                 TreePath selectionPath = tree.getSelectionPath();
145                 Rectangle bounds = tree.getPathBounds(selectionPath);
146 
147                 int x = e.getX();
148                 boolean doEdit = (bounds != null && x >= (bounds.x) && (x - 12) <= (bounds.x));
149 
150                 if (doEdit) {
151 
152                     doSelectNode(tree, selectionPath);
153 
154                 }
155 
156             }
157         }
158 
159         protected void doSelectNode(JTree tree, TreePath selectionPath) {
160             DataSelectTreeNodeSupport lastPathComponent = (DataSelectTreeNodeSupport) selectionPath.getLastPathComponent();
161             ((DataSelectTreeModel) tree.getModel()).select(lastPathComponent);
162         }
163 
164         @Override
165         public void keyTyped(KeyEvent e) {
166 
167         }
168 
169         @Override
170         public void keyPressed(KeyEvent e) {
171 
172         }
173 
174         @Override
175         public void mouseClicked(MouseEvent e) {
176 
177         }
178 
179         @Override
180         public void mousePressed(MouseEvent e) {
181 
182         }
183 
184         @Override
185         public void mouseEntered(MouseEvent e) {
186 
187         }
188 
189         @Override
190         public void mouseExited(MouseEvent e) {
191 
192         }
193     }
194 }