Package net.sf.cannagrower.gui

Source Code of net.sf.cannagrower.gui.FrameResources

package net.sf.cannagrower.gui;

import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.JPanel;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;

import net.sf.cannagrower.data.DataFactory;
import net.sf.cannagrower.i18n.Messages;
import net.sf.orexio.lopf.Data;
import net.sf.orexio.lopf.DataListener;
import net.sf.orexio.lopf.container.Container;
import net.sf.orexio.lopf.container.ContainerVector;
import javax.swing.JSplitPane;
import javax.swing.JList;
import javax.swing.WindowConstants;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.BorderFactory;
import java.awt.Dimension;

// TODO to_v0.2.0_Beta:  Make panel of this and use heritage to simplify GUI conception

public class FrameResources extends JInternalFrame implements DataListener{
  private static final long   serialVersionUID = 1L;
 
  private ContainerVector vector;
 
  private PanelData viewer;
 
  private DataFactory factory;
 
  private JPanel jContentPane = null;

  private JSplitPane jSplitPane = null;

  private JPanel jPanelList = null;

  private JList jList = null;

  private JPanel jPanelListEdit = null;

  private JButton jButtonListAdd = null;

  private JButton jButtonListRemove = null;

  private JPanel jPanelElement = null;

  /**
   * This is the xxx default constructor
   */
  public FrameResources(ContainerVector vector,PanelData viewer,DataFactory factory) {
    super();
    this.vector=vector;
    this.viewer=viewer;
    this.factory=factory;
    initialize();
  }
 
  public void dataModification(Data data) {
    Class<?> dataClass;
   
    // Checking if data ancestor
    dataClass=data.getClass();
    if(dataClass==null){return;}
    if(dataClass.equals(factory.create().getClass())) {
      // An element was updated (or added/removed)
      SwingUtilities.invokeLater(new Runnable(){
        public void run() {
          java.util.Collections.sort(vector);
          jList.updateUI();
          elementShow();
        }
      });
    }
  }

  /**
   * This method initializes this
   *
   * @return void
   */
  private void initialize() {
    Data.addDataListener(this);
    this.setSize(300, 200);
    this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
    this.setResizable(true);
    this.setMaximizable(true);
    this.setIconifiable(true);
    this.setClosable(true);
    this.setContentPane(getJContentPane());
    viewer.setVisible(false);
  }

  /**
   * This method initializes jContentPane
   *
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
    }
    return jContentPane;
  }

  /**
   * This method initializes jSplitPane 
   *  
   * @return javax.swing.JSplitPane 
   */
  private JSplitPane getJSplitPane() {
    if (jSplitPane == null) {
      jSplitPane = new JSplitPane();
      jSplitPane.setDividerLocation(0.5);
      jSplitPane.setOneTouchExpandable(true);
      jSplitPane.setLeftComponent(getJPanelList());
      jSplitPane.setRightComponent(getJPanelElement());
    }
    return jSplitPane;
  }

  /**
   * This method initializes jPanelList 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelList() {
    if (jPanelList == null) {
      jPanelList = new JPanel();
      jPanelList.setLayout(new BorderLayout());
      jPanelList.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      jPanelList.add(getJPanelListEdit(), BorderLayout.EAST);
      jPanelList.add(new JScrollPane(getJList()), BorderLayout.CENTER);
    }
    return jPanelList;
  }

  /**
   * This method initializes jList 
   *  
   * @return javax.swing.JList 
   */
  private JList getJList() {
    if (jList == null) {
      jList = new JList(vector);
      jList.setPreferredSize(new Dimension(250, 52));
      jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
        public void valueChanged(javax.swing.event.ListSelectionEvent e) {
          SwingUtilities.invokeLater(new Runnable(){
            public void run() {
              elementShow();
          };});
        }
      });
    }
    return jList;
  }
 
  private void elementShow(){
    Data data=null;
   
    // Retrieve selected element
    if (jList.getSelectedIndex() >= 0
        && jList.getSelectedIndex() < jList
            .getModel().getSize()){
      Container container=(Container)jList.getSelectedValue();
      try{
        data=container.getData();
      }catch(IOException e){
        Messages.showException(e);
      }catch(ClassNotFoundException e){
        Messages.showException(e);
      }
    }
   
    // Checking if selected event is already active
    if(viewer.getData()!=null && data!=null){
      if(data==viewer.getData()){return;}
    }
   
    // Loading selected element
    if(data!=null){ 
      viewer.setData(data);
      viewer.setVisible(true);
    }else{
      viewer.setVisible(false);
    }
  }

  /**
   * This method initializes jPanelListEdit 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelListEdit() {
    if (jPanelListEdit == null) {
      jPanelListEdit = new JPanel();
      jPanelListEdit.setLayout(new BoxLayout(getJPanelListEdit(), BoxLayout.Y_AXIS));
      jPanelListEdit.add(getJButtonListAdd(), null);
      jPanelListEdit.add(getJButtonListRemove(), null);
    }
    return jPanelListEdit;
  }

  /**
   * This method initializes jButtonListAdd 
   *  
   * @return javax.swing.JButton 
   */
  private JButton getJButtonListAdd() {
    if (jButtonListAdd == null) {
      jButtonListAdd = new JButton();
      jButtonListAdd.setIcon(new ImageIcon(getClass().getResource("/net/sf/cannagrower/images/edit_add_16.png")));
      jButtonListAdd.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          elementAdd();
        }
      });
    }
    return jButtonListAdd;
  }

  /**
   * This method initializes jButtonListRemove 
   *  
   * @return javax.swing.JButton 
   */
  private JButton getJButtonListRemove() {
    if (jButtonListRemove == null) {
      jButtonListRemove = new JButton();
      jButtonListRemove.setIcon(new ImageIcon(getClass().getResource("/net/sf/cannagrower/images/edit_remove_16.png")));
      jButtonListRemove.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          elementRemove();
        }
      });
    }
    return jButtonListRemove;
  }
 
  private void elementAdd(){
    Data data=factory.create();
    jList.setSelectedValue(vector.store(data), true);
  }
 
  private void elementRemove(){
    if (!jList.isSelectionEmpty()) {
      try {
        for (Object containerObject : jList.getSelectedValues()) {
          Container container= (Container) containerObject;
          try{vector.remove(container);}
          catch(java.io.IOException e){Messages.showException(e);}
          catch(ClassNotFoundException e){Messages.showException(e);}
        }
      } catch (ArrayIndexOutOfBoundsException e) {
      }
      elementShow();
      jList.updateUI();
    }
  }

  /**
   * This method initializes jPanelElement 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelElement() {
    if (jPanelElement == null) {
      jPanelElement = new JPanel();
      jPanelElement.setLayout(new BorderLayout());
      jPanelElement.add(viewer,BorderLayout.CENTER);
    }
    return jPanelElement;
  }
}
TOP

Related Classes of net.sf.cannagrower.gui.FrameResources

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.