Package net.sf.cannagrower.gui

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

package net.sf.cannagrower.gui;

import java.util.Vector;
import java.io.IOException;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import net.sf.cannagrower.data.Hardware;
import net.sf.cannagrower.i18n.Messages;

import net.sf.orexio.lopf.container.Container;

/**
* This class is a popup to choose and add new hardware
*
* @author alois_cochard@users.sf.net
*
*/
public class PopupMenuHardware extends JPopupMenu {

  private static final long serialVersionUID = 1L;

  private FrameCulture cultureViewer;

  /**
   * This is the default constructor
   *
   * @param hardwares Vector where new hardware will be added
   */
  public PopupMenuHardware(FrameCulture cultureViewer) {
    super();
    this.cultureViewer = cultureViewer;
    initialize();
  }

  /**
   * This method initializes this
   *
   * @return void
   */
  private void initialize() {
    Vector<Class<?>>   hardwaresType         = Hardware.getHardwares();
    Vector<Class<?>>     hardwaresInstalled    = new Vector<Class<?>>();
   
    // Searching all unique hardware installed
    for (Container ghost: cultureViewer.culture.getHardwares()) {
      Hardware hardware;
      try{
        hardware=(Hardware)ghost.getData();
      }catch(IOException e){
        Messages.showException(e);
        hardware=null;
      }catch(ClassNotFoundException e){
        Messages.showException(e);
        hardware=null;
      }
      if(hardware!=null){
        if (hardware.isInstalled()) {
          if (hardware.isUnique()) {
            hardwaresInstalled.add(hardware.getClass());
          }
        }
      }
    }

    for (Class<?> hardwareType : hardwaresType) {
      // Checking if hardware type unique and installed.
      if (!hardwaresInstalled.contains(hardwareType)) {
        // Adding jMenu
        JMenuItem jMenuItem = new JMenuItem();
        jMenuItem.setText(Messages.getMessage(Hardware.class
            .getSimpleName().toLowerCase()
            + "." + hardwareType.getSimpleName().toLowerCase()));
        jMenuItem.setIcon(Hardware.getIcon(hardwareType));
        jMenuItem.setName(hardwareType.getName());
        jMenuItem
            .addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(
                  java.awt.event.ActionEvent e) {
                JMenuItem jMenuItem = (JMenuItem) e.getSource();
                try {
                  Hardware hardware=(Hardware) Class.forName(jMenuItem.getName()).newInstance();
                  cultureViewer.culture.getHardwares().store(hardware);
                  cultureViewer.getJListHardwares().setSelectedValue(hardware, true);
                } catch (Exception ex) {
                  Messages.showException(ex);
                }
              }
            });
        add(jMenuItem);
      }
    }
  }

}
TOP

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

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.