001: package jimm.datavision.gui;
002:
003: import jimm.util.I18N;
004: import java.awt.*;
005: import java.awt.event.*;
006: import javax.swing.*;
007: import java.io.File;
008: import java.net.*;
009: import java.util.prefs.Preferences;
010:
011: /**
012: * This class is called from the DataVision class when the designer is
013: * started with no command line. It is a startup dialog with the
014: * DataVision splash screen and two buttons, one to create a new
015: * report and one to open an existing report.
016: *
017: * @author Frank W. Zammetti, <a href="mailto:fzammetti@omnytex.com">fzammetti@omnytex.com</a>
018: */
019: public class StartupDialog extends JDialog implements ActionListener {
020:
021: /** This string is what is returned when we're creating a new report */
022: public static final String NEW_REPORT_STRING = "*StartANewReport*";
023:
024: /** The all-seeing eye. */
025: protected static final String TITLE_IMAGE = "images/DVTitle.png";
026:
027: // The two buttons
028: private JButton newReport = new JButton(I18N
029: .get("StartupDialog.new"));
030: private JButton existingReport = new JButton(I18N
031: .get("StartupDialog.open"));
032: private JButton quit = new JButton(I18N.get("StartupDialog.quit"));
033:
034: // The button for the title. Am I just missing something or is there really
035: // no Image widget, or something along those lines, for displaying an image
036: // in Swing? Do you really have to make it a button?!?
037: private JButton titleImage = null;
038:
039: // The result of this dialog. Will either be null, meaning the dialog
040: // was dismissed with the close button, the value of NEW_REPORT_STRING
041: // if creating a new report, or the path to the selected file
042: // (only one is selectable!)
043: private String selectedFile = null;
044:
045: /** Constructor for our class. The dialog is built here. */
046: public StartupDialog() {
047: super ((Frame) null, I18N.get("StartupDialog.title"), true);
048:
049: // Set it's size and resizability
050: setResizable(false);
051: setSize(580, 420);
052:
053: // What to do when the close button is pressed (default didn't
054: // work, I don't know why, so this had to be done explicitly)
055: setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
056: addWindowListener(new WindowAdapter() {
057: public void windowClosing(WindowEvent we) {
058: dispose();
059: }
060: });
061:
062: // Center it on the screen
063: Toolkit kit = this .getToolkit();
064: Dimension screenSize = kit.getScreenSize();
065: int screenWidth = screenSize.width;
066: int screenHeight = screenSize.height;
067: Dimension windowSize = getSize();
068: int windowWidth = windowSize.width;
069: int windowHeight = windowSize.height;
070: int upperLeftX = (screenWidth - windowWidth) / 2;
071: int upperLeftY = (screenHeight - windowHeight) / 2;
072: setLocation(upperLeftX, upperLeftY);
073:
074: // Add it's content... first, the image
075: URL url = getClass().getClassLoader().getResource(TITLE_IMAGE);
076: Image img = Toolkit.getDefaultToolkit().getImage(url);
077: JPanel p1 = new JPanel();
078: titleImage = new JButton(new ImageIcon(img));
079: titleImage.setBorderPainted(false);
080: titleImage.setContentAreaFilled(false);
081: titleImage.setFocusPainted(false);
082: p1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
083: p1.add(titleImage);
084: getContentPane().add("Center", p1);
085:
086: // Now the two buttons, along with their event handlers...
087: JPanel p2 = new JPanel();
088: p2.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 10));
089: newReport.addActionListener(this );
090: p2.add(newReport);
091: existingReport.addActionListener(this );
092: p2.add(existingReport);
093: quit.addActionListener(this );
094: p2.add(quit);
095: getContentPane().add("South", p2);
096:
097: // Show it
098: show();
099:
100: } // End StartupDialog()
101:
102: /** Callback to handle all user interactions */
103: public void actionPerformed(ActionEvent ae) {
104:
105: String ac = ae.getActionCommand();
106:
107: // Set the starting report directory (null is default home directory)
108: // Store the report directory in the preferences for this package
109: Preferences prefs = Preferences.userRoot().node(
110: "/jimm/datavision");
111: String reportDir = prefs.get("reportDir", null);
112: System.out.println("reportDir(1) = " + reportDir);
113:
114: // Open an existing report
115: if (ac.equalsIgnoreCase(I18N.get("StartupDialog.open"))) {
116: JFileChooser jfc = new JFileChooser();
117: if (reportDir != null) {
118: jfc.setCurrentDirectory(new File(reportDir));
119: }
120: jfc.setMultiSelectionEnabled(false);
121: int rv = jfc.showOpenDialog(this );
122: if (rv == JFileChooser.APPROVE_OPTION) {
123: selectedFile = jfc.getSelectedFile().getPath();
124: // If the file path has changed then store it in the preferences
125: String jfcap = jfc.getSelectedFile().getAbsolutePath();
126: if (jfcap != null) {
127: File f = new File(jfcap);
128: String newReportDir = f.getParent();
129: if (newReportDir == null) {
130: newReportDir = reportDir;
131: }
132: if (newReportDir != null) {
133: boolean changed = true;
134: if (reportDir != null
135: && newReportDir.compareTo(reportDir) == 0) {
136: changed = false;
137: }
138: if (changed) {
139: prefs.put("reportDir", newReportDir);
140: }
141: }
142: }
143: dispose();
144: }
145:
146: // Start a new report
147: } else if (ac.equalsIgnoreCase(I18N.get("StartupDialog.new"))) {
148: selectedFile = NEW_REPORT_STRING;
149: dispose();
150:
151: // Quit
152: } else if (ac.equalsIgnoreCase(I18N.get("StartupDialog.quit"))) {
153: dispose();
154: System.exit(0);
155: }
156:
157: } // End actionPerformed()
158:
159: /** Method to return the selected File object. */
160: public String getSelectedFile() {
161: return selectedFile;
162: } // End getSelectedFile()
163:
164: } // End Class
|