ALMA Computing Group

MyTableModel.java

Go to the documentation of this file.
00001 package alma.demo.dyncomp; 00002 00003 /* 00004 * Created on Oct 29, 2003 00005 * 00006 * To change the template for this generated file go to 00007 * Window - Preferences - Java - Code Generation - Code and Comments 00008 */ 00009 00010 import javax.swing.table.AbstractTableModel; 00011 import javax.swing.JButton; 00012 00019 public class MyTableModel extends AbstractTableModel 00020 { 00022 private final int ROWNUM=32; 00023 private final int COLNUM=3; 00024 00028 final String colNames[] = { 00029 "Dynamic component", 00030 "cUrl", 00031 "" 00032 }; 00033 00038 Object tableData[][]; 00039 00044 public MyTableModel() { 00045 tableData = new Object[ROWNUM][COLNUM]; 00046 for (int r=0; r<ROWNUM; r++) 00047 for (int j=0; j<COLNUM; j++) 00048 tableData[r][j]=null; 00049 } 00050 00058 public Object getValueAt(int row, int column) { 00059 return tableData[row][column]; 00060 } 00061 00066 public int getRowCount() { 00067 return ROWNUM; 00068 } 00069 00074 public int getColumnCount() { 00075 return COLNUM; 00076 } 00077 00086 public void append(String name,String cUrl) { 00087 // Scans the table to find the first free cell in the table 00088 int freePos=0; 00089 while (freePos<ROWNUM && tableData[freePos][0]!=null) freePos++; 00090 if (freePos<ROWNUM) { 00091 // Set the name at column 0 00092 setValueAt((Object)name,freePos,0); 00093 // Set the cUrl at col 1 00094 setValueAt((Object)cUrl,freePos,1); 00095 // Set the button at column 2 00096 JButton btn =new JButton("Release "+name); 00097 setValueAt((Object)btn,freePos,2); 00098 btn.setVisible(true); 00099 } 00100 } 00101 00108 public Class getColumnClass(int c) { 00109 if (c==0 || c==1) try { 00110 return Class.forName("java.lang.String"); 00111 } catch (ClassNotFoundException cnfe) { System.err.println(cnfe.toString()); return null; } 00112 else if (c==2) try { 00113 return Class.forName("javax.swing.JButton"); 00114 } catch (ClassNotFoundException cnfe) { System.err.println(cnfe.toString()); return null; } 00115 return null; 00116 } 00117 00118 00125 public boolean exist(String name) { 00126 for (int t=0; t<ROWNUM; t++) { 00127 if (tableData[t][0]!=null) 00128 if (name.compareTo((String)tableData[t][0])==0) return true; 00129 } 00130 return false; 00131 } 00132 00137 public void deleteEntry(String url) { 00138 int t; 00139 for (t=0; t<ROWNUM; t++) { 00140 if (tableData[t][1]!=null) 00141 if (url.compareTo((String)tableData[t][1])==0) break; 00142 } 00143 if (t<ROWNUM) { 00144 setValueAt(null,t,0); 00145 setValueAt(null,t,1); 00146 setValueAt(null,t,2); 00147 } 00148 } 00149 00152 public void sort() { 00153 int a=0; 00154 int b; 00155 while (a<ROWNUM-2) { 00156 b=a+1; 00157 while (b<ROWNUM-1 && tableData[b][0]==null) b++; 00158 if (b<ROWNUM-1) { 00159 setValueAt((Object)tableData[b][0],a,0); 00160 setValueAt((Object)tableData[b][1],a,1); 00161 setValueAt((Object)tableData[b][2],a,2); 00162 setValueAt(null,b,0); 00163 setValueAt(null,b,1); 00164 setValueAt(null,b,2); 00165 a++; 00166 } else { 00167 // No more items found 00168 break; 00169 } 00170 } 00171 // The last one is always null 00172 setValueAt(null,ROWNUM-1,0); 00173 setValueAt(null,ROWNUM-1,1); 00174 setValueAt(null,ROWNUM-1,2); 00175 } 00176 00183 public void setValueAt(Object obj, int row, int col) { 00184 tableData[row][col]=obj; 00185 fireTableCellUpdated(row,col); 00186 } 00187 00192 public String getColumnName(int col) { 00193 return colNames[col]; 00194 } 00195 00201 public boolean isCellEditable(int row, int col) { 00202 if (tableData[row][col]==null) return false; 00203 if (col==0 || col==1) return false; 00204 else return true; 00205 } 00206 00213 public String getURL(String name) { 00214 for (int t=0; t<ROWNUM; t++){ 00215 if (tableData[t][0]!=null) { 00216 if (((String)(tableData[t][0])).compareTo(name)==0) { 00217 return (String)tableData[t][1]; 00218 } 00219 } 00220 } 00221 return null; 00222 } 00223 00224 } 00225