jdk/test/sanity/client/lib/SwingSet3/src/com/sun/swingset3/demos/splitpane/SplitPaneDemo.java
changeset 36744 a00905527ec2
child 37574 906cb708a629
equal deleted inserted replaced
36743:bdc3f1b79fb7 36744:a00905527ec2
       
     1 /*
       
     2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 package com.sun.swingset3.demos.splitpane;
       
    24 
       
    25 import java.awt.BorderLayout;
       
    26 import java.awt.Color;
       
    27 import java.awt.Dimension;
       
    28 import java.awt.GridBagConstraints;
       
    29 import java.awt.GridBagLayout;
       
    30 import java.awt.Insets;
       
    31 import java.awt.event.ActionEvent;
       
    32 import java.awt.event.ActionListener;
       
    33 import javax.swing.*;
       
    34 import javax.swing.event.ChangeEvent;
       
    35 import javax.swing.event.ChangeListener;
       
    36 
       
    37 import com.sun.swingset3.DemoProperties;
       
    38 import com.sun.swingset3.demos.ResourceManager;
       
    39 
       
    40 /**
       
    41  * Split Pane demo
       
    42  *
       
    43  * @version 1.12 11/17/05
       
    44  * @author Scott Violet
       
    45  * @author Jeff Dinkins
       
    46  */
       
    47 @DemoProperties(
       
    48         value = "JSplitPane Demo",
       
    49         category = "Containers",
       
    50         description = "Demonstrates JSplitPane, a container which lays out two components in an adjustable split view (horizontal or vertical)",
       
    51         sourceFiles = {
       
    52             "com/sun/swingset3/demos/splitpane/SplitPaneDemo.java",
       
    53             "com/sun/swingset3/demos/ResourceManager.java",
       
    54             "com/sun/swingset3/demos/splitpane/resources/SplitPaneDemo.properties",
       
    55             "com/sun/swingset3/demos/splitpane/resources/images/day.jpg",
       
    56             "com/sun/swingset3/demos/splitpane/resources/images/night.jpg",
       
    57             "com/sun/swingset3/demos/splitpane/resources/images/SplitPaneDemo.gif"
       
    58         }
       
    59 )
       
    60 public class SplitPaneDemo extends JPanel {
       
    61 
       
    62     private static final ResourceManager resourceManager = new ResourceManager(SplitPaneDemo.class);
       
    63     public static final String VERTICAL_SPLIT = resourceManager.getString("SplitPaneDemo.vert_split");
       
    64     public static final String HORIZONTAL_SPLIT = resourceManager.getString("SplitPaneDemo.horz_split");
       
    65     public static final String ONE_TOUCH_EXPANDABLE = resourceManager.getString("SplitPaneDemo.one_touch_expandable");
       
    66     public static final String SECOND_COMPONENT_MIN_SIZE = resourceManager.getString("SplitPaneDemo.second_component_min_size");
       
    67     public static final String FIRST_COMPONENT_MIN_SIZE = resourceManager.getString("SplitPaneDemo.first_component_min_size");
       
    68     public static final String DIVIDER_SIZE = resourceManager.getString("SplitPaneDemo.divider_size");
       
    69     public static final String DEMO_TITLE = SplitPaneDemo.class.getAnnotation(DemoProperties.class).value();
       
    70 
       
    71     private static final Insets insets = new Insets(4, 8, 4, 8);
       
    72 
       
    73     private final JSplitPane splitPane;
       
    74     private final JLabel day;
       
    75     private final JLabel night;
       
    76 
       
    77     private JPanel controlPanel;
       
    78     private GridBagLayout gridbag;
       
    79     private GridBagConstraints c;
       
    80 
       
    81     /**
       
    82      * main method allows us to run as a standalone demo.
       
    83      *
       
    84      * @param args
       
    85      */
       
    86     public static void main(String[] args) {
       
    87         JFrame frame = new JFrame(DEMO_TITLE);
       
    88 
       
    89         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    90         frame.getContentPane().add(new SplitPaneDemo());
       
    91         frame.setPreferredSize(new Dimension(800, 600));
       
    92         frame.pack();
       
    93         frame.setLocationRelativeTo(null);
       
    94         frame.setVisible(true);
       
    95     }
       
    96 
       
    97     /**
       
    98      * SplitPaneDemo Constructor
       
    99      */
       
   100     public SplitPaneDemo() {
       
   101         setLayout(new BorderLayout());
       
   102 
       
   103         //<snip>Create horizontal SplitPane with day and night
       
   104         day = new JLabel(resourceManager.createImageIcon("day.jpg",
       
   105                 resourceManager.getString("SplitPaneDemo.day")));
       
   106         night = new JLabel(resourceManager.createImageIcon("night.jpg",
       
   107                 resourceManager.getString("SplitPaneDemo.night")));
       
   108 
       
   109         splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, day, night);
       
   110         //</snip>
       
   111 
       
   112         //<snip>Turn on continuous layout
       
   113         splitPane.setContinuousLayout(true);
       
   114         //</snip>
       
   115 
       
   116         //<snip>Turn on one-touch expansion
       
   117         splitPane.setOneTouchExpandable(true);
       
   118         //</snip>
       
   119 
       
   120         //<snip>Set divider location
       
   121         splitPane.setDividerLocation(200);
       
   122         //</snip>
       
   123 
       
   124         //<snip>Set minimum size for each child
       
   125         day.setMinimumSize(new Dimension(20, 20));
       
   126         night.setMinimumSize(new Dimension(20, 20));
       
   127         //</snip>
       
   128 
       
   129         add(splitPane, BorderLayout.CENTER);
       
   130         setBackground(Color.black);
       
   131 
       
   132         add(createSplitPaneControls(), BorderLayout.SOUTH);
       
   133     }
       
   134 
       
   135     /**
       
   136      * Creates controls to alter the JSplitPane.
       
   137      *
       
   138      * @return
       
   139      */
       
   140     protected final JPanel createSplitPaneControls() {
       
   141 
       
   142         gridbag = new GridBagLayout();
       
   143         c = new GridBagConstraints();
       
   144         controlPanel = new JPanel(gridbag);
       
   145 
       
   146         //<snip>Create radio box to edit splitpane orientation
       
   147         Box box = Box.createHorizontalBox();
       
   148         ButtonGroup group = new ButtonGroup();
       
   149 
       
   150         OrientationListener orientationListener = new OrientationListener();
       
   151 
       
   152         JRadioButton button = new JRadioButton(VERTICAL_SPLIT);
       
   153         button.setActionCommand("vertical");
       
   154         button.addActionListener(orientationListener);
       
   155         group.add(button);
       
   156         box.add(button);
       
   157 
       
   158         button = new JRadioButton(HORIZONTAL_SPLIT);
       
   159         button.setActionCommand("horizontal");
       
   160         button.setSelected(true);
       
   161         button.addActionListener(orientationListener);
       
   162         group.add(button);
       
   163         box.add(button);
       
   164         //</snip>
       
   165 
       
   166         addToGridbag(box, 0, 0, 1, 1,
       
   167                 GridBagConstraints.NONE, GridBagConstraints.WEST);
       
   168 
       
   169         //<snip>Create checkbox to edit continuous layout
       
   170         JCheckBox checkBox = new JCheckBox(resourceManager.getString("SplitPaneDemo.cont_layout"));
       
   171         checkBox.setSelected(true);
       
   172 
       
   173         checkBox.addChangeListener((ChangeEvent e) -> {
       
   174             splitPane.setContinuousLayout(
       
   175                     ((JCheckBox) e.getSource()).isSelected());
       
   176         });
       
   177         //</snip>
       
   178 
       
   179         c.gridy++;
       
   180         addToGridbag(checkBox, 0, 1, 1, 1,
       
   181                 GridBagConstraints.NONE, GridBagConstraints.WEST);
       
   182 
       
   183         //<snip>Create checkbox to edit one-touch-expandable
       
   184         checkBox = new JCheckBox(ONE_TOUCH_EXPANDABLE);
       
   185         checkBox.setSelected(true);
       
   186 
       
   187         checkBox.addChangeListener((ChangeEvent e) -> {
       
   188             splitPane.setOneTouchExpandable(
       
   189                     ((JCheckBox) e.getSource()).isSelected());
       
   190         });
       
   191         //</snip>
       
   192 
       
   193         addToGridbag(checkBox, 0, 2, 1, 1,
       
   194                 GridBagConstraints.NONE, GridBagConstraints.WEST);
       
   195 
       
   196         JSeparator separator = new JSeparator(JSeparator.VERTICAL);
       
   197         addToGridbag(separator, 1, 0, 1, 3,
       
   198                 GridBagConstraints.VERTICAL, GridBagConstraints.CENTER);
       
   199 
       
   200         //<snip>Create spinner to edit divider size
       
   201         final JSpinner spinner = new JSpinner(
       
   202                 new SpinnerNumberModel(splitPane.getDividerSize(), 5, 50, 2));
       
   203 
       
   204         spinner.addChangeListener((ChangeEvent event) -> {
       
   205             SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
       
   206             splitPane.setDividerSize(model.getNumber().intValue());
       
   207         });
       
   208         //</snip>
       
   209 
       
   210         JLabel label = new JLabel(DIVIDER_SIZE);
       
   211         label.setLabelFor(spinner);
       
   212         addToGridbag(label, 2, 0, 1, 1,
       
   213                 GridBagConstraints.NONE, GridBagConstraints.EAST);
       
   214         addToGridbag(spinner, 3, 0, 1, 1,
       
   215                 GridBagConstraints.NONE, GridBagConstraints.WEST);
       
   216 
       
   217         //<snip>Create spinners to edit day & night's minimum sizes
       
   218         JSpinner minSizeSpinner = new JSpinner(
       
   219                 new SpinnerNumberModel(day.getMinimumSize().width, 0, 300, 10));
       
   220 
       
   221         minSizeSpinner.addChangeListener(new MinimumSizeListener(day));
       
   222         //</snip>
       
   223 
       
   224         label = new JLabel(FIRST_COMPONENT_MIN_SIZE);
       
   225         label.setLabelFor(minSizeSpinner);
       
   226         addToGridbag(label, 2, 1, 1, 1,
       
   227                 GridBagConstraints.NONE, GridBagConstraints.EAST);
       
   228         addToGridbag(minSizeSpinner, 3, 1, 1, 1,
       
   229                 GridBagConstraints.NONE, GridBagConstraints.WEST);
       
   230 
       
   231         //<snip>Create spinners to edit day & night's minimum sizes
       
   232         minSizeSpinner = new JSpinner(
       
   233                 new SpinnerNumberModel(night.getMinimumSize().width, 0, 300, 10));
       
   234 
       
   235         minSizeSpinner.addChangeListener(new MinimumSizeListener(night));
       
   236         //</snip>
       
   237 
       
   238         label = new JLabel(SECOND_COMPONENT_MIN_SIZE);
       
   239         label.setLabelFor(minSizeSpinner);
       
   240         addToGridbag(label, 2, 2, 1, 1,
       
   241                 GridBagConstraints.NONE, GridBagConstraints.EAST);
       
   242         addToGridbag(minSizeSpinner, 3, 2, 1, 1,
       
   243                 GridBagConstraints.NONE, GridBagConstraints.WEST);
       
   244 
       
   245         return controlPanel;
       
   246     }
       
   247 
       
   248     protected void addToGridbag(JComponent child, int gx, int gy,
       
   249             int gwidth, int gheight, int fill, int anchor) {
       
   250         c.insets = insets;
       
   251         c.gridx = gx;
       
   252         c.gridy = gy;
       
   253         c.gridwidth = gwidth;
       
   254         c.gridheight = gheight;
       
   255         c.fill = fill;
       
   256         c.anchor = anchor;
       
   257         gridbag.addLayoutComponent(child, c);
       
   258         controlPanel.add(child);
       
   259 
       
   260     }
       
   261 
       
   262     //<snip>Create radio box to edit splitpane orientation
       
   263     private class OrientationListener implements ActionListener {
       
   264 
       
   265         @Override
       
   266         public void actionPerformed(ActionEvent event) {
       
   267             splitPane.setOrientation(event.getActionCommand().equals("vertical")
       
   268                     ? JSplitPane.VERTICAL_SPLIT : JSplitPane.HORIZONTAL_SPLIT);
       
   269         }
       
   270 
       
   271     }
       
   272     //</snip>
       
   273 
       
   274     //<snip>Create spinners to edit day & night's minimum sizes
       
   275     public class MinimumSizeListener implements ChangeListener {
       
   276 
       
   277         private final JComponent component;
       
   278 
       
   279         public MinimumSizeListener(JComponent c) {
       
   280             this.component = c;
       
   281         }
       
   282 
       
   283         @Override
       
   284         public void stateChanged(ChangeEvent event) {
       
   285             JSpinner spinner = (JSpinner) event.getSource();
       
   286             SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
       
   287             int min = model.getNumber().intValue();
       
   288             component.setMinimumSize(new Dimension(min, min));
       
   289         }
       
   290     }
       
   291     //</snip>
       
   292 }