KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > StartupDialog


1 package jimm.datavision.gui;
2 import jimm.util.I18N;
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import java.net.*;
7
8
9 /**
10  * This class is called from the DataVision class when the designer is
11  * started with no command line. It is a startup dialog with the
12  * DataVision splash screen and two buttons, one to create a new
13  * report and one to open an existing report.
14  *
15  * @author Frank W. Zammetti, <a HREF="mailto:fzammetti@omnytex.com">fzammetti@omnytex.com</a>
16  */

17 public class StartupDialog extends JDialog implements ActionListener {
18
19 /** This string is what is returned when we're creating a new report */
20 public static final String JavaDoc NEW_REPORT_STRING = "*StartANewReport*";
21
22 /** The all-seeing eye. */
23 protected static final String JavaDoc TITLE_IMAGE = "images/DVTitle.png";
24
25 // The two buttons
26
private JButton newReport = new JButton(I18N.get("StartupDialog.new"));
27 private JButton existingReport = new JButton(I18N.get("StartupDialog.open"));
28 private JButton quit = new JButton(I18N.get("StartupDialog.quit"));
29
30 // The button for the title. Am I just missing something or is there really
31
// no Image widget, or something along those lines, for displaying an image
32
// in Swing? Do you really have to make it a button?!?
33
private JButton titleImage = null;
34
35 // The result of this dialog. Will either be null, meaning the dialog
36
// was dismissed with the close button, the value of NEW_REPORT_STRING
37
// if creating a new report, or the path to the selected file
38
// (only one is selectable!)
39
private String JavaDoc selectedFile = null;
40
41
42 /** Constructor for our class. The dialog is built here. */
43 public StartupDialog() {
44     super((Frame)null, I18N.get("StartupDialog.title"), true);
45
46     // Set it's size and resizability
47
setResizable(false);
48     setSize(580, 420);
49     
50     // What to do when the close button is pressed (default didn't
51
// work, I don't know why, so this had to be done explicitly)
52
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
53     addWindowListener(new WindowAdapter() {
54     public void windowClosing(WindowEvent we) {
55         dispose();
56     }
57     });
58
59     // Center it on the screen
60
Toolkit kit = this.getToolkit();
61     Dimension screenSize = kit.getScreenSize();
62     int screenWidth = screenSize.width;
63     int screenHeight = screenSize.height;
64     Dimension windowSize = getSize();
65     int windowWidth = windowSize.width;
66     int windowHeight = windowSize.height;
67     int upperLeftX = (screenWidth - windowWidth) / 2;
68     int upperLeftY = (screenHeight - windowHeight) / 2;
69     setLocation(upperLeftX, upperLeftY);
70
71     // Add it's content... first, the image
72
URL url = getClass().getClassLoader().getResource(TITLE_IMAGE);
73     Image img = Toolkit.getDefaultToolkit().getImage(url);
74     JPanel p1 = new JPanel();
75     titleImage = new JButton(new ImageIcon(img));
76     titleImage.setBorderPainted(false);
77     titleImage.setContentAreaFilled(false);
78     titleImage.setFocusPainted(false);
79     p1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
80     p1.add(titleImage);
81     getContentPane().add("Center", p1);
82
83     // Now the two buttons, along with their event handlers...
84
JPanel p2 = new JPanel();
85     p2.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 10));
86     newReport.addActionListener(this);
87     p2.add(newReport);
88     existingReport.addActionListener(this);
89     p2.add(existingReport);
90     quit.addActionListener(this);
91     p2.add(quit);
92     getContentPane().add("South", p2);
93
94     // Show it
95
show();
96
97 } // End StartupDialog()
98

99
100 /** Callback to handle all user interactions */
101 public void actionPerformed(ActionEvent ae) {
102
103     String JavaDoc ac = ae.getActionCommand();
104
105     // Open an existing report
106
if (ac.equalsIgnoreCase(I18N.get("StartupDialog.open"))) {
107     JFileChooser jfc = new JFileChooser();
108     jfc.setMultiSelectionEnabled(false);
109     int rv = jfc.showOpenDialog(this);
110     if (rv == JFileChooser.APPROVE_OPTION) {
111         selectedFile = jfc.getSelectedFile().getPath();
112         dispose();
113     }
114     }
115
116     // Start a new report
117
else if (ac.equalsIgnoreCase(I18N.get("StartupDialog.new"))) {
118     selectedFile = NEW_REPORT_STRING;
119     dispose();
120     }
121
122     // Quit
123
else if (ac.equalsIgnoreCase(I18N.get("StartupDialog.quit"))) {
124     dispose();
125     System.exit(0);
126     }
127
128 } // End actionPerformed()
129

130
131 /** Method to return the selected File object. */
132 public String JavaDoc getSelectedFile() {
133     return selectedFile;
134 } // End getSelectedFile()
135

136
137 } // End Class
138
Popular Tags