KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > demo > DialogsTab


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. 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 are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.demo;
32
33 import java.awt.Container JavaDoc;
34 import java.awt.Dialog JavaDoc;
35 import java.awt.FileDialog JavaDoc;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.ActionListener JavaDoc;
38
39 import javax.swing.*;
40
41 import com.jgoodies.forms.builder.ButtonBarBuilder;
42 import com.jgoodies.forms.builder.PanelBuilder;
43 import com.jgoodies.forms.layout.CellConstraints;
44 import com.jgoodies.forms.layout.FormLayout;
45
46 /**
47  * Contains a bunch of buttons to open a bunch of standard dialogs.
48  *
49  * @author Karsten Lentzsch
50  * @version $Revision: 1.4 $
51  */

52 final class DialogsTab {
53     
54     private Container JavaDoc parent;
55     
56     private JButton informationButton;
57     private JButton warningButton;
58     private JButton questionButton;
59     private JButton errorButton;
60     private JButton chooseFileNativeButton;
61     private JButton chooseFileSwingButton;
62     
63     
64     /**
65      * Creates and configures the UI components.
66      */

67     private void initComponents() {
68         informationButton = new JButton("Information");
69         informationButton.addActionListener(new ActionListener JavaDoc() {
70             public void actionPerformed(ActionEvent JavaDoc e) {
71                 JOptionPane.showMessageDialog(
72                     getParentFrame(),
73                     "We just wanted to let you know that you have pressed\n" +
74                     "the Information button to open this sample message dialog.\n\n",
75                     "Information",
76                     JOptionPane.INFORMATION_MESSAGE);
77             }
78         });
79         warningButton = new JButton("Warning");
80         warningButton.addActionListener(new ActionListener JavaDoc() {
81             public void actionPerformed(ActionEvent JavaDoc e) {
82                 JOptionPane.showMessageDialog(
83                     getParentFrame(),
84                     "We just wanted to let you know that you have pressed\n" +
85                     "the Warning button to open this sample message dialog.\n\n",
86                     "Warning",
87                     JOptionPane.WARNING_MESSAGE);
88             }
89         });
90         questionButton = new JButton("Question");
91         questionButton.addActionListener(new ActionListener JavaDoc() {
92             public void actionPerformed(ActionEvent JavaDoc e) {
93                 JOptionPane.showConfirmDialog(
94                     getParentFrame(),
95                     "We just wanted to let you know that you have pressed\n" +
96                     "the Question button to open this sample question dialog.\n\n" +
97                     "Are you satisfied with the dialog's appearance?\n\n",
98                     "Question",
99                     JOptionPane.YES_NO_OPTION
100                     );
101             }
102         });
103         errorButton = new JButton("Error");
104         errorButton.addActionListener(new ActionListener JavaDoc() {
105             public void actionPerformed(ActionEvent JavaDoc e) {
106                 JOptionPane.showMessageDialog(
107                     getParentFrame(),
108                     "We just wanted to let you know that you have pressed\n" +
109                     "the Error button to open this error message dialog.\n\n" +
110                     "Just go ahead and proceed.\n\n",
111                     "Error",
112                     JOptionPane.ERROR_MESSAGE);
113             }
114         });
115         chooseFileNativeButton = new JButton("Open...");
116         chooseFileNativeButton.addActionListener(new ActionListener JavaDoc() {
117             public void actionPerformed(ActionEvent JavaDoc e) {
118                 Dialog JavaDoc dialog = new FileDialog JavaDoc(
119                         getParentFrame(), "Open File (Native)");
120                 dialog.setResizable(true);
121                 dialog.setVisible(true);
122             }
123         });
124         chooseFileSwingButton = new JButton("Open...");
125         chooseFileSwingButton.addActionListener(new ActionListener JavaDoc() {
126             public void actionPerformed(ActionEvent JavaDoc e) {
127                 new JFileChooser("Open File (Swing)").showOpenDialog(
128                     getParentFrame());
129             }
130         });
131     }
132     
133     /**
134      * Builds and returns the panel.
135      */

136     JComponent build(Container JavaDoc aParent) {
137         this.parent = aParent;
138         initComponents();
139
140         FormLayout layout =
141             new FormLayout(
142                 "0:grow, left:pref, 0:grow",
143                 "0:grow, pref, 4dlu, pref, 14dlu, pref, 4dlu, pref, 14dlu, pref, 4dlu, pref, 0:grow");
144         PanelBuilder builder = new PanelBuilder(layout);
145         builder.setDefaultDialogBorder();
146
147         CellConstraints cc = new CellConstraints();
148
149         builder.addLabel("Press a button to open a message dialog.", cc.xy(2, 2));
150         builder.add(buildButtonBar(), cc.xy(2, 4));
151         builder.addLabel("This opens the native file chooser.", cc.xy(2, 6));
152         builder.add(chooseFileNativeButton, cc.xy(2, 8));
153         builder.addLabel("This opens the Swing file chooser.", cc.xy(2, 10));
154         builder.add(chooseFileSwingButton, cc.xy(2, 12));
155
156         return builder.getPanel();
157     }
158     
159     /**
160      * Builds and returns the message dialog button bar.
161      *
162      * @return the message dialog button bar
163      */

164     private JPanel buildButtonBar() {
165         ButtonBarBuilder builder = new ButtonBarBuilder();
166         builder.addGriddedButtons(new JButton[]{
167             informationButton, warningButton, questionButton, errorButton});
168         return builder.getPanel();
169     }
170     
171     
172     // Helper Code ************************************************************
173

174     JFrame getParentFrame() {
175         return (JFrame) (SwingUtilities.getWindowAncestor(parent));
176     }
177
178 }
Popular Tags