home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / ListPanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  15.1 KB  |  441 lines

  1. /*
  2.  * @(#)ListPanel.java    1.8 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.text.*;
  17.  
  18. import java.awt.*;
  19. import java.awt.event.*;
  20. import java.util.*;
  21.  
  22.  
  23. /**
  24.  * ListBox!
  25.  *
  26.  * @version 1.8 08/26/98
  27.  * @author Jeff Dinkins
  28.  */
  29. public class ListPanel extends JPanel
  30. {
  31.     int fastfoodIndex;
  32.     int dessertIndex;
  33.     int fruitIndex;
  34.     int veggieIndex;
  35.  
  36.     boolean fastfoodShown;
  37.     boolean dessertShown;
  38.     boolean fruitShown;
  39.     boolean veggieShown;
  40.  
  41.     // Fast Food
  42.     public ImageIcon burger    = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/burger.gif","burger");
  43.     public ImageIcon fries     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/fries.gif","fries");
  44.     public ImageIcon softdrink = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/softdrink.gif","soft drink");
  45.     public ImageIcon hotdog    = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/hotdog.gif","hot dog");
  46.     public ImageIcon pizza     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/pizza.gif","pizza");
  47.  
  48.     // Dessert
  49.     public ImageIcon icecream = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/icecream.gif","ice cream");
  50.     public ImageIcon pie      = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/pie.gif","pie");
  51.     public ImageIcon cake     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/cake.gif","cake");
  52.     public ImageIcon donut    = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/donut.gif","donut");
  53.     public ImageIcon treat    = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/treat.gif","treat");
  54.  
  55.     // Fruit
  56.     public ImageIcon grapes      = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/grapes.gif","grapes");
  57.     public ImageIcon banana      = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/banana.gif","banana");
  58.     public ImageIcon watermelon  = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/watermelon.gif","watermelon");
  59.     public ImageIcon cantaloupe  = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/cantaloupe.gif","cantaloupe");
  60.     public ImageIcon peach       = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/peach.gif","peach");
  61.  
  62.     // Veggie
  63.     public ImageIcon broccoli = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/broccoli.gif","broccoli");
  64.     public ImageIcon carrot   = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/carrot.gif","carrot");
  65.     public ImageIcon peas     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/peas.gif","peas");
  66.     public ImageIcon corn     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/corn.gif","corn");
  67.     public ImageIcon radish   = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/radish.gif","radish");
  68.  
  69.  
  70.     // The Frame
  71.     SwingSet swing;
  72.     JList listBox;
  73.     JScrollPane scrollPane;
  74.  
  75.     DefaultListModel model = new DefaultListModel();
  76.     JLabel priceLabel;
  77.     int listPrice = 0;
  78.  
  79.     JButton reset;
  80.     JButton purchase;
  81.  
  82.     JRadioButton dessertRadioButton;
  83.     JRadioButton veggieRadioButton;
  84.     JRadioButton fruitRadioButton;
  85.     JRadioButton fastfoodRadioButton;
  86.  
  87.     JCheckBox dessertCheckbox;
  88.     JCheckBox veggieCheckbox;
  89.     JCheckBox fruitCheckbox;
  90.     JCheckBox fastfoodCheckbox;
  91.  
  92.     public ListPanel(SwingSet swing) {
  93.     this.swing = swing;
  94.  
  95.     setBorder(swing.emptyBorder5);
  96.     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  97.  
  98.     // create the list
  99.     for(int i = 0; i < ITEMS; i++) {
  100.             model.addElement(new Integer(i));
  101.         }
  102.  
  103.     listBox = new JList(model) {
  104.         public Dimension getMaximumSize() {
  105.         return new Dimension(400, super.getMaximumSize().height);
  106.         }
  107.     };
  108.         listBox.setCellRenderer(new TestCellRenderer(listBox));
  109.  
  110.     // Create the controls
  111.     JPanel controlPanel = new JPanel() {
  112.         public Dimension getMaximumSize() {
  113.         return new Dimension(300, super.getMaximumSize().height);
  114.         }
  115.     };
  116.     controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
  117.     controlPanel.setBorder(swing.loweredBorder);
  118.     controlPanel.setAlignmentY(TOP_ALIGNMENT);
  119.  
  120.     // List operations
  121.  
  122.     JPanel pricePanel = swing.createHorizontalPanel(false);
  123.     pricePanel.setAlignmentY(TOP_ALIGNMENT);
  124.     pricePanel.setAlignmentX(LEFT_ALIGNMENT);
  125.     controlPanel.add(pricePanel);
  126.     purchase = new JButton("Purchase");
  127.     purchase.setToolTipText("Adds the selected item(s) to your grocery bill.");
  128.     pricePanel.add(purchase);
  129.     pricePanel.add(Box.createRigidArea(swing.hpad10));
  130.  
  131.     priceLabel = new JLabel("Total:                           ");
  132.     pricePanel.add(priceLabel);
  133.  
  134.     controlPanel.add(Box.createRigidArea(swing.vpad20));
  135.     JLabel l = new JLabel("Jump To:");
  136.     l.setFont(swing.boldFont);
  137.     controlPanel.add(l);
  138.     ButtonGroup group = new ButtonGroup();
  139.  
  140.     fastfoodRadioButton = new JRadioButton("Fast Food");
  141.     fastfoodRadioButton.setToolTipText("Calls list.ensureVisible() to jump to the items.");
  142.     group.add(fastfoodRadioButton);
  143.     controlPanel.add(fastfoodRadioButton);
  144.  
  145.     dessertRadioButton = new JRadioButton("Desserts");
  146.     dessertRadioButton.setToolTipText("Calls list.ensureVisible() to jump to the items.");
  147.     group.add(dessertRadioButton);
  148.     controlPanel.add(dessertRadioButton);
  149.  
  150.     fruitRadioButton = new JRadioButton("Fruits");
  151.     fruitRadioButton.setToolTipText("Calls list.ensureVisible() to jump to the items.");
  152.     group.add(fruitRadioButton);
  153.     controlPanel.add(fruitRadioButton);
  154.  
  155.     veggieRadioButton = new JRadioButton("Vegetables");
  156.     veggieRadioButton.setToolTipText("Calls list.ensureVisible(index) to jump to the items.");
  157.     group.add(veggieRadioButton);
  158.     controlPanel.add(veggieRadioButton);
  159.  
  160.     controlPanel.add(Box.createRigidArea(swing.vpad20));
  161.     l = new JLabel("Show:");
  162.     l.setFont(swing.boldFont);
  163.     controlPanel.add(l);
  164.     fastfoodCheckbox = new JCheckBox("Fast Food");
  165.     fastfoodCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
  166.     fastfoodCheckbox.setSelected(true);
  167.     controlPanel.add(fastfoodCheckbox);
  168.  
  169.     dessertCheckbox = new JCheckBox("Desserts");
  170.     dessertCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
  171.     dessertCheckbox.setSelected(true);
  172.     controlPanel.add(dessertCheckbox);
  173.  
  174.     fruitCheckbox = new JCheckBox("Fruits");
  175.     fruitCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
  176.     fruitCheckbox.setSelected(true);
  177.     controlPanel.add(fruitCheckbox);
  178.  
  179.     veggieCheckbox = new JCheckBox("Vegetables");
  180.     veggieCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
  181.     veggieCheckbox.setSelected(true);
  182.     controlPanel.add(veggieCheckbox);
  183.  
  184.     controlPanel.add(Box.createGlue());
  185.     reset = new JButton("Reset");
  186.     reset.setToolTipText("Resets the state of the demo.");
  187.     controlPanel.add(reset);
  188.  
  189.  
  190.     scrollPane = new JScrollPane(listBox);
  191.     scrollPane.setAlignmentX(LEFT_ALIGNMENT);
  192.     scrollPane.setAlignmentY(TOP_ALIGNMENT);
  193.     add(scrollPane);
  194.     add(Box.createRigidArea(swing.hpad10));
  195.      add(controlPanel);
  196.  
  197.     ActionListener purchaseListener = new ActionListener() {
  198.             public void actionPerformed(ActionEvent e) {
  199.         int first = listBox.getMinSelectionIndex();
  200.                 int last = listBox.getMaxSelectionIndex();
  201.         if(first < 0) {
  202.             return;
  203.         }
  204.         for(int i = first; i <= last; i++) {
  205.             Integer item = (Integer) model.getElementAt(i);
  206.             listPrice += price[item.intValue()];
  207.         }
  208.         priceLabel.setText("Total: $" + ((double) listPrice)/100.0);
  209.         priceLabel.repaint();
  210.         }
  211.     };
  212.     purchase.addActionListener(purchaseListener);
  213.  
  214.     ActionListener showListener = new ActionListener() {
  215.             public void actionPerformed(ActionEvent e) {
  216.         JCheckBox cb = (JCheckBox) e.getSource();
  217.         String label = cb.getText();
  218.         if(!cb.isSelected()) {
  219.  
  220.             if(label.equals("Fast Food")) {
  221.             fastfoodShown = false;
  222.             for(int i = fastfoodIndex; i < fastfoodIndex+5; i++) {
  223.                 model.removeElementAt(fastfoodIndex);
  224.             }
  225.             fastfoodRadioButton.setEnabled(false);
  226.             dessertIndex -= 5;
  227.             fruitIndex -= 5;
  228.             veggieIndex -= 5;
  229.             scrollPane.validate();
  230.             } else if(label.equals("Desserts")) {
  231.             for(int i = dessertIndex; i < dessertIndex+5; i++) {
  232.                 model.removeElementAt(dessertIndex);
  233.             }
  234.             dessertRadioButton.setEnabled(false);
  235.             fruitIndex -= 5;
  236.             veggieIndex -= 5;
  237.             scrollPane.validate();
  238.             } else if(label.equals("Fruits")) {
  239.             for(int i = fruitIndex; i < fruitIndex+5; i++) {
  240.                 model.removeElementAt(fruitIndex);
  241.             }
  242.             fruitRadioButton.setEnabled(false);
  243.             veggieIndex -= 5;
  244.             scrollPane.validate();
  245.             } else if(label.equals("Vegetables")) {
  246.             for(int i = veggieIndex; i < veggieIndex+5; i++) {
  247.                 model.removeElementAt(veggieIndex);
  248.             }
  249.             veggieRadioButton.setEnabled(false);
  250.             scrollPane.validate();
  251.             }
  252.             if(model.getSize() < 1)
  253.                 listBox.getParent().repaint();
  254.         } else {
  255.             if(label.equals("Fast Food")) {
  256.             model.insertElementAt(new Integer(4), 0);
  257.             model.insertElementAt(new Integer(3), 0);
  258.             model.insertElementAt(new Integer(2), 0);
  259.             model.insertElementAt(new Integer(1), 0);
  260.             model.insertElementAt(new Integer(0), 0);
  261.             dessertIndex += 5;
  262.             fruitIndex += 5;
  263.             veggieIndex += 5;
  264.             fastfoodRadioButton.setEnabled(true);
  265.             scrollPane.validate();
  266.             } else if(label.equals("Desserts")) {
  267.             model.insertElementAt(new Integer(9), dessertIndex);
  268.             model.insertElementAt(new Integer(8), dessertIndex);
  269.             model.insertElementAt(new Integer(7), dessertIndex);
  270.             model.insertElementAt(new Integer(6), dessertIndex);
  271.             model.insertElementAt(new Integer(5), dessertIndex);
  272.             fruitIndex += 5;
  273.             veggieIndex += 5;
  274.             dessertRadioButton.setEnabled(true);
  275.             scrollPane.validate();
  276.             } else if(label.equals("Fruits")) {
  277.             model.insertElementAt(new Integer(14), fruitIndex);
  278.             model.insertElementAt(new Integer(13), fruitIndex);
  279.             model.insertElementAt(new Integer(12), fruitIndex);
  280.             model.insertElementAt(new Integer(11), fruitIndex);
  281.             model.insertElementAt(new Integer(10), fruitIndex);
  282.             veggieIndex += 5;
  283.             fruitRadioButton.setEnabled(true);
  284.             scrollPane.validate();
  285.             } else if(label.equals("Vegetables")) {
  286.             model.insertElementAt(new Integer(19), veggieIndex);
  287.             model.insertElementAt(new Integer(18), veggieIndex);
  288.             model.insertElementAt(new Integer(17), veggieIndex);
  289.             model.insertElementAt(new Integer(16), veggieIndex);
  290.             model.insertElementAt(new Integer(15), veggieIndex);
  291.             veggieRadioButton.setEnabled(true);
  292.             scrollPane.validate();
  293.             }
  294.         }
  295.         }
  296.     };
  297.     fruitCheckbox.addActionListener(showListener);
  298.     veggieCheckbox.addActionListener(showListener);
  299.     dessertCheckbox.addActionListener(showListener);
  300.     fastfoodCheckbox.addActionListener(showListener);
  301.  
  302.     ActionListener jumpListener = new ActionListener() {
  303.             public void actionPerformed(ActionEvent e) {
  304.         JRadioButton rb = (JRadioButton) e.getSource();
  305.         if(rb.isSelected()) {
  306.             String label = rb.getText();
  307.             if(label.equals("Fruits")) {
  308.             listBox.ensureIndexIsVisible(fruitIndex+5);
  309.             listBox.ensureIndexIsVisible(fruitIndex);
  310.             } else if(label.equals("Desserts")) {
  311.             listBox.ensureIndexIsVisible(dessertIndex+5);
  312.             listBox.ensureIndexIsVisible(dessertIndex);
  313.             } else if(label.equals("Vegetables")) {
  314.             listBox.ensureIndexIsVisible(veggieIndex+5);
  315.             listBox.ensureIndexIsVisible(veggieIndex);
  316.             } else if(label.equals("Fast Food")) {
  317.             listBox.ensureIndexIsVisible(fastfoodIndex+5);
  318.             listBox.ensureIndexIsVisible(fastfoodIndex);
  319.             }
  320.         }
  321.         }
  322.     };
  323.     fruitRadioButton.addActionListener(jumpListener);
  324.     veggieRadioButton.addActionListener(jumpListener);
  325.     dessertRadioButton.addActionListener(jumpListener);
  326.     fastfoodRadioButton.addActionListener(jumpListener);
  327.  
  328.     ActionListener resetListener = new ActionListener() {
  329.             public void actionPerformed(ActionEvent e) {
  330.         resetAll();
  331.             }
  332.     };
  333.     reset.addActionListener(resetListener);
  334.     }
  335.  
  336.     public void resetAll() {
  337.     model.removeAllElements();
  338.  
  339.     fastfoodCheckbox.setSelected(true);
  340.     fruitCheckbox.setSelected(true);
  341.     veggieCheckbox.setSelected(true);
  342.     dessertCheckbox.setSelected(true);
  343.  
  344.     fastfoodRadioButton.setEnabled(true);
  345.     fruitRadioButton.setEnabled(true);
  346.     veggieRadioButton.setEnabled(true);
  347.     dessertRadioButton.setEnabled(true);
  348.  
  349.     fastfoodRadioButton.setSelected(true);
  350.  
  351.     for(int i = 0; i < ITEMS; i++) {
  352.         model.addElement(new Integer(i));
  353.     }
  354.  
  355.     fastfoodShown = true;
  356.     dessertShown  = true;
  357.     fruitShown    = true;
  358.     veggieShown   = true;
  359.  
  360.     fastfoodIndex = 0;
  361.     dessertIndex  = 5;
  362.     fruitIndex    = 10;
  363.     veggieIndex   = 15;
  364.  
  365.     listPrice = 0;
  366.  
  367.     priceLabel.setText("Total:  $0.00   ");
  368.     listBox.ensureIndexIsVisible(fastfoodIndex);
  369.  
  370.     scrollPane.validate();
  371.     }
  372.  
  373.     static int ITEMS = 20;
  374.     ImageIcon images[];
  375.     String     desc[];
  376.     int        price[];
  377.  
  378.     class TestCellRenderer extends DefaultListCellRenderer
  379.     {
  380.     TestCellRenderer(JList listBox) {
  381.         super();
  382.  
  383.         images = new ImageIcon[ITEMS];
  384.         desc = new String[ITEMS];
  385.         price = new int[ITEMS];
  386.  
  387.         int i = 0;
  388.         // 5 - FastFood
  389.         images[i] = burger;      price[i] = 199;   desc[i++] = "Burger";
  390.         images[i] = fries;       price[i] = 99;    desc[i++] = "Fries";
  391.         images[i] = softdrink;   price[i] = 89;    desc[i++] = "Cola";
  392.         images[i] = pizza;       price[i] = 399;   desc[i++] = "Pizza";
  393.         images[i] = hotdog;      price[i] = 299;   desc[i++] = "Hotdog";
  394.  
  395.         // 10 - Dessert
  396.         images[i] = icecream;    price[i] = 199;   desc[i++] = "Ice Cream";
  397.         images[i] = pie;         price[i] = 249;   desc[i++] = "Cherry Pie";
  398.         images[i] = cake;        price[i] = 355;   desc[i++] = "Cake";
  399.         images[i] = donut;       price[i] = 25;    desc[i++] = "Donut";
  400.         images[i] = treat;       price[i] = 52;    desc[i++] = "Fruit Pop";
  401.  
  402.         // 15 Fruit
  403.         images[i] = grapes;      price[i] = 99;    desc[i++] = "Grapes";
  404.         images[i] = watermelon;  price[i] = 59;    desc[i++] = "Watermelon";
  405.         images[i] = peach;       price[i] = 35;    desc[i++] = "Peach";
  406.         images[i] = cantaloupe;  price[i] = 85;    desc[i++] = "Cantaloupe";
  407.         images[i] = banana;      price[i] = 25;    desc[i++] = "Banana";
  408.  
  409.         // 20 - Veggies
  410.         images[i] = broccoli;    price[i] = 99;    desc[i++] = "Broccoli";
  411.         images[i] = corn;        price[i] = 65;    desc[i++] = "Corn";
  412.         images[i] = carrot;      price[i] = 25;    desc[i++] = "Carrot";
  413.         images[i] = peas;        price[i] =  3;    desc[i++] = "Peas";
  414.         images[i] = radish;      price[i] = 45;    desc[i++] = "Radish";
  415.     }
  416.  
  417.  
  418.     public Component getListCellRendererComponent(
  419.             JList list,
  420.             Object value,
  421.             int modelIndex,
  422.             boolean isSelected,
  423.             boolean cellHasFocus)
  424.         {
  425.         int index = ((Integer)value).intValue();
  426.         String text;
  427.         if(isSelected) {
  428.         text = "  " + desc[index] + "    $" + ((double)price[index])/100.0;
  429.         }
  430.         else {
  431.         text = "  " + desc[index];
  432.         }
  433.         setIcon(images[index]);
  434.  
  435.         return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
  436.     }
  437.     }
  438. }
  439.  
  440.  
  441.