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 / SplitPanePanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  12.1 KB  |  334 lines

  1. /*
  2.  * @(#)SplitPanePanel.java    1.13 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.event.*;
  17. import javax.accessibility.*;
  18.  
  19. import java.awt.*;
  20. import java.awt.event.*;
  21.  
  22. /*
  23.  * An example of using a JSplitPane. The key thing to remember about using
  24.  * a JSplitPane is that it used the minimum size to determine where the
  25.  * components can be resize to! Some of the components in swing will return
  26.  * the current size as the minimum size, meaning JSplitPane will not allow
  27.  * you to resize it. Luckily JComponent has methods for setting the
  28.  * preferred/min sizes.
  29.  *
  30.  * @version 1.13 08/26/98
  31.  * @author Scott Violet
  32.  * @author Peter Korn (accessibility support)
  33.  */
  34. public class SplitPanePanel extends JPanel
  35. {
  36.     /** JSplitPane being shown to the user. */
  37.     protected JSplitPane             splitPane;
  38.     /** Left component being split. */
  39.     protected GridComponent          leftGrid;
  40.     /** Right component being split. */
  41.     protected GridComponent          rightGrid;
  42.     /** Grand puba swingset. */
  43.     protected SwingSet               swing;
  44.  
  45.     public SplitPanePanel(SwingSet swing) {
  46.         super();
  47.         this.swing = swing;
  48.         setDoubleBuffered(true);
  49.         setLayout(new BorderLayout());
  50.         createSplitPane();
  51.         createInformationControls();
  52.     }
  53.  
  54.     /**
  55.      * Creates the JSplitPane.
  56.      */
  57.     protected void createSplitPane() {
  58.         leftGrid = new GridComponent(4);
  59.         leftGrid.setPreferredSize(10);
  60.         rightGrid = new GridComponent(4);
  61.         rightGrid.setPreferredSize(10);
  62.         splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftGrid,
  63.                                    rightGrid);
  64.         splitPane.setContinuousLayout(true);
  65.         splitPane.setPreferredSize(new Dimension(400, 100));
  66.         splitPane.getAccessibleContext().setAccessibleName(
  67.                                                 "Split pane example");
  68.         add(splitPane, BorderLayout.CENTER);
  69.     }
  70.  
  71.     /**
  72.      * Creates controls to alter the JSplitPane.
  73.      */
  74.     protected void createInformationControls() {
  75.         JPanel                wrapper = new JPanel();
  76.         ButtonGroup           group = new ButtonGroup();
  77.         JRadioButton          button;
  78.  
  79.         Box                   buttonWrapper = new Box(BoxLayout.X_AXIS);
  80.  
  81.         wrapper.setLayout(new GridLayout(0, 1));
  82.  
  83.         /* Create a radio button to vertically split the split pane. */
  84.         button = new JRadioButton("Vertically split");
  85.         button.setMnemonic('V');
  86.         button.addActionListener(new ActionListener() {
  87.             public void actionPerformed(ActionEvent e) {
  88.                 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  89.             }
  90.         });
  91.         group.add(button);
  92.         buttonWrapper.add(button);
  93.  
  94.         /* Create a radio button the horizontally split the split pane. */
  95.         button = new JRadioButton("Horizontally split");
  96.         button.setMnemonic('r');
  97.         button.setSelected(true);
  98.         button.addActionListener(new ActionListener() {
  99.             public void actionPerformed(ActionEvent e) {
  100.                 splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  101.             }
  102.         });
  103.         group.add(button);
  104.         buttonWrapper.add(button);
  105.  
  106.         /* Create a check box as to whether or not the split pane continually
  107.            lays out the component when dragging. */
  108.         JCheckBox checkBox = new JCheckBox("Continuous Layout");
  109.         checkBox.setMnemonic('C');
  110.         checkBox.setSelected(true);
  111.  
  112.         checkBox.addChangeListener(new ChangeListener() {
  113.             public void stateChanged(ChangeEvent e) {
  114.                 splitPane.setContinuousLayout(
  115.                                 ((JCheckBox)e.getSource()).isSelected());
  116.             }
  117.         });
  118.         buttonWrapper.add(checkBox);
  119.  
  120.         /* Create a check box as to whether or not the split pane divider
  121.            contains the oneTouchExpandable buttons. */
  122.         checkBox = new JCheckBox("One-Touch expandable");
  123.         checkBox.setMnemonic('O');
  124.         checkBox.setSelected(false);
  125.  
  126.         checkBox.addChangeListener(new ChangeListener() {
  127.             public void stateChanged(ChangeEvent e) {
  128.                 splitPane.setOneTouchExpandable(
  129.                                 ((JCheckBox) e.getSource()).isSelected());
  130.             }
  131.         });
  132.         buttonWrapper.add(checkBox);
  133.         wrapper.add(buttonWrapper);
  134.  
  135.         /* Create a text field to change the divider size. */
  136.         JPanel                   tfWrapper;
  137.         JTextField               tf;
  138.         JLabel                   label;
  139.  
  140.         tf = new JTextField();
  141.         tf.setText(new Integer(splitPane.getDividerSize()).toString());
  142.         tf.setColumns(5);
  143.         tf.getAccessibleContext().setAccessibleName("Divider Size");
  144.         tf.addActionListener(new ActionListener() {
  145.             public void actionPerformed(ActionEvent e) {
  146.                 String           value = ((JTextField)e.getSource()).getText();
  147.                 int              newSize;
  148.  
  149.                 try {
  150.                     newSize = Integer.parseInt(value);
  151.                 } catch (Exception ex) {
  152.                     newSize = -1;
  153.                 }
  154.                 if(newSize > 0)
  155.                     splitPane.setDividerSize(newSize);
  156.                 else
  157.                     JOptionPane.showMessageDialog(splitPane,
  158.                                                   "Invalid Divider Size",
  159.                                                   "Error",
  160.                                                   JOptionPane.ERROR_MESSAGE);
  161.             }
  162.         });
  163.         label = new JLabel("Divider Size");
  164.         tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
  165.         tfWrapper.add(label);
  166.         tfWrapper.add(tf);
  167.         label.setLabelFor(tf);
  168.         label.setDisplayedMnemonic('z');
  169.         wrapper.add(tfWrapper);
  170.  
  171.         /* Create a text field that will change the preferred/minimum size
  172.            of the left component. */
  173.         tf = new JTextField(String.valueOf(leftGrid.getPreferredSize().width));
  174.         tf.setColumns(5);
  175.         tf.getAccessibleContext().setAccessibleName(
  176.                                         "First Component minimum size");
  177.         tf.addActionListener(new ActionListener() {
  178.             public void actionPerformed(ActionEvent e) {
  179.                 String           value = ((JTextField)e.getSource()).getText();
  180.                 int              newSize;
  181.  
  182.                 try {
  183.                     newSize = Integer.parseInt(value);
  184.                 } catch (Exception ex) {
  185.                     newSize = -1;
  186.                 }
  187.                 if(newSize > 10)
  188.                     leftGrid.setPreferredSize(newSize);
  189.                 else
  190.                     JOptionPane.showMessageDialog(splitPane,
  191.                                                   "Invalid Minimum Size, " +
  192.                                                   "must be greater than 10",
  193.                                                   "Error",
  194.                                                   JOptionPane.ERROR_MESSAGE);
  195.             }
  196.         });
  197.         label = new JLabel("First Components Minimum Size");
  198.         tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
  199.         tfWrapper.add(label);
  200.         tfWrapper.add(tf);
  201.         label.setLabelFor(tf);
  202.         label.setDisplayedMnemonic('i');
  203.         wrapper.add(tfWrapper);
  204.         
  205.         /* Create a text field that will change the preferred/minimum size
  206.            of the right component. */
  207.         tf = new JTextField(String.valueOf(rightGrid.getPreferredSize().
  208.                                            width));
  209.         tf.setColumns(5);
  210.         tf.getAccessibleContext().setAccessibleName(
  211.                                         "Second Component minimum size");
  212.         tf.addActionListener(new ActionListener() {
  213.             public void actionPerformed(ActionEvent e) {
  214.                 String           value = ((JTextField)e.getSource()).getText();
  215.                 int              newSize;
  216.  
  217.                 try {
  218.                     newSize = Integer.parseInt(value);
  219.                 } catch (Exception ex) {
  220.                     newSize = -1;
  221.                 }
  222.                 if(newSize > 10)
  223.                     rightGrid.setPreferredSize(newSize);
  224.                 else
  225.                     JOptionPane.showMessageDialog(splitPane,
  226.                                                   "Invalid Minimum Size, " +
  227.                                                   "must be greater than 10",
  228.                                                   "Error",
  229.                                                   JOptionPane.ERROR_MESSAGE);
  230.             }
  231.         });
  232.         label = new JLabel("Second Components Minimum Size");
  233.         tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
  234.         tfWrapper.add(label);
  235.         tfWrapper.add(tf);
  236.         label.setLabelFor(tf);
  237.         label.setDisplayedMnemonic('n');
  238.         wrapper.add(tfWrapper);
  239.  
  240.         add(wrapper, BorderLayout.SOUTH);
  241.     }
  242. }
  243.  
  244. class GridComponent extends Component
  245. {
  246.     /** Number of grids to show. */
  247.     protected int            gridCount;
  248.     /** Size of each grid. */
  249.     protected int            gridSize;
  250.     /** Color for lines. */
  251.     protected Color          currentColor;
  252.     /** Preferred size. */
  253.     protected int            preferredSize;
  254.  
  255.     public GridComponent(int gridCount) {
  256.         this.gridCount = gridCount;
  257.         gridSize = 10;
  258.         currentColor = Color.lightGray;
  259.     }
  260.  
  261.     /**
  262.      * Sets the preferred size to value.
  263.      */
  264.     public void setPreferredSize(int value) {
  265.         preferredSize = value;
  266.     }
  267.  
  268.     /**
  269.      * Returns the preferred size, which is the value of setPreferredSize
  270.      * as a Dimension.
  271.      */
  272.     public Dimension getPreferredSize() {
  273.         return new Dimension(preferredSize, preferredSize);
  274.     }
  275.  
  276.     /**
  277.      * Returns the minimum size. JSplitPane keys of the minimum size to
  278.      * determine where the divider can drag to. If the minimum size of the
  279.      * two components being split are greater than the current size of the
  280.      * splitpane, the splitpane will not allow you to drag the divider 
  281.      * around.
  282.      */
  283.     public Dimension getMinimumSize() {
  284.         return getPreferredSize();
  285.     }
  286.  
  287.     /**
  288.      * Resets the <code>currentColor</code> and messages super.
  289.      */
  290.     public void setBounds(int x, int y, int width, int height) {
  291.         int          minSize = Math.min(width, height);
  292.  
  293.         if(minSize < 100)
  294.             currentColor = Color.red;
  295.         else if(minSize < 200)
  296.             currentColor = Color.blue;
  297.         else if(minSize < 300)
  298.             currentColor = Color.yellow;
  299.         else
  300.             currentColor = Color.white;
  301.  
  302.         gridSize = Math.max(1, minSize / gridCount);
  303.  
  304.         super.setBounds(x, y, width, height);
  305.     }
  306.  
  307.     /**
  308.      * Paints the grid.
  309.      */
  310.     public void paint(Graphics g) {
  311.         Rectangle        paintBounds = g.getClipBounds();
  312.  
  313.         // g.setColor(Color.lightGray);
  314.         // g.fillRect(paintBounds.x, paintBounds.y, paintBounds.width,
  315.         //         paintBounds.height);
  316.         if(gridSize > 0) {
  317.             g.setColor(currentColor);
  318.  
  319.             int              maxY = paintBounds.y + paintBounds.height;
  320.             int              maxX = paintBounds.x + paintBounds.width;
  321.             int              drawMinY = paintBounds.y / gridSize * gridSize;
  322.             int              drawMaxY = maxY / gridSize * gridSize;
  323.             int              drawMinX = paintBounds.x / gridSize * gridSize;
  324.             int              drawMaxX = maxX / gridSize * gridSize;
  325.             int              counter;
  326.  
  327.             for(counter = drawMinX; counter <= drawMaxX; counter += gridSize)
  328.                 g.drawLine(counter, paintBounds.y, counter, maxY);
  329.             for(counter = drawMinY; counter <= drawMaxY; counter += gridSize)
  330.                 g.drawLine(paintBounds.x, counter, maxX, counter);
  331.         }
  332.     }
  333. }
  334.