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

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.accessibility.Accessible;
  4. import com.sun.java.accessibility.AccessibleContext;
  5. import com.sun.java.swing.event.ListSelectionEvent;
  6. import com.sun.java.swing.event.ListSelectionListener;
  7. import com.sun.java.swing.plaf.ListUI;
  8. import java.awt.Color;
  9. import java.awt.Component;
  10. import java.awt.Container;
  11. import java.awt.Dimension;
  12. import java.awt.Font;
  13. import java.awt.Insets;
  14. import java.awt.Point;
  15. import java.awt.Rectangle;
  16. import java.util.Vector;
  17.  
  18. public class JList extends JComponent implements Scrollable, Accessible {
  19.    private int fixedCellWidth;
  20.    private int fixedCellHeight;
  21.    private Object prototypeCellValue;
  22.    private int visibleRowCount;
  23.    private Color selectionForeground;
  24.    private Color selectionBackground;
  25.    private ListSelectionModel selectionModel;
  26.    private ListModel dataModel;
  27.    private ListCellRenderer cellRenderer;
  28.    private ListSelectionListener selectionListener;
  29.    // $FF: synthetic field
  30.    static Class class$com$sun$java$swing$event$ListSelectionListener;
  31.  
  32.    public JList(ListModel var1) {
  33.       this.fixedCellWidth = -1;
  34.       this.fixedCellHeight = -1;
  35.       this.visibleRowCount = 8;
  36.       if (var1 == null) {
  37.          throw new IllegalArgumentException("dataModel must be non null");
  38.       } else {
  39.          this.dataModel = var1;
  40.          this.selectionModel = this.createSelectionModel();
  41.          ((JComponent)this).setAutoscrolls(true);
  42.          this.updateUI();
  43.       }
  44.    }
  45.  
  46.    public JList(Object[] var1) {
  47.       this((ListModel)(new 1(var1)));
  48.    }
  49.  
  50.    public JList(Vector var1) {
  51.       this((ListModel)(new 2(var1)));
  52.    }
  53.  
  54.    public JList() {
  55.       this((ListModel)(new 3()));
  56.    }
  57.  
  58.    public ListUI getUI() {
  59.       return (ListUI)super.ui;
  60.    }
  61.  
  62.    public void setUI(ListUI var1) {
  63.       super.setUI(var1);
  64.    }
  65.  
  66.    public void updateUI() {
  67.       this.setUI((ListUI)UIManager.getUI(this));
  68.       ((Container)this).invalidate();
  69.    }
  70.  
  71.    public String getUIClassID() {
  72.       return "ListUI";
  73.    }
  74.  
  75.    public boolean isOpaque() {
  76.       return true;
  77.    }
  78.  
  79.    private void updateFixedCellSize() {
  80.       ListCellRenderer var1 = this.getCellRenderer();
  81.       Object var2 = this.getPrototypeCellValue();
  82.       if (var1 != null && var2 != null) {
  83.          Component var3 = var1.getListCellRendererComponent(this, var2, 0, false, false);
  84.          Font var4 = var3.getFont();
  85.          var3.setFont(((Component)this).getFont());
  86.          Dimension var5 = var3.getPreferredSize();
  87.          this.fixedCellWidth = var5.width;
  88.          this.fixedCellHeight = var5.height;
  89.          var3.setFont(var4);
  90.       }
  91.  
  92.    }
  93.  
  94.    public Object getPrototypeCellValue() {
  95.       return this.prototypeCellValue;
  96.    }
  97.  
  98.    public void setPrototypeCellValue(Object var1) {
  99.       Object var2 = this.prototypeCellValue;
  100.       this.prototypeCellValue = var1;
  101.       if (var1 != null && !var1.equals(var2)) {
  102.          this.updateFixedCellSize();
  103.       }
  104.  
  105.       ((JComponent)this).firePropertyChange("prototypeCellValue", var2, var1);
  106.    }
  107.  
  108.    public int getFixedCellWidth() {
  109.       return this.fixedCellWidth;
  110.    }
  111.  
  112.    public void setFixedCellWidth(int var1) {
  113.       int var2 = this.fixedCellWidth;
  114.       this.fixedCellWidth = var1;
  115.       ((JComponent)this).firePropertyChange("fixedCellWidth", var2, this.fixedCellWidth);
  116.    }
  117.  
  118.    public int getFixedCellHeight() {
  119.       return this.fixedCellHeight;
  120.    }
  121.  
  122.    public void setFixedCellHeight(int var1) {
  123.       int var2 = this.fixedCellHeight;
  124.       this.fixedCellHeight = var1;
  125.       ((JComponent)this).firePropertyChange("fixedCellHeight", var2, this.fixedCellHeight);
  126.    }
  127.  
  128.    public ListCellRenderer getCellRenderer() {
  129.       return this.cellRenderer;
  130.    }
  131.  
  132.    public void setCellRenderer(ListCellRenderer var1) {
  133.       ListCellRenderer var2 = this.cellRenderer;
  134.       this.cellRenderer = var1;
  135.       if (var1 != null && !var1.equals(var2)) {
  136.          this.updateFixedCellSize();
  137.       }
  138.  
  139.       ((JComponent)this).firePropertyChange("cellRenderer", var2, var1);
  140.    }
  141.  
  142.    public Color getSelectionForeground() {
  143.       return this.selectionForeground;
  144.    }
  145.  
  146.    public void setSelectionForeground(Color var1) {
  147.       Color var2 = this.selectionForeground;
  148.       this.selectionForeground = var1;
  149.       ((JComponent)this).firePropertyChange("selectionForeground", var2, var1);
  150.    }
  151.  
  152.    public Color getSelectionBackground() {
  153.       return this.selectionBackground;
  154.    }
  155.  
  156.    public void setSelectionBackground(Color var1) {
  157.       Color var2 = this.selectionBackground;
  158.       this.selectionBackground = var1;
  159.       ((JComponent)this).firePropertyChange("selectionBackground", var2, var1);
  160.    }
  161.  
  162.    public int getVisibleRowCount() {
  163.       return this.visibleRowCount;
  164.    }
  165.  
  166.    public void setVisibleRowCount(int var1) {
  167.       int var2 = this.visibleRowCount;
  168.       this.visibleRowCount = Math.max(0, var1);
  169.       ((JComponent)this).firePropertyChange("visibleRowCount", var2, var1);
  170.    }
  171.  
  172.    public int getFirstVisibleIndex() {
  173.       Point var1 = ((JComponent)this).getVisibleRect().getLocation();
  174.       return this.locationToIndex(var1);
  175.    }
  176.  
  177.    public int getLastVisibleIndex() {
  178.       Rectangle var1 = ((JComponent)this).getVisibleRect();
  179.       Point var2 = new Point(var1.x + var1.width - 1, var1.y + var1.height - 1);
  180.       return this.locationToIndex(var2);
  181.    }
  182.  
  183.    public void ensureIndexIsVisible(int var1) {
  184.       Rectangle var2 = this.getCellBounds(var1, var1);
  185.       if (var2 != null) {
  186.          ((JComponent)this).scrollRectToVisible(var2);
  187.       }
  188.  
  189.    }
  190.  
  191.    public int locationToIndex(Point var1) {
  192.       ListUI var2 = this.getUI();
  193.       return var2 != null ? var2.locationToIndex(this, var1) : -1;
  194.    }
  195.  
  196.    public Point indexToLocation(int var1) {
  197.       ListUI var2 = this.getUI();
  198.       return var2 != null ? var2.indexToLocation(this, var1) : null;
  199.    }
  200.  
  201.    public Rectangle getCellBounds(int var1, int var2) {
  202.       ListUI var3 = this.getUI();
  203.       return var3 != null ? var3.getCellBounds(this, var1, var2) : null;
  204.    }
  205.  
  206.    public ListModel getModel() {
  207.       return this.dataModel;
  208.    }
  209.  
  210.    public void setModel(ListModel var1) {
  211.       ListModel var2 = this.dataModel;
  212.       this.dataModel = var1;
  213.       ((JComponent)this).firePropertyChange("model", var2, this.dataModel);
  214.       this.clearSelection();
  215.    }
  216.  
  217.    public void setListData(Object[] var1) {
  218.       this.setModel(new 4(var1));
  219.    }
  220.  
  221.    public void setListData(Vector var1) {
  222.       this.setModel(new 5(var1));
  223.    }
  224.  
  225.    protected ListSelectionModel createSelectionModel() {
  226.       return new DefaultListSelectionModel();
  227.    }
  228.  
  229.    public ListSelectionModel getSelectionModel() {
  230.       return this.selectionModel;
  231.    }
  232.  
  233.    protected void fireSelectionValueChanged(int var1, int var2, boolean var3) {
  234.       Object[] var4 = super.listenerList.getListenerList();
  235.       ListSelectionEvent var5 = null;
  236.  
  237.       for(int var6 = var4.length - 2; var6 >= 0; var6 -= 2) {
  238.          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 = JComponent.class$("com.sun.java.swing.event.ListSelectionListener")))) {
  239.             if (var5 == null) {
  240.                var5 = new ListSelectionEvent(this, var1, var2, var3);
  241.             }
  242.  
  243.             ((ListSelectionListener)var4[var6 + 1]).valueChanged(var5);
  244.          }
  245.       }
  246.  
  247.    }
  248.  
  249.    public void addListSelectionListener(ListSelectionListener var1) {
  250.       if (this.selectionListener == null) {
  251.          this.selectionListener = new 6(this);
  252.          this.getSelectionModel().addListSelectionListener(this.selectionListener);
  253.       }
  254.  
  255.       super.listenerList.add(class$com$sun$java$swing$event$ListSelectionListener != null ? class$com$sun$java$swing$event$ListSelectionListener : (class$com$sun$java$swing$event$ListSelectionListener = JComponent.class$("com.sun.java.swing.event.ListSelectionListener")), var1);
  256.    }
  257.  
  258.    public void removeListSelectionListener(ListSelectionListener var1) {
  259.       super.listenerList.remove(class$com$sun$java$swing$event$ListSelectionListener != null ? class$com$sun$java$swing$event$ListSelectionListener : (class$com$sun$java$swing$event$ListSelectionListener = JComponent.class$("com.sun.java.swing.event.ListSelectionListener")), var1);
  260.    }
  261.  
  262.    public void setSelectionModel(ListSelectionModel var1) {
  263.       if (var1 == null) {
  264.          throw new IllegalArgumentException("selectionModel must be non null");
  265.       } else {
  266.          if (this.selectionListener != null) {
  267.             this.selectionModel.removeListSelectionListener(this.selectionListener);
  268.             var1.addListSelectionListener(this.selectionListener);
  269.          }
  270.  
  271.          ListSelectionModel var2 = this.selectionModel;
  272.          this.selectionModel = var1;
  273.          ((JComponent)this).firePropertyChange("selectionModel", var2, var1);
  274.          if (super.accessibleContext != null) {
  275.             super.accessibleContext.firePropertyChange("AccessibleSelection", new Boolean(false), new Boolean(true));
  276.          }
  277.  
  278.       }
  279.    }
  280.  
  281.    public void setSelectionMode(int var1) {
  282.       this.getSelectionModel().setSelectionMode(var1);
  283.    }
  284.  
  285.    public int getSelectionMode() {
  286.       return this.getSelectionModel().getSelectionMode();
  287.    }
  288.  
  289.    public int getAnchorSelectionIndex() {
  290.       return this.getSelectionModel().getAnchorSelectionIndex();
  291.    }
  292.  
  293.    public int getLeadSelectionIndex() {
  294.       return this.getSelectionModel().getLeadSelectionIndex();
  295.    }
  296.  
  297.    public int getMinSelectionIndex() {
  298.       return this.getSelectionModel().getMinSelectionIndex();
  299.    }
  300.  
  301.    public int getMaxSelectionIndex() {
  302.       return this.getSelectionModel().getMaxSelectionIndex();
  303.    }
  304.  
  305.    public boolean isSelectedIndex(int var1) {
  306.       return this.getSelectionModel().isSelectedIndex(var1);
  307.    }
  308.  
  309.    public boolean isSelectionEmpty() {
  310.       return this.getSelectionModel().isSelectionEmpty();
  311.    }
  312.  
  313.    public void clearSelection() {
  314.       this.getSelectionModel().clearSelection();
  315.    }
  316.  
  317.    public void setSelectionInterval(int var1, int var2) {
  318.       this.getSelectionModel().setSelectionInterval(var1, var2);
  319.    }
  320.  
  321.    public void addSelectionInterval(int var1, int var2) {
  322.       this.getSelectionModel().addSelectionInterval(var1, var2);
  323.    }
  324.  
  325.    public void removeSelectionInterval(int var1, int var2) {
  326.       this.getSelectionModel().removeSelectionInterval(var1, var2);
  327.    }
  328.  
  329.    public void setValueIsAdjusting(boolean var1) {
  330.       this.getSelectionModel().setValueIsAdjusting(var1);
  331.    }
  332.  
  333.    public boolean getValueIsAdjusting() {
  334.       return this.getSelectionModel().getValueIsAdjusting();
  335.    }
  336.  
  337.    public int[] getSelectedIndices() {
  338.       ListSelectionModel var1 = this.getSelectionModel();
  339.       int var2 = var1.getMinSelectionIndex();
  340.       int var3 = var1.getMaxSelectionIndex();
  341.       if (var2 >= 0 && var3 >= 0) {
  342.          int[] var4 = new int[1 + (var3 - var2)];
  343.          int var5 = 0;
  344.  
  345.          for(int var6 = var2; var6 <= var3; ++var6) {
  346.             if (var1.isSelectedIndex(var6)) {
  347.                var4[var5++] = var6;
  348.             }
  349.          }
  350.  
  351.          int[] var7 = new int[var5];
  352.          System.arraycopy(var4, 0, var7, 0, var5);
  353.          return var7;
  354.       } else {
  355.          return new int[0];
  356.       }
  357.    }
  358.  
  359.    public void setSelectedIndex(int var1) {
  360.       this.getSelectionModel().setSelectionInterval(var1, var1);
  361.    }
  362.  
  363.    public void setSelectedIndices(int[] var1) {
  364.       ListSelectionModel var2 = this.getSelectionModel();
  365.       var2.clearSelection();
  366.  
  367.       for(int var3 = 0; var3 < var1.length; ++var3) {
  368.          var2.addSelectionInterval(var1[var3], var1[var3]);
  369.       }
  370.  
  371.    }
  372.  
  373.    public Object[] getSelectedValues() {
  374.       ListSelectionModel var1 = this.getSelectionModel();
  375.       ListModel var2 = this.getModel();
  376.       int var3 = var1.getMinSelectionIndex();
  377.       int var4 = var1.getMaxSelectionIndex();
  378.       if (var3 >= 0 && var4 >= 0) {
  379.          Object[] var5 = new Object[1 + (var4 - var3)];
  380.          int var6 = 0;
  381.  
  382.          for(int var7 = var3; var7 <= var4; ++var7) {
  383.             if (var1.isSelectedIndex(var7)) {
  384.                var5[var6++] = var2.getElementAt(var7);
  385.             }
  386.          }
  387.  
  388.          Object[] var8 = new Object[var6];
  389.          System.arraycopy(var5, 0, var8, 0, var6);
  390.          return var8;
  391.       } else {
  392.          return new Object[0];
  393.       }
  394.    }
  395.  
  396.    public int getSelectedIndex() {
  397.       return this.getMinSelectionIndex();
  398.    }
  399.  
  400.    public Object getSelectedValue() {
  401.       int var1 = this.getMinSelectionIndex();
  402.       return var1 == -1 ? null : this.getModel().getElementAt(var1);
  403.    }
  404.  
  405.    public void setSelectedValue(Object var1, boolean var2) {
  406.       if (var1 == null) {
  407.          this.setSelectedIndex(-1);
  408.       } else if (!var1.equals(this.getSelectedValue())) {
  409.          ListModel var5 = this.getModel();
  410.          int var3 = 0;
  411.  
  412.          for(int var4 = var5.getSize(); var3 < var4; ++var3) {
  413.             if (var1.equals(var5.getElementAt(var3))) {
  414.                this.setSelectedIndex(var3);
  415.                if (var2) {
  416.                   this.ensureIndexIsVisible(var3);
  417.                }
  418.  
  419.                ((Component)this).repaint();
  420.                return;
  421.             }
  422.          }
  423.  
  424.          this.setSelectedIndex(-1);
  425.       }
  426.  
  427.       ((Component)this).repaint();
  428.    }
  429.  
  430.    public Dimension getPreferredScrollableViewportSize() {
  431.       Insets var1 = ((JComponent)this).getInsets();
  432.       int var2 = var1.left + var1.right;
  433.       int var3 = var1.top + var1.bottom;
  434.       int var4 = this.getVisibleRowCount();
  435.       int var5 = this.getFixedCellWidth();
  436.       int var6 = this.getFixedCellHeight();
  437.       if (var5 > 0 && var6 > 0) {
  438.          int var12 = var5 + var2;
  439.          int var13 = var4 * var6 + var3;
  440.          return new Dimension(var12, var13);
  441.       } else if (this.getModel().getSize() > 0) {
  442.          int var7 = ((JComponent)this).getPreferredSize().width;
  443.          Rectangle var8 = this.getCellBounds(0, 0);
  444.          int var9 = var4 * var8.height + var3;
  445.          return new Dimension(var7, var9);
  446.       } else {
  447.          var5 = var5 > 0 ? var5 : 256;
  448.          var6 = var6 > 0 ? var6 : 16;
  449.          return new Dimension(var5, var6 * var4);
  450.       }
  451.    }
  452.  
  453.    public int getScrollableUnitIncrement(Rectangle var1, int var2, int var3) {
  454.       if (var2 == 0) {
  455.          return 1;
  456.       } else {
  457.          int var4 = this.getFirstVisibleIndex();
  458.          if (var4 == -1) {
  459.             return 0;
  460.          } else if (var3 > 0) {
  461.             Rectangle var7 = this.getCellBounds(var4, var4);
  462.             return var7 == null ? 0 : var7.height - (var1.y - var7.y);
  463.          } else {
  464.             Rectangle var5 = this.getCellBounds(var4, var4);
  465.             if (var5.y == var1.y && var4 == 0) {
  466.                return 0;
  467.             } else if (var5.y == var1.y) {
  468.                Rectangle var6 = this.getCellBounds(var4 - 1, var4 - 1);
  469.                return var6 == null ? 0 : var6.height;
  470.             } else {
  471.                return var1.y - var5.y;
  472.             }
  473.          }
  474.       }
  475.    }
  476.  
  477.    public int getScrollableBlockIncrement(Rectangle var1, int var2, int var3) {
  478.       return var2 == 1 ? var1.height : var1.width;
  479.    }
  480.  
  481.    public boolean getScrollableTracksViewportWidth() {
  482.       return false;
  483.    }
  484.  
  485.    public boolean getScrollableTracksViewportHeight() {
  486.       return false;
  487.    }
  488.  
  489.    public AccessibleContext getAccessibleContext() {
  490.       if (super.accessibleContext == null) {
  491.          super.accessibleContext = new AccessibleJList(this);
  492.       }
  493.  
  494.       return super.accessibleContext;
  495.    }
  496.  
  497.    // $FF: synthetic method
  498.    static ListSelectionModel access$0(JList var0) {
  499.       return var0.selectionModel;
  500.    }
  501. }
  502.