Java tutorial
/* * Copyright (c) 2011 Davis Marques * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA. */ package ryerson.daspub.ui; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.io.File; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JTextArea; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import org.apache.commons.lang3.exception.ExceptionUtils; import ryerson.daspub.Config; import ryerson.daspub.Main; /** * Main application interface frame. * @author dmarques */ public class ApplicationJFrame extends javax.swing.JFrame { private static ApplicationJFrame instance = null; // configuration private File configFile; private Config config; // create actions private NewProjectAction newProjectAction = new NewProjectAction(); private OpenProjectAction openProjectAction = new OpenProjectAction(); private SaveProjectAction saveProjectAction = new SaveProjectAction(); private ExitAction exitAction = new ExitAction(); private PublishAllAction publishAllAction = new PublishAllAction(); private PublishMobileAction publishMobileAction = new PublishMobileAction(); private PublishArtifactPagesAction publishArtifactAction = new PublishArtifactPagesAction(); private PublishArtifactQRCodesAction publishQRCodeAction = new PublishArtifactQRCodesAction(); private PublishSlideshowAction publishSlideshowAction = new PublishSlideshowAction(); private PublishReportAction publishReportAction = new PublishReportAction(); private ShowHelpAction showHelpAction = new ShowHelpAction(); private ShowAboutAction showAboutAction = new ShowAboutAction(); private static final Logger logger = Logger.getLogger(ApplicationJFrame.class.getName()); //-------------------------------------------------------------------------- /** * ApplicationJFrame constructor */ protected ApplicationJFrame() { // set the native look and feel setLookAndFeel(); // initialize gui components initComponents(); // set frame title setTitle(Config.APPLICATION_TITLE); // set menu item actions // jMenuItem1.setAction(newProjectAction); jMenuItem2.setAction(openProjectAction); // jMenuItem3.setAction(saveProjectAction); jMenuItem4.setAction(exitAction); jMenuItem7.setAction(publishAllAction); jMenuItem8.setAction(publishMobileAction); jMenuItem9.setAction(publishArtifactAction); jMenuItem10.setAction(publishQRCodeAction); jMenuItem11.setAction(publishSlideshowAction); jMenuItem12.setAction(publishReportAction); jMenuItem5.setAction(showHelpAction); jMenuItem6.setAction(showAboutAction); // disable menu items until project is loaded setProjectActionsEnabled(false); // center the window pack(); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); int w = this.getSize().width; int h = this.getSize().height; int x = (dim.width - w) / 2; int y = (dim.height - h) / 2; setLocation(x, y); } //-------------------------------------------------------------------------- /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jComboBox1 = new javax.swing.JComboBox(); jScrollPane2 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem2 = new javax.swing.JMenuItem(); jSeparator1 = new javax.swing.JPopupMenu.Separator(); jMenuItem4 = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); jMenuItem7 = new javax.swing.JMenuItem(); jMenuItem8 = new javax.swing.JMenuItem(); jMenuItem9 = new javax.swing.JMenuItem(); jMenuItem10 = new javax.swing.JMenuItem(); jMenuItem11 = new javax.swing.JMenuItem(); jMenuItem12 = new javax.swing.JMenuItem(); jMenu3 = new javax.swing.JMenu(); jMenuItem5 = new javax.swing.JMenuItem(); jSeparator2 = new javax.swing.JPopupMenu.Separator(); jMenuItem6 = new javax.swing.JMenuItem(); jComboBox1.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); setMinimumSize(new java.awt.Dimension(640, 480)); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } }); jTextArea1.setColumns(20); jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 12)); jTextArea1.setRows(5); jScrollPane2.setViewportView(jTextArea1); jMenu1.setMnemonic('f'); jMenu1.setText("File"); jMenuItem2.setText("Open Project"); jMenu1.add(jMenuItem2); jMenu1.add(jSeparator1); jMenuItem4.setText("Exit"); jMenu1.add(jMenuItem4); jMenuBar1.add(jMenu1); jMenu2.setMnemonic('p'); jMenu2.setText("Publish"); jMenuItem7.setText("All"); jMenu2.add(jMenuItem7); jMenuItem8.setText("Mobile Presentation"); jMenu2.add(jMenuItem8); jMenuItem9.setText("Artifact Pages"); jMenu2.add(jMenuItem9); jMenuItem10.setText("QR Code Label Sheet"); jMenu2.add(jMenuItem10); jMenuItem11.setText("Slideshow"); jMenu2.add(jMenuItem11); jMenuItem12.setText("Report"); jMenu2.add(jMenuItem12); jMenuBar1.add(jMenu2); jMenu3.setMnemonic('h'); jMenu3.setText("Help"); jMenuItem5.setText("Help Documentation"); jMenu3.add(jMenuItem5); jMenu3.add(jSeparator2); jMenuItem6.setText("About this Application"); jMenu3.add(jMenuItem6); jMenuBar1.add(jMenu3); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents /** * Close project. */ public void closeProject() { setProjectActionsEnabled(false); config = null; updateInterface(); } /** * Handle window closing event. * @param evt Window event */ private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing ExitAction a = new ExitAction(); ActionEvent e = new ActionEvent(this, 0, ""); a.actionPerformed(e); }//GEN-LAST:event_formWindowClosing /** * Get configuration. * @return */ public Config getConfiguration() { return config; } /** * Get singleton instance. * @return */ public static ApplicationJFrame getInstance() { if (instance == null) { instance = new ApplicationJFrame(); } return instance; } /** * Get console output screen. * @return */ public JTextArea getLogOutputTextArea() { return jTextArea1; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc="Look and feel setting code (optional)" /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(ApplicationJFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ApplicationJFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ApplicationJFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ApplicationJFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* * Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new ApplicationJFrame().setVisible(true); } }); } /** * Open project configuration file. * @param C Project configuration * @throws Exception Could not open project file */ public void openProject(File F) throws Exception { config = Config.load(F); setProjectActionsEnabled(true); updateInterface(); } /** * Set the interface look and feel. */ private void setLookAndFeel() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (UnsupportedLookAndFeelException ex) { String stack = ExceptionUtils.getStackTrace(ex); logger.log(Level.SEVERE, "Could not set application look and feel\n\n{0}", stack); System.exit(Main.FAIL); } catch (ClassNotFoundException ex) { String stack = ExceptionUtils.getStackTrace(ex); logger.log(Level.SEVERE, "Could not set application look and feel\n\n{0}", stack); System.exit(Main.FAIL); } catch (InstantiationException ex) { String stack = ExceptionUtils.getStackTrace(ex); logger.log(Level.SEVERE, "Could not set application look and feel\n\n{0}", stack); System.exit(Main.FAIL); } catch (IllegalAccessException ex) { String stack = ExceptionUtils.getStackTrace(ex); logger.log(Level.SEVERE, "Could not set application look and feel\n\n{0}", stack); System.exit(Main.FAIL); } } /** * Save the currently loaded project. */ public void saveProject() { if (configFile != null && config != null) { Config.save(config, configFile); } } /** * Set project menu item state. * @param State True if items enabled, false otherwise. */ private void setProjectActionsEnabled(Boolean State) { // this.saveProjectAction.setEnabled(State); this.publishAllAction.setEnabled(State); this.publishArtifactAction.setEnabled(State); this.publishMobileAction.setEnabled(State); this.publishQRCodeAction.setEnabled(State); this.publishSlideshowAction.setEnabled(State); this.publishReportAction.setEnabled(State); } /** * Update the interface. */ private void updateInterface() { // @TODO update any interface components here } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox jComboBox1; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenu jMenu3; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem10; private javax.swing.JMenuItem jMenuItem11; private javax.swing.JMenuItem jMenuItem12; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JMenuItem jMenuItem4; private javax.swing.JMenuItem jMenuItem5; private javax.swing.JMenuItem jMenuItem6; private javax.swing.JMenuItem jMenuItem7; private javax.swing.JMenuItem jMenuItem8; private javax.swing.JMenuItem jMenuItem9; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JPopupMenu.Separator jSeparator1; private javax.swing.JPopupMenu.Separator jSeparator2; private javax.swing.JTextArea jTextArea1; // End of variables declaration//GEN-END:variables } // end class