1   /*
2    * Copyright (c) 2003 Sun Microsystems, Inc. All  Rights Reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    * 
8    * -Redistributions of source code must retain the above copyright
9    *  notice, this list of conditions and the following disclaimer.
10   * 
11   * -Redistribution in binary form must reproduct the above copyright
12   *  notice, this list of conditions and the following disclaimer in
13   *  the documentation and/or other materials provided with the distribution.
14   * 
15   * Neither the name of Sun Microsystems, Inc. or the names of contributors
16   * may be used to endorse or promote products derived from this software
17   * without specific prior written permission.
18   * 
19   * This software is provided "AS IS," without a warranty of any kind. ALL
20   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23   * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24   * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29   * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30   * 
31   * You acknowledge that Software is not designed, licensed or intended for
32   * use in the design, construction, operation or maintenance of any nuclear
33   * facility.
34   */
35  
36  /*
37   * @(#)SplitPaneDemo.java   1.8 03/01/23
38   */
39  
40  
41  import javax.swing.*;
42  import javax.swing.event.*;
43  import javax.swing.text.*;
44  import javax.swing.table.*;
45  import javax.swing.border.*;
46  import javax.swing.colorchooser.*;
47  import javax.swing.filechooser.*;
48  import javax.accessibility.*;
49  
50  import java.awt.*;
51  import java.awt.event.*;
52  import java.beans.*;
53  import java.util.*;
54  import java.io.*;
55  import java.applet.*;
56  import java.net.*;
57  
58  /**
59   * Split Pane demo
60   *
61   * @version 1.8 01/23/03
62   * @author Scott Violet
63   * @author Jeff Dinkins
64   */
65  public class SplitPaneDemo extends DemoModule {
66  
67      JSplitPane splitPane = null;
68      JLabel earth = null;
69      JLabel moon = null;
70      
71      /**
72       * main method allows us to run as a standalone demo.
73       */
74      public static void main(String[] args) {
75      SplitPaneDemo demo = new SplitPaneDemo(null);
76      demo.mainImpl();
77      }
78      
79      /**
80       * SplitPaneDemo Constructor
81       */
82      public SplitPaneDemo(SwingSet2 swingset) {
83      super(swingset, "SplitPaneDemo", "toolbar/JSplitPane.gif");
84  
85      earth = new JLabel(createImageIcon("splitpane/earth.jpg", getString("SplitPaneDemo.earth")));
86      earth.setMinimumSize(new Dimension(20, 20));
87  
88      moon = new JLabel(createImageIcon("splitpane/moon.jpg", getString("SplitPaneDemo.moon")));
89      moon.setMinimumSize(new Dimension(20, 20));
90      
91          splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, earth, moon);
92          splitPane.setContinuousLayout(true);
93      splitPane.setOneTouchExpandable(true);
94  
95          splitPane.setDividerLocation(200);
96  
97      getDemoPanel().add(splitPane, BorderLayout.CENTER);
98      getDemoPanel().setBackground(Color.black);
99  
100     getDemoPanel().add(createSplitPaneControls(), BorderLayout.SOUTH);
101     }
102     
103     /**
104      * Creates controls to alter the JSplitPane.
105      */
106     protected JPanel createSplitPaneControls() {
107         JPanel wrapper = new JPanel();
108         ButtonGroup group = new ButtonGroup();
109         JRadioButton button;
110 
111         Box buttonWrapper = new Box(BoxLayout.X_AXIS);
112     
113         wrapper.setLayout(new GridLayout(0, 1));
114     
115         /* Create a radio button to vertically split the split pane. */
116         button = new JRadioButton(getString("SplitPaneDemo.vert_split"));
117         button.setMnemonic(getMnemonic("SplitPaneDemo.vert_split_mnemonic"));
118         button.addActionListener(new ActionListener() {
119             public void actionPerformed(ActionEvent e) {
120                 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
121             }
122         });
123         group.add(button);
124         buttonWrapper.add(button);
125 
126         /* Create a radio button the horizontally split the split pane. */
127         button = new JRadioButton(getString("SplitPaneDemo.horz_split"));
128         button.setMnemonic(getMnemonic("SplitPaneDemo.horz_split_mnemonic"));
129         button.setSelected(true);
130         button.addActionListener(new ActionListener() {
131             public void actionPerformed(ActionEvent e) {
132                 splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
133             }
134         });
135         group.add(button);
136         buttonWrapper.add(button);
137     
138         /* Create a check box as to whether or not the split pane continually
139            lays out the component when dragging. */
140         JCheckBox checkBox = new JCheckBox(getString("SplitPaneDemo.cont_layout"));
141         checkBox.setMnemonic(getMnemonic("SplitPaneDemo.cont_layout_mnemonic"));
142         checkBox.setSelected(true);
143     
144         checkBox.addChangeListener(new ChangeListener() {
145             public void stateChanged(ChangeEvent e) {
146                 splitPane.setContinuousLayout(
147             ((JCheckBox)e.getSource()).isSelected());
148             }
149         });
150         buttonWrapper.add(checkBox);
151     
152         /* Create a check box as to whether or not the split pane divider
153            contains the oneTouchExpandable buttons. */
154         checkBox = new JCheckBox(getString("SplitPaneDemo.one_touch_expandable"));
155         checkBox.setMnemonic(getMnemonic("SplitPaneDemo.one_touch_expandable_mnemonic"));
156         checkBox.setSelected(true);
157     
158         checkBox.addChangeListener(new ChangeListener() {
159             public void stateChanged(ChangeEvent e) {
160                 splitPane.setOneTouchExpandable(
161             ((JCheckBox) e.getSource()).isSelected());
162         }
163     });
164     buttonWrapper.add(checkBox);
165     wrapper.add(buttonWrapper);
166     
167     /* Create a text field to change the divider size. */
168     JPanel                   tfWrapper;
169     JTextField               tf;
170     JLabel                   label;
171     
172     tf = new JTextField();
173     tf.setText(new Integer(splitPane.getDividerSize()).toString());
174     tf.setColumns(5);
175     tf.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.divider_size"));
176     tf.addActionListener(new ActionListener() {
177         public void actionPerformed(ActionEvent e) {
178         String  value = ((JTextField)e.getSource()).getText();
179         int newSize;
180         
181         try {
182             newSize = Integer.parseInt(value);
183         } catch (Exception ex) {
184             newSize = -1;
185         }
186         if(newSize > 0) {
187             splitPane.setDividerSize(newSize);
188         } else {
189             JOptionPane.showMessageDialog(splitPane,
190                           getString("SplitPaneDemo.invalid_divider_size"),
191                           getString("SplitPaneDemo.error"),
192                           JOptionPane.ERROR_MESSAGE);
193         }
194         }
195     });
196     label = new JLabel(getString("SplitPaneDemo.divider_size"));
197     tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
198     tfWrapper.add(label);
199     tfWrapper.add(tf);
200     label.setLabelFor(tf);
201     label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.divider_size_mnemonic"));
202     wrapper.add(tfWrapper);
203     
204     /* Create a text field that will change the preferred/minimum size
205        of the earth component. */
206     tf = new JTextField(String.valueOf(earth.getMinimumSize().width));
207     tf.setColumns(5);
208     tf.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.first_component_min_size"));
209     tf.addActionListener(new ActionListener() {
210         public void actionPerformed(ActionEvent e) {
211         String           value = ((JTextField)e.getSource()).getText();
212         int              newSize;
213         
214         try {
215             newSize = Integer.parseInt(value);
216         } catch (Exception ex) {
217             newSize = -1;
218         }
219         if(newSize > 10) {
220             earth.setMinimumSize(new Dimension(newSize, newSize));
221         } else {
222             JOptionPane.showMessageDialog(splitPane,
223                           getString("SplitPaneDemo.invalid_min_size") +
224                           getString("SplitPaneDemo.must_be_greater_than") + 10,
225                           getString("SplitPaneDemo.error"),
226                           JOptionPane.ERROR_MESSAGE);
227         }
228         }
229     });
230     label = new JLabel(getString("SplitPaneDemo.first_component_min_size"));
231     tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
232     tfWrapper.add(label);
233     tfWrapper.add(tf);
234     label.setLabelFor(tf);
235     label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.first_component_min_size_mnemonic"));
236     wrapper.add(tfWrapper);
237     
238     /* Create a text field that will change the preferred/minimum size
239        of the moon component. */
240     tf = new JTextField(String.valueOf(moon.getMinimumSize().width));
241     tf.setColumns(5);
242     tf.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.second_component_min_size"));
243     tf.addActionListener(new ActionListener() {
244         public void actionPerformed(ActionEvent e) {
245         String           value = ((JTextField)e.getSource()).getText();
246         int              newSize;
247         
248         try {
249             newSize = Integer.parseInt(value);
250         } catch (Exception ex) {
251             newSize = -1;
252         }
253         if(newSize > 10) {
254             moon.setMinimumSize(new Dimension(newSize, newSize));
255         } else {
256             JOptionPane.showMessageDialog(splitPane,
257                           getString("SplitPaneDemo.invalid_min_size") +
258                           getString("SplitPaneDemo.must_be_greater_than") + 10,
259                           getString("SplitPaneDemo.error"),
260                           JOptionPane.ERROR_MESSAGE);
261         }
262         }
263     });
264     label = new JLabel(getString("SplitPaneDemo.second_component_min_size"));
265     tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
266     tfWrapper.add(label);
267     tfWrapper.add(tf);
268     label.setLabelFor(tf);
269     label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.second_component_min_size_mnemonic"));
270     wrapper.add(tfWrapper);
271     
272     return wrapper;
273     }
274     
275 }
276