net.landora.video.manager.ManagerFrame.java Source code

Java tutorial

Introduction

Here is the source code for net.landora.video.manager.ManagerFrame.java

Source

/**
 * Copyright (C) 2012-2014 Blake Dickie
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

/*
 * ManagerFrame.java
 *
 * Created on Dec 27, 2011, 6:33:32 PM
 */
package net.landora.video.manager;

import java.awt.CardLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JOptionPane;
import net.landora.video.tasks.TaskProgressImpl;
import net.landora.video.tasks.TaskProgressManager;
import net.landora.video.ui.InfoPanel;
import net.landora.video.ui.UIAddon;
import net.landora.video.utils.ComparisionUtils;
import net.landora.video.utils.UIUtils;
import org.apache.commons.collections.map.MultiValueMap;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import org.pushingpixels.substance.api.DecorationAreaType;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;

/**
 *
 * @author bdickie
 */
public class ManagerFrame extends javax.swing.JFrame {

    private ContentPanelPropertyChange contentPanelPropertyChange = new ContentPanelPropertyChange();

    /**
     * Creates new form ManagerFrame
     */
    public ManagerFrame() {
        initComponents();

        SubstanceLookAndFeel.setDecorationType(pnlStatusBar, DecorationAreaType.FOOTER);

        BindingGroup bindings = new BindingGroup();

        bindings.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ,
                TaskProgressManager.getInstance(), BeanProperty.create(TaskProgressManager.PROP_CURRENTMAINTASK),
                this, BeanProperty.create(PROP_CURRENTMAINTASK)));

        bindings.bind();

    }

    /**
     * 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();

        jPanel1 = new javax.swing.JPanel();
        splitMain = new javax.swing.JSplitPane();
        pnlMainContent = new javax.swing.JPanel();
        tabMain = new javax.swing.JTabbedPane();
        pnlInfo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        toolbar = new javax.swing.JToolBar();
        pnlStatusBar = new javax.swing.JPanel();
        pnlCurrentTask = new javax.swing.JPanel();
        lblCurrentTask = new javax.swing.JLabel();
        currentTaskProgress = new javax.swing.JProgressBar();
        menuBar = new javax.swing.JMenuBar();
        mnuFile = new javax.swing.JMenu();
        mnuConfig = new javax.swing.JMenuItem();
        mnuQuit = new javax.swing.JMenuItem();

        jPanel1.setName("jPanel1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("GS Video Manager");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        splitMain.setResizeWeight(1.0);
        splitMain.setName("splitMain"); // NOI18N

        pnlMainContent.setName("pnlMainContent"); // NOI18N
        pnlMainContent.setLayout(new java.awt.BorderLayout());

        tabMain.setName("tabMain"); // NOI18N
        tabMain.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabMainStateChanged(evt);
            }
        });
        pnlMainContent.add(tabMain, java.awt.BorderLayout.CENTER);

        splitMain.setLeftComponent(pnlMainContent);

        pnlInfo.setName("pnlInfo"); // NOI18N
        pnlInfo.setLayout(new java.awt.CardLayout());

        jLabel1.setFont(jLabel1.getFont().deriveFont((jLabel1.getFont().getStyle() | java.awt.Font.ITALIC)));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("No Info Avaliable");
        jLabel1.setName("jLabel1"); // NOI18N
        pnlInfo.add(jLabel1, "no_info");

        splitMain.setRightComponent(pnlInfo);

        getContentPane().add(splitMain, java.awt.BorderLayout.CENTER);

        toolbar.setRollover(true);
        toolbar.setName("toolbar"); // NOI18N
        getContentPane().add(toolbar, java.awt.BorderLayout.NORTH);

        pnlStatusBar.setName("pnlStatusBar"); // NOI18N
        pnlStatusBar.setLayout(new java.awt.BorderLayout());

        pnlCurrentTask.setName("pnlCurrentTask"); // NOI18N
        pnlCurrentTask.setLayout(new java.awt.BorderLayout());

        lblCurrentTask.setName("lblCurrentTask"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
                org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, this,
                org.jdesktop.beansbinding.ELProperty.create("${currentMainTask.name}"), lblCurrentTask,
                org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("");
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);

        pnlCurrentTask.add(lblCurrentTask, java.awt.BorderLayout.LINE_START);

        currentTaskProgress.setName("currentTaskProgress"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
                org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, this,
                org.jdesktop.beansbinding.ELProperty.create("${!currentMainTask.determinate}"), currentTaskProgress,
                org.jdesktop.beansbinding.BeanProperty.create("indeterminate"));
        binding.setSourceNullValue(false);
        binding.setSourceUnreadableValue(false);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
                org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, this,
                org.jdesktop.beansbinding.ELProperty.create("${currentMainTask.message}"), currentTaskProgress,
                org.jdesktop.beansbinding.BeanProperty.create("toolTipText"));
        binding.setSourceNullValue(null);
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
                org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, this,
                org.jdesktop.beansbinding.ELProperty.create("${currentMainTask.progress}"), currentTaskProgress,
                org.jdesktop.beansbinding.BeanProperty.create("value"));
        binding.setSourceNullValue(0);
        binding.setSourceUnreadableValue(0);
        bindingGroup.addBinding(binding);

        pnlCurrentTask.add(currentTaskProgress, java.awt.BorderLayout.CENTER);

        pnlStatusBar.add(pnlCurrentTask, java.awt.BorderLayout.EAST);

        getContentPane().add(pnlStatusBar, java.awt.BorderLayout.SOUTH);

        menuBar.setName("menuBar"); // NOI18N

        mnuFile.setText("File");
        mnuFile.setName("mnuFile"); // NOI18N

        mnuConfig.setText("Configure");
        mnuConfig.setName("mnuConfig"); // NOI18N
        mnuConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuConfigActionPerformed(evt);
            }
        });
        mnuFile.add(mnuConfig);

        mnuQuit.setText("Quit");
        mnuQuit.setName("mnuQuit"); // NOI18N
        mnuQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuQuitActionPerformed(evt);
            }
        });
        mnuFile.add(mnuQuit);

        menuBar.add(mnuFile);

        setJMenuBar(menuBar);

        bindingGroup.bind();

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

    protected Collection<?> context;

    public void setContext(Collection<?> context) {
        Collection<?> oldContext = this.context;
        if (!ComparisionUtils.equals(context, oldContext)) {
            this.context = context;
            checkInfoPanels();
        }

    }

    private void tabMainStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabMainStateChanged
        ContentPanel panel = (ContentPanel) tabMain.getSelectedComponent();
        if (panel != null) {
            panel.loadView();
        }
        checkContext();
    }//GEN-LAST:event_tabMainStateChanged

    private void mnuQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuQuitActionPerformed
        exit();
    }//GEN-LAST:event_mnuQuitActionPerformed

    private void mnuConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConfigActionPerformed
        configure();
    }//GEN-LAST:event_mnuConfigActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        exit();
    }//GEN-LAST:event_formWindowClosing

    public void addContentPanel(ContentPanel panel) {
        tabMain.add(panel.getTitle(), panel);

        panel.addPropertyChangeListener(contentPanelPropertyChange);
    }

    private List<InfoPanel> infoPanels = new ArrayList<InfoPanel>();

    public void addInfoPanel(InfoPanel panel) {
        int index = infoPanels.size();
        infoPanels.add(panel);
        pnlInfo.add(panel, String.valueOf(index));

        checkInfoPanels();
    }

    private void checkContext() {
        ContentPanel panel = (ContentPanel) tabMain.getSelectedComponent();
        setContext(panel.getCurrentContext());
    }

    private void checkInfoPanels() {
        MultiValueMap completeContext = UIUtils.createCompleteContextByClass(context);

        int index = -1;
        for (int i = 0; i < infoPanels.size(); i++) {
            InfoPanel panel = infoPanels.get(i);
            if (index < 0 && panel.supportsContext(completeContext)) {
                index = i;
                panel.loadContext(completeContext);
            } else {
                panel.clearCurrentContext();
            }
        }

        String cardLayoutName;
        if (index >= 0) {
            cardLayoutName = String.valueOf(index);
        } else {
            cardLayoutName = "no_info";
        }

        CardLayout layout = (CardLayout) pnlInfo.getLayout();
        layout.show(pnlInfo, cardLayoutName);

    }

    private void contentPanelPropertyChanged(ContentPanel panel, PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(ContentPanel.PROP_CURRENTCONTEXT)) {
            checkContext();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar currentTaskProgress;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblCurrentTask;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem mnuConfig;
    private javax.swing.JMenu mnuFile;
    private javax.swing.JMenuItem mnuQuit;
    private javax.swing.JPanel pnlCurrentTask;
    private javax.swing.JPanel pnlInfo;
    private javax.swing.JPanel pnlMainContent;
    private javax.swing.JPanel pnlStatusBar;
    private javax.swing.JSplitPane splitMain;
    private javax.swing.JTabbedPane tabMain;
    private javax.swing.JToolBar toolbar;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private class ContentPanelPropertyChange implements PropertyChangeListener {

        public void propertyChange(PropertyChangeEvent evt) {
            String propName = evt.getPropertyName();
            if (propName.equals(ContentPanel.PROP_CURRENTCONTEXT) || propName.equals(ContentPanel.PROP_TITLE)) {
                contentPanelPropertyChanged((ContentPanel) evt.getSource(), evt);
            }
        }

    }

    public void configure() {
        UIAddon.getInstance().showConfigurationDialog(this);
    }

    protected TaskProgressImpl currentMainTask;
    public static final String PROP_CURRENTMAINTASK = "currentMainTask";

    public TaskProgressImpl getCurrentMainTask() {
        return currentMainTask;
    }

    public void setCurrentMainTask(TaskProgressImpl currentMainTask) {
        TaskProgressImpl oldCurrentMainTask = this.currentMainTask;
        this.currentMainTask = currentMainTask;
        firePropertyChange(PROP_CURRENTMAINTASK, oldCurrentMainTask, currentMainTask);

        //        pnlCurrentTask.setVisible(currentMainTask != null);
        currentTaskProgress.setEnabled(currentMainTask != null);
    }

    public void exit() {
        int reply = JOptionPane.showConfirmDialog(this, "Are you sure you wish to quit?", "Exit",
                JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }

}