Package net.sf.cannagrower.gui

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

package net.sf.cannagrower.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.JSplitPane;

import net.sf.cannagrower.CannaGrower;
import net.sf.cannagrower.data.Culture;
import net.sf.cannagrower.data.Hardware;
import net.sf.cannagrower.data.Plantation;
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 javax.swing.WindowConstants;
import javax.swing.JLabel;

/**
* This class is used to visualize and edit a culture.
*
* @author alois_cochard@users.sf.net
*
*/

public class FrameCulture extends JInternalFrame implements DataListener{

  private static final long serialVersionUID = 1L;

  Culture   culture     = null;
 
  private File     cultureFile   = null;
 
  private HardwareListModel jListHardwaresModel;

  private JTabbedPane jTabbedPaneMain = null;

  private JPanel jPanelCulture = null;

  private JPanel jPanelPlantations = null;

  private JPanel jPanelCultureCenter = null;

  private JList jListHardwares = null;

  private JButton jButtonCultureHardwaresAdd = null;

  private JPanel jPanelHardwaresListEdit = null;

  private JButton jButtonCultureHardwaresRemove = null;

  private JPanel jPanelPlantationsList = null;

  private PanelPlantation jPanelPlantationsViewer = null;

  private JPanel jPanelPlantationsEdit = null;

  private JButton jButtonPlantationsAdd = null;

  private JButton jButtonPlantationsRemove = null;

  private JPanel jPanelHardwares = null;
 
  private JComboBox jComboBoxPlantations;

  private JPanel jPanelPlantationsDetails;
 
  private JPanel jPanelHardwaresDetails;

  private JPanel jPanelHardwaresList = null;

  private PanelHardware jPanelHardwaresViewer = null;

  private JCheckBox jCheckBoxHardwaresInstalledOnly = null;

  private JButton jButtonCulutureHardwaresAdvice = null;

  private JPanel jPanelHardwaresListSouth = null;

  private JPanel jPanelHardwaresAdvices = null;

  private JSplitPane  jSplitPaneHardware = null;

  private JPanel jPanelPlantationsLabel = null//  @jve:decl-index=0:visual-constraint="256,664"

  private JLabel jLabelPlantation = null;

  private JPanel jPanelHardwaresHeader = null//  @jve:decl-index=0:visual-constraint="876,297"

  private JLabel jLabelHardwares = null;

  private JPanel jPanelPlantationsHeader = null//  @jve:decl-index=0:visual-constraint="835,339"
 
  /**
   * This is the default constructor
   */
  public FrameCulture(JFrame jFrame, Culture culture, File cultureFile) {
    super();
    this.culture = culture;
    this.cultureFile = cultureFile;
    Data.addDataListener(this);
    initialize();
  }
 
  /**
   * This method initializes this
   *
   * @return void
   */
  private void initialize() {
    this.setFrameIcon(new javax.swing.ImageIcon(FrameCulture.class.getResource("/net/sf/cannagrower/images/data/plants_16.png")));
    this.setLayout(new BorderLayout());
    this.setSize(768, 400);
    this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    this.setMaximizable(true);
    this.setIconifiable(true);
    this.setClosable(true);
    this.setResizable(true);
    this.add(getJTabbedPaneMain(), BorderLayout.CENTER);
    this.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() {
      public void internalFrameClosing(javax.swing.event.InternalFrameEvent e) {
        close();
      }
    });
    updateTitle();
  }
 
  /**
   * This method close the culture
   *
   * @return False if user abort operation
   */
  public boolean close() {
    if (culture == null) {return true;}
   
    if (culture.isModified() || culture.isNew()) {
      switch(JOptionPane.showConfirmDialog(this, Messages
          .getMessage(Messages.cultureAskSave,culture.getName()), getTitle(),
          JOptionPane.YES_NO_CANCEL_OPTION)){
        case JOptionPane.YES_OPTION:
          if (!save()) {return false;}
          break;
        case JOptionPane.NO_OPTION:
          break;
        case JOptionPane.CANCEL_OPTION:
          return false;
      }
    }

    try{
      culture.getRepository().close();
    }catch(IOException e){
      Messages.showException(e);
    }
   
    culture = null;
    cultureFile = null;
   
    Data.removeDataListener(this);
   
    this.dispose();
   
    return true;
  }

  /**
   * This method save the active culture
   *
   * @return False if user abort operation
   */
  public boolean save() {
    File backupSource = null;
    File backup = null;

    if (culture == null) {
      return true;
    }
    if (!culture.isModified() && !culture.isNew()) {
      return true;
    }

    if (culture.isNew() || cultureFile == null) {
      // Ask for cultureFile
      CannaGrower.fileChooser.setSelectedFile(new File(culture.getName()));
      if (CannaGrower.fileChooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
        return false;
      }
      cultureFile = CannaGrower.fileChooser.getSelectedFile();
    }

    try {
      // Add extention
      if (!cultureFile.getCanonicalPath().endsWith(CannaGrower.extension)) {
        cultureFile = new File(cultureFile.getCanonicalFile()
            + CannaGrower.extension);
      }

      // Create backup
      backupSource = new File(cultureFile.getCanonicalPath());
      backup = new File(cultureFile.getCanonicalPath() + "~");
      if (backupSource.exists()) {
        backupSource.renameTo(backup);
      }

      // Write culture to file
      Culture.write(culture, cultureFile);
    } catch (IOException e) {
      Messages.showException(e);
    }
    return true;
  }
 
  /**
   * This method open popup to add new hardware
   *
   * @return void
   */
  private void hardwareAdd() {
    PopupMenuHardware jPopupMenuHardwaresAdd = new PopupMenuHardware(this);
    jPopupMenuHardwaresAdd.show(jButtonCultureHardwaresAdd, 0, 0);
  }

  /**
   * This method remove selected hardwares
   *
   * @return void
   */
  private void hardwareRemove() {
    if (!jListHardwares.isSelectionEmpty()) {
      try {
        for (Object object: jListHardwares.getSelectedValues()) {
          Hardware hardware=(Hardware)object;
          culture.getHardwares().unstore(hardware);
        }
      }
      catch(ArrayIndexOutOfBoundsException e) {}
      catch(IOException e){Messages.showException(e);}
      catch(ClassNotFoundException e){Messages.showException(e);}
      hardwareShow();
      jListHardwares.updateUI();
    }
  }

  /**
   * This method show the hardware selected in the list
   *
   * @return void
   */
  public void hardwareShow() {
    Hardware   hardware=null;
   
    // Retrieve selected element
    if (jListHardwares.getSelectedIndex() >= 0
        && jListHardwares.getSelectedIndex() < jListHardwares
            .getModel().getSize()){
      hardware=(Hardware)jListHardwares.getSelectedValue();
    }
   
    if (jPanelHardwaresViewer != null) {
      // Checking if selected element is already active
      if(jPanelHardwaresViewer.getHardware()!=null && hardware!=null){
        if(jPanelHardwaresViewer.getHardware()==hardware){return;}
      }
     
      // Cleaning active element
      jPanelHardwaresViewer.setVisible(false);
      jPanelHardwaresDetails.remove(jPanelHardwaresViewer);
      jPanelHardwaresViewer = null;
     
    }
   
    // Loading selected element
    if (hardware!=null){
      jPanelHardwaresViewer = new PanelHardware(hardware);
      jPanelHardwaresDetails.add(jPanelHardwaresViewer, BorderLayout.CENTER);
      jPanelHardwaresDetails.updateUI();
    }
  }
 
  public void hardwareShowFirst(){
    // Opening first hardware
    if(getJListHardwares().getModel().getSize()>0){
      getJListHardwares().setSelectedIndex(0);
    }
  }

  /**
   * This method define the display mode (installed only/all) and do a refresh<br>
   * Used when 'InstalledOnly' checkbox is switched
   *
   * @return void
   */
  private void hardwareToggleShowInstalled() {
    jListHardwaresModel
        .setShowInstalledOnly(jCheckBoxHardwaresInstalledOnly
            .isSelected());
    hardwareShow();
  }
 
  private void plantationsAdd(){
    Plantation plantation=new Plantation(culture);
    jComboBoxPlantations.getModel().setSelectedItem(culture.getPlantations().store(plantation));
  }
 
  private void plantationsRemove(){
    if (jComboBoxPlantations.getModel().getSize()>0) {
      culture.getPlantations().remove(jComboBoxPlantations.getSelectedItem());
      if(culture.getPlantations().size()>0){
        jComboBoxPlantations.setSelectedItem(culture.getPlantations().lastElement());
      }else{
        jComboBoxPlantations.setSelectedItem(null);
        plantationsShow();
      }
    }
  }
 
  private void plantationsShow(){
    if (jPanelPlantationsViewer != null) {
      Container   container=(Container)jComboBoxPlantations.getSelectedItem();
      Plantation   plantation;
     
      if(container!=null){
        try{
          plantation=(Plantation)container.getData();
        }catch(IOException e){
          Messages.showException(e);
          return;
        }catch(ClassNotFoundException e){
          Messages.showException(e);
          return;
        }
       
        if(jPanelPlantationsViewer.getPlantation().equals(plantation)){
          return;
        }
      }
     
      jPanelPlantationsViewer.setVisible(false);
      jPanelPlantationsDetails.remove(jPanelPlantationsViewer);
      Data.removeDataListener(jPanelPlantationsViewer);
      jPanelPlantationsViewer = null;
    }
    if (jComboBoxPlantations.getModel().getSize() > 0 && jComboBoxPlantations.getSelectedItem()!=null) {
      Plantation plantation;
     
     
      Container container;
      container=(Container)jComboBoxPlantations.getSelectedItem();
      try{
        plantation=(Plantation)container.getData();
      }catch(IOException e){
        Messages.showException(e);
        return;
      }catch(ClassNotFoundException e){
        Messages.showException(e);
        return;
      }
     
     
      jPanelPlantationsViewer = new PanelPlantation(plantation);
      jPanelPlantationsDetails.add(jPanelPlantationsViewer, BorderLayout.CENTER);
      jPanelPlantationsDetails.updateUI();
      Data.addDataListener(jPanelPlantationsViewer);
     
      SwingUtilities.invokeLater(new Runnable(){
        public void run() {
          jPanelPlantationsViewer.eventShowFirst();     
        }
      });
     
    }
  }

  /**
   * This method return the modified state of the culture
   *
   * @return True if culture is modified
   */
  public boolean isModified() {
    return culture.isModified();
  }

  /**
   * This method return the 'new' state of the culture
   *
   * @return True if culture is new
   */
  public boolean isNew() {
    return culture.isNew();
  }


  /**
   * This method is called each time any culture data is update
   *
   * @see net.sf.cannagrower.util.data.DataHandler
   */
  public void dataModification(Data data) {
    Class<?> dataClass;
   
    // Updating title to add modified flag
    updateTitle();
   
    // Checking if data ancestor
    dataClass=data.getClass();
    while(true){
      if(dataClass==null){break;}
      if (dataClass.equals(Hardware.class)) {
        // An hardware was updated
        SwingUtilities.invokeLater(new Runnable(){
          public void run() {
            java.util.Collections.sort(culture.getHardwares());
            jListHardwares.updateUI();
            hardwareShow();
          }
        });
        break;
      }else if(dataClass.equals(Plantation.class)){
        // A plantation was updated
        SwingUtilities.invokeLater(new Runnable(){
          public void run() {
            java.util.Collections.sort(culture.getPlantations());
            jComboBoxPlantations.updateUI();
            plantationsShow();
          }
        });
        break;
      }
      dataClass=dataClass.getSuperclass();
    }
  }

  /**
   * This method update the title of the frame depending on culutre name and modified/new state
   *
   * @return void
   */
  private void updateTitle() {
    String title ="";// CannaGrower.class.getSimpleName() + " - ";
    if (culture.isModified() || culture.isNew()) {
      title += "*";
    }
    title += culture.getName();
    setTitle(title);
  }

  JButton getJButtonHardwaresAdd() {
    if (jButtonCultureHardwaresAdd == null) {
      jButtonCultureHardwaresAdd = new JButton();
      jButtonCultureHardwaresAdd.setIcon(new ImageIcon(
          getClass().getResource(
              "/net/sf/cannagrower/images/edit_add_16.png")));
      jButtonCultureHardwaresAdd
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              hardwareAdd();
            }
          });
    }
    return jButtonCultureHardwaresAdd;
  }

  JButton getJButtonHardwaresRemove() {
    if (jButtonCultureHardwaresRemove == null) {
      jButtonCultureHardwaresRemove = new JButton();
      jButtonCultureHardwaresRemove
          .setIcon(new ImageIcon(getClass().getResource(
              "/net/sf/cannagrower/images/edit_remove_16.png")));
      jButtonCultureHardwaresRemove
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              hardwareRemove();
            }
          });
    }
    return jButtonCultureHardwaresRemove;
  }

  /**
   * This method initializes jButtonCulutureHardwaresAdvice
   *
   * @return javax.swing.JButton
   */
  private JButton getJButtonHardwaresAdvice() {
    if (jButtonCulutureHardwaresAdvice == null) {
      jButtonCulutureHardwaresAdvice = new JButton();
      jButtonCulutureHardwaresAdvice.setIcon(new ImageIcon(getClass()
          .getResource("/net/sf/cannagrower/images/help_16.png")));
      jButtonCulutureHardwaresAdvice.setText(Messages
          .getMessage(Messages.hardwareAdvices));
      jButtonCulutureHardwaresAdvice.setMnemonic(KeyEvent.VK_UNDEFINED);
      jButtonCulutureHardwaresAdvice.setEnabled(false);
    }
    return jButtonCulutureHardwaresAdvice;
  }

  private JButton getJButtonPlantationsAdd() {
    if (jButtonPlantationsAdd == null) {
      jButtonPlantationsAdd = new JButton();
      jButtonPlantationsAdd.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/edit_add_16.png")));
      jButtonPlantationsAdd.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          plantationsAdd();
        }
      });
    }
    return jButtonPlantationsAdd;
  }

  private JButton getJButtonPlantationsRemove() {
    if (jButtonPlantationsRemove == null) {
      jButtonPlantationsRemove = new JButton();
      jButtonPlantationsRemove
          .setIcon(new ImageIcon(getClass().getResource(
              "/net/sf/cannagrower/images/edit_remove_16.png")));
      jButtonPlantationsRemove.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          plantationsRemove();
        }
      });
    }
    return jButtonPlantationsRemove;
  }

  /**
   * This method initializes jCheckBoxHardwaresInstalledOnly
   *
   * @return javax.swing.JCheckBox
   */
  private JCheckBox getJCheckBoxHardwaresInstalledOnly() {
    if (jCheckBoxHardwaresInstalledOnly == null) {
      jCheckBoxHardwaresInstalledOnly = new JCheckBox();
      jCheckBoxHardwaresInstalledOnly.setSelected(true);
      jCheckBoxHardwaresInstalledOnly.setText(Messages
          .getMessage(Messages.hardwareInstalledShowOnly));
      jCheckBoxHardwaresInstalledOnly
          .addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent e) {
              hardwareToggleShowInstalled();
            }
          });
    }
    return jCheckBoxHardwaresInstalledOnly;
  }

  JList getJListHardwares() {
    if (jListHardwares == null) {
      jListHardwaresModel = new HardwareListModel(this);
      jListHardwares = new JList(jListHardwaresModel);
      jListHardwares.setCellRenderer(new HardwareListCellRenderer());
      jListHardwares
          .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(
                javax.swing.event.ListSelectionEvent e) {
              hardwareShow();
            }
          });
    }
    return jListHardwares;
  }

  private JPanel getJPanelCulture() {
    if (jPanelCulture == null) {
      jPanelCulture = new JPanel();
      jPanelCulture.setLayout(new BorderLayout());
      jPanelCulture.add(getJPanelCultureCenter(), BorderLayout.CENTER);
    }
    return jPanelCulture;
  }

  private JPanel getJPanelCultureCenter() {
    if (jPanelCultureCenter == null) {
      jPanelCultureCenter = new JPanel();
      jPanelCultureCenter.setLayout(new BorderLayout());
      jPanelCultureCenter.add(getJPanelHardwares(), BorderLayout.CENTER);
    }
    return jPanelCultureCenter;
  }

  private JPanel getJPanelHardwares() {
    if (jPanelHardwares == null) {
      jPanelHardwares = new JPanel();
      jPanelHardwares.setLayout(new BorderLayout());
      jPanelHardwares
          .add(getJPanelHardwaresAdvices(), BorderLayout.SOUTH);
      // Define split
      jSplitPaneHardware=new JSplitPane();
      jSplitPaneHardware.setDividerLocation(0.5);
      jSplitPaneHardware.add(getJPanelHardwaresList(), JSplitPane.LEFT);
      jSplitPaneHardware.add(getJPanelHardwaresDetails(), JSplitPane.RIGHT);
      jSplitPaneHardware.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
      jSplitPaneHardware.setOneTouchExpandable(true);
      jPanelHardwares.add(getJPanelHardwaresHeader(),BorderLayout.NORTH);
      jPanelHardwares.add(jSplitPaneHardware,BorderLayout.CENTER);
    }
    return jPanelHardwares;
  }

  /**
   * This method initializes jPanelHardwaresAdvices
   *
   * @return javax.swing.JPanel
   */
  private JPanel getJPanelHardwaresAdvices() {
    if (jPanelHardwaresAdvices == null) {
      jPanelHardwaresAdvices = new JPanel();
      jPanelHardwaresAdvices.setLayout(new FlowLayout());
      jPanelHardwaresAdvices.add(getJButtonHardwaresAdvice(),
          null);
    }
    return jPanelHardwaresAdvices;
  }

  JPanel getJPanelHardwaresList() {
    if (jPanelHardwaresList == null) {
      jPanelHardwaresList = new JPanel();
      jPanelHardwaresList.setLayout(new BorderLayout());
      jPanelHardwaresList.add(getJPanelHardwaresListSouth(),
          BorderLayout.SOUTH);
      jPanelHardwaresList.add(new JScrollPane(getJListHardwares()), BorderLayout.CENTER);
      jPanelHardwaresList.add(getJPanelHardwaresListEdit(),
          BorderLayout.EAST);
    }
    return jPanelHardwaresList;
  }

  private JPanel getJPanelHardwaresListEdit() {
    if (jPanelHardwaresListEdit == null) {
      jPanelHardwaresListEdit = new JPanel();
      jPanelHardwaresListEdit.setLayout(new BoxLayout(
          getJPanelHardwaresListEdit(), BoxLayout.Y_AXIS));
      jPanelHardwaresListEdit.add(getJButtonHardwaresAdd(), null);
      jPanelHardwaresListEdit.add(getJButtonHardwaresRemove(),
          null);
    }
    return jPanelHardwaresListEdit;
  }

  /**
   * This method initializes jPanelHardwaresListSouth
   *
   * @return javax.swing.JPanel
   */
  private JPanel getJPanelHardwaresListSouth() {
    if (jPanelHardwaresListSouth == null) {
      jPanelHardwaresListSouth = new JPanel();
      jPanelHardwaresListSouth.setLayout(new BorderLayout());
      jPanelHardwaresListSouth.add(getJCheckBoxHardwaresInstalledOnly(),
          BorderLayout.NORTH);
    }
    return jPanelHardwaresListSouth;
  }

  private JPanel getJPanelPlantations() {
    if (jPanelPlantations == null) {
      jPanelPlantations = new JPanel();
      jPanelPlantations.setLayout(new BorderLayout());
      jPanelPlantations.add(getJPanelPlantationsHeader(),BorderLayout.NORTH);
      jPanelPlantations.add(getJPanelPlantationsDetails(),BorderLayout.CENTER);
    }
    return jPanelPlantations;
  }

  private JPanel getJPanelPlantationsEdit() {
    if (jPanelPlantationsEdit == null) {
      jPanelPlantationsEdit = new JPanel();
      jPanelPlantationsEdit.setLayout(new BoxLayout(
          getJPanelPlantationsEdit(), BoxLayout.X_AXIS));
      jPanelPlantationsEdit.add(getJButtonPlantationsAdd(), null);
      jPanelPlantationsEdit.add(getJButtonPlantationsRemove(), null);
    }
    return jPanelPlantationsEdit;
  }

  private JPanel getJPanelPlantationsList() {
    if (jPanelPlantationsList == null) {
      jPanelPlantationsList = new JPanel();
      jPanelPlantationsList.setLayout(new BorderLayout());
      jPanelPlantationsList.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      jPanelPlantationsList.add(getJComboBoxPlantations(), BorderLayout.CENTER);
      jPanelPlantationsList.add(getJPanelPlantationsEdit(),BorderLayout.EAST);
    }
    return jPanelPlantationsList;
  }

  JTabbedPane getJTabbedPaneMain() {
    if (jTabbedPaneMain == null) {
      jTabbedPaneMain = new JTabbedPane();
      jTabbedPaneMain.setTabPlacement(JTabbedPane.LEFT);
      jTabbedPaneMain.addTab(Messages.getMessage(Messages.cultureHardwares), new ImageIcon(getClass().getResource("/net/sf/cannagrower/images/data/blow_16.png")),
          getJPanelCulture(), null);
      jTabbedPaneMain.addTab(Messages
          .getMessage(Messages.culturePlantations), new ImageIcon(getClass().getResource("/net/sf/cannagrower/images/data/plants_16.png")),
          getJPanelPlantations(), null);
      jTabbedPaneMain.addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(javax.swing.event.ChangeEvent e) {
          if(jTabbedPaneMain.getSelectedComponent().equals(getJPanelPlantations())){
            if (jPanelPlantationsViewer == null) {
              SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                  plantationsShow();                 
                }
              });
             
            }
          }
        }
      });
    }
    return jTabbedPaneMain;
  }
 
  JPanel getJPanelHardwaresDetails() {
    if (jPanelHardwaresDetails == null) {
      jPanelHardwaresDetails = new JPanel();
      jPanelHardwaresDetails.setBorder(BorderFactory.createEtchedBorder());
      jPanelHardwaresDetails.setLayout(new BorderLayout());
    }
    return jPanelHardwaresDetails;
  }
 
  JPanel getJPanelPlantationsDetails() {
    if (jPanelPlantationsDetails == null) {
      jPanelPlantationsDetails = new JPanel();
      BorderLayout jPanelPlantationsDetailsLayout = new BorderLayout();
      jPanelPlantationsDetails.setLayout(jPanelPlantationsDetailsLayout);
    }
    return jPanelPlantationsDetails;
  }
 
  private JComboBox getJComboBoxPlantations() {
    if (jComboBoxPlantations == null) {
      jComboBoxPlantations = new JComboBox(culture.getPlantations());
      jComboBoxPlantations.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          plantationsShow();
        }
      });
    }
    return jComboBoxPlantations;
  }

  /**
   * This method initializes jPanelPlantationsLabel 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelPlantationsLabel() {
    if (jPanelPlantationsLabel == null) {
      jLabelPlantation = new JLabel();
      jLabelPlantation.setText("<html><b>"+Messages.getMessage(Messages.culturePlantations)+"</b></html>");
      jLabelPlantation.setIcon(new javax.swing.ImageIcon(FrameCulture.class.getResource("/net/sf/cannagrower/images/data/plants_16.png")));
      jPanelPlantationsLabel = new JPanel();
      FlowLayout layout=new FlowLayout();
      layout.setAlignment(FlowLayout.LEFT);
      jPanelPlantationsLabel.setLayout(layout);
      jPanelPlantationsLabel.add(jLabelPlantation);
    }
    return jPanelPlantationsLabel;
  }

  /**
   * This method initializes jPanelHardwaresHeader 
   *  
   * @return javax.swing.JPanel
   */
  private JPanel getJPanelHardwaresHeader() {
    if (jPanelHardwaresHeader == null) {
      jLabelHardwares = new JLabel();
      jLabelHardwares.setText("<html><b>"+Messages.getMessage(Messages.cultureHardwares)+"</b></html>");
      jLabelHardwares.setIcon(new javax.swing.ImageIcon(FrameCulture.class.getResource("/net/sf/cannagrower/images/data/blow_16.png")));
      jPanelHardwaresHeader = new JPanel();
      FlowLayout layout=new FlowLayout();
      layout.setAlignment(FlowLayout.LEFT);
      jPanelHardwaresHeader.setLayout(layout);
      jPanelHardwaresHeader.add(jLabelHardwares);
    }
    return jPanelHardwaresHeader;
  }

  /**
   * This method initializes getJPanelPlantationsHeader 
   *  
   * @return javax.swing.JPanel 
   */
  JPanel getJPanelPlantationsHeader() {
    if (jPanelPlantationsHeader == null) {
      jPanelPlantationsHeader = new JPanel();
      jPanelPlantationsHeader.setLayout(new BoxLayout(getJPanelPlantationsHeader(), BoxLayout.Y_AXIS));
      jPanelPlantationsHeader.add(getJPanelPlantationsLabel());
      jPanelPlantationsHeader.add(getJPanelPlantationsList());
    }
    return jPanelPlantationsHeader;
  }
}

/**
* This class is a list model to show culture hardwares including option to show only installed hardware
*
* @author alois_cochard@users.sf.net
*
*/
class HardwareListModel extends javax.swing.AbstractListModel {
  private static final long serialVersionUID = 1L;

  private FrameCulture cultureViewer;

  private boolean showInstalledOnly = true;

  HardwareListModel(FrameCulture cultureViewer) {
    this.cultureViewer = cultureViewer;
  }

  public Object getElementAt(int index) {
    Object object = null;
    try{
      if (!showInstalledOnly) {
        Container container=cultureViewer.culture.getHardwares().get(index);
        object = container.getData();
      } else {
        int rank = 0;
        for (Container container : cultureViewer.culture.getHardwares()) {
          Hardware hardware;
          hardware=(Hardware)container.getData();
          if(hardware!=null){
            if (rank == index) {
              object = hardware;
            }
            if (hardware.isInstalled()) {
              rank++;
            }
          }
        }
      }
    }catch(IOException e){
      object=null;
      Messages.showException(e);
    }catch(ClassNotFoundException e){
      object=null;
      Messages.showException(e);
    }
   
    return object;
  }

  public int getSize() {
    int size = 0;
    if (!showInstalledOnly) {
      size = cultureViewer.culture.getHardwares().size();
    } else {
      if(cultureViewer==null)return 0;
      if(cultureViewer.culture==null)return 0;
      for (Container container : cultureViewer.culture.getHardwares()) {
        Hardware hardware;
        try{
          hardware=(Hardware)container.getData();
        }catch(IOException e){
          hardware=null;
          Messages.showException(e);
        }catch(ClassNotFoundException e){
          hardware=null;
          Messages.showException(e);
        }
        if(hardware!=null){
          if (hardware.isInstalled()) {size++;}
        }
      }
    }
    return size;
  }

  void setShowInstalledOnly(boolean showInstalledOnly) {
    this.showInstalledOnly = showInstalledOnly;
    cultureViewer.getJListHardwares().updateUI();
   
  }
}

class HardwareListCellRenderer extends javax.swing.JLabel implements javax.swing.ListCellRenderer {
  private static final long serialVersionUID = 1L;

    public java.awt.Component getListCellRendererComponent(
      JList list,              // the list
      Object value,            // value to display
      int index,               // cell index
      boolean isSelected,      // is the cell selected
      boolean cellHasFocus)    // does the cell have focus
    {
      setText(value.toString());
      setIcon(Hardware.getIcon(value.getClass()));
       
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        setEnabled(list.isEnabled());
        setFont(list.getFont());
        setOpaque(true);
        return this;
    }
}
TOP

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

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.