home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / ComboBoxPanel.java < prev    next >
Text File  |  1998-06-30  |  11KB  |  304 lines

  1. /*
  2.  * @(#)ComboBoxPanel.java    1.5 98/03/10
  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.5 03/10/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 JComboBox(new CustomComboBoxModel());
  155.       custom.setRenderer(new TestCellRenderer(custom));
  156.       custom.setSelectedIndex(0);
  157.       custom.setMaximumRowCount(4);
  158.       custom.getAccessibleContext().setAccessibleName("Custom ComboBox");
  159.       custom.getAccessibleContext().setAccessibleDescription("Sample custom ComboBox with icons in them, one of which changes when selected");
  160.       tp.add(custom);
  161.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  162.  
  163.       DefaultMutableTreeNode swingNode = new DefaultMutableTreeNode("Swing");
  164.       DefaultMutableTreeNode spec  = new DefaultMutableTreeNode("spec");
  165.       DefaultMutableTreeNode api   = new DefaultMutableTreeNode("api");
  166.  
  167.       swingNode.add(spec);
  168.       swingNode.add(api);
  169.  
  170.       api.add(new DefaultMutableTreeNode("JComponent"));
  171.       api.add(new DefaultMutableTreeNode("JTable"));
  172.       api.add(new DefaultMutableTreeNode("JTree"));
  173.       api.add(new DefaultMutableTreeNode("JComboBox"));
  174.       api.add(new DefaultMutableTreeNode("JTextComponent"));
  175.  
  176.       spec.add(new DefaultMutableTreeNode("JComponent"));
  177.       spec.add(new DefaultMutableTreeNode("JTable"));
  178.       spec.add(new DefaultMutableTreeNode("JTree"));
  179.       spec.add(new DefaultMutableTreeNode("JComboBox"));
  180.       spec.add(new DefaultMutableTreeNode("JTextComponent"));
  181.       
  182.       treeComboBox = new TreeCombo(new DefaultTreeModel(swingNode));
  183.       treeComboBox.getAccessibleContext().setAccessibleName("Swing specs and APIs");
  184.       treeComboBox.getAccessibleContext().setAccessibleDescription("Sample custom ComboBox with a tree heirarchy");
  185.       tp.add(treeComboBox);
  186.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  187.       panel.add(tp);
  188.     }
  189.  
  190.     class CustomComboBoxModel extends AbstractListModel implements ComboBoxModel {
  191.       Object currentValue;
  192.       ImageIcon images[];
  193.       ImageIcon images_down[];      
  194.       Hashtable cache[];
  195.  
  196.       public CustomComboBoxModel() {
  197.     images = new ImageIcon[5];
  198.     images_down = new ImageIcon[5];
  199.     images[0] = SwingSet.sharedInstance().loadImageIcon("images/list/a1.gif","blue profile of robot");
  200.         images_down[0] = SwingSet.sharedInstance().loadImageIcon("images/list/a1d.gif","greyed out blue profile of robot");
  201.     images[1] = SwingSet.sharedInstance().loadImageIcon("images/list/a2.gif","pinkish profile of robot");
  202.         images_down[1] = SwingSet.sharedInstance().loadImageIcon("images/list/a2d.gif","greyed out pinkish profile of robot");
  203.     images[2] = SwingSet.sharedInstance().loadImageIcon("images/list/a3.gif","yellow profile of robot");
  204.         images_down[2] = SwingSet.sharedInstance().loadImageIcon("images/list/a3d.gif","greyed out yellow profile of robot");
  205.     images[3] = SwingSet.sharedInstance().loadImageIcon("images/list/a4.gif","green profile of robot");
  206.         images_down[3] = SwingSet.sharedInstance().loadImageIcon("images/list/a4d.gif","greyed out green profile of robot");
  207.     images[4] = SwingSet.sharedInstance().loadImageIcon("images/list/a5.gif","profile of robot");
  208.         images_down[4] = SwingSet.sharedInstance().loadImageIcon("images/list/a5d.gif","greyed out profile of robot");
  209.     cache = new Hashtable[getSize()];
  210.       }
  211.  
  212.       public void setSelectedItem(Object anObject) {
  213.     currentValue = anObject;
  214.     fireContentsChanged(this,-1,-1);
  215.       }
  216.       
  217.       public Object getSelectedItem() {
  218.     return currentValue;
  219.       }
  220.  
  221.       public int getSize() {
  222.     return 25;
  223.       }
  224.  
  225.       public Object getElementAt(int index) {
  226.     if(cache[index] != null)
  227.       return cache[index];
  228.     else {
  229.       Hashtable result = new Hashtable();
  230.         if(index != 24) {
  231.           result.put("title","Hello I'm the choice " + index);
  232.           result.put("image",images[index % 5]);
  233.           result.put("Himage",images_down[index % 5]);
  234.         } else {
  235.           result.put("title","Hello I'm Duke");
  236.           result.put("image",swing.dukeSnooze);
  237.           result.put("Himage",swing.dukeWave);
  238.         }
  239.       cache[index] = result;
  240.       return result;
  241.     }
  242.       }
  243.     }
  244.  
  245.     class TestCellRenderer extends JLabel implements ListCellRenderer   {
  246.     JComboBox combobox;
  247.  
  248.  
  249.       public TestCellRenderer(JComboBox x) {
  250.         this.combobox = x;
  251.         setOpaque(true);
  252.       }
  253.  
  254.       public Component getListCellRendererComponent(
  255.           JList listbox, 
  256.           Object value, 
  257.           int index, 
  258.           boolean isSelected, 
  259.           boolean cellHasFocus) 
  260.       {
  261.     Hashtable h = (Hashtable) value;
  262.         if(UIManager.getLookAndFeel().getName().equals("CDE/Motif")) {
  263.             if(index == -1 )
  264.                 setOpaque(false);
  265.             else
  266.                 setOpaque(true);
  267.         } else 
  268.             setOpaque(true);
  269.  
  270.         if(value == null) {
  271.       setText("");
  272.       setIcon(null);
  273.     } else if(isSelected) {
  274.         setBackground(UIManager.getColor("ComboBox.selectedBackground"));
  275.         setForeground(UIManager.getColor("ComboBox.selectedForeground"));
  276.             setIcon((ImageIcon)h.get("Himage"));
  277.             setText((String)h.get("title"));
  278.     } else {
  279.             setIcon((ImageIcon)h.get("image"));
  280.             setText((String)h.get("title"));
  281.         setBackground(UIManager.getColor("ComboBox.background"));
  282.         setForeground(UIManager.getColor("ComboBox.foreground"));
  283.     }
  284.     return this;
  285.       }
  286.     }
  287.  
  288.  
  289.     public void hideAllPopups() {
  290.     months.getUI().hidePopup();
  291.     days.getUI().hidePopup();
  292.     cb.getUI().hidePopup();
  293.     cb1.getUI().hidePopup();
  294.     custom.getUI().hidePopup();
  295.     treeComboBox.getUI().hidePopup();
  296.     }
  297. }
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.