

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import cds.aladin.*;

public class CatalogBrowserPlug extends AladinPlugin implements TableModel {
   
   public String menu() { return "Catalog browser"; }
   public String description() {
      return "PLUGIN TUTORIAL:\n" +
      "This plugin is an example for manipulating the catalogs.\n" +
      "This plugin will display the content of the selected catalogn in a java JTable.";
   }   
   public String category()    { return "Plugin tutorial/Catalog"; }
   public String version()     { return "1.0 - January 2007"; }
   public String author()      { return "Pierre Fernique [CDS]"; }
   public String url()         { return "http://aladin.u-strasbg.fr/java/Plugins/CatalogBrowserPlug.java"; }
   
   // To memorise the concerned catalog
   private AladinData aladinCat=null;
   
   /**
    * 1) Memorize the AladinData associated to the selected catalog plane
    * 2) Create java Swing objects to display a JTable for which its model
    *    is this plugin itself
    */
   public void exec() {
      
      try {
         aladinCat = aladin.getAladinData();
         if( !aladinCat.getPlaneType().startsWith("Overlay/Catalog") ) throw new AladinException("Not a catalog plane");
      } catch( AladinException e ) { aladin.warning("Plugin error: "+e.getMessage());  }
      
      JFrame f = new JFrame();
      f.getContentPane().setLayout(new BorderLayout());
      f.getContentPane().add( new JScrollPane(new JTable(this)), "Center");
      f.pack();
      f.setVisible(true);
   }
   
   // JTable Model to fill the JTable
   
   public int getRowCount() {
      try { return aladinCat.getNbObj(); }
      catch( Exception e ) { return 0; }
   }
   
   public Object getValueAt(int rowIndex, int columnIndex) {
      try {
         Obj [] obj=aladinCat.seeObj();
         String [] f = obj[rowIndex].getValues();
         return f[columnIndex];
      } catch( Exception e ) { return ""; }
   }
   
   public String getColumnName(int columnIndex) {
      try {
         // We assume here that we have an homogeneous table
         // => all objects have the same names, units, ucds
         //    than the first one
         Obj obj = aladinCat.seeObj()[0];
         String [] names = obj.getNames();
         String [] units = obj.getUnits();
         String [] ucds = obj.getUCDs();
         return names[columnIndex]
                +" ("+units[columnIndex]+")"
                +" ["+ucds[columnIndex]+"]";
      } catch( Exception e ) { return ""; }
   }
   
   public int getColumnCount() {
      try {
         // We assume here that we have an homogeneous table
         Obj obj = aladinCat.seeObj()[0];
         return obj.getValues().length;
      } catch( Exception e ) { return 0; }
   }
   
   public Class getColumnClass(int columnIndex) { return String.class; }
   public boolean isCellEditable(int rowIndex, int columnIndex) { return false; }
   public void addTableModelListener(TableModelListener l) { }
   public void removeTableModelListener(TableModelListener l) { }
   public void setValueAt(Object aValue, int rowIndex, int columnIndex) { } 
   
   

}