/* 
 * Proview   Open Source Process Control.
 * Copyright (C) 2005-2016 SSAB EMEA AB.
 *
 * This file is part of Proview.
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation, either version 2 of 
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with Proview. If not, see <http://www.gnu.org/licenses/>
 *
 * Linking Proview statically or dynamically with other modules is
 * making a combined work based on Proview. Thus, the terms and 
 * conditions of the GNU General Public License cover the whole 
 * combination.
 *
 * In addition, as a special exception, the copyright holders of
 * Proview give you permission to, from the build function in the
 * Proview Configurator, combine Proview with modules generated by the
 * Proview PLC Editor to a PLC program, regardless of the license
 * terms of these modules. You may copy and distribute the resulting
 * combined work under the terms of your choice, provided that every 
 * copy of the combined work is accompanied by a complete copy of 
 * the source code of Proview (the version used to produce the 
 * combined work), being distributed under the terms of the GNU 
 * General Public License plus this exception.
 */

package jpwr.jop;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import jpwr.rt.Mh;
import jpwr.rt.MhData;
import jpwr.rt.MhrEvent;


/* The EventTableModel is an AbstractTableModel designed to keep the 
 *result from a search made with the AlertSearch engine. The result
 *to present is contained in the MhData mhdata.*/

  class EventTableModel extends AbstractTableModel {     
  
      MhData mhData = new MhData(0,100);  
      String[] columnNamesEventTable = {"Prio","Type","Time","Event Text","Object"};

      public final Object[] longValues = {"A", "Acknowledge",
					  "10-12-31 12:12:12.98",
					  "QWERTYUIOPAAOLK_JHGFDSAZXCVBNM__POAIUYTRQWERTYUIOAAOL",
					  "QWERTYUIOPAAOLK"};


      /**
       *  Constructor for the EventTableModel object
       */
      public EventTableModel() {}
    
      /**
       *  Gets the columnCount attribute of the EventTableModel object
       *
       *@return    The columnCount value
       */
      public int getColumnCount() {
	  return columnNamesEventTable.length;
      }


      /**
       *  Gets the rowCount attribute of the EventTableModel object
       *
       *@return    The rowCount value
       */
      public int getRowCount() {
	  return mhData.getNrOfEvents();
      }


      /**
       *  Gets the columnName attribute of the EventTableModel object
       *
       *@param  col  Description of the Parameter
       *@return      The columnName value
       */
      public String getColumnName(int col) {
	  return JopLang.transl((String)columnNamesEventTable[col]);
      }


      /**
       *  Gets the valueAt attribute of the EventTableModel object
       *
       *@param  row  Description of the Parameter
       *@param  col  Description of the Parameter
       *@return      The valueAt value
       */
      public Object getValueAt(int row, int col) {
	  try {
	      MhrEvent ev = mhData.getEvent(row);//(MhrEvent)eventData.get(row);
	      if (col == 0) {
		  //System.out.println("col == 0 i eventTable.getValueAt()");
		  if ( ev.eventType ==  Mh.mh_eEvent_Info ||
		       ev.eventType ==  Mh.mh_eEvent_InfoSuccess)
		      return "";
		  if(ev.eventPrio == Mh.mh_eEventPrio_A)
		      return "A";
		  if(ev.eventPrio == Mh.mh_eEventPrio_B)
		      return "B";
		  if(ev.eventPrio == Mh.mh_eEventPrio_C)
		      return "C";
		  if(ev.eventPrio == Mh.mh_eEventPrio_D)
		      return "D";
		  else
		      return "U";
	      }
	      if (col == 1) {
		  String returnString = " ";
		  switch (ev.eventType) {
		  case Mh.mh_eEvent_Alarm:
		      returnString = "Alarm";
		      break;
		  case Mh.mh_eEvent_MaintenanceAlarm:
		      returnString = "MaintenenaceAlarm";
		      break;
		  case Mh.mh_eEvent_SystemAlarm:
		      returnString = "SystemAlarm";
		      break;
		  case Mh.mh_eEvent_UserAlarm1:
		      returnString = "UserAlarm1";
		      break;
		  case Mh.mh_eEvent_UserAlarm2:
		      returnString = "UserAlarm2";
		      break;
		  case Mh.mh_eEvent_UserAlarm3:
		      returnString = "UserAlarm3";
		      break;
		  case Mh.mh_eEvent_UserAlarm4:
		      returnString = "UserAlarm4";
		      break;
		  case Mh.mh_eEvent_Ack:
		      returnString = "Acknowledge";
		      break;
		  case Mh.mh_eEvent_Block:
		      returnString = "Block";
		      break;
		  case Mh.mh_eEvent_Cancel:
		      returnString = "Cancel";
		      break;
		  case Mh.mh_eEvent_CancelBlock:
		      returnString = "CancelBlock";
		      break;
		  case Mh.mh_eEvent_Missing:
		      returnString = "Missing";
		      break;
		  case Mh.mh_eEvent_Reblock:
		      returnString = "Reblock";
		      break;
		  case Mh.mh_eEvent_Return:
		      returnString = "Return";
		      break;
		  case Mh.mh_eEvent_Unblock:
		      returnString = "Unblock";
		      break;
		  case Mh.mh_eEvent_Info:
		      returnString = "Info";
		      break;
		  case Mh.mh_eEvent_InfoSuccess:
		      returnString = "InfoSuccess";
		      break;
		  case Mh.mh_eEvent_:
		      returnString = "?";
		      break;
		  default:
		      returnString = " ";
		      break;
		  }
		  return returnString;
	      }
	      if (col == 2) {
		  return ev.eventTime;
	      }
	      if (col == 3) {
		  return ev.eventText;
	      }
	      if (col == 4) {
		  return ev.eventName;
	      }
	  }

	  catch(ArrayIndexOutOfBoundsException e) {
	      System.out.println(e.toString());
	      if (col == 1) {
		  return new Boolean(false);
	      }
	  }
	  return "FEEELLLL";
      }


      /**
       *  Gets the columnClass attribute of the EventTableModel object
       *
       *@param  c  Description of the Parameter
       *@return    The columnClass value
       */
      public Class getColumnClass(int c) {
	  return longValues[c].getClass();
      }


      /**
       *  Sets the valueAt attribute of the EventTableModel object
       *
       *@param  value  The new valueAt value
       *@param  row    The new valueAt value
       *@param  col    The new valueAt value
       */
      public void setValueAt(Object value, int row, int col) {
      }


      /**
       *  Gets the rowObject attribute of the EventTableModel object
       *
       *@param  row  Description of the Parameter
       *@return      The rowObject value
       */
      public MhrEvent getRowObject(int row) {
	  try {
	      return mhData.getEvent(row);//(MhrEvent)eventData.get(row);
	  }
	  catch(ArrayIndexOutOfBoundsException e) {
	      System.out.println("eventable.getRowObject " + e.toString());
	  }
	  return null;
      }


      public void rowInserted() {
	  fireTableRowsInserted(0, 0);
      }


      public void rowRemoved(int row) {
	  fireTableRowsDeleted(row, row);	  
      }


      public void reloadTable() {
	  fireTableStructureChanged();
      }


      public void updateTable() {
	  fireTableDataChanged();
      }
    
      public void setMhData(MhData data) {
	  mhData=data;
	  updateTable();
      }


  }