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      */
main(String[] args)86     public static void main(String[] args) {
87         JFrame frame = new JFrame(DEMO_TITLE);
88 
89         frame.getContentPane().add(new SplitPaneDemo());
90         frame.setPreferredSize(new Dimension(800, 600));
91         frame.pack();
92         frame.setLocationRelativeTo(null);
93         frame.setVisible(true);
94     }
95 
96     /**
97      * SplitPaneDemo Constructor
98      */
SplitPaneDemo()99     public SplitPaneDemo() {
100         setLayout(new BorderLayout());
101 
102         //<snip>Create horizontal SplitPane with day and night
103         day = new JLabel(resourceManager.createImageIcon("day.jpg",
104                 resourceManager.getString("SplitPaneDemo.day")));
105         night = new JLabel(resourceManager.createImageIcon("night.jpg",
106                 resourceManager.getString("SplitPaneDemo.night")));
107 
108         splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, day, night);
109         //</snip>
110 
111         //<snip>Turn on continuous layout
112         splitPane.setContinuousLayout(true);
113         //</snip>
114 
115         //<snip>Turn on one-touch expansion
116         splitPane.setOneTouchExpandable(true);
117         //</snip>
118 
119         //<snip>Set divider location
120         splitPane.setDividerLocation(200);
121         //</snip>
122 
123         //<snip>Set minimum size for each child
124         day.setMinimumSize(new Dimension(20, 20));
125         night.setMinimumSize(new Dimension(20, 20));
126         //</snip>
127 
128         add(splitPane, BorderLayout.CENTER);
129         setBackground(Color.black);
130 
131         add(createSplitPaneControls(), BorderLayout.SOUTH);
132     }
133 
134     /**
135      * Creates controls to alter the JSplitPane.
136      *
137      * @return
138      */
createSplitPaneControls()139     protected final JPanel createSplitPaneControls() {
140 
141         gridbag = new GridBagLayout();
142         c = new GridBagConstraints();
143         controlPanel = new JPanel(gridbag);
144 
145         //<snip>Create radio box to edit splitpane orientation
146         Box box = Box.createHorizontalBox();
147         ButtonGroup group = new ButtonGroup();
148 
149         OrientationListener orientationListener = new OrientationListener();
150 
151         JRadioButton button = new JRadioButton(VERTICAL_SPLIT);
152         button.setActionCommand("vertical");
153         button.addActionListener(orientationListener);
154         group.add(button);
155         box.add(button);
156 
157         button = new JRadioButton(HORIZONTAL_SPLIT);
158         button.setActionCommand("horizontal");
159         button.setSelected(true);
160         button.addActionListener(orientationListener);
161         group.add(button);
162         box.add(button);
163         //</snip>
164 
165         addToGridbag(box, 0, 0, 1, 1,
166                 GridBagConstraints.NONE, GridBagConstraints.WEST);
167 
168         //<snip>Create checkbox to edit continuous layout
169         JCheckBox checkBox = new JCheckBox(resourceManager.getString("SplitPaneDemo.cont_layout"));
170         checkBox.setSelected(true);
171 
172         checkBox.addChangeListener((ChangeEvent e) -> {
173             splitPane.setContinuousLayout(
174                     ((JCheckBox) e.getSource()).isSelected());
175         });
176         //</snip>
177 
178         c.gridy++;
179         addToGridbag(checkBox, 0, 1, 1, 1,
180                 GridBagConstraints.NONE, GridBagConstraints.WEST);
181 
182         //<snip>Create checkbox to edit one-touch-expandable
183         checkBox = new JCheckBox(ONE_TOUCH_EXPANDABLE);
184         checkBox.setSelected(true);
185 
186         checkBox.addChangeListener((ChangeEvent e) -> {
187             splitPane.setOneTouchExpandable(
188                     ((JCheckBox) e.getSource()).isSelected());
189         });
190         //</snip>
191 
192         addToGridbag(checkBox, 0, 2, 1, 1,
193                 GridBagConstraints.NONE, GridBagConstraints.WEST);
194 
195         JSeparator separator = new JSeparator(JSeparator.VERTICAL);
196         addToGridbag(separator, 1, 0, 1, 3,
197                 GridBagConstraints.VERTICAL, GridBagConstraints.CENTER);
198 
199         //<snip>Create spinner to edit divider size
200         final JSpinner spinner = new JSpinner(
201                 new SpinnerNumberModel(splitPane.getDividerSize(), 5, 50, 2));
202 
203         spinner.addChangeListener((ChangeEvent event) -> {
204             SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
205             splitPane.setDividerSize(model.getNumber().intValue());
206         });
207         //</snip>
208 
209         JLabel label = new JLabel(DIVIDER_SIZE);
210         label.setLabelFor(spinner);
211         addToGridbag(label, 2, 0, 1, 1,
212                 GridBagConstraints.NONE, GridBagConstraints.EAST);
213         addToGridbag(spinner, 3, 0, 1, 1,
214                 GridBagConstraints.NONE, GridBagConstraints.WEST);
215 
216         //<snip>Create spinners to edit day & night's minimum sizes
217         JSpinner minSizeSpinner = new JSpinner(
218                 new SpinnerNumberModel(day.getMinimumSize().width, 0, 300, 10));
219 
220         minSizeSpinner.addChangeListener(new MinimumSizeListener(day));
221         //</snip>
222 
223         label = new JLabel(FIRST_COMPONENT_MIN_SIZE);
224         label.setLabelFor(minSizeSpinner);
225         addToGridbag(label, 2, 1, 1, 1,
226                 GridBagConstraints.NONE, GridBagConstraints.EAST);
227         addToGridbag(minSizeSpinner, 3, 1, 1, 1,
228                 GridBagConstraints.NONE, GridBagConstraints.WEST);
229 
230         //<snip>Create spinners to edit day & night's minimum sizes
231         minSizeSpinner = new JSpinner(
232                 new SpinnerNumberModel(night.getMinimumSize().width, 0, 300, 10));
233 
234         minSizeSpinner.addChangeListener(new MinimumSizeListener(night));
235         //</snip>
236 
237         label = new JLabel(SECOND_COMPONENT_MIN_SIZE);
238         label.setLabelFor(minSizeSpinner);
239         addToGridbag(label, 2, 2, 1, 1,
240                 GridBagConstraints.NONE, GridBagConstraints.EAST);
241         addToGridbag(minSizeSpinner, 3, 2, 1, 1,
242                 GridBagConstraints.NONE, GridBagConstraints.WEST);
243 
244         return controlPanel;
245     }
246 
addToGridbag(JComponent child, int gx, int gy, int gwidth, int gheight, int fill, int anchor)247     protected void addToGridbag(JComponent child, int gx, int gy,
248             int gwidth, int gheight, int fill, int anchor) {
249         c.insets = insets;
250         c.gridx = gx;
251         c.gridy = gy;
252         c.gridwidth = gwidth;
253         c.gridheight = gheight;
254         c.fill = fill;
255         c.anchor = anchor;
256         gridbag.addLayoutComponent(child, c);
257         controlPanel.add(child);
258 
259     }
260 
261     //<snip>Create radio box to edit splitpane orientation
262     private class OrientationListener implements ActionListener {
263 
264         @Override
actionPerformed(ActionEvent event)265         public void actionPerformed(ActionEvent event) {
266             splitPane.setOrientation(event.getActionCommand().equals("vertical")
267                     ? JSplitPane.VERTICAL_SPLIT : JSplitPane.HORIZONTAL_SPLIT);
268         }
269 
270     }
271     //</snip>
272 
273     //<snip>Create spinners to edit day & night's minimum sizes
274     public class MinimumSizeListener implements ChangeListener {
275 
276         private final JComponent component;
277 
MinimumSizeListener(JComponent c)278         public MinimumSizeListener(JComponent c) {
279             this.component = c;
280         }
281 
282         @Override
stateChanged(ChangeEvent event)283         public void stateChanged(ChangeEvent event) {
284             JSpinner spinner = (JSpinner) event.getSource();
285             SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
286             int min = model.getNumber().intValue();
287             component.setMinimumSize(new Dimension(min, min));
288         }
289     }
290     //</snip>
291 }
292