ALMA Computing Group

MyTableModel.java

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * ALMA - Atacama Large Millimeter Array
00003  * Copyright (c) ESO - European Southern Observatory, 2011
00004  * (in the framework of the ALMA collaboration).
00005  * All rights reserved.
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020  *******************************************************************************/
00021 package alma.demo.dyncomp;
00022 
00023 /*
00024  * Created on Oct 29, 2003
00025  *
00026  * To change the template for this generated file go to
00027  * Window - Preferences - Java - Code Generation - Code and Comments
00028  */
00029  
00030 import javax.swing.table.AbstractTableModel;
00031 import javax.swing.JButton;
00032 
00039 public class MyTableModel extends AbstractTableModel 
00040 {
00042         private final int ROWNUM=32;
00043         private final int COLNUM=3;
00044         
00048         final String colNames[] = { 
00049                 "Dynamic component",
00050                 "cUrl",
00051                 ""
00052                  };
00053         
00058         Object tableData[][];
00059         
00064         public MyTableModel() {
00065                 tableData = new Object[ROWNUM][COLNUM];
00066                 for (int r=0; r<ROWNUM; r++)
00067                         for (int j=0; j<COLNUM; j++)
00068                                 tableData[r][j]=null;
00069         }
00070         
00078         public Object getValueAt(int row, int column) {
00079                 return tableData[row][column];
00080         }
00081         
00086         public int getRowCount() { 
00087                 return ROWNUM;
00088         }
00089         
00094         public int getColumnCount() {
00095                 return COLNUM;
00096         }
00097         
00106         public void append(String name,String cUrl) {
00107                 // Scans the table to find the first free cell in the table
00108                 int freePos=0;
00109                 while (freePos<ROWNUM && tableData[freePos][0]!=null) freePos++;
00110                 if (freePos<ROWNUM) {
00111                         // Set the name at column 0
00112                         setValueAt((Object)name,freePos,0);
00113                         // Set the cUrl at col 1
00114                         setValueAt((Object)cUrl,freePos,1);
00115                         // Set the button at column 2
00116                         JButton btn =new JButton("Release "+name);
00117                         setValueAt((Object)btn,freePos,2);
00118                         btn.setVisible(true);
00119                 }
00120         }
00121         
00128         public Class getColumnClass(int c) {
00129                 if (c==0 || c==1) try {
00130                         return Class.forName("java.lang.String");
00131                 } catch (ClassNotFoundException cnfe) { System.err.println(cnfe.toString()); return null; }
00132                 else if (c==2) try {
00133                         return Class.forName("javax.swing.JButton");
00134                 }  catch (ClassNotFoundException cnfe) {  System.err.println(cnfe.toString()); return null; }
00135                 return null;
00136         }
00137 
00138 
00145         public boolean exist(String name) {
00146                 for (int t=0; t<ROWNUM; t++) {
00147                         if (tableData[t][0]!=null)
00148                                 if (name.compareTo((String)tableData[t][0])==0) return true;
00149                 }
00150                 return false;
00151         }
00152 
00157         public void deleteEntry(String url) {
00158                 int t;
00159                 for (t=0; t<ROWNUM; t++) {
00160                         if (tableData[t][1]!=null)
00161                                 if (url.compareTo((String)tableData[t][1])==0) break;
00162                 }
00163                 if (t<ROWNUM) {
00164                         setValueAt(null,t,0);
00165                         setValueAt(null,t,1);
00166                         setValueAt(null,t,2);
00167                 }
00168         }
00169 
00172         public void sort() {
00173                 int a=0;
00174                 int b;
00175                 while (a<ROWNUM-2) {
00176                         b=a+1;
00177                         while (b<ROWNUM-1 && tableData[b][0]==null) b++;
00178                         if (b<ROWNUM-1) {
00179                                 setValueAt((Object)tableData[b][0],a,0);
00180                                 setValueAt((Object)tableData[b][1],a,1);
00181                                 setValueAt((Object)tableData[b][2],a,2);
00182                                 setValueAt(null,b,0);
00183                                 setValueAt(null,b,1);
00184                                 setValueAt(null,b,2);
00185                                 a++;
00186                         } else {
00187                                 // No more items found
00188                                 break;
00189                         }
00190                 }
00191                 // The last one is always null
00192                 setValueAt(null,ROWNUM-1,0);
00193                 setValueAt(null,ROWNUM-1,1);
00194                 setValueAt(null,ROWNUM-1,2);
00195         }
00196         
00203         public void setValueAt(Object obj, int row, int col) {
00204                 tableData[row][col]=obj;
00205                 fireTableCellUpdated(row,col);
00206         }
00207         
00212         public String getColumnName(int col) {
00213                 return colNames[col];
00214         } 
00215          
00221         public boolean isCellEditable(int row, int col) {
00222                 if (tableData[row][col]==null) return false;
00223                 if (col==0 || col==1) return false;
00224                 else return true;
00225         }
00226 
00233     public String getURL(String name) {
00234         for (int t=0; t<ROWNUM; t++){
00235             if (tableData[t][0]!=null) {
00236                 if (((String)(tableData[t][0])).compareTo(name)==0) {
00237                         return (String)tableData[t][1];
00238                 }
00239             }
00240         }
00241         return null;
00242     }
00243 
00244 }
00245