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.dialog;
24 
25 import java.awt.*;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import javax.swing.*;
29 import javax.swing.border.EmptyBorder;
30 import com.sun.swingset3.DemoProperties;
31 import com.sun.swingset3.demos.DemoUtilities;
32 import com.sun.swingset3.demos.ResourceManager;
33 import com.sun.swingset3.demos.slider.SliderDemo;
34 
35 /**
36  *
37  * @author aim
38  */
39 @DemoProperties(
40         value = "JDialog Demo",
41         category = "Toplevel Containers",
42         description = "Demonstrates JDialog, Swing's top-level dialog container.",
43         sourceFiles = {
44             "com/sun/swingset3/demos/dialog/DialogDemo.java",
45             "com/sun/swingset3/demos/DemoUtilities.java",
46             "com/sun/swingset3/demos/dialog/resources/images/DialogDemo.gif"
47         }
48 )
49 public class DialogDemo extends JPanel {
50 
51     private JDialog dialog;
52     private JComponent dialogSpaceholder;
53 
54     public static final String DIALOG_TITLE = "Demo JDialog";
55     public static final String SHOW_BUTTON_TITLE = "Show JDialog...";
56     public static final String LABEL_CONTENT = "I'm content.";
57     public static final String DIALOG_DEMO_TITLE = DialogDemo.class
58             .getAnnotation(DemoProperties.class).value();
59 
DialogDemo()60     public DialogDemo() {
61         initComponents();
62     }
63 
initComponents()64     protected void initComponents() {
65         dialog = createDialog();
66 
67         setLayout(new BorderLayout());
68 
69         add(createControlPanel(), BorderLayout.WEST);
70         dialogSpaceholder = createDialogSpaceholder(dialog);
71         add(dialogSpaceholder, BorderLayout.CENTER);
72     }
73 
createDialogSpaceholder(JDialog dialog)74     private static JComponent createDialogSpaceholder(JDialog dialog) {
75         // Create placeholder panel to provide space in which to
76         // display the toplevel dialog so that the control panel is not
77         // obscured by it.
78         JPanel placeholder = new JPanel();
79         Dimension prefSize = dialog.getPreferredSize();
80         prefSize.width += 12;
81         prefSize.height += 12;
82         placeholder.setPreferredSize(prefSize);
83         return placeholder;
84     }
85 
createControlPanel()86     protected JComponent createControlPanel() {
87         // Create control panel on Left
88         Box panel = Box.createVerticalBox();
89         panel.setBorder(new EmptyBorder(8, 8, 8, 8));
90 
91         // Create button to control visibility of frame
92         JButton showButton = new JButton(SHOW_BUTTON_TITLE);
93         showButton.addActionListener(new ShowActionListener());
94         panel.add(showButton);
95 
96         return panel;
97     }
98 
createDialog()99     private static JDialog createDialog() {
100 
101         //<snip>Create dialog
102         JDialog dialog = new JDialog(new JFrame(), DIALOG_TITLE, false);
103         //</snip>
104 
105         //<snip>Add dialog's content
106         JLabel label = new JLabel(LABEL_CONTENT);
107         label.setHorizontalAlignment(JLabel.CENTER);
108         label.setPreferredSize(new Dimension(200, 140));
109         dialog.add(label);
110         //</snip>
111 
112         //<snip>Initialize dialog's size
113         // which will shrink-to-fit its contents
114         dialog.pack();
115         //</snip>
116 
117         return dialog;
118     }
119 
start()120     public void start() {
121         DemoUtilities.setToplevelLocation(dialog, dialogSpaceholder, SwingConstants.CENTER);
122         showDialog();
123     }
124 
stop()125     public void stop() {
126         //<snip>Hide dialog
127         dialog.setVisible(false);
128         //</snip>
129     }
130 
showDialog()131     public void showDialog() {
132         //<snip>Show dialog
133         // if dialog already visible, then bring to the front
134         if (dialog.isShowing()) {
135             dialog.toFront();
136         } else {
137             dialog.setVisible(true);
138         }
139         //</snip>
140     }
141 
142     private class ShowActionListener implements ActionListener {
143 
actionPerformed(ActionEvent actionEvent)144         public void actionPerformed(ActionEvent actionEvent) {
145             showDialog();
146         }
147     }
148 
main(String args[])149     public static void main(String args[]) {
150         EventQueue.invokeLater(new Runnable() {
151             public void run() {
152                 JFrame frame = new JFrame(DIALOG_DEMO_TITLE);
153                 DialogDemo demo = new DialogDemo();
154                 frame.add(demo);
155                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
156                 frame.pack();
157                 frame.setVisible(true);
158                 demo.start();
159             }
160         });
161     }
162 }
163