package net.sf.cannagrower.gui;
import net.sf.orexio.lopf.container.Container;
import net.sf.orexio.lopf.container.ContainerVector;
import net.sf.cannagrower.data.Event;
import net.sf.cannagrower.data.Variety;
import net.sf.cannagrower.data.Resources;
import net.sf.cannagrower.data.event.Plants;
import net.sf.cannagrower.i18n.Messages;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import java.io.IOException;
import javax.swing.BorderFactory;
public class PanelEventPlants extends PanelEvent {
static final long serialVersionUID = 1L;
private JPanel jPanelCenter = null;
private JScrollPane jScrollPanePlants = null;
private JTable jTablePlants = null;
private JPanel jPanelCenterEast;
private JButton jButtonRemove;
private JButton jButtonAdd;
/**
* This method initializes
*
*/
public PanelEventPlants() {
initialize();
}
/**
* This method initializes this
*
*/
private void initialize() {
this.add(getJPanelCenter(), BorderLayout.CENTER);
}
/**
* This method initializes jPanelCenter
*
* @return javax.swing.JPanel
*/
protected JPanel getJPanelCenter() {
if (jPanelCenter == null) {
jPanelCenter = new JPanel();
jPanelCenter.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
BorderLayout jPanelCenterLayout = new BorderLayout();
jPanelCenter.setLayout(jPanelCenterLayout);
jPanelCenter.add(getJScrollPanePlants(), BorderLayout.CENTER);
jPanelCenter.add(getJPanelCenterEast(), BorderLayout.EAST);
}
return jPanelCenter;
}
/**
* This method initializes jScrollPanePlants
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPanePlants() {
if (jScrollPanePlants == null) {
jScrollPanePlants = new JScrollPane();
jScrollPanePlants.setViewportView(getJTablePlants());
}
return jScrollPanePlants;
}
/**
* This method initializes jTablePlants
*
* @return javax.swing.JTable
*/
private JTable getJTablePlants() {
if (jTablePlants == null) {
jTablePlants = new JTable();
jTablePlants.setCellSelectionEnabled(true);
jTablePlants.setShowGrid(false);
jTablePlants
.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
return jTablePlants;
}
public void setEvent(Event event){
super.setEvent(event);
jTablePlants.setModel(new PlantsTableModel((Plants)getEvent()));
}
private JPanel getJPanelCenterEast() {
if (jPanelCenterEast == null) {
jPanelCenterEast = new JPanel();
BoxLayout jPanelCenterEastLayout = new BoxLayout(
jPanelCenterEast,
javax.swing.BoxLayout.Y_AXIS);
jPanelCenterEast.setLayout(jPanelCenterEastLayout);
jPanelCenterEast.add(getJButtonAdd());
jPanelCenterEast.add(getJButtonRemove());
}
return jPanelCenterEast;
}
private JButton getJButtonAdd() {
if (jButtonAdd == null) {
jButtonAdd = new JButton();
jButtonAdd.setIcon(new ImageIcon(getClass().getClassLoader().getResource("net/sf/cannagrower/images/edit_add_16.png")));
jButtonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
doAdd();
}
});
}
return jButtonAdd;
}
private JButton getJButtonRemove() {
if (jButtonRemove == null) {
jButtonRemove = new JButton();
jButtonRemove.setIcon(new ImageIcon(getClass().getClassLoader()
.getResource("net/sf/cannagrower/images/edit_remove_16.png")));
jButtonRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
doRemove();
}
});
}
return jButtonRemove;
}
protected void doAdd(){
PlantsViewerAddVariety jPopupMenuVarietiesAdd = new PlantsViewerAddVariety(this);
jPopupMenuVarietiesAdd.show(jButtonAdd, 0, 0);
}
protected void doRemove(){
int variety=getJTablePlants().getSelectedRow();
Plants plants=(Plants)this.getEvent();
if(variety<0){return;}
plants.setPlants(plants.getVarieties().get(variety), 0);
}
}
class PlantsTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1l;
private Plants plants;
PlantsTableModel(Plants plants) {
this.plants = plants;
}
public int getColumnCount() {
return 2;
}
public String getColumnName(int columnIndex) {
if (columnIndex == 0) {
return Messages.getMessage(Messages.eventVariety);
} else {
return Messages.getMessage(Messages.eventPlants);
}
}
public int getRowCount() {
return plants.getVarieties().size();
}
public Object getValueAt(int row, int col) {
if (col == 0) {
// Variety
return plants.getVarieties().get(row);
} else {
// Plants
return plants.getPlants(plants.getVarieties().get(row));
}
}
public boolean isCellEditable(int row, int col) {
return col != 0;
}
public void setValueAt(Object valueObject, int row, int col) {
if(col==0){
}else{
int value;
try{
value=Integer.parseInt((String)valueObject);
}catch(java.lang.NumberFormatException e){return;}
plants.setPlants(plants.getVarieties().get(row),value);
}
}
}
class PlantsViewerAddVariety extends JPopupMenu {
private static final long serialVersionUID = 1L;
private PanelEventPlants panel;
/**
* This is the default constructor
*
* @param hardwares Vector where new hardware will be added
*/
public PlantsViewerAddVariety(PanelEventPlants panel) {
super();
this.panel = panel;
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
ContainerVector varietiesAvaible = Resources.getResources().getVarieties();
java.util.Iterator<Container> iterator=varietiesAvaible.iterator();
while(iterator.hasNext()){
Container ghost=(Container)iterator.next();
Variety variety;
try{variety=(Variety)ghost.getData();}
catch(ClassNotFoundException e){Messages.showException(e);variety=null;}
catch(IOException e){Messages.showException(e);variety=null;}
// Checking if variety type is added.
Plants plants=(Plants)panel.getEvent();
if (!plants.getVarieties().contains(variety)) {
// Adding jMenu
JMenuItem jMenuItem = new JMenuItem();
jMenuItem.setText(variety.getName());
jMenuItem.setName(variety.toString());
jMenuItem
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(
java.awt.event.ActionEvent e) {
JMenuItem jMenuItem = (JMenuItem) e.getSource();
ContainerVector varietiesAvaible = Resources.getResources().getVarieties();
java.util.Iterator<Container> iterator=varietiesAvaible.iterator();
while(iterator.hasNext()){
Container ghost=(Container)iterator.next();
Variety variety;
try{variety=(Variety)ghost.getData();}
catch(ClassNotFoundException ex){Messages.showException(ex);variety=null;}
catch(IOException ex){Messages.showException(ex);variety=null;}
if(variety.toString().equals(jMenuItem.getName())){
Plants plants=(Plants)panel.getEvent();
plants.setPlants(variety,1);
// Refresh
panel.setEvent(panel.getEvent());
}
}
}
});
add(jMenuItem);
}
}
}
}