| 1 | package pizzaorder; |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | import java.awt.event.ActionEvent; |
|---|
| 5 | import java.awt.event.KeyEvent; |
|---|
| 6 | import java.io.FileInputStream; |
|---|
| 7 | import java.io.FileNotFoundException; |
|---|
| 8 | import java.util.Scanner; |
|---|
| 9 | import javax.swing.AbstractAction; |
|---|
| 10 | import javax.swing.ActionMap; |
|---|
| 11 | import javax.swing.InputMap; |
|---|
| 12 | import javax.swing.JComponent; |
|---|
| 13 | import javax.swing.KeyStroke; |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * Dialog box for author credits. |
|---|
| 17 | * @author jdalbey |
|---|
| 18 | */ |
|---|
| 19 | public class CreditsBox extends javax.swing.JDialog |
|---|
| 20 | { |
|---|
| 21 | /** |
|---|
| 22 | * A return status code - returned if Cancel button has been pressed |
|---|
| 23 | */ |
|---|
| 24 | public static final int RET_CANCEL = 0; |
|---|
| 25 | /** |
|---|
| 26 | * A return status code - returned if OK button has been pressed |
|---|
| 27 | */ |
|---|
| 28 | public static final int RET_OK = 1; |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Creates new form CreditsBox |
|---|
| 32 | */ |
|---|
| 33 | public CreditsBox(java.awt.Frame parent, boolean modal) |
|---|
| 34 | { |
|---|
| 35 | super(parent, modal); |
|---|
| 36 | initComponents(); |
|---|
| 37 | // Read credits file and display in the dialog |
|---|
| 38 | try |
|---|
| 39 | { |
|---|
| 40 | FileInputStream fis = new FileInputStream("src/resources/Credits.txt"); |
|---|
| 41 | Scanner s = new Scanner(fis).useDelimiter("\\A"); |
|---|
| 42 | txtAuthors.setText(s.next()); |
|---|
| 43 | } |
|---|
| 44 | catch (FileNotFoundException ex) |
|---|
| 45 | { |
|---|
| 46 | ex.printStackTrace(); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | // Close the dialog when Esc is pressed |
|---|
| 50 | String cancelName = "cancel"; |
|---|
| 51 | InputMap inputMap = getRootPane().getInputMap( |
|---|
| 52 | JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
|---|
| 53 | inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName); |
|---|
| 54 | ActionMap actionMap = getRootPane().getActionMap(); |
|---|
| 55 | actionMap.put(cancelName, new AbstractAction() |
|---|
| 56 | { |
|---|
| 57 | public void actionPerformed(ActionEvent e) |
|---|
| 58 | { |
|---|
| 59 | doClose(RET_CANCEL); |
|---|
| 60 | } |
|---|
| 61 | }); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * @return the return status of this dialog - one of RET_OK or RET_CANCEL |
|---|
| 66 | */ |
|---|
| 67 | public int getReturnStatus() |
|---|
| 68 | { |
|---|
| 69 | return returnStatus; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * This method is called from within the constructor to initialize the form. |
|---|
| 74 | * WARNING: Do NOT modify this code. The content of this method is always |
|---|
| 75 | * regenerated by the Form Editor. |
|---|
| 76 | */ |
|---|
| 77 | @SuppressWarnings("unchecked") |
|---|
| 78 | // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
|---|
| 79 | private void initComponents() { |
|---|
| 80 | |
|---|
| 81 | closeButton = new javax.swing.JButton(); |
|---|
| 82 | jLabel1 = new javax.swing.JLabel(); |
|---|
| 83 | jLabel2 = new javax.swing.JLabel(); |
|---|
| 84 | jScrollPane1 = new javax.swing.JScrollPane(); |
|---|
| 85 | txtAuthors = new javax.swing.JTextArea(); |
|---|
| 86 | |
|---|
| 87 | addWindowListener(new java.awt.event.WindowAdapter() { |
|---|
| 88 | public void windowClosing(java.awt.event.WindowEvent evt) { |
|---|
| 89 | closeDialog(evt); |
|---|
| 90 | } |
|---|
| 91 | }); |
|---|
| 92 | |
|---|
| 93 | closeButton.setText("Close"); |
|---|
| 94 | closeButton.addActionListener(new java.awt.event.ActionListener() { |
|---|
| 95 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
|---|
| 96 | closeButtonActionPerformed(evt); |
|---|
| 97 | } |
|---|
| 98 | }); |
|---|
| 99 | |
|---|
| 100 | jLabel1.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N |
|---|
| 101 | jLabel1.setText("Pizza Order International"); |
|---|
| 102 | |
|---|
| 103 | jLabel2.setText("Credits:"); |
|---|
| 104 | |
|---|
| 105 | txtAuthors.setColumns(20); |
|---|
| 106 | txtAuthors.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N |
|---|
| 107 | txtAuthors.setRows(5); |
|---|
| 108 | jScrollPane1.setViewportView(txtAuthors); |
|---|
| 109 | |
|---|
| 110 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); |
|---|
| 111 | getContentPane().setLayout(layout); |
|---|
| 112 | layout.setHorizontalGroup( |
|---|
| 113 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 114 | .addGroup(layout.createSequentialGroup() |
|---|
| 115 | .addContainerGap() |
|---|
| 116 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 117 | .addComponent(closeButton, javax.swing.GroupLayout.Alignment.TRAILING) |
|---|
| 118 | .addGroup(layout.createSequentialGroup() |
|---|
| 119 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 120 | .addComponent(jLabel1) |
|---|
| 121 | .addComponent(jLabel2)) |
|---|
| 122 | .addGap(0, 127, Short.MAX_VALUE)) |
|---|
| 123 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)) |
|---|
| 124 | .addContainerGap()) |
|---|
| 125 | ); |
|---|
| 126 | layout.setVerticalGroup( |
|---|
| 127 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 128 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
|---|
| 129 | .addContainerGap() |
|---|
| 130 | .addComponent(jLabel1) |
|---|
| 131 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) |
|---|
| 132 | .addComponent(jLabel2) |
|---|
| 133 | .addGap(18, 18, 18) |
|---|
| 134 | .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE) |
|---|
| 135 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE) |
|---|
| 136 | .addComponent(closeButton) |
|---|
| 137 | .addContainerGap()) |
|---|
| 138 | ); |
|---|
| 139 | |
|---|
| 140 | pack(); |
|---|
| 141 | }// </editor-fold>//GEN-END:initComponents |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * Closes the dialog |
|---|
| 145 | */ |
|---|
| 146 | private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog |
|---|
| 147 | doClose(RET_CANCEL); |
|---|
| 148 | }//GEN-LAST:event_closeDialog |
|---|
| 149 | |
|---|
| 150 | private void closeButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_closeButtonActionPerformed |
|---|
| 151 | {//GEN-HEADEREND:event_closeButtonActionPerformed |
|---|
| 152 | doClose(RET_CANCEL); |
|---|
| 153 | }//GEN-LAST:event_closeButtonActionPerformed |
|---|
| 154 | |
|---|
| 155 | private void doClose(int retStatus) |
|---|
| 156 | { |
|---|
| 157 | returnStatus = retStatus; |
|---|
| 158 | setVisible(false); |
|---|
| 159 | dispose(); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * @param args the command line arguments |
|---|
| 164 | */ |
|---|
| 165 | public static void main(String args[]) |
|---|
| 166 | { |
|---|
| 167 | /* Set the Nimbus look and feel */ |
|---|
| 168 | //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> |
|---|
| 169 | /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. |
|---|
| 170 | * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html |
|---|
| 171 | */ |
|---|
| 172 | try |
|---|
| 173 | { |
|---|
| 174 | for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager. |
|---|
| 175 | getInstalledLookAndFeels()) |
|---|
| 176 | { |
|---|
| 177 | if ("Nimbus".equals(info.getName())) |
|---|
| 178 | { |
|---|
| 179 | javax.swing.UIManager.setLookAndFeel(info.getClassName()); |
|---|
| 180 | break; |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | catch (ClassNotFoundException ex) |
|---|
| 185 | { |
|---|
| 186 | java.util.logging.Logger.getLogger(CreditsBox.class.getName()).log( |
|---|
| 187 | java.util.logging.Level.SEVERE, null, ex); |
|---|
| 188 | } |
|---|
| 189 | catch (InstantiationException ex) |
|---|
| 190 | { |
|---|
| 191 | java.util.logging.Logger.getLogger(CreditsBox.class.getName()).log( |
|---|
| 192 | java.util.logging.Level.SEVERE, null, ex); |
|---|
| 193 | } |
|---|
| 194 | catch (IllegalAccessException ex) |
|---|
| 195 | { |
|---|
| 196 | java.util.logging.Logger.getLogger(CreditsBox.class.getName()).log( |
|---|
| 197 | java.util.logging.Level.SEVERE, null, ex); |
|---|
| 198 | } |
|---|
| 199 | catch (javax.swing.UnsupportedLookAndFeelException ex) |
|---|
| 200 | { |
|---|
| 201 | java.util.logging.Logger.getLogger(CreditsBox.class.getName()).log( |
|---|
| 202 | java.util.logging.Level.SEVERE, null, ex); |
|---|
| 203 | } |
|---|
| 204 | //</editor-fold> |
|---|
| 205 | |
|---|
| 206 | /* Create and display the dialog */ |
|---|
| 207 | java.awt.EventQueue.invokeLater(new Runnable() |
|---|
| 208 | { |
|---|
| 209 | public void run() |
|---|
| 210 | { |
|---|
| 211 | CreditsBox dialog = new CreditsBox(new javax.swing.JFrame(), |
|---|
| 212 | true); |
|---|
| 213 | dialog.addWindowListener(new java.awt.event.WindowAdapter() |
|---|
| 214 | { |
|---|
| 215 | @Override |
|---|
| 216 | public void windowClosing(java.awt.event.WindowEvent e) |
|---|
| 217 | { |
|---|
| 218 | System.exit(0); |
|---|
| 219 | } |
|---|
| 220 | }); |
|---|
| 221 | dialog.setVisible(true); |
|---|
| 222 | } |
|---|
| 223 | }); |
|---|
| 224 | } |
|---|
| 225 | // Variables declaration - do not modify//GEN-BEGIN:variables |
|---|
| 226 | private javax.swing.JButton closeButton; |
|---|
| 227 | private javax.swing.JLabel jLabel1; |
|---|
| 228 | private javax.swing.JLabel jLabel2; |
|---|
| 229 | private javax.swing.JScrollPane jScrollPane1; |
|---|
| 230 | private javax.swing.JTextArea txtAuthors; |
|---|
| 231 | // End of variables declaration//GEN-END:variables |
|---|
| 232 | private int returnStatus = RET_CANCEL; |
|---|
| 233 | } |
|---|