]> git.mxchange.org Git - addressbook-lib.git/blobdiff - Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java
Moved default close operation into init method to keep constructors small
[addressbook-lib.git] / Addressbook / src / org / mxchange / addressbook / client / gui / AddressbookFrame.java
index ea890cc20063184eaf373a8dd9db3cc68491f268..679684641897d4b3963daf725b13775f1612de93 100644 (file)
  */
 package org.mxchange.addressbook.client.gui;
 
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
+import java.awt.BorderLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.text.MessageFormat;
+import javax.swing.BorderFactory;
+import javax.swing.BoxLayout;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+import javax.swing.JPanel;
+import org.mxchange.addressbook.BaseFrameworkSystem;
+import org.mxchange.addressbook.FrameAlreadyInitializedException;
 import org.mxchange.addressbook.application.AddressbookApplication;
-import org.mxchange.addressbook.application.Application;
 import org.mxchange.addressbook.client.Client;
-import org.mxchange.addressbook.manager.contact.ManageableContact;
 
 /**
  *
  * @author Roland Haeder
  */
-public class AddressbookFrame extends javax.swing.JFrame implements ClientFrame {
+public class AddressbookFrame extends BaseFrameworkSystem implements ClientFrame {
+
     /**
-     * Class' logger
+     * Own instance
      */
-    private final Logger LOG;
+    private static ClientFrame self;
 
     /**
-     * Client instance
+     * Singelton getter for this frame instance.
+     *
+     * @param client Client instance
+     * @return Returns a singelton instance of this frame
      */
-    private final Client client;
+    public static final ClientFrame getSelfInstance (final Client client) {
+       // Is it set?
+       if (!(self instanceof ClientFrame)) {
+           // Create new instance
+           self = new AddressbookFrame(client);
+       }
+       
+       // Return instance
+       return self;
+    }
 
     /**
-     * Initialize object
+     * Frame instance
      */
-    {
-       LOG = LogManager.getLogger(this);
-    }
+    private final JFrame frame;
+
+    /**
+     * Whether this frame has been initialized
+     */
+    private boolean isInitialized;
+
+    /**
+     * Status label needs to be updated
+     */
+    private JLabel statusLabel;
 
     /**
      * Creates an instance of this frame with a client instance
-     * @param client 
+     * 
+     * @param client
      */
-    public AddressbookFrame (final Client client) {
+    private AddressbookFrame (final Client client) {
        // Debug line
-       this.getLogger().debug("Initializing Swing frame ...");
+       this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
 
-       // Init components
-       initComponents();
+       // Set frame instance
+       this.frame = new JFrame(AddressbookApplication.printableTitle());
 
        // Set client here
-       this.client = client;
-    }
-
-    @Override
-    public Application getApplication () {
-       throw new UnsupportedOperationException("Not implemented.");
-    }
-
-    @Override
-    public final Client getClient () {
-       return this.client;
-    }
-
-    @Override
-    public ManageableContact getContactManager () {
-       throw new UnsupportedOperationException("Not implemented.");
+       this.setClient(client);
     }
 
     /**
-     * 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.
+     * Shutdown this frame
      */
-    @SuppressWarnings ("unchecked")
-    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
-    private void initComponents() {
-
-        enterDownDataDialog = new javax.swing.JDialog();
-        menuBar = new javax.swing.JMenuBar();
-        mainMenu = new javax.swing.JMenu();
-        exitProgram = new javax.swing.JMenuItem();
-        addressbookMenu = new javax.swing.JMenu();
-        addOwnData = new javax.swing.JMenuItem();
-
-        enterDownDataDialog.setAlwaysOnTop(true);
-
-        javax.swing.GroupLayout enterDownDataDialogLayout = new javax.swing.GroupLayout(enterDownDataDialog.getContentPane());
-        enterDownDataDialog.getContentPane().setLayout(enterDownDataDialogLayout);
-        enterDownDataDialogLayout.setHorizontalGroup(
-            enterDownDataDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGap(0, 400, Short.MAX_VALUE)
-        );
-        enterDownDataDialogLayout.setVerticalGroup(
-            enterDownDataDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGap(0, 300, Short.MAX_VALUE)
-        );
-
-        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/mxchange/addressbook/client/gui/Bundle"); // NOI18N
-        enterDownDataDialog.getAccessibleContext().setAccessibleName(bundle.getString("AddressbookFrame.enterDownDataDialog.AccessibleContext.accessibleName")); // NOI18N
-
-        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
-        setTitle(AddressbookApplication.printableTitle());
-        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
-        setName("main"); // NOI18N
-
-        mainMenu.setText(bundle.getString("AddressbookFrame.text")); // NOI18N
-        mainMenu.setFocusable(false);
-        mainMenu.setName(""); // NOI18N
-
-        exitProgram.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK));
-        exitProgram.setText(bundle.getString("AddressbookFrame.exitProgram.text")); // NOI18N
-        exitProgram.setToolTipText(bundle.getString("AddressbookFrame.exitProgram.toolTipText")); // NOI18N
-        exitProgram.addActionListener(new java.awt.event.ActionListener() {
-            public void actionPerformed(java.awt.event.ActionEvent evt) {
-                exitProgramActionPerformed(evt);
-            }
-        });
-        mainMenu.add(exitProgram);
-
-        menuBar.add(mainMenu);
-
-        addressbookMenu.setText(bundle.getString("AddressbookFrame.addressbookMenu.text")); // NOI18N
-
-        addOwnData.setText(bundle.getString("AddressbookFrame.addOwn.text")); // NOI18N
-        addOwnData.setToolTipText(bundle.getString("AddressbookFrame.addOwnData.toolTipText")); // NOI18N
-        addOwnData.setEnabled(false);
-        addOwnData.setName("addOwn"); // NOI18N
-        addOwnData.addActionListener(new java.awt.event.ActionListener() {
-            public void actionPerformed(java.awt.event.ActionEvent evt) {
-                addOwnDataActionPerformed(evt);
-            }
-        });
-        addressbookMenu.add(addOwnData);
-        addOwnData.getAccessibleContext().setAccessibleName(bundle.getString("AddressbookFrame.addOwnData.AccessibleContext.accessibleName")); // NOI18N
-        addOwnData.getAccessibleContext().setAccessibleDescription(bundle.getString("AddressbookFrame.addOwnData.AccessibleContext.accessibleDescription")); // NOI18N
-
-        menuBar.add(addressbookMenu);
-
-        setJMenuBar(menuBar);
-
-        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
-        getContentPane().setLayout(layout);
-        layout.setHorizontalGroup(
-            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGap(0, 400, Short.MAX_VALUE)
-        );
-        layout.setVerticalGroup(
-            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGap(0, 279, Short.MAX_VALUE)
-        );
-
-        pack();
-    }// </editor-fold>//GEN-END:initComponents
-
-    private void exitProgramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitProgramActionPerformed
-        // Close application instance
-       this.getClient().getApplication().doShutdown();
-    }//GEN-LAST:event_exitProgramActionPerformed
-
-    private void addOwnDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addOwnDataActionPerformed
-        // Asks the user to enter own data
-       this.getClient().getContactManager().doEnterOwnData();
-    }//GEN-LAST:event_addOwnDataActionPerformed
+    @Override
+    public void doShutdown () {
+       // First only show shutdown status
+       this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.shutdown.text"));
+    }
 
     /**
-     * Setups the frame
+     * Setups the frame, do not set isInitialized here
      * 
      * @param client Client instance
      */
     @Override
     public void setupFrame (final Client client) {
+       // Debug line
+       this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
+
        // Has the user entered own data?
-       if (!this.getClient().getContactManager().isOwnContactAdded()) {
+       if (this.getClient().getContactManager().isOwnContactAdded()) {
            // Debug message
-           this.getLogger().debug("Enabling menus: isOwnContactAdded()=false");
+           this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
 
            // Not entered yet, so enable menu
-           addOwnData.setEnabled(true);
+           //addOwnData.setEnabled(false);
        }
 
-       /*
-        * 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 (final ClassNotFoundException ex) {
-           this.getLogger().catching(ex);
-       } catch (final InstantiationException ex) {
-           this.getLogger().catching(ex);
-       } catch (final IllegalAccessException ex) {
-           this.getLogger().catching(ex);
-       } catch (final javax.swing.UnsupportedLookAndFeelException ex) {
-           this.getLogger().catching(ex);
+       // Make the frame visible
+       this.frame.setVisible(true);
+
+       // All done here
+       this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.done.text"));
+    }
+
+    /**
+     * Initalizes this frame. Having initComponents() exposed (publicly
+     * accessible) means that any other object can initialize components which
+     * you may not want.
+     * 
+     * @throws org.mxchange.addressbook.FrameAlreadyInitializedException If this method has been called twice
+     */
+    @Override
+    public void initFrame () throws FrameAlreadyInitializedException {
+       // Debug line
+       this.getLogger().trace("CALLED!");
+
+       // Has this frame been initialized?
+       if (this.isInitialized()) {
+           // Throw exception
+           throw new FrameAlreadyInitializedException();
        }
-        //</editor-fold>
 
+       // Init components
+       this.initComponents();
+
+       // Set flag
+       this.isInitialized = true;
+    }
+
+    /**
+     * Returns field isInitialized. This flag indicates whether this frame has been initialized or not.
+     * 
+     * @return Field isInitialized
+     */
+    @Override
+    public final boolean isInitialized () {
+       return this.isInitialized;
+    }
+
+    /**
+     * Initialize components
+     */
+    private void initComponents () {
        // Debug line
-       this.getLogger().debug("Displaying form ...");
+       this.getLogger().trace("CALLED!");
+
+       // Set default close operation
+       this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-       /*
-        * Create and display the form
-        */
-       java.awt.EventQueue.invokeLater(new Runnable() {
+       // Register shutdown listener
+       this.frame.addWindowListener(new WindowAdapter() {
+           /**
+            * Invoked when a window has been closed.
+            */
            @Override
-           public void run () {
-               new AddressbookFrame(client).setVisible(true);
+           public void windowClosed(final WindowEvent e) {
+               // Shutdown application cleanly
+               self.getClient().getApplication().doShutdown();
+           }
+
+           /**
+            * Invoked when a window is in the process of being closed.
+            * The close operation can be overridden at this point.
+            */
+           @Override
+           public void windowClosing(final WindowEvent e) {
+               // Also shutdown cleanly here
+               self.getClient().getApplication().doShutdown();
            }
        });
-    }
 
-    /**
-     * Getter for logger
-     *
-     * @return Logger
-     */
-    protected final Logger getLogger () {
-       return this.LOG;
+       // Setup layout manager
+       this.frame.setLayout(new BorderLayout(2, 2));
+
+       // Set window size
+       this.frame.setSize(700, 400);
+
+       // Center window in middle of screen, instead of top-left corner
+       this.frame.setLocationRelativeTo(null);
+
+       // Init menu bar, menu and item instances
+       JMenuBar menuBar = new JMenuBar();
+       JMenu menu;
+       JMenuItem item;
+
+       // Init some menus:
+       // 1) File menu
+       menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
+
+       // Add menu items:
+       // 1.x) Exit program (should be last)
+       item = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.text"));
+       item.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.toolTipText"));
+
+       // Add listener to exit menu
+       item.addActionListener(new ActionListener() {
+           /**
+            * If the user has performed this action
+            *
+            * @param e An instance of an ActionEvent class
+            */
+           @Override
+           public void actionPerformed (final ActionEvent e) {
+               self.getClient().getApplication().doShutdown();
+           }
+       });
+
+       // Add item -> menu
+       menu.add(item);
+
+       // Add menu -> menu bar
+       menuBar.add(menu);
+
+       // Add menu bar -> frame
+       this.frame.add(menuBar, BorderLayout.NORTH);
+
+       // Init status label (which needs to be updated
+       this.statusLabel = new JLabel(this.getBundle().getString("AddressbookFrame.statusLabel.initializing.text"));
+
+       // Init status bar in south
+       JPanel panel = new JPanel();
+       panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
+       panel.add(this.statusLabel);
+       panel.setBorder(BorderFactory.createEtchedBorder());
+
+       // Add panel to frame
+       this.frame.add(panel, BorderLayout.SOUTH);
     }
 
-    // Variables declaration - do not modify//GEN-BEGIN:variables
-    private javax.swing.JMenuItem addOwnData;
-    private javax.swing.JMenu addressbookMenu;
-    private javax.swing.JDialog enterDownDataDialog;
-    private javax.swing.JMenuItem exitProgram;
-    private javax.swing.JMenu mainMenu;
-    private javax.swing.JMenuBar menuBar;
-    // End of variables declaration//GEN-END:variables
 }