com.josescalia.tumblr.app.MainFrame.java Source code

Java tutorial

Introduction

Here is the source code for com.josescalia.tumblr.app.MainFrame.java

Source

/*
 * To chan
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
    
@Override
public int getIconWidth() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
    
@Override
public int getIconHeight() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
ge this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.josescalia.tumblr.app;

import com.josescalia.tumblr.util.BareBonesBrowserLaunch;
import com.josescalia.tumblr.util.swing.UIAlert;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutionException;

/**
 *
 * @author josescalia
 */
public class MainFrame extends JFrame {
    static Logger logger = Logger.getLogger(MainFrame.class.getName());

    private ApplicationContext context;
    private String appLookAndFeel;
    private String lblFooter;
    private String currentDate;
    private String currentTime;
    private Timer timer;
    private boolean startProgress;

    public void doShow() {
        try {
            UIManager.setLookAndFeel(appLookAndFeel);
            //date and time
            currentDate = "Date : " + new SimpleDateFormat("dd-MMM-yyyy").format(new Date());
            createTimer();
            timer.start();
        } catch (Exception ex) {
            logger.info(ex);
        }
        initComponents();
        executeMenu("tumblrImageViewer");
        //loadMainForm();
        //this.setVisible(true);
    }

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        //initComponents();
    }

    /**
     * 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() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        mainPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        lblDate = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        lblTime = new javax.swing.JLabel();
        lblMark = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        mnTumblrImageViewer = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        mnTumblrFavList = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mnConfiguration = new javax.swing.JMenuItem();
        mnHelp = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnExit = new javax.swing.JMenuItem();

        jMenuItem1.setText("jMenuItem1");

        jMenuItem2.setText("jMenuItem2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setIconImage(new ImageIcon(getClass().getResource("/icons/tumblr.png")).getImage());
        setPreferredSize(new java.awt.Dimension(1360, 700));

        mainPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(mainPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        mainPanelLayout.setVerticalGroup(mainPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 652, Short.MAX_VALUE));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblDate.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
                org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this,
                org.jdesktop.beansbinding.ELProperty.create("${currentDate}"), lblDate,
                org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        progressBar.setString("");
        progressBar.setStringPainted(true);

        lblTime.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
                org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this,
                org.jdesktop.beansbinding.ELProperty.create("${currentTime}"), lblTime,
                org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        lblMark.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
        lblMark.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblMark.setToolTipText("");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
                org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this,
                org.jdesktop.beansbinding.ELProperty.create("${lblFooter}"), lblMark,
                org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 175,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblDate, javax.swing.GroupLayout.PREFERRED_SIZE, 105,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblTime, javax.swing.GroupLayout.PREFERRED_SIZE, 109,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 690, Short.MAX_VALUE)
                        .addComponent(lblMark, javax.swing.GroupLayout.PREFERRED_SIZE, 234,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(1, 1, 1).addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblDate, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(progressBar, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblTime, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblMark, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(3, 3, 3)));

        jMenu3.setText("Tumblr");

        mnTumblrImageViewer.setText("Tumblr Image Viewer");
        mnTumblrImageViewer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnTumblrImageViewerActionPerformed(evt);
            }
        });
        jMenu3.add(mnTumblrImageViewer);

        jMenuBar1.add(jMenu3);

        jMenu1.setText("Favorite");

        mnTumblrFavList.setText("Tumblr Rss Fav List");
        mnTumblrFavList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnTumblrFavListActionPerformed(evt);
            }
        });
        jMenu1.add(mnTumblrFavList);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("System");

        mnConfiguration.setText("Configuration");
        mnConfiguration.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnConfigurationActionPerformed(evt);
            }
        });
        jMenu2.add(mnConfiguration);

        mnHelp.setText("Help");
        mnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnHelpActionPerformed(evt);
            }
        });
        jMenu2.add(mnHelp);
        jMenu2.add(jSeparator1);

        mnExit.setText("Exit");
        mnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnExitActionPerformed(evt);
            }
        });
        jMenu2.add(mnExit);

        jMenuBar1.add(jMenu2);

        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().addGap(1, 1, 1)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(1, 1, 1).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnExitActionPerformed(ActionEvent evt) {//GEN-FIRST:event_mnExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_mnExitActionPerformed

    private void mnHelpActionPerformed(ActionEvent evt) {//GEN-FIRST:event_mnHelpActionPerformed
        BareBonesBrowserLaunch.openURL("docs/index.html");
    }//GEN-LAST:event_mnHelpActionPerformed

    private void mnTumblrImageViewerActionPerformed(ActionEvent evt) {//GEN-FIRST:event_mnTumblrImageViewerActionPerformed
        executeMenu("tumblrImageViewer");

    }//GEN-LAST:event_mnTumblrImageViewerActionPerformed

    private void mnConfigurationActionPerformed(ActionEvent evt) {//GEN-FIRST:event_mnConfigurationActionPerformed
        executeMenu("preferenceForm");
        //propertyChange(evt);
    }//GEN-LAST:event_mnConfigurationActionPerformed

    private void mnTumblrFavListActionPerformed(ActionEvent evt) {//GEN-FIRST:event_mnTumblrFavListActionPerformed
        executeMenu("tumblrRssFavList");
    }//GEN-LAST:event_mnTumblrFavListActionPerformed

    /**
     * @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 (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblMark;
    private javax.swing.JLabel lblTime;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuItem mnConfiguration;
    private javax.swing.JMenuItem mnExit;
    private javax.swing.JMenuItem mnHelp;
    private javax.swing.JMenuItem mnTumblrFavList;
    private javax.swing.JMenuItem mnTumblrImageViewer;
    private javax.swing.JProgressBar progressBar;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void executeMenu(final String beanName) {
        mainPanel.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        progressBar.setIndeterminate(true);
        new SwingWorker<BaseForm, BaseForm>() {
            @Override
            protected BaseForm doInBackground() throws Exception {
                return (BaseForm) context.getBean(beanName);
            }

            @Override
            protected void done() {
                BaseForm frm;
                try {
                    removePanelFromMain();
                    frm = get();
                    frm.showForm();
                    frm.setVisible(true);
                    mainPanel.setLayout(new BorderLayout());
                    mainPanel.add(frm);
                    mainPanel.validate();

                } catch (InterruptedException ex) {
                    logger.error(ex.getMessage());
                    UIAlert.showError(mainPanel, "No Such Form Defined");
                } catch (ExecutionException ex) {
                    logger.error(ex.getMessage());
                    UIAlert.showError(mainPanel, "Something wrong with the system");
                }
                progressBar.setIndeterminate(false);
                mainPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        }.execute();

        //this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }

    private void createTimer() {
        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                setCurrentTime("Time : " + new SimpleDateFormat("hh:mm:ss a").format(new Date()));
            }
        });
    }

    private void removePanelFromMain() {
        if (mainPanel.getComponentCount() > 0) {
            //remove All from MainPanel
            for (int i = 0; i < mainPanel.getComponentCount(); i++) {
                mainPanel.remove(i);
                mainPanel.validate();
            }
        }
    }

    public ApplicationContext getContext() {
        return context;
    }

    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    public String getAppLookAndFeel() {
        return appLookAndFeel;
    }

    public void setAppLookAndFeel(String appLookAndFeel) {
        this.appLookAndFeel = appLookAndFeel;
    }

    public String getLblFooter() {
        return lblFooter;
    }

    public void setLblFooter(String lblFooter) {
        this.lblFooter = lblFooter;
    }

    public String getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(String currentDate) {
        String old = this.currentDate;
        this.currentDate = currentDate;
        firePropertyChange("currentDate", old, currentDate);
    }

    public String getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(String currentTime) {
        String old = this.currentTime;
        this.currentTime = currentTime;
        firePropertyChange("currentTime", old, currentTime);
    }

    public void startProgressBar(String displayStatus) {
        progressBar.setIndeterminate(true);
        progressBar.setString(displayStatus);
    }

    public void stopProgressBar(String displayStatus) {
        progressBar.setIndeterminate(false);
        progressBar.setString(displayStatus);
    }
}