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 / BasicTreeCellEditorContainer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  4.2 KB  |  182 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.Icon;
  4. import com.sun.java.swing.JTree;
  5. import com.sun.java.swing.event.CellEditorListener;
  6. import com.sun.java.swing.tree.TreeCellEditor;
  7. import java.awt.Component;
  8. import java.awt.Container;
  9. import java.awt.Dimension;
  10. import java.awt.Graphics;
  11. import java.awt.LayoutManager;
  12. import java.io.IOException;
  13. import java.io.ObjectInputStream;
  14. import java.io.ObjectOutputStream;
  15. import java.io.Serializable;
  16. import java.util.EventObject;
  17. import java.util.Vector;
  18.  
  19. public class BasicTreeCellEditorContainer extends Container implements TreeCellEditor {
  20.    protected BasicTreeCellRenderer renderer;
  21.    protected transient Component editor;
  22.    protected transient TreeCellEditor realEditor;
  23.    protected transient Icon editingIcon;
  24.    protected int editingOffset;
  25.    protected boolean editingLeaf;
  26.    protected boolean editingExpanded;
  27.  
  28.    public BasicTreeCellEditorContainer(TreeCellEditor var1, BasicTreeCellRenderer var2) {
  29.       this.realEditor = var1;
  30.       this.renderer = var2;
  31.       ((Container)this).setLayout((LayoutManager)null);
  32.    }
  33.  
  34.    public Object getCellEditorValue() {
  35.       return this.realEditor.getCellEditorValue();
  36.    }
  37.  
  38.    public boolean isCellEditable(EventObject var1) {
  39.       if (this.realEditor.isCellEditable(var1)) {
  40.          this.prepareForEditing();
  41.          return true;
  42.       } else {
  43.          return false;
  44.       }
  45.    }
  46.  
  47.    public boolean shouldSelectCell(EventObject var1) {
  48.       return this.realEditor.shouldSelectCell(var1);
  49.    }
  50.  
  51.    protected void prepareForEditing() {
  52.       ((Container)this).add(this.editor);
  53.       if (this.renderer != null) {
  54.          if (this.editingLeaf) {
  55.             this.editingIcon = this.renderer.getLeafIcon();
  56.          } else if (this.editingExpanded) {
  57.             this.editingIcon = this.renderer.getOpenIcon();
  58.          } else {
  59.             this.editingIcon = this.renderer.getClosedIcon();
  60.          }
  61.  
  62.          if (this.editingIcon == null) {
  63.             this.editingOffset = 0;
  64.          } else {
  65.             this.editingOffset = this.renderer.getIconTextGap() + this.editingIcon.getIconWidth();
  66.          }
  67.       } else {
  68.          this.editingOffset = 0;
  69.          this.editingIcon = null;
  70.       }
  71.    }
  72.  
  73.    public boolean stopCellEditing() {
  74.       if (this.realEditor.stopCellEditing()) {
  75.          if (this.editor != null) {
  76.             ((Container)this).remove(this.editor);
  77.          }
  78.  
  79.          this.editingIcon = null;
  80.          return true;
  81.       } else {
  82.          return false;
  83.       }
  84.    }
  85.  
  86.    public void cancelCellEditing() {
  87.       this.realEditor.cancelCellEditing();
  88.       if (this.editor != null) {
  89.          ((Container)this).remove(this.editor);
  90.       }
  91.  
  92.       this.editingIcon = null;
  93.    }
  94.  
  95.    public void addCellEditorListener(CellEditorListener var1) {
  96.       this.realEditor.addCellEditorListener(var1);
  97.    }
  98.  
  99.    public void removeCellEditorListener(CellEditorListener var1) {
  100.       this.realEditor.removeCellEditorListener(var1);
  101.    }
  102.  
  103.    public void doLayout() {
  104.       if (this.editor != null) {
  105.          Dimension var1 = ((Component)this).getSize();
  106.          this.editor.getPreferredSize();
  107.          this.editor.setLocation(this.editingOffset, 0);
  108.          this.editor.setBounds(this.editingOffset, 0, var1.width - this.editingOffset, var1.height);
  109.       }
  110.  
  111.    }
  112.  
  113.    public Component getTreeCellEditorComponent(JTree var1, Object var2, boolean var3, boolean var4, boolean var5, int var6) {
  114.       this.editingLeaf = var5;
  115.       this.editingExpanded = var4;
  116.       this.editor = this.realEditor.getTreeCellEditorComponent(var1, var2, var3, var4, var5, var6);
  117.       return this;
  118.    }
  119.  
  120.    public void paint(Graphics var1) {
  121.       if (this.editingIcon != null) {
  122.          int var2 = Math.max(0, (((Component)this).getSize().height - this.editingIcon.getIconHeight()) / 2);
  123.          this.editingIcon.paintIcon(this, var1, 0, var2);
  124.       }
  125.  
  126.       super.paint(var1);
  127.    }
  128.  
  129.    public Dimension getPreferredSize() {
  130.       if (this.editor != null) {
  131.          Dimension var1 = this.editor.getPreferredSize();
  132.          var1.width += this.editingOffset + 5;
  133.          return var1;
  134.       } else {
  135.          return new Dimension(0, 0);
  136.       }
  137.    }
  138.  
  139.    public void requestFocus() {
  140.       if (this.editor != null) {
  141.          this.editor.requestFocus();
  142.       } else {
  143.          super.requestFocus();
  144.       }
  145.    }
  146.  
  147.    private void writeObject(ObjectOutputStream var1) throws IOException {
  148.       Vector var2 = new Vector();
  149.       var1.defaultWriteObject();
  150.       if (this.editingIcon != null && this.editingIcon instanceof Serializable) {
  151.          var2.addElement("editingIcon");
  152.          var2.addElement(this.editingIcon);
  153.       }
  154.  
  155.       if (this.realEditor != null && this.realEditor instanceof Serializable) {
  156.          var2.addElement("realEditor");
  157.          var2.addElement(this.realEditor);
  158.       }
  159.  
  160.       var1.writeObject(var2);
  161.    }
  162.  
  163.    private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
  164.       var1.defaultReadObject();
  165.       Vector var2 = (Vector)var1.readObject();
  166.       int var3 = 0;
  167.       int var4 = var2.size();
  168.       if (var3 < var4 && var2.elementAt(var3).equals("editingIcon")) {
  169.          ++var3;
  170.          this.editingIcon = (Icon)var2.elementAt(var3);
  171.          ++var3;
  172.       }
  173.  
  174.       if (var3 < var4 && var2.elementAt(var3).equals("realEditor")) {
  175.          ++var3;
  176.          this.realEditor = (TreeCellEditor)var2.elementAt(var3);
  177.          ++var3;
  178.       }
  179.  
  180.    }
  181. }
  182.