home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / plaf / basic / BasicTreeCellEditor.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  3.0 KB  |  114 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.DefaultCellEditor;
  4. import com.sun.java.swing.JTextField;
  5. import com.sun.java.swing.JTree;
  6. import com.sun.java.swing.Timer;
  7. import com.sun.java.swing.event.CellEditorListener;
  8. import com.sun.java.swing.event.TreeSelectionEvent;
  9. import com.sun.java.swing.event.TreeSelectionListener;
  10. import com.sun.java.swing.tree.TreeCellEditor;
  11. import com.sun.java.swing.tree.TreePath;
  12. import java.awt.Component;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import java.awt.event.MouseEvent;
  16. import java.util.EventObject;
  17.  
  18. public class BasicTreeCellEditor extends BasicTreeCellEditorContainer implements ActionListener, TreeSelectionListener {
  19.    protected boolean canEdit;
  20.    protected JTree changeTree;
  21.    protected TreePath lastPath;
  22.    protected Timer timer;
  23.  
  24.    public BasicTreeCellEditor(BasicTreeCellRenderer var1) {
  25.       super((TreeCellEditor)null, var1);
  26.       JTextField var2 = new JTextField();
  27.       super.realEditor = new DefaultCellEditor(var2);
  28.       ((DefaultCellEditor)super.realEditor).setClickCountToStart(1);
  29.    }
  30.  
  31.    public Component getTreeCellEditorComponent(JTree var1, Object var2, boolean var3, boolean var4, boolean var5, int var6) {
  32.       TreePath var7 = var1.getPathForRow(var6);
  33.       Component var8 = super.getTreeCellEditorComponent(var1, var2, var3, var4, var5, var6);
  34.       this.canEdit = this.lastPath != null && var7 != null && this.lastPath.equals(var7);
  35.       if (this.timer != null) {
  36.          this.timer.stop();
  37.       }
  38.  
  39.       return var8;
  40.    }
  41.  
  42.    public boolean isCellEditable(EventObject var1) {
  43.       if (var1 != null && (!(var1 instanceof MouseEvent) || ((MouseEvent)var1).getClickCount() <= 2)) {
  44.          if (this.canEdit && ((MouseEvent)var1).getClickCount() == 1) {
  45.             if (this.timer == null) {
  46.                this.timer = new Timer(1200, this);
  47.                this.timer.setRepeats(false);
  48.             }
  49.  
  50.             this.timer.start();
  51.          }
  52.  
  53.          return false;
  54.       } else {
  55.          return super.isCellEditable(var1);
  56.       }
  57.    }
  58.  
  59.    public void addCellEditorListener(CellEditorListener var1) {
  60.       super.addCellEditorListener(var1);
  61.       if (var1 instanceof BasicTreeUI) {
  62.          this.setChangeTree(((BasicTreeUI)var1).tree);
  63.       } else {
  64.          this.setChangeTree((JTree)null);
  65.       }
  66.    }
  67.  
  68.    public void removeCellEditorListener(CellEditorListener var1) {
  69.       super.removeCellEditorListener(var1);
  70.       if (var1 instanceof BasicTreeUI) {
  71.          this.setChangeTree((JTree)null);
  72.       }
  73.  
  74.    }
  75.  
  76.    protected void setChangeTree(JTree var1) {
  77.       if (this.changeTree != null) {
  78.          this.changeTree.removeTreeSelectionListener(this);
  79.       }
  80.  
  81.       this.changeTree = var1;
  82.       if (this.changeTree != null) {
  83.          this.changeTree.addTreeSelectionListener(this);
  84.       }
  85.  
  86.       if (this.timer != null) {
  87.          this.timer.stop();
  88.       }
  89.  
  90.    }
  91.  
  92.    public void valueChanged(TreeSelectionEvent var1) {
  93.       if (this.changeTree != null) {
  94.          if (this.changeTree.getSelectionCount() == 1) {
  95.             this.lastPath = this.changeTree.getSelectionPath();
  96.          } else {
  97.             this.lastPath = null;
  98.          }
  99.       }
  100.  
  101.       if (this.timer != null) {
  102.          this.timer.stop();
  103.       }
  104.  
  105.    }
  106.  
  107.    public void actionPerformed(ActionEvent var1) {
  108.       if (this.changeTree != null && this.lastPath != null) {
  109.          this.changeTree.startEditingAtPath(this.lastPath);
  110.       }
  111.  
  112.    }
  113. }
  114.