Package net.sf.cannagrower.gui

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

package net.sf.cannagrower.gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import java.util.Date;

import net.sf.cannagrower.adviser.plantation.PlantationAdvicesFinder;
import net.sf.cannagrower.data.Event;
import net.sf.cannagrower.data.Plantation;
import net.sf.cannagrower.i18n.Messages;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import net.sf.cannagrower.util.DateChooser;

import net.sf.orexio.common.ui.Tools;
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 java.awt.GridLayout;
import java.awt.FlowLayout;

public class PanelPlantation extends JPanel implements DataListener{

  private static final long   serialVersionUID = 1L;
 
  Plantation          plantation;
 
  private JPanel         jPanelHeader = null;

  private JPanel jPanelHeaderLabel = null;

  private JLabel jLabelStarted = null;

  private JPanel jPanelStartedDate = null;

  private DateChooser dateChooserStarted = null;

  private JLabel jLabelFinished = null;

  private JPanel jPanelFinishedDate = null;

  private DateChooser dateChooserFinished = null;

  private JButton jButtonFinishedClear = null;

  private JList jListEvents = null;

  private JPanel jPanelEventsList = null;

  private JPanel jPanelEventsListEdit = null;

  private JButton jButtonEventsAdd = null;

  private JButton jButtonEventsRemove = null;

  private JSplitPane jSplitPaneEvents = null;

  private JPanel jPanelEventsDetails = null;
 
  private PanelEvent jPanelEventsDetailsViewer = null;

  private JScrollPane jScrollPaneEvents = null;

  private JPanel jPanelHeaderValue = null;

  private JPanel jPanelHeaderWest = null;

  private JButton jButtonEmpty = null;

  private JPanel jPanelFeatures = null;

  private JButton jButtonFeaturesAdvices = null;

  private JButton jButtonFeaturesStatistics = null;

  /**
   * This is the default constructor
   */
  public PanelPlantation(Plantation plantation){
    super();
    this.plantation=plantation;
    initialize();
  }
 
  public void dataModification(Data data) {
    Class<?> dataClass;
   
    // Checking if data ancestor
    dataClass=data.getClass();
    while(true){
      if(dataClass==null){break;}
      if (dataClass.equals(Event.class) || dataClass.equals(Plantation.class)) {
        // An event was updated (or added/removed)
        SwingUtilities.invokeLater(new Runnable(){
          public void run() {
            java.util.Collections.sort(plantation.getEvents());
            jListEvents.updateUI();
            eventShow();
          }
        });
        break;
      }
      dataClass=dataClass.getSuperclass();
    }
  }

  /**
   * This method initializes this
   *
   * @return void
   */
  private void initialize() {
    this.setSize(300, 200);
    this.setLayout(new BorderLayout());
    this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    this.add(getJSplitPaneEvents(), BorderLayout.CENTER);
    this.add(getJPanelHeader(), BorderLayout.NORTH);
    this.add(getJPanelFeatures(), BorderLayout.SOUTH);
   
  }

  private void advicesOpen(){
    PlantationAdvicesFinder advicesFinder=new PlantationAdvicesFinder(plantation);
    DialogAdvices      advices=new DialogAdvices(Tools.getFrame(this),advicesFinder.getAdvices());
    advices.setLocationRelativeTo(this);
    advices.setModal(true);
    advices.setVisible(true);
  }
 
  private void eventAdd(){
    PopupMenuEvent jPopupMenuHardwaresAdd = new PopupMenuEvent(this);
    jPopupMenuHardwaresAdd.show(jButtonEventsAdd, 0, 0);
  }
 
  private void eventRemove(){
    if (!jListEvents.isSelectionEmpty()) {
      try {
        for (Object eventObject : jListEvents.getSelectedValues()) {
          Event event= (Event) eventObject;
          plantation.getEvents().remove(event);
        }
      } catch (ArrayIndexOutOfBoundsException e) {
      }
      eventShow();
      jListEvents.updateUI();
    }
  }
 
  public void eventShowFirst(){
    //  Opening first event
    if(getJListEvents().getModel().getSize()>0){
      getJListEvents().setSelectedIndex(0);
    }
  }
  public void eventShow(){
    Event event=null;
   
    // Retrieve selected event
    if (jListEvents.getSelectedIndex() >= 0
        && jListEvents.getSelectedIndex() < jListEvents
            .getModel().getSize()) {
      EventsListModel listModel=(EventsListModel)jListEvents.getModel();
      event=listModel.getDataAt(jListEvents.getSelectedIndex());
    }   
   
    if (jPanelEventsDetailsViewer != null) {
      // Checking if selected event is already active
      if(event!=null && jPanelEventsDetailsViewer.getEvent() != null){
        if(event==jPanelEventsDetailsViewer.getEvent()){return;}
      }
     
      // Cleaning active event
      jPanelEventsDetailsViewer.setVisible(false);
      jPanelEventsDetails.remove(jPanelEventsDetailsViewer);
      jPanelEventsDetailsViewer = null;
    }
   
    // Loading selected event
    if (event!=null){
      jPanelEventsDetailsViewer = event.getViewer();
      jPanelEventsDetailsViewer.setEvent(event);
      jPanelEventsDetails.add(jPanelEventsDetailsViewer, BorderLayout.CENTER);
      jPanelEventsDetails.updateUI();
    }
  }

  private void statisticsOpen(){
    DialogPlantationStatistics   statistics=new DialogPlantationStatistics(Tools.getFrame(this),plantation);
    statistics.setLocationRelativeTo(this);
    statistics.setModal(true);
    statistics.setVisible(true);
  }
 
  /**
   * This method initializes jPanelHeader 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelHeader() {
    if (jPanelHeader == null) {
      jPanelHeader = new JPanel();
      jPanelHeader.setLayout(new BorderLayout());
      jPanelHeader.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
      jPanelHeader.add(getJPanelHeaderWest(), BorderLayout.WEST);
    }
    return jPanelHeader;
  }

  /**
   * This method initializes jPanelHeaderCenter 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelHeaderLabel() {
    if (jPanelHeaderLabel == null) {
      GridLayout gridLayout = new GridLayout();
      gridLayout.setRows(2);
      gridLayout.setColumns(1);
      jPanelHeaderLabel = new JPanel();
      jPanelHeaderLabel.setLayout(gridLayout);
      jLabelStarted = new JLabel();
      jLabelStarted.setText(Messages.getMessage(Messages.plantationStarted)+"     ");
      jPanelHeaderLabel.add(jLabelStarted, null);
      jLabelFinished = new JLabel();
      jLabelFinished.setText(Messages.getMessage(Messages.plantationFinished)+"     ");
      jPanelHeaderLabel.add(jLabelFinished, null);
     
    }
    return jPanelHeaderLabel;
  }

  /**
   * This method initializes jPanelStartedDate 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelStartedDate() {
    if (jPanelStartedDate == null) {
      jPanelStartedDate = new JPanel();
      jPanelStartedDate.setLayout(new BorderLayout());
      jPanelStartedDate.add(getDateChooserStarted(), BorderLayout.CENTER);
      jPanelStartedDate.add(getJButtonEmpty(), BorderLayout.EAST);
    }
    return jPanelStartedDate;
  }

  /**
   * This method initializes jDateChooserStarted 
   *  
   * @return com.toedter.calendar.JDateChooser 
   */
  private DateChooser getDateChooserStarted() {
    if (dateChooserStarted == null) {
      dateChooserStarted = new DateChooser();
      dateChooserStarted.setDate(plantation.getStarted());
      dateChooserStarted.addPropertyChangeListener("date",
          new java.beans.PropertyChangeListener() {
        public void propertyChange(java.beans.PropertyChangeEvent e) {
          Date date=dateChooserStarted.getDate();
          if (date == null) {
            JOptionPane.showMessageDialog(null,Messages.getMessage(Messages.messageDateInvalid),plantation.toString(),JOptionPane.OK_OPTION);
            dateChooserStarted.setDate(plantation.getStarted(),false);
            return;
          } else {
            plantation.setStarted(date);
          }
        }
      });
    }
    return dateChooserStarted;
  }

  /**
   * This method initializes jPanelFinishedDate 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelFinishedDate() {
    if (jPanelFinishedDate == null) {
      jPanelFinishedDate = new JPanel();
      jPanelFinishedDate.setLayout(new BorderLayout());
      jPanelFinishedDate.add(getDateChooserFinished(), BorderLayout.CENTER);
      jPanelFinishedDate.add(getJButtonFinishedClear(), BorderLayout.EAST);
    }
    return jPanelFinishedDate;
  }

  /**
   * This method initializes jDateChooserFinished 
   *  
   * @return com.toedter.calendar.JDateChooser 
   */
  private DateChooser getDateChooserFinished() {
    if (dateChooserFinished == null) {
      dateChooserFinished = new DateChooser();
      dateChooserFinished.setDate(plantation.getFinished());
      dateChooserFinished.addPropertyChangeListener("date",
          new java.beans.PropertyChangeListener() {
        public void propertyChange(java.beans.PropertyChangeEvent e) {
          Date date = dateChooserFinished.getDate();
          plantation.setFinished(date);
        }
      });
    }
    return dateChooserFinished;
  }

  /**
   * This method initializes jButtonFinishedClear 
   *  
   * @return javax.swing.JButton 
   */
  private JButton getJButtonFinishedClear() {
    if (jButtonFinishedClear == null) {
      jButtonFinishedClear = new JButton();
      jButtonFinishedClear.setIcon(new ImageIcon(getClass().getResource("/net/sf/cannagrower/images/clear_left_16.png")));
      jButtonFinishedClear.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          plantation.setFinished(null);
          dateChooserFinished.setDate(plantation.getFinished(),false);
        }
      });
    }
    return jButtonFinishedClear;
  }

  /**
   * This method initializes jListEvents 
   *  
   * @return javax.swing.JList 
   */
  public JList getJListEvents() {
    if (jListEvents == null) {
      jListEvents = new JList();
      jListEvents.setModel(new EventsListModel(plantation.getEvents()));
      jListEvents.setCellRenderer(new EventsListCellRenderer());
      jListEvents
          .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent e) {
              eventShow();
            }
          });
    }
    return jListEvents;
  }

  /**
   * This method initializes jPanelEventsList 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelEventsList() {
    if (jPanelEventsList == null) {
      jPanelEventsList = new JPanel();
      jPanelEventsList.setLayout(new BorderLayout());
      jPanelEventsList.add(getJScrollPaneEvents(), BorderLayout.CENTER);
      jPanelEventsList.add(getJPanelEventsListEdit(), BorderLayout.EAST);
    }
    return jPanelEventsList;
  }

  /**
   * This method initializes jPanelEventsListEdit 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelEventsListEdit() {
    if (jPanelEventsListEdit == null) {
      jPanelEventsListEdit = new JPanel();
      jPanelEventsListEdit.setLayout(new BoxLayout(getJPanelEventsListEdit(), BoxLayout.Y_AXIS));
      jPanelEventsListEdit.add(getJButtonEventsAdd(), null);
      jPanelEventsListEdit.add(getJButtonEventsRemove(), null);
    }
    return jPanelEventsListEdit;
  }

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

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

  /**
   * This method initializes jSplitPaneEvents 
   *  
   * @return javax.swing.JSplitPane 
   */
  private JSplitPane getJSplitPaneEvents() {
    if (jSplitPaneEvents == null) {
      jSplitPaneEvents = new JSplitPane();
      jSplitPaneEvents.setDividerLocation(0.5);
      jSplitPaneEvents.setOneTouchExpandable(true);
      jSplitPaneEvents.add(getJPanelEventsDetails(),JSplitPane.RIGHT);
      jSplitPaneEvents.add(getJPanelEventsList(),JSplitPane.LEFT);
      jSplitPaneEvents.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    }
    return jSplitPaneEvents;
  }

  /**
   * This method initializes jPanelEventsDetails 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelEventsDetails() {
    if (jPanelEventsDetails == null) {
      jPanelEventsDetails = new JPanel();
      jPanelEventsDetails.setLayout(new BorderLayout());
      jPanelEventsDetails.setBorder(BorderFactory.createEtchedBorder());
    }
    return jPanelEventsDetails;
  }


  /**
   * This method initializes jScrollPaneEvents 
   *  
   * @return javax.swing.JScrollPane 
   */
  private JScrollPane getJScrollPaneEvents() {
    if (jScrollPaneEvents == null) {
      jScrollPaneEvents = new JScrollPane();
      jScrollPaneEvents.setViewportView(getJListEvents());
    }
    return jScrollPaneEvents;
  }

  public Plantation getPlantation() {
    return plantation;
  }

  /**
   * This method initializes jPanelCenter 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelHeaderValue() {
    if (jPanelHeaderValue == null) {
      GridLayout gridLayout1 = new GridLayout();
      gridLayout1.setRows(2);
      gridLayout1.setColumns(1);
      jPanelHeaderValue = new JPanel();
      jPanelHeaderValue.setLayout(gridLayout1);
      jPanelHeaderValue.add(getJPanelStartedDate(), null);
      jPanelHeaderValue.add(getJPanelFinishedDate(), null);
    }
    return jPanelHeaderValue;
  }

  /**
   * This method initializes jPanelHeaderWest 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelHeaderWest() {
    if (jPanelHeaderWest == null) {
      jPanelHeaderWest = new JPanel();
      jPanelHeaderWest.setLayout(new BorderLayout());
      jPanelHeaderWest.add(getJPanelHeaderLabel(), BorderLayout.WEST);
      jPanelHeaderWest.add(getJPanelHeaderValue(), BorderLayout.CENTER);
    }
    return jPanelHeaderWest;
  }

  /**
   * This method initializes jButtonEmpty 
   *  
   * @return javax.swing.JButton 
   */
  private JButton getJButtonEmpty() {
    if (jButtonEmpty == null) {
      jButtonEmpty = new JButton();
      jButtonEmpty.setIcon(new ImageIcon(getClass().getResource("/net/sf/cannagrower/images/clear_left_16.png")));
      jButtonEmpty.setEnabled(false);
    }
    return jButtonEmpty;
  }

  /**
   * This method initializes jPanelFeatures 
   *  
   * @return javax.swing.JPanel
   */
  private JPanel getJPanelFeatures() {
    if (jPanelFeatures == null) {
      jPanelFeatures = new JPanel();
      jPanelFeatures.setLayout(new FlowLayout());
      jPanelFeatures.add(getJButtonFeaturesStatistics(), null);
      jPanelFeatures.add(getJButtonFeaturesAdvices(), null);
    }
    return jPanelFeatures;
  }

  /**
   * This method initializes jButtonFeaturesAdvices 
   *  
   * @return javax.swing.JButton 
   */
  private JButton getJButtonFeaturesAdvices() {
    if (jButtonFeaturesAdvices == null) {
      jButtonFeaturesAdvices = new JButton();
      jButtonFeaturesAdvices.setText(Messages.getMessage(Messages.plantationAdvices));
      jButtonFeaturesAdvices.setIcon(new ImageIcon(getClass()
          .getResource("/net/sf/cannagrower/images/help_16.png")));
      jButtonFeaturesAdvices.setEnabled(true);
      jButtonFeaturesAdvices.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          advicesOpen();
        }
      });
    }
    return jButtonFeaturesAdvices;
  }

  /**
   * This method initializes jButtonFeaturesStatistics 
   *  
   * @return javax.swing.JButton 
   */
  private JButton getJButtonFeaturesStatistics() {
    if (jButtonFeaturesStatistics == null) {
      jButtonFeaturesStatistics = new JButton();
      jButtonFeaturesStatistics.setText(Messages.getMessage(Messages.plantationStatistics));
      jButtonFeaturesStatistics.setIcon(new ImageIcon(getClass()
          .getResource("/net/sf/cannagrower/images/klipper_dock_16.png")));
      jButtonFeaturesStatistics
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              statisticsOpen();
            }
          });
    }
    return jButtonFeaturesStatistics;
  }
}

class EventsListCellRenderer 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
    {
      Container    container=(Container)value;
   
      setText(container.toString());
     
      try{setIcon(Event.getIcon(Class.forName(container.getType())));}
      catch(ClassNotFoundException e){}
       
        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;
    }
}

class EventsListModel extends javax.swing.AbstractListModel {
  private static final long serialVersionUID = 1L;

  private ContainerVector events;

  EventsListModel(ContainerVector events) {
    this.events = events;
  }

  public Object getElementAt(int index) {
    return events.get(index);
  }

  public Event getDataAt(int index){
    Container   container=events.get(index);
    Event    event=null;
   
    try{
      event=(Event)container.getData();
    }catch(ClassNotFoundException e){
      Messages.showException(e);
    }catch(java.io.IOException e){
      Messages.showException(e);
    }
   
    return event;
  }
  public int getSize() {
    int size = 0;
    size = events.size();
    return size;
  }
}
TOP

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

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.