home *** CD-ROM | disk | FTP | other *** search
/ PC World 1998 October / PCWorld_1998-10_cd.bin / software / prehled / inprise / JSAMPLES.Z / ComboBoxPanel.java < prev    next >
Text File  |  1998-05-08  |  11KB  |  322 lines

  1. /*
  2.  * @(#)ComboBoxPanel.java    1.4 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.event.*;
  23. import com.sun.java.swing.text.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.basic.BasicComboBoxUI;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import java.util.*;
  29. import com.sun.java.swing.plaf.*;
  30. import com.sun.java.swing.tree.*;
  31. import com.sun.java.swing.plaf.basic.BasicLookAndFeel;
  32. import com.sun.java.accessibility.*;
  33.  
  34. /**
  35.  * SwingSet panel for JComboBox
  36.  *
  37.  * @version 1.4 02/02/98
  38.  * @author Arnaud Weber
  39.  * @author Peter Korn (accessibility support)
  40.  */
  41. public class ComboBoxPanel extends JPanel {
  42.     // The Frame
  43.     SwingSet swing;
  44.     JComboBox  months;
  45.     JComboBox  days;
  46.     JComboBox  cb;
  47.     JComboBox  cb1;
  48.     JComboBox  custom;
  49.     JComboBox  treeComboBox;
  50.  
  51.     DefaultListModel model = new DefaultListModel();
  52.  
  53.     public ComboBoxPanel(SwingSet swing) {
  54.       JPanel tp;
  55.       this.swing = swing;
  56.       this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
  57.       setBorder(swing.emptyBorder5);
  58.       
  59.       this.add(Box.createRigidArea(new Dimension(1,50)));
  60.       JPanel panel = new JPanel(false);
  61.       panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
  62.       add(panel);
  63.       // Classic combo box
  64.       tp = new JPanel();
  65.       tp.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
  66.       tp.setBorder(BorderFactory.createTitledBorder("Classic ComboBox"));
  67. //      tp.setTitle("Classic ComboBox");
  68.       tp.setLayout(new BoxLayout(tp,BoxLayout.X_AXIS));
  69.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  70.       months = new JComboBox();
  71.       months.addItem("January");
  72.       months.addItem("February");
  73.       months.addItem("March");
  74.       months.addItem("April");
  75.       months.addItem("May");
  76.       months.addItem("June");
  77.       months.addItem("July");
  78.       months.addItem("August");
  79.       months.addItem("September");
  80.       months.addItem("October");
  81.       months.addItem("November");
  82.       months.addItem("December");
  83.       months.getAccessibleContext().setAccessibleName("Months");
  84.       months.getAccessibleContext().setAccessibleDescription("Choose a month of the year");
  85.       tp.add(months);
  86.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  87.       days = new JComboBox();
  88.       days.addItem("Monday");
  89.       days.addItem("Tuesday");
  90.       days.addItem("Wednesday");
  91.       days.addItem("Thursday");
  92.       days.addItem("Friday");
  93.       days.addItem("Saturday");
  94.       days.addItem("Sunday");
  95.       days.getAccessibleContext().setAccessibleName("Days");
  96.       days.getAccessibleContext().setAccessibleDescription("Choose a day of the week");
  97.       tp.add(days);
  98.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  99.       panel.add(tp);
  100.  
  101.       // Editable combo box
  102.  
  103.       add(panel);
  104.       tp = new JPanel();
  105.       tp.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
  106.       tp.setBorder(BorderFactory.createTitledBorder("Editable ComboBox"));
  107.       tp.setLayout(new BoxLayout(tp,BoxLayout.X_AXIS));
  108.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  109.       cb = new JComboBox();
  110.       cb.setEditable(true);
  111.       cb.addItem("0");
  112.       cb.addItem("10");
  113.       cb.addItem("20");
  114.       cb.addItem("30");
  115.       cb.addItem("40");
  116.       cb.addItem("50");
  117.       cb.addItem("60");
  118.       cb.addItem("70");
  119.       cb.addItem("80");
  120.       cb.addItem("90");
  121.       cb.addItem("100");
  122.       cb.addItem("More");
  123.       cb.getAccessibleContext().setAccessibleName("Numbers");
  124.       cb.getAccessibleContext().setAccessibleDescription("Demonstration editable ComboBox with numbers 0-100");
  125.       cb.setSelectedItem("50");
  126.       tp.add(cb);
  127.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  128.  
  129.       cb1 = new JComboBox();
  130.       cb1.setEditable(true);
  131.       cb1.addItem("0");
  132.       cb1.addItem(".25");
  133.       cb1.addItem(".5");
  134.       cb1.addItem(".75");
  135.       cb1.addItem("1.0");
  136.       cb1.getAccessibleContext().setAccessibleName("Small numbers");
  137.       cb1.getAccessibleContext().setAccessibleDescription("Demonstration editable ComboBox with numbers 0-1");
  138.       cb1.setSelectedItem(".5");
  139.       tp.add(cb1);
  140.  
  141.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  142.       panel.add(tp);
  143.  
  144.       // Custom combobox
  145.       panel = new JPanel(false);
  146.       panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
  147.       add(panel);
  148.       tp = new JPanel();
  149.       tp.setMaximumSize(new Dimension(Short.MAX_VALUE,200));
  150.       tp.setBorder(BorderFactory.createTitledBorder("Custom ComboBox"));
  151. //      tp.setTitle("Custom ComboBox");
  152.       tp.setLayout(new BoxLayout(tp,BoxLayout.X_AXIS));
  153.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  154.       custom = new CustomComboBox(new CustomComboBoxModel());
  155.       custom.setRenderer(new TestCellRenderer(custom));
  156.       ((CustomComboBox)custom).setCanChangeRenderer(false);
  157.       custom.setSelectedIndex(0);
  158.       custom.setMaximumRowCount(4);
  159.       custom.getAccessibleContext().setAccessibleName("Custom ComboBox");
  160.       custom.getAccessibleContext().setAccessibleDescription("Sample custom ComboBox with icons in them, one of which changes when selected");
  161.       tp.add(custom);
  162.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  163.  
  164.       DefaultMutableTreeNode swingNode = new DefaultMutableTreeNode("Swing");
  165.       DefaultMutableTreeNode spec  = new DefaultMutableTreeNode("spec");
  166.       DefaultMutableTreeNode api   = new DefaultMutableTreeNode("api");
  167.  
  168.       swingNode.add(spec);
  169.       swingNode.add(api);
  170.  
  171.       api.add(new DefaultMutableTreeNode("JComponent"));
  172.       api.add(new DefaultMutableTreeNode("JTable"));
  173.       api.add(new DefaultMutableTreeNode("JTree"));
  174.       api.add(new DefaultMutableTreeNode("JComboBox"));
  175.       api.add(new DefaultMutableTreeNode("JTextComponent"));
  176.  
  177.       spec.add(new DefaultMutableTreeNode("JComponent"));
  178.       spec.add(new DefaultMutableTreeNode("JTable"));
  179.       spec.add(new DefaultMutableTreeNode("JTree"));
  180.       spec.add(new DefaultMutableTreeNode("JComboBox"));
  181.       spec.add(new DefaultMutableTreeNode("JTextComponent"));
  182.       
  183.       treeComboBox = new TreeCombo(new DefaultTreeModel(swingNode));
  184.       treeComboBox.getAccessibleContext().setAccessibleName("Swing specs and APIs");
  185.       treeComboBox.getAccessibleContext().setAccessibleDescription("Sample custom ComboBox with a tree heirarchy");
  186.       tp.add(treeComboBox);
  187.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  188.       panel.add(tp);
  189.     }
  190.  
  191.     class CustomComboBoxModel extends AbstractListModel implements ComboBoxModel {
  192.       Object currentValue;
  193.       ImageIcon images[];
  194.       ImageIcon images_down[];      
  195.       Hashtable cache[];
  196.  
  197.       public CustomComboBoxModel() {
  198.     images = new ImageIcon[5];
  199.     images_down = new ImageIcon[5];
  200.     images[0] = SwingSet.sharedInstance().loadImageIcon("images/list/a1.gif","blue profile of robot");
  201.         images_down[0] = SwingSet.sharedInstance().loadImageIcon("images/list/a1d.gif","greyed out blue profile of robot");
  202.     images[1] = SwingSet.sharedInstance().loadImageIcon("images/list/a2.gif","pinkish profile of robot");
  203.         images_down[1] = SwingSet.sharedInstance().loadImageIcon("images/list/a2d.gif","greyed out pinkish profile of robot");
  204.     images[2] = SwingSet.sharedInstance().loadImageIcon("images/list/a3.gif","yellow profile of robot");
  205.         images_down[2] = SwingSet.sharedInstance().loadImageIcon("images/list/a3d.gif","greyed out yellow profile of robot");
  206.     images[3] = SwingSet.sharedInstance().loadImageIcon("images/list/a4.gif","green profile of robot");
  207.         images_down[3] = SwingSet.sharedInstance().loadImageIcon("images/list/a4d.gif","greyed out green profile of robot");
  208.     images[4] = SwingSet.sharedInstance().loadImageIcon("images/list/a5.gif","profile of robot");
  209.         images_down[4] = SwingSet.sharedInstance().loadImageIcon("images/list/a5d.gif","greyed out profile of robot");
  210.     cache = new Hashtable[getSize()];
  211.       }
  212.  
  213.       public void setSelectedItem(Object anObject) {
  214.     currentValue = anObject;
  215.     fireContentsChanged(this,-1,-1);
  216.       }
  217.       
  218.       public Object getSelectedItem() {
  219.     return currentValue;
  220.       }
  221.  
  222.       public int getSize() {
  223.     return 25;
  224.       }
  225.  
  226.       public Object getElementAt(int index) {
  227.     if(cache[index] != null)
  228.       return cache[index];
  229.     else {
  230.       Hashtable result = new Hashtable();
  231.         if(index != 24) {
  232.           result.put("title","Hello I'm the choice " + index);
  233.           result.put("image",images[index % 5]);
  234.           result.put("Himage",images_down[index % 5]);
  235.         } else {
  236.           result.put("title","Hello I'm Duke");
  237.           result.put("image",swing.dukeSnooze);
  238.           result.put("Himage",swing.dukeWave);
  239.         }
  240.       cache[index] = result;
  241.       return result;
  242.     }
  243.       }
  244.     }
  245.  
  246.     class TestCellRenderer extends JLabel implements ListCellRenderer   {
  247.     JComboBox combobox;
  248.  
  249.  
  250.       public TestCellRenderer(JComboBox x) {
  251.         this.combobox = x;
  252.         setOpaque(true);
  253.       }
  254.  
  255.       public Component getListCellRendererComponent(
  256.           JList listbox, 
  257.           Object value, 
  258.           int index, 
  259.           boolean isSelected, 
  260.           boolean cellHasFocus) 
  261.       {
  262.     Hashtable h = (Hashtable) value;
  263.         if(UIManager.getLookAndFeel().getName().equals("CDE/Motif")) {
  264.             if(index == -1 )
  265.                 setOpaque(false);
  266.             else
  267.                 setOpaque(true);
  268.         } else 
  269.             setOpaque(true);
  270.  
  271.         if(value == null) {
  272.       setText("");
  273.       setIcon(null);
  274.     } else if(isSelected) {
  275.         setBackground(UIManager.getColor("ComboBox.selectedBackground"));
  276.         setForeground(UIManager.getColor("ComboBox.selectedForeground"));
  277.             setIcon((ImageIcon)h.get("Himage"));
  278.             setText((String)h.get("title"));
  279.     } else {
  280.             setIcon((ImageIcon)h.get("image"));
  281.             setText((String)h.get("title"));
  282.         setBackground(UIManager.getColor("ComboBox.background"));
  283.         setForeground(UIManager.getColor("ComboBox.foreground"));
  284.     }
  285.     return this;
  286.       }
  287.     }
  288.  
  289.  
  290.     public void hideAllPopups() {
  291.     months.getUI().hidePopup();
  292.     days.getUI().hidePopup();
  293.     cb.getUI().hidePopup();
  294.     cb1.getUI().hidePopup();
  295.     custom.getUI().hidePopup();
  296.     treeComboBox.getUI().hidePopup();
  297.     }
  298.  
  299.     static class CustomComboBox extends JComboBox {
  300.         boolean canChangeRenderer = true;
  301.  
  302.         public CustomComboBox(ComboBoxModel m) {
  303.             super(m);
  304.         }
  305.  
  306.         public void setRenderer(ListCellRenderer r) {
  307.             if(canChangeRenderer)
  308.                 super.setRenderer(r);
  309.         }
  310.         
  311.         public void setCanChangeRenderer(boolean f) {
  312.             canChangeRenderer = f;
  313.         }
  314.     }
  315. }
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.