View Javadoc

1   // $Id: AbstractTreeSelectionGuard.java 165 2009-05-28 21:46:38Z agony $
2   /**
3    * 
4    */
5   package de.mindcrimeilab.xsanalyzer.model;
6   
7   import java.beans.PropertyChangeEvent;
8   import java.beans.PropertyChangeListener;
9   
10  import javax.swing.tree.TreePath;
11  
12  import org.springframework.binding.value.ValueModel;
13  import org.springframework.richclient.core.Guarded;
14  
15  /**
16   * @author Michael Engelhardt<me@mindcrime-ilab.de>
17   * @author $Author: agony $
18   * @version $Revision: 165 $
19   * 
20   */
21  public abstract class AbstractTreeSelectionGuard implements PropertyChangeListener {
22  
23      private final ValueModel selectionHolder;
24  
25      private final Guarded guarded;
26  
27      public AbstractTreeSelectionGuard(ValueModel selectionHolder, Guarded guarded) {
28          this.selectionHolder = selectionHolder;
29          this.selectionHolder.addValueChangeListener(this);
30          this.guarded = guarded;
31          propertyChange(null);
32      }
33  
34      /*
35       * (non-Javadoc)
36       * 
37       * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
38       */
39      @Override
40      public void propertyChange(PropertyChangeEvent evt) {
41          TreePath[] selected = (TreePath[]) selectionHolder.getValue();
42          guarded.setEnabled(shouldEnable(selected));
43  
44      }
45  
46      public Guarded getGuarded() {
47          return guarded;
48      }
49  
50      public ValueModel getSelectionHolder() {
51          return selectionHolder;
52      }
53  
54      protected abstract boolean shouldEnable(TreePath[] selected);
55  }