home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 144 / DPCS0200.iso / Internet / Supanet / system / swing.jar / javax / swing / plaf / basic / BasicListUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-05  |  12.6 KB  |  434 lines

  1. package javax.swing.plaf.basic;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Insets;
  8. import java.awt.LayoutManager;
  9. import java.awt.Point;
  10. import java.awt.Rectangle;
  11. import java.awt.event.FocusListener;
  12. import java.beans.PropertyChangeListener;
  13. import javax.swing.CellRendererPane;
  14. import javax.swing.JComponent;
  15. import javax.swing.JList;
  16. import javax.swing.KeyStroke;
  17. import javax.swing.ListCellRenderer;
  18. import javax.swing.ListModel;
  19. import javax.swing.ListSelectionModel;
  20. import javax.swing.LookAndFeel;
  21. import javax.swing.UIManager;
  22. import javax.swing.event.ListDataListener;
  23. import javax.swing.event.ListSelectionListener;
  24. import javax.swing.event.MouseInputListener;
  25. import javax.swing.plaf.ComponentUI;
  26. import javax.swing.plaf.ListUI;
  27. import javax.swing.plaf.UIResource;
  28.  
  29. public class BasicListUI extends ListUI {
  30.    protected JList list = null;
  31.    protected CellRendererPane rendererPane;
  32.    protected FocusListener focusListener;
  33.    protected MouseInputListener mouseInputListener;
  34.    protected ListSelectionListener listSelectionListener;
  35.    protected ListDataListener listDataListener;
  36.    protected PropertyChangeListener propertyChangeListener;
  37.    protected int[] cellHeights = null;
  38.    protected int cellHeight = -1;
  39.    protected int cellWidth = -1;
  40.    protected int updateLayoutStateNeeded = 1;
  41.    protected static final int modelChanged = 1;
  42.    protected static final int selectionModelChanged = 2;
  43.    protected static final int fontChanged = 4;
  44.    protected static final int fixedCellWidthChanged = 8;
  45.    protected static final int fixedCellHeightChanged = 16;
  46.    protected static final int prototypeCellValueChanged = 32;
  47.    protected static final int cellRendererChanged = 64;
  48.    private static final int CHANGE_LEAD = 0;
  49.    private static final int CHANGE_SELECTION = 1;
  50.    private static final int EXTEND_SELECTION = 2;
  51.  
  52.    // $FF: synthetic method
  53.    static void access$0(BasicListUI var0) {
  54.       var0.redrawList();
  55.    }
  56.  
  57.    protected int convertRowToY(int var1) {
  58.       int var2 = this.list.getModel().getSize();
  59.       Insets var3 = this.list.getInsets();
  60.       if (var1 >= 0 && var1 < var2) {
  61.          if (this.cellHeights == null) {
  62.             return var3.top + this.cellHeight * var1;
  63.          } else if (var1 >= this.cellHeights.length) {
  64.             return -1;
  65.          } else {
  66.             int var4 = var3.top;
  67.  
  68.             for(int var5 = 0; var5 < var1; ++var5) {
  69.                var4 += this.cellHeights[var5];
  70.             }
  71.  
  72.             return var4;
  73.          }
  74.       } else {
  75.          return -1;
  76.       }
  77.    }
  78.  
  79.    protected int convertYToRow(int var1) {
  80.       int var2 = this.list.getModel().getSize();
  81.       Insets var3 = this.list.getInsets();
  82.       if (this.cellHeights == null) {
  83.          int var7 = this.cellHeight == 0 ? 0 : (var1 - var3.top) / this.cellHeight;
  84.          return var7 >= 0 && var7 < var2 ? var7 : -1;
  85.       } else if (var2 > this.cellHeights.length) {
  86.          return -1;
  87.       } else {
  88.          int var4 = var3.top;
  89.          int var5 = 0;
  90.  
  91.          for(int var6 = 0; var6 < var2; ++var6) {
  92.             if (var1 >= var4 && var1 < var4 + this.cellHeights[var6]) {
  93.                return var5;
  94.             }
  95.  
  96.             var4 += this.cellHeights[var6];
  97.             ++var5;
  98.          }
  99.  
  100.          return -1;
  101.       }
  102.    }
  103.  
  104.    protected FocusListener createFocusListener() {
  105.       return new FocusHandler(this);
  106.    }
  107.  
  108.    protected ListDataListener createListDataListener() {
  109.       return new ListDataHandler(this);
  110.    }
  111.  
  112.    protected ListSelectionListener createListSelectionListener() {
  113.       return new ListSelectionHandler(this);
  114.    }
  115.  
  116.    protected MouseInputListener createMouseInputListener() {
  117.       return new MouseInputHandler(this);
  118.    }
  119.  
  120.    protected PropertyChangeListener createPropertyChangeListener() {
  121.       return new PropertyChangeHandler(this);
  122.    }
  123.  
  124.    public static ComponentUI createUI(JComponent var0) {
  125.       return new BasicListUI();
  126.    }
  127.  
  128.    public Rectangle getCellBounds(JList var1, int var2, int var3) {
  129.       this.maybeUpdateLayoutState();
  130.       int var4 = Math.min(var2, var3);
  131.       int var5 = Math.max(var2, var3);
  132.       int var6 = this.convertRowToY(var4);
  133.       int var7 = this.convertRowToY(var5);
  134.       if (var6 != -1 && var7 != -1) {
  135.          Insets var8 = ((JComponent)var1).getInsets();
  136.          int var9 = var8.left;
  137.          int var11 = ((JComponent)var1).getWidth() - (var8.left + var8.right);
  138.          int var12 = var7 + this.getRowHeight(var5) - var6;
  139.          return new Rectangle(var9, var6, var11, var12);
  140.       } else {
  141.          return null;
  142.       }
  143.    }
  144.  
  145.    public Dimension getMaximumSize(JComponent var1) {
  146.       return this.getPreferredSize(var1);
  147.    }
  148.  
  149.    public Dimension getMinimumSize(JComponent var1) {
  150.       return this.getPreferredSize(var1);
  151.    }
  152.  
  153.    public Dimension getPreferredSize(JComponent var1) {
  154.       this.maybeUpdateLayoutState();
  155.       int var2 = this.list.getModel().getSize() - 1;
  156.       if (var2 < 0) {
  157.          return new Dimension(0, 0);
  158.       } else {
  159.          Insets var3 = this.list.getInsets();
  160.          int var4 = this.cellWidth + var3.left + var3.right;
  161.          int var5 = this.convertRowToY(var2) + this.getRowHeight(var2) + var3.bottom;
  162.          return new Dimension(var4, var5);
  163.       }
  164.    }
  165.  
  166.    protected int getRowHeight(int var1) {
  167.       if (var1 >= 0 && var1 < this.list.getModel().getSize()) {
  168.          return this.cellHeights == null ? this.cellHeight : (var1 < this.cellHeights.length ? this.cellHeights[var1] : -1);
  169.       } else {
  170.          return -1;
  171.       }
  172.    }
  173.  
  174.    public Point indexToLocation(JList var1, int var2) {
  175.       this.maybeUpdateLayoutState();
  176.       return new Point(0, this.convertRowToY(var2));
  177.    }
  178.  
  179.    protected void installDefaults() {
  180.       this.list.setLayout((LayoutManager)null);
  181.       LookAndFeel.installBorder(this.list, "List.border");
  182.       LookAndFeel.installColorsAndFont(this.list, "List.background", "List.foreground", "List.font");
  183.       if (this.list.getCellRenderer() == null) {
  184.          this.list.setCellRenderer((ListCellRenderer)UIManager.get("List.cellRenderer"));
  185.       }
  186.  
  187.       Color var1 = this.list.getSelectionBackground();
  188.       if (var1 == null || var1 instanceof UIResource) {
  189.          this.list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));
  190.       }
  191.  
  192.       Color var2 = this.list.getSelectionForeground();
  193.       if (var2 == null || var2 instanceof UIResource) {
  194.          this.list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));
  195.       }
  196.  
  197.    }
  198.  
  199.    protected void installKeyboardActions() {
  200.       this.list.registerKeyboardAction(new IncrementLeadSelectionAction(this, "SelectPreviousRow", 1, -1), KeyStroke.getKeyStroke(38, 0), 0);
  201.       this.list.registerKeyboardAction(new IncrementLeadSelectionAction(this, "ExtendSelectPreviousRow", 2, -1), KeyStroke.getKeyStroke(38, 1), 0);
  202.       this.list.registerKeyboardAction(new IncrementLeadSelectionAction(this, "SelectNextRow", 1, 1), KeyStroke.getKeyStroke(40, 0), 0);
  203.       this.list.registerKeyboardAction(new IncrementLeadSelectionAction(this, "ExtendSelectPreviousRow", 2, 1), KeyStroke.getKeyStroke(40, 1), 0);
  204.       this.list.registerKeyboardAction(new HomeAction(this, "SelectHome", 1), KeyStroke.getKeyStroke(36, 0), 0);
  205.       this.list.registerKeyboardAction(new HomeAction(this, "ExtendSelectHome", 2), KeyStroke.getKeyStroke(36, 1), 0);
  206.       this.list.registerKeyboardAction(new EndAction(this, "SelectEnd", 1), KeyStroke.getKeyStroke(35, 0), 0);
  207.       this.list.registerKeyboardAction(new EndAction(this, "ExtendSelectEnd", 2), KeyStroke.getKeyStroke(35, 1), 0);
  208.       this.list.registerKeyboardAction(new PageUpAction(this, "SelectPageUp", 1), KeyStroke.getKeyStroke(33, 0), 0);
  209.       this.list.registerKeyboardAction(new PageUpAction(this, "ExtendSelectPageUp", 2), KeyStroke.getKeyStroke(33, 1), 0);
  210.       this.list.registerKeyboardAction(new PageDownAction(this, "SelectPageDown", 1), KeyStroke.getKeyStroke(34, 0), 0);
  211.       this.list.registerKeyboardAction(new PageDownAction(this, "ExtendSelectPageDown", 2), KeyStroke.getKeyStroke(34, 1), 0);
  212.       SelectAllAction var1 = new SelectAllAction(this, (1)null, "SelectAll");
  213.       this.list.registerKeyboardAction(var1, KeyStroke.getKeyStroke(65, 2), 0);
  214.       this.list.registerKeyboardAction(var1, KeyStroke.getKeyStroke(47, 2), 0);
  215.       this.list.registerKeyboardAction(new ClearSelectionAction(this, (1)null, "ClearSelection"), KeyStroke.getKeyStroke(92, 2), 0);
  216.    }
  217.  
  218.    protected void installListeners() {
  219.       this.focusListener = this.createFocusListener();
  220.       this.mouseInputListener = this.createMouseInputListener();
  221.       this.propertyChangeListener = this.createPropertyChangeListener();
  222.       this.listSelectionListener = this.createListSelectionListener();
  223.       this.listDataListener = this.createListDataListener();
  224.       this.list.addFocusListener(this.focusListener);
  225.       this.list.addMouseListener(this.mouseInputListener);
  226.       this.list.addMouseMotionListener(this.mouseInputListener);
  227.       this.list.addPropertyChangeListener(this.propertyChangeListener);
  228.       ListModel var1 = this.list.getModel();
  229.       if (var1 != null) {
  230.          var1.addListDataListener(this.listDataListener);
  231.       }
  232.  
  233.       ListSelectionModel var2 = this.list.getSelectionModel();
  234.       if (var2 != null) {
  235.          var2.addListSelectionListener(this.listSelectionListener);
  236.       }
  237.  
  238.    }
  239.  
  240.    public void installUI(JComponent var1) {
  241.       this.list = (JList)var1;
  242.       this.rendererPane = new CellRendererPane();
  243.       this.list.add(this.rendererPane);
  244.       this.installDefaults();
  245.       this.installListeners();
  246.       this.installKeyboardActions();
  247.    }
  248.  
  249.    public int locationToIndex(JList var1, Point var2) {
  250.       this.maybeUpdateLayoutState();
  251.       return this.convertYToRow(var2.y);
  252.    }
  253.  
  254.    protected void maybeUpdateLayoutState() {
  255.       if (this.updateLayoutStateNeeded != 0) {
  256.          this.updateLayoutState();
  257.          this.updateLayoutStateNeeded = 0;
  258.       }
  259.  
  260.    }
  261.  
  262.    public void paint(Graphics var1, JComponent var2) {
  263.       this.maybeUpdateLayoutState();
  264.       ListCellRenderer var3 = this.list.getCellRenderer();
  265.       ListModel var4 = this.list.getModel();
  266.       ListSelectionModel var5 = this.list.getSelectionModel();
  267.       if (var3 != null && var4.getSize() != 0) {
  268.          Rectangle var6 = var1.getClipBounds();
  269.          int var7 = this.convertYToRow(var6.y);
  270.          int var8 = this.convertYToRow(var6.y + var6.height - 1);
  271.          if (var7 == -1) {
  272.             var7 = 0;
  273.          }
  274.  
  275.          if (var8 == -1) {
  276.             var8 = var4.getSize() - 1;
  277.          }
  278.  
  279.          Rectangle var9 = this.getCellBounds(this.list, var7, var7);
  280.          if (var9 != null) {
  281.             int var10 = this.list.getLeadSelectionIndex();
  282.  
  283.             for(int var11 = var7; var11 <= var8; ++var11) {
  284.                var9.height = this.getRowHeight(var11);
  285.                var1.setClip(var9.x, var9.y, var9.width, var9.height);
  286.                var1.clipRect(var6.x, var6.y, var6.width, var6.height);
  287.                this.paintCell(var1, var11, var9, var3, var4, var5, var10);
  288.                var9.y += var9.height;
  289.             }
  290.  
  291.          }
  292.       }
  293.    }
  294.  
  295.    protected void paintCell(Graphics var1, int var2, Rectangle var3, ListCellRenderer var4, ListModel var5, ListSelectionModel var6, int var7) {
  296.       Object var8 = var5.getElementAt(var2);
  297.       boolean var9 = this.list.hasFocus() && var2 == var7;
  298.       boolean var10 = var6.isSelectedIndex(var2);
  299.       Component var11 = var4.getListCellRendererComponent(this.list, var8, var2, var10, var9);
  300.       int var12 = var3.x;
  301.       int var13 = var3.y;
  302.       int var14 = var3.width;
  303.       int var15 = var3.height;
  304.       this.rendererPane.paintComponent(var1, var11, this.list, var12, var13, var14, var15, true);
  305.    }
  306.  
  307.    private void redrawList() {
  308.       this.list.revalidate();
  309.       this.list.repaint();
  310.    }
  311.  
  312.    protected void selectNextIndex() {
  313.       int var1 = this.list.getSelectedIndex();
  314.       if (var1 + 1 < this.list.getModel().getSize()) {
  315.          ++var1;
  316.          this.list.setSelectedIndex(var1);
  317.          this.list.ensureIndexIsVisible(var1);
  318.       }
  319.  
  320.    }
  321.  
  322.    protected void selectPreviousIndex() {
  323.       int var1 = this.list.getSelectedIndex();
  324.       if (var1 > 0) {
  325.          --var1;
  326.          this.list.setSelectedIndex(var1);
  327.          this.list.ensureIndexIsVisible(var1);
  328.       }
  329.  
  330.    }
  331.  
  332.    protected void uninstallDefaults() {
  333.       if (this.list.getCellRenderer() instanceof UIResource) {
  334.          this.list.setCellRenderer((ListCellRenderer)null);
  335.       }
  336.  
  337.    }
  338.  
  339.    protected void uninstallKeyboardActions() {
  340.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(38, 0));
  341.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(38, 1));
  342.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(40, 0));
  343.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(40, 1));
  344.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(36, 0));
  345.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(36, 1));
  346.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(35, 0));
  347.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(35, 1));
  348.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(33, 0));
  349.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(33, 1));
  350.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(34, 0));
  351.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(34, 1));
  352.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(65, 2));
  353.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(47, 2));
  354.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(92, 2));
  355.    }
  356.  
  357.    protected void uninstallListeners() {
  358.       this.list.removeFocusListener(this.focusListener);
  359.       this.list.removeMouseListener(this.mouseInputListener);
  360.       this.list.removeMouseMotionListener(this.mouseInputListener);
  361.       this.list.removePropertyChangeListener(this.propertyChangeListener);
  362.       ListModel var1 = this.list.getModel();
  363.       if (var1 != null) {
  364.          var1.removeListDataListener(this.listDataListener);
  365.       }
  366.  
  367.       ListSelectionModel var2 = this.list.getSelectionModel();
  368.       if (var2 != null) {
  369.          var2.removeListSelectionListener(this.listSelectionListener);
  370.       }
  371.  
  372.       this.focusListener = null;
  373.       this.mouseInputListener = null;
  374.       this.listSelectionListener = null;
  375.       this.listDataListener = null;
  376.       this.propertyChangeListener = null;
  377.    }
  378.  
  379.    public void uninstallUI(JComponent var1) {
  380.       this.uninstallDefaults();
  381.       this.uninstallListeners();
  382.       this.uninstallKeyboardActions();
  383.       this.cellWidth = this.cellHeight = -1;
  384.       this.cellHeights = null;
  385.       this.list.remove(this.rendererPane);
  386.       this.rendererPane = null;
  387.       this.list = null;
  388.    }
  389.  
  390.    protected void updateLayoutState() {
  391.       int var1 = this.list.getFixedCellHeight();
  392.       int var2 = this.list.getFixedCellWidth();
  393.       this.cellWidth = var2 != -1 ? var2 : -1;
  394.       if (var1 != -1) {
  395.          this.cellHeight = var1;
  396.          this.cellHeights = null;
  397.       } else {
  398.          this.cellHeight = -1;
  399.          this.cellHeights = new int[this.list.getModel().getSize()];
  400.       }
  401.  
  402.       if (var2 == -1 || var1 == -1) {
  403.          ListModel var3 = this.list.getModel();
  404.          int var4 = var3.getSize();
  405.          ListCellRenderer var5 = this.list.getCellRenderer();
  406.          if (var5 != null) {
  407.             for(int var6 = 0; var6 < var4; ++var6) {
  408.                Object var7 = var3.getElementAt(var6);
  409.                Component var8 = var5.getListCellRendererComponent(this.list, var7, var6, false, false);
  410.                this.rendererPane.add(var8);
  411.                Dimension var9 = var8.getPreferredSize();
  412.                if (var2 == -1) {
  413.                   this.cellWidth = Math.max(var9.width, this.cellWidth);
  414.                }
  415.  
  416.                if (var1 == -1) {
  417.                   this.cellHeights[var6] = var9.height;
  418.                }
  419.             }
  420.          } else {
  421.             if (this.cellWidth == -1) {
  422.                this.cellWidth = 0;
  423.             }
  424.  
  425.             for(int var10 = 0; var10 < var4; ++var10) {
  426.                this.cellHeights[var10] = 0;
  427.             }
  428.          }
  429.       }
  430.  
  431.       this.list.invalidate();
  432.    }
  433. }
  434.