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

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.swing.event.EventListenerList;
  4. import com.sun.java.swing.event.ListSelectionEvent;
  5. import com.sun.java.swing.event.ListSelectionListener;
  6. import java.io.Serializable;
  7. import java.util.BitSet;
  8.  
  9. public class DefaultListSelectionModel implements ListSelectionModel, Cloneable, Serializable {
  10.    private int selectionMode = 2;
  11.    private int minIndex = -1;
  12.    private int maxIndex = -1;
  13.    private int anchorIndex = -1;
  14.    private int leadIndex = -1;
  15.    private int firstChangedIndex = -1;
  16.    private int lastChangedIndex = -1;
  17.    private boolean isAdjusting = false;
  18.    protected BitSet value = new BitSet(32);
  19.    protected EventListenerList listenerList = new EventListenerList();
  20.    protected boolean leadAnchorNotificationEnabled = true;
  21.    // $FF: synthetic field
  22.    static Class class$com$sun$java$swing$event$ListSelectionListener;
  23.  
  24.    public int getMinSelectionIndex() {
  25.       return this.minIndex;
  26.    }
  27.  
  28.    public int getMaxSelectionIndex() {
  29.       return this.maxIndex;
  30.    }
  31.  
  32.    public boolean getValueIsAdjusting() {
  33.       return this.isAdjusting;
  34.    }
  35.  
  36.    public int getSelectionMode() {
  37.       return this.selectionMode;
  38.    }
  39.  
  40.    public void setSelectionMode(int var1) {
  41.       switch (var1) {
  42.          case 0:
  43.          case 1:
  44.          case 2:
  45.             this.selectionMode = var1;
  46.             return;
  47.          default:
  48.             throw new IllegalArgumentException("invalid selectionMode");
  49.       }
  50.    }
  51.  
  52.    public boolean isSelectedIndex(int var1) {
  53.       return var1 != -1 && var1 >= this.minIndex && var1 <= this.maxIndex ? this.value.get(var1) : false;
  54.    }
  55.  
  56.    public boolean isSelectionEmpty() {
  57.       return this.minIndex == -1 && this.maxIndex == -1;
  58.    }
  59.  
  60.    public void addListSelectionListener(ListSelectionListener var1) {
  61.       this.listenerList.add(class$com$sun$java$swing$event$ListSelectionListener != null ? class$com$sun$java$swing$event$ListSelectionListener : (class$com$sun$java$swing$event$ListSelectionListener = class$("com.sun.java.swing.event.ListSelectionListener")), var1);
  62.    }
  63.  
  64.    public void removeListSelectionListener(ListSelectionListener var1) {
  65.       this.listenerList.remove(class$com$sun$java$swing$event$ListSelectionListener != null ? class$com$sun$java$swing$event$ListSelectionListener : (class$com$sun$java$swing$event$ListSelectionListener = class$("com.sun.java.swing.event.ListSelectionListener")), var1);
  66.    }
  67.  
  68.    protected void fireValueChanged(boolean var1) {
  69.       this.fireValueChanged(this.getMinSelectionIndex(), this.getMaxSelectionIndex(), var1);
  70.    }
  71.  
  72.    protected void fireValueChanged(int var1, int var2) {
  73.       this.fireValueChanged(var1, var2, this.getValueIsAdjusting());
  74.    }
  75.  
  76.    protected void fireValueChanged(int var1, int var2, boolean var3) {
  77.       Object[] var4 = this.listenerList.getListenerList();
  78.       ListSelectionEvent var5 = null;
  79.  
  80.       for(int var6 = var4.length - 2; var6 >= 0; var6 -= 2) {
  81.          if (var4[var6] == (class$com$sun$java$swing$event$ListSelectionListener != null ? class$com$sun$java$swing$event$ListSelectionListener : (class$com$sun$java$swing$event$ListSelectionListener = class$("com.sun.java.swing.event.ListSelectionListener")))) {
  82.             if (var5 == null) {
  83.                var5 = new ListSelectionEvent(this, var1, var2, var3);
  84.             }
  85.  
  86.             ((ListSelectionListener)var4[var6 + 1]).valueChanged(var5);
  87.          }
  88.       }
  89.  
  90.    }
  91.  
  92.    public void clearSelection() {
  93.       if (this.minIndex != -1 && this.maxIndex != -1) {
  94.          boolean var1 = false;
  95.  
  96.          for(int var2 = this.minIndex; var2 <= this.maxIndex; ++var2) {
  97.             if (this.value.get(var2)) {
  98.                var1 = true;
  99.             }
  100.  
  101.             this.value.clear(var2);
  102.          }
  103.  
  104.          this.minIndex = this.maxIndex = this.anchorIndex = this.leadIndex = -1;
  105.          if (var1) {
  106.             this.fireValueChanged(this.minIndex, this.maxIndex);
  107.          }
  108.  
  109.       }
  110.    }
  111.  
  112.    public void setLeadAnchorNotificationEnabled(boolean var1) {
  113.       this.leadAnchorNotificationEnabled = var1;
  114.    }
  115.  
  116.    public boolean isLeadAnchorNotificationEnabled() {
  117.       return this.leadAnchorNotificationEnabled;
  118.    }
  119.  
  120.    private void updateLeadAnchorIndices(int var1, int var2) {
  121.       if (this.leadAnchorNotificationEnabled) {
  122.          if (this.anchorIndex != -1 && this.anchorIndex != var1) {
  123.             int var3 = Math.min(this.anchorIndex, var1);
  124.             int var4 = Math.max(this.anchorIndex, var1);
  125.             this.firstChangedIndex = this.firstChangedIndex == -1 ? var4 : Math.min(var3, this.firstChangedIndex);
  126.             this.lastChangedIndex = Math.max(var4, this.lastChangedIndex);
  127.          }
  128.  
  129.          if (this.leadIndex != -1 && this.leadIndex != var2) {
  130.             int var5 = Math.min(this.leadIndex, var2);
  131.             int var6 = Math.max(this.leadIndex, var2);
  132.             this.firstChangedIndex = this.firstChangedIndex == -1 ? var5 : Math.min(var5, this.firstChangedIndex);
  133.             this.lastChangedIndex = Math.max(var6, this.lastChangedIndex);
  134.          }
  135.       }
  136.  
  137.       this.anchorIndex = var1;
  138.       this.leadIndex = var2;
  139.    }
  140.  
  141.    public void setSelectionInterval(int var1, int var2) {
  142.       if (this.getSelectionMode() == 0) {
  143.          var1 = var2;
  144.       }
  145.  
  146.       if (var1 == -1 && var2 == -1) {
  147.          this.clearSelection();
  148.       } else {
  149.          int var3 = Math.min(var1, var2);
  150.          int var4 = Math.max(var1, var2);
  151.          if (this.minIndex == -1 || var3 < this.minIndex) {
  152.             this.minIndex = var3;
  153.          }
  154.  
  155.          if (this.maxIndex == -1 || var4 > this.maxIndex) {
  156.             this.maxIndex = var4;
  157.          }
  158.  
  159.          this.firstChangedIndex = -1;
  160.          this.lastChangedIndex = -1;
  161.  
  162.          for(int var5 = this.minIndex; var5 < var3; ++var5) {
  163.             if (this.value.get(var5)) {
  164.                if (this.firstChangedIndex == -1) {
  165.                   this.firstChangedIndex = this.lastChangedIndex = var5;
  166.                } else if (var5 > this.lastChangedIndex) {
  167.                   this.lastChangedIndex = var5;
  168.                }
  169.             }
  170.  
  171.             this.value.clear(var5);
  172.          }
  173.  
  174.          for(int var6 = var3; var6 <= var4; ++var6) {
  175.             if (!this.value.get(var6)) {
  176.                if (this.firstChangedIndex == -1) {
  177.                   this.firstChangedIndex = this.lastChangedIndex = var6;
  178.                } else if (var6 > this.lastChangedIndex) {
  179.                   this.lastChangedIndex = var6;
  180.                }
  181.             }
  182.  
  183.             this.value.set(var6);
  184.          }
  185.  
  186.          for(int var7 = var4 + 1; var7 <= this.maxIndex; ++var7) {
  187.             if (this.value.get(var7)) {
  188.                if (this.firstChangedIndex == -1) {
  189.                   this.firstChangedIndex = this.lastChangedIndex = var7;
  190.                } else if (var7 > this.lastChangedIndex) {
  191.                   this.lastChangedIndex = var7;
  192.                }
  193.             }
  194.  
  195.             this.value.clear(var7);
  196.          }
  197.  
  198.          this.updateLeadAnchorIndices(var1, var2);
  199.          this.minIndex = var3;
  200.          this.maxIndex = var4;
  201.          if (this.firstChangedIndex != -1 && this.lastChangedIndex != -1) {
  202.             this.fireValueChanged(this.firstChangedIndex, this.lastChangedIndex);
  203.          }
  204.  
  205.       }
  206.    }
  207.  
  208.    public void addSelectionInterval(int var1, int var2) {
  209.       int var3 = this.getSelectionMode();
  210.       if (var3 != 0 && var3 != 1) {
  211.          if (var1 != -1 || var2 != -1) {
  212.             int var4 = Math.min(var1, var2);
  213.             int var5 = Math.max(var1, var2);
  214.             if (this.minIndex == -1 || var4 < this.minIndex) {
  215.                this.minIndex = var4;
  216.             }
  217.  
  218.             if (this.maxIndex == -1 || var5 > this.maxIndex) {
  219.                this.maxIndex = var5;
  220.             }
  221.  
  222.             this.firstChangedIndex = -1;
  223.             this.lastChangedIndex = -1;
  224.  
  225.             for(int var6 = var4; var6 <= var5; ++var6) {
  226.                if (!this.value.get(var6)) {
  227.                   if (this.firstChangedIndex == -1) {
  228.                      this.firstChangedIndex = this.lastChangedIndex = var6;
  229.                   } else if (var6 > this.lastChangedIndex) {
  230.                      this.lastChangedIndex = var6;
  231.                   }
  232.  
  233.                   this.value.set(var6);
  234.                }
  235.             }
  236.  
  237.             this.updateLeadAnchorIndices(var1, var2);
  238.             if (this.firstChangedIndex != -1 && this.lastChangedIndex != -1) {
  239.                this.fireValueChanged(this.firstChangedIndex, this.lastChangedIndex);
  240.             }
  241.  
  242.          }
  243.       } else {
  244.          this.setSelectionInterval(var1, var2);
  245.       }
  246.    }
  247.  
  248.    public void removeSelectionInterval(int var1, int var2) {
  249.       if (var1 != -1 || var2 != -1) {
  250.          if (this.minIndex != -1 || this.maxIndex != -1) {
  251.             int var3 = Math.min(var1, var2);
  252.             int var4 = Math.max(var1, var2);
  253.             int var5 = -1;
  254.             int var6 = -1;
  255.  
  256.             for(int var7 = var3; var7 <= var4; ++var7) {
  257.                if (this.value.get(var7)) {
  258.                   if (var5 == -1) {
  259.                      var6 = var7;
  260.                      var5 = var7;
  261.                   } else if (var7 > var6) {
  262.                      var6 = var7;
  263.                   }
  264.  
  265.                   this.value.clear(var7);
  266.                }
  267.             }
  268.  
  269.             int var8 = -1;
  270.             int var9 = -1;
  271.  
  272.             for(int var10 = this.minIndex; var10 <= this.maxIndex; ++var10) {
  273.                if (this.value.get(var10)) {
  274.                   if (var8 == -1) {
  275.                      var9 = var10;
  276.                      var8 = var10;
  277.                   } else if (var10 > var9) {
  278.                      var9 = var10;
  279.                   }
  280.                }
  281.             }
  282.  
  283.             this.minIndex = var8;
  284.             this.maxIndex = var9;
  285.             if (var5 != -1 && var6 != -1) {
  286.                this.fireValueChanged(var5, var6);
  287.             }
  288.  
  289.          }
  290.       }
  291.    }
  292.  
  293.    public void insertIndexInterval(int var1, int var2, boolean var3) {
  294.       int var4 = var3 ? Math.max(0, var1 - 1) : var1 + 1;
  295.       int var5 = var4 + var2 - 1;
  296.       boolean var6 = this.value.get(var1) && this.value.get(var4);
  297.  
  298.       for(int var7 = this.maxIndex; var7 >= var4; --var7) {
  299.          if (this.value.get(var7)) {
  300.             this.value.set(var7 + var2);
  301.          } else {
  302.             this.value.clear(var7 + var2);
  303.          }
  304.       }
  305.  
  306.       for(int var8 = var4; var8 <= var5; ++var8) {
  307.          if (var6) {
  308.             this.value.set(var8);
  309.          } else {
  310.             this.value.clear(var8);
  311.          }
  312.       }
  313.  
  314.       if (this.minIndex != -1 && (this.minIndex > var4 || this.minIndex == var4 && !var6)) {
  315.          this.minIndex += var2;
  316.       }
  317.  
  318.       if (this.maxIndex != -1 && this.maxIndex >= var4) {
  319.          this.maxIndex += var2;
  320.       }
  321.  
  322.       this.fireValueChanged(var4, this.maxIndex);
  323.    }
  324.  
  325.    public void removeIndexInterval(int var1, int var2) {
  326.       if (var1 >= 0 && var2 >= 0) {
  327.          if (this.minIndex != -1 || this.maxIndex != -1) {
  328.             int var3 = Math.min(var1, var2);
  329.             int var4 = Math.max(var1, var2);
  330.             int var5 = var4 - var3 + 1;
  331.             if (var3 <= this.maxIndex && var4 >= this.minIndex) {
  332.                if (var3 <= this.minIndex && var4 >= this.maxIndex) {
  333.                   this.clearSelection();
  334.                } else {
  335.                   for(int var6 = var3; var6 <= var4; ++var6) {
  336.                      if (this.value.get(var6 + var5)) {
  337.                         this.value.set(var6);
  338.                      } else {
  339.                         this.value.clear(var6);
  340.                      }
  341.                   }
  342.  
  343.                   for(int var7 = this.maxIndex; var7 > this.maxIndex - var5; --var7) {
  344.                      this.value.clear(var7);
  345.                   }
  346.  
  347.                   int var8 = this.minIndex;
  348.                   int var9 = this.maxIndex;
  349.                   if (var8 >= var3) {
  350.                      this.minIndex = -1;
  351.  
  352.                      for(int var10 = var3; var10 <= var9; ++var10) {
  353.                         if (this.value.get(var10)) {
  354.                            this.minIndex = var10;
  355.                            break;
  356.                         }
  357.                      }
  358.                   }
  359.  
  360.                   if (this.minIndex == -1) {
  361.                      this.maxIndex = -1;
  362.                   } else if (var9 > var4) {
  363.                      this.maxIndex = var9 - var5;
  364.                   } else {
  365.                      for(int var11 = var9; var11 >= this.minIndex; --var11) {
  366.                         if (this.value.get(var11)) {
  367.                            this.maxIndex = var11;
  368.                            break;
  369.                         }
  370.                      }
  371.                   }
  372.  
  373.                   if (this.maxIndex == -1 && this.minIndex == -1) {
  374.                      this.fireValueChanged(var3, var4);
  375.                   } else {
  376.                      this.firstChangedIndex = Math.min(var3, this.minIndex);
  377.                      this.lastChangedIndex = Math.max(var4, this.maxIndex);
  378.                      this.fireValueChanged(this.firstChangedIndex, this.lastChangedIndex);
  379.                   }
  380.                }
  381.             }
  382.          }
  383.       }
  384.    }
  385.  
  386.    public void setValueIsAdjusting(boolean var1) {
  387.       if (var1 != this.isAdjusting) {
  388.          this.isAdjusting = var1;
  389.          this.fireValueChanged(var1);
  390.       }
  391.  
  392.    }
  393.  
  394.    public String toString() {
  395.       String var1 = (this.getValueIsAdjusting() ? "~" : "=") + this.value.toString();
  396.       return this.getClass().getName() + " " + Integer.toString(this.hashCode()) + " " + var1;
  397.    }
  398.  
  399.    public Object clone() throws CloneNotSupportedException {
  400.       DefaultListSelectionModel var1 = (DefaultListSelectionModel)super.clone();
  401.       var1.value = (BitSet)this.value.clone();
  402.       var1.listenerList = new EventListenerList();
  403.       return var1;
  404.    }
  405.  
  406.    public int getAnchorSelectionIndex() {
  407.       return this.anchorIndex;
  408.    }
  409.  
  410.    public int getLeadSelectionIndex() {
  411.       return this.leadIndex;
  412.    }
  413.  
  414.    public void setAnchorSelectionIndex(int var1) {
  415.       this.anchorIndex = var1;
  416.    }
  417.  
  418.    public void setLeadSelectionIndex(int var1) {
  419.       this.updateLead(var1);
  420.       this.leadIndex = var1;
  421.    }
  422.  
  423.    private boolean contains(int var1, int var2, int var3) {
  424.       if (var1 <= var2) {
  425.          return var3 >= var1 && var3 < var2;
  426.       } else {
  427.          return var3 <= var1 && var3 > var2;
  428.       }
  429.    }
  430.  
  431.    private int sign(int var1, int var2) {
  432.       if (var1 < var2) {
  433.          return -1;
  434.       } else {
  435.          return var1 > var2 ? 1 : 0;
  436.       }
  437.    }
  438.  
  439.    private void updateLead(int var1) {
  440.       if (this.anchorIndex == -1) {
  441.          this.anchorIndex = var1;
  442.       }
  443.  
  444.       boolean var2 = false;
  445.       if (!var2 && this.contains(this.leadIndex, var1, this.anchorIndex)) {
  446.          this.removeSelectionInterval(this.anchorIndex, this.leadIndex);
  447.          this.addSelectionInterval(this.anchorIndex, var1);
  448.       }
  449.  
  450.       if (!var2 && !this.contains(this.anchorIndex, this.leadIndex, var1)) {
  451.          this.addSelectionInterval(this.anchorIndex, var1);
  452.       } else {
  453.          int var3 = this.sign(this.anchorIndex, this.leadIndex);
  454.          this.removeSelectionInterval(var1 - var3, this.leadIndex);
  455.       }
  456.    }
  457.  
  458.    // $FF: synthetic method
  459.    static Class class$(String var0) {
  460.       try {
  461.          return Class.forName(var0);
  462.       } catch (ClassNotFoundException var2) {
  463.          throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  464.       }
  465.    }
  466. }
  467.