View Javadoc

1   // $Id: TreeSelectionValueModelAdapter.java 165 2009-05-28 21:46:38Z agony $
2   /**
3    * 
4    */
5   package de.mindcrimeilab.xsanalyzer.model;
6   
7   import javax.swing.event.TreeSelectionEvent;
8   import javax.swing.event.TreeSelectionListener;
9   import javax.swing.tree.TreePath;
10  import javax.swing.tree.TreeSelectionModel;
11  
12  import org.springframework.binding.value.support.AbstractValueModel;
13  
14  /**
15   * @author Michael Engelhardt<me@mindcrime-ilab.de>
16   * @author $Author: agony $
17   * @version $Revision: 165 $
18   * 
19   */
20  public class TreeSelectionValueModelAdapter extends AbstractValueModel implements TreeSelectionListener {
21  
22      private final TreeSelectionModel model;
23  
24      private TreePath[] currentSelection = new TreePath[0];
25  
26      private boolean skipSelectionModelUpdate = false;
27  
28      public TreeSelectionValueModelAdapter(TreeSelectionModel model) {
29          this.model = model;
30          this.model.addTreeSelectionListener(this);
31      }
32  
33      /*
34       * (non-Javadoc)
35       * 
36       * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
37       */
38      @Override
39      public void valueChanged(TreeSelectionEvent evt) {
40          skipSelectionModelUpdate = true;
41          setValue(getSelectedRows());
42          skipSelectionModelUpdate = false;
43      }
44  
45      /*
46       * (non-Javadoc)
47       * 
48       * @see org.springframework.binding.value.ValueModel#getValue()
49       */
50      @Override
51      public Object getValue() {
52          return currentSelection;
53      }
54  
55      /*
56       * (non-Javadoc)
57       * 
58       * @see org.springframework.binding.value.ValueModel#setValue(java.lang.Object)
59       */
60      @Override
61      public void setValue(Object newValue) {
62          logger.debug("New Value " + newValue);
63          TreePath[] newSelection = (TreePath[]) newValue;
64          if (hasChanged(currentSelection, newSelection)) {
65  
66              TreePath[] oldValue = currentSelection;
67              currentSelection = newSelection;
68              fireValueChange(oldValue, currentSelection);
69  
70              if (!skipSelectionModelUpdate) {
71                  model.removeTreeSelectionListener(this);
72                  model.clearSelection();
73  
74                  model.addSelectionPaths(newSelection);
75  
76                  // Reinstall listener
77                  model.addTreeSelectionListener(this);
78              }
79          }
80      }
81  
82      /**
83       * See if two arrays are different.
84       */
85      private boolean hasChanged(TreePath[] oldValue, TreePath[] newValue) {
86          if (null == oldValue && null == newValue) { return false; }
87          if ((null == oldValue && null != newValue) || (null != oldValue && null == newValue)) { return false; }
88          if (oldValue.length == newValue.length) {
89              for (int i = 0; i < newValue.length; i++) {
90                  if (oldValue[i] != newValue[i]) { return true; }
91              }
92              return false;
93          }
94          return true;
95      }
96  
97      private Object getSelectedRows() {
98          return model.getSelectionPaths();
99      }
100 }