/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

package ipipan.poliqarp.gui;

import java.util.Map;
import java.util.Vector;
import java.util.List;

import javax.swing.*;
import javax.swing.table.*;

import ipipan.poliqarp.logic.Corpus;
import ipipan.poliqarp.logic.MetaData;
import ipipan.poliqarp.logic.MetaDataValue;

import ipipan.poliqarp.util.Pair;

/**
 * A model for a <code>MetaDataTable</code>.
 */
public class MetaDataModel extends AbstractTableModel
{
   private MetaData meta;
   private String[] columnNames;
   
   private Vector<Pair<String, String> > data;
   
   /**
    * Constructs a default model for a metadata table.
    */
   public MetaDataModel()
   {
      super();
      meta = null;
      columnNames = new String[2];
   }

   /**
    * Sets the source of data for this model.
    * @param meta the metadata set that will be used as source
    */
   public void setSource(Corpus corpus, MetaData meta)
   {
      this.meta = meta;
      if (meta == null) {
         fireTableDataChanged();
         return;
      }
      List<Pair<MetaData.Type, String> > kinds = null;
      try {
         kinds = corpus.getMetaTypes();
      } catch (Exception e) {
         e.printStackTrace();
      }
      data = new Vector<Pair<String, String> >();
      for (Pair<MetaData.Type, String> k : kinds) {
         List<MetaDataValue> vals = meta.get(k.second());
         if (vals == null)
            continue;
         StringBuilder s = new StringBuilder();
         boolean firstdone = false;
         for (MetaDataValue v : vals) {
            if (firstdone)
               s.append("; ");
            else
               firstdone = true;
            s.append(v.toString());
         }
         data.add(new Pair<String, String>(k.second(), s.toString())); 
      }
         
      fireTableDataChanged();
   }
   
   /**
    * Always returns <code><b>false</b></code>, since no cells in a metadata
    * table can be edited.
    */
   public boolean isCellEditable(int x, int y)
   {
      return false;
   }

   /**
    * Updates the column names for this model.
    */
   public void setColumnNames(Configuration config)
   {
      for (int i = 0; i < 2; i++)
         columnNames[i] = config.getComponentName("TEXT_METADATA_COL" + i);
      fireTableStructureChanged();
   }

   /**
    * Returns the number of columns in this table -- always two.
    */
   public int getColumnCount()
   {
      return 2;
   }

   /**
    * Returns the number of rows in this table.
    */
   public int getRowCount()
   {
      if (meta == null)
         return 0;
      return data.size();
   }

   /**
    * Returns the name of <code>index</code>th column.
    */
   public String getColumnName(int index)
   {
      return columnNames[index];
   }
   
   /**
    * Returns the string displayed at the given place of a table.
    */
   public Object getValueAt(int row, int col)
   {
      if (meta == null)
         return "";
      Pair<String, String> pair = data.get(row);
      return col == 0 ? pair.first() : pair.second();
   }
}
