View Javadoc
1 package edu.psu.geovista.app.spreadsheet.table; 2 3 /* 4 * Description: 5 * Date: Mar 13, 2003 6 * Time: 1:12:36 PM 7 * @author Jin Chen 8 */ 9 10 import edu.psu.geovista.app.spreadsheet.formula.Cell; 11 import edu.psu.geovista.app.spreadsheet.formula.FormulaEditor; 12 13 import javax.swing.*; 14 import javax.swing.border.Border; 15 import javax.swing.plaf.UIResource; 16 import javax.swing.event.ListSelectionEvent; 17 import javax.swing.event.TableModelEvent; 18 import javax.swing.event.ChangeEvent; 19 import javax.swing.table.*; 20 import java.awt.*; 21 import java.util.Vector; 22 import java.util.Enumeration; 23 24 import edu.psu.geovista.app.spreadsheet.util.Debug; 25 import edu.psu.geovista.app.spreadsheet.SpreadSheetBean; 26 import edu.psu.geovista.app.spreadsheet.functions.FunctionManager; 27 import edu.psu.geovista.app.spreadsheet.event.SSTableModelEvent; 28 29 public class SSTable extends JTable { 30 31 private FormulaEditor fe; //on tool bar 32 private Object bean;// 33 private FunctionManager funManager; 34 35 //edu.psu.geovista.app.spreadsheet.model.TableColumnSort tcs; 36 public SSTable(SSTableModel tm) { 37 super(tm); 38 tm.setTable(this); 39 funManager=new FunctionManager(this); 40 41 //tcs=new edu.psu.geovista.app.spreadsheet.model.TableColumnSort(this); 42 } 43 /********************************************************************* 44 * Event handler 45 ********************************************************************/ 46 47 /*** 48 * called by BasicTableUI$MouseInputHandler: table.changeSelection(row, column, e.isControlDown(), e.isShiftDown()); 49 * @param rowIndex affects the selection at <code>row</code> 50 * @param columnIndex affects the selection at <code>column</code> 51 */ 52 53 public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) { 54 55 super.changeSelection(rowIndex,columnIndex,toggle,extend); //MUST at beginning 56 if (!toggle&&!extend){//normal selecting without holding Ctrl and Shift 57 //Debug.println("select:"+rowIndex+","+columnIndex); 58 59 Cell cell=(Cell)this.getValueAt(rowIndex,columnIndex); 60 if(cell.isFormula() ){ 61 this.fe.getFormulaField().setText("="+cell.getFormula().toString() ); 62 } 63 else{ 64 this.fe.getFormulaField().setText(cell.toString() ); 65 } 66 67 String cellAddr=cell.getViewAddressText() ; 68 fe.getCellField().setText(cellAddr); 69 70 71 } 72 if (columnIndex<SSTableModel.BASE_COL&&rowIndex>=0) { 73 if (!toggle&&!extend){ 74 this.setColumnSelectionInterval(0,this.getColumnCount()-1 ); 75 this.setRowSelectionInterval(rowIndex,rowIndex); 76 return; 77 } 78 int selectedRow=this.getSelectedRow() ; 79 if (extend){ 80 setRowSelectionInterval(selectedRow , rowIndex); 81 this.setColumnSelectionInterval(0,this.getColumnCount()-1 ); 82 } 83 //Not need worry about toggle since if you have selected a row, the next selection would be a row 84 //this.setCellSelectionEnabled(false); 85 /*setColumnSelectionInterval(0, getColumnCount()-1); 86 setRowSelectionInterval(selectedRow, selectedRow); */ 87 /*setColumnSelectionAllowed(false); 88 setRowSelectionAllowed(true); 89 setCellSelectionEnabled(false); */ 90 } 91 else{ 92 //this.setCellSelectionEnabled(true); 93 } 94 95 96 } 97 98 public void setSelectedRow(int rowIndex){ 99 this.setColumnSelectionInterval(0,this.getColumnCount()-1 ); 100 this.setRowSelectionInterval(rowIndex,rowIndex); 101 102 } 103 public void addSelectedRow(int rowIndex){ 104 addRowSelectionInterval(rowIndex,rowIndex); 105 setColumnSelectionInterval(0,getColumnCount()-1 ); 106 107 } 108 public void tableChanged(TableModelEvent e) { 109 if (e.getType()==SSTableModelEvent.RESET_DATA ){ 110 clearSelection(); 111 //rowModel = null; 112 createDefaultColumnsFromModel(); 113 reSetRowHeader() ; 114 reSetColumnHeader() ; 115 return; 116 } 117 else{ 118 super.tableChanged(e) ; 119 } 120 121 } 122 123 public void valueChanged(ListSelectionEvent e) { 124 super.valueChanged(e); 125 if (bean instanceof SpreadSheetBean){ 126 ((SpreadSheetBean)bean).stateChanged(null); 127 128 } 129 } 130 /********************************************************************* 131 * Sort 132 ********************************************************************/ 133 public void sortTable(int col, boolean ascend){ 134 //tcs.sortRows(col,ascend); 135 } 136 public void clearSort(){ 137 ((SSTableModel)this.getModel()).clearSort() ; 138 this.revaluate() ; 139 this.reSetRowHeader(0);//do it from the every beginning 140 } 141 public void sort(boolean ascend){ 142 143 int[] rows= this.getSelectedRows() ; 144 int[] cols= this.getSelectedColumns() ; 145 146 int rs=rows.length ; // row selected 147 int cs=cols.length ; 148 int rc=this.getRowCount() ;//row count 149 int cc=this.getColumnCount() ;//col count 150 SSTableModel tm=(SSTableModel)this.getModel() ; 151 152 if (rs<=0||cs<=0) return; //Not select sorting range yet 153 if (rs==rc&&cs==cc){//select entire table 154 Debug.println("select entire table"); 155 156 int col=this.showSortDialog() ; 157 if (col<0) return;//cancel sort 158 /* 159 160 this.sortRows(col,false); 161 */ 162 int mcol=this.convertColumnIndexToModel(col); 163 tm.sortTable(mcol,ascend); 164 } 165 else if (cs==cc&&rs >0){ 166 // rs >0&&rs<rc 167 //select some rows 168 /*for (int i=0;i<rows.length ;i++){ 169 int row=rows[i]; 170 sr.setRow(row); 171 } 172 sr.setType(edu.psu.geovista.app.spreadsheet.model.SortRange.ROWS ); */ 173 int col=this.showSortDialog() ; 174 if (col<0) return;//cancel sort 175 /*this.createSortMap(0);//use table sort 176 this.sortRows(col,false); */ 177 int mcol=this.convertColumnIndexToModel(col); 178 tm.sortRows(mcol,rows,ascend); 179 Debug.println("select some rows"); 180 } 181 else if (rs==rc&&cs>0){//select some columns 182 /*for (int i=0;i<cols.length ;i++){ 183 int col=cols[i];//selected column in table view 184 sr.setColumn(col); 185 } 186 sr.setType(edu.psu.geovista.app.spreadsheet.model.SortRange.COLUMNS ); */ 187 int col=this.showSortDialog() ; 188 if (col<0) return;//cancel sort 189 /* 190 int[] selCols=table.getSelectedColumns() ; 191 for (int i=0;i<selCols.length ;i++){ 192 this.createSortMap(selCols[i]); 193 194 } */ 195 int mcol=this.convertColumnIndexToModel(col); 196 int[] mcols=new int[cols.length ]; 197 for (int i=0;i<mcols.length ;i++){ 198 mcols[i]=this.convertColumnIndexToModel(cols[i]); 199 } 200 tm.sortColumns(mcol,mcols,ascend); 201 Debug.println("select some columns"); 202 } 203 else{ 204 205 if (this.isArrayContinue(rows)&& 206 this.isArrayContinue(cols)){//a rectangle range 207 int col=this.showSortDialog() ; 208 if (col<0) return;//cancel sort 209 int mcol=this.convertColumnIndexToModel(col); 210 int[] mcols=new int[cols.length ]; 211 for (int i=0;i<mcols.length ;i++){ 212 mcols[i]=this.convertColumnIndexToModel(cols[i]); 213 } 214 int[]mrows=new int[rows.length ]; 215 for (int i=0;i<mrows.length ;i++){ 216 /*//mrows[i]=Cell.transRowViewToTable(rows[i]);// 217 //From table perspective, table' row index=Model's row index 218 //(which may be the sort edu.psu.geovista.app.spreadsheet.model instead of real edu.psu.geovista.app.spreadsheet.model) 219 Now rows[i] is table's row index=Model row index 220 */ 221 mrows[i]=rows[i]; 222 } 223 tm.sortRange(mcol,mrows, mcols,ascend); 224 Debug.println("select a range"); 225 226 227 } 228 else{ 229 //JOptionPane.showConfirmDialog(SpreadSheetBean.getTableInstance(),"Unable deal with the range"); 230 JOptionPane.showConfirmDialog(this,"Unable deal with the range"); 231 } 232 233 //select a range 234 /* 235 for (int i=0;i<cols.length ;i++){ 236 int col=cols[i];//selected column in table view 237 sr.setColumn(col); 238 } 239 for (int i=0;i<rows.length ;i++){ 240 int row=rows[i]; 241 sr.setRow(row); 242 243 } 244 sr.setType(edu.psu.geovista.app.spreadsheet.model.SortRange.RANGE ); 245 util.Debug.println("select a range"); */ 246 } 247 this.revaluate() ; 248 this.reSetRowHeader(0);//do it from the every beginning 249 250 } 251 /*** 252 * True if the element of array meet a[i+1]=a[i]+1; 253 */ 254 private boolean isArrayContinue(int[] a){ 255 if (a.length <=0)return false; 256 int ai=a[0]; 257 for (int i=1;i<a.length ;i++){ 258 if ((a[i]-ai)!=1){ 259 return false; 260 } 261 ai=a[i]; 262 263 } 264 return true; 265 266 } 267 268 269 /*** 270 * Ask to select a column based on which do sorting 271 * return the column be selected in Table View 272 * -1 mean cancel 273 */ 274 private int showSortDialog() { 275 int selectedCol; 276 int numOfCol=this.getColumnCount(); 277 String[] options=new String[numOfCol] ; 278 for (int i=0;i<options.length ;i++){ 279 String colName=translateVVColumn(i); 280 options[i]=colName; 281 } 282 //JFrame mf=(JFrame)SwingUtilities.getAncestorOfClass(JFrame.class,SpreadSheetBean.getTableInstance() ); 283 JFrame mf=(JFrame)SwingUtilities.getAncestorOfClass(JFrame.class,this ); 284 String selectedValue =(String) 285 JOptionPane.showInputDialog(mf ,"Sort by", "Sort", 286 JOptionPane.INFORMATION_MESSAGE ,null,options,options[1]) ; 287 if (selectedValue!=null){ 288 selectedCol=translateVVColumn(selectedValue); 289 } 290 else{ 291 selectedCol=-1; 292 } 293 294 Debug.println("selected column:"+selectedCol); 295 return selectedCol; 296 297 } 298 public void reSetRowHeader(int row){ 299 //TableColumnModel tcmd=tb.getColumnModel() ; 300 //TableColumn rowHeader=tcmd.getColumn(0); 301 for(int i=row;i<getRowCount() ;i++){ 302 int viewRow=i+1; 303 Integer v=new Integer(viewRow); 304 setValueAt(v,transRowViewToTable(viewRow) ,0); 305 } 306 } 307 public void reSetRowHeader(){ 308 TableColumnModel tcmd=getColumnModel() ; 309 TableColumn rowHeader=tcmd.getColumn(0); 310 rowHeader.setHeaderValue(""); 311 312 rowHeader.setPreferredWidth(30); 313 314 DefaultTableCellRenderer rhRender=new DefaultTableCellRenderer(); 315 rhRender.setBackground(Color.LIGHT_GRAY ); 316 rowHeader.setCellRenderer(rhRender); 317 for (int i=0;i<getRowCount() ;i++){ 318 Integer v=new Integer(i+1); 319 setValueAt(v,i,0); 320 } 321 } 322 323 /*** 324 * Ruturn value at cell in view(what you see ) 325 */ 326 public Object getValueAtIndex(int row, int column) { 327 int rowindex=transRowViewToTable(row); 328 return super.getValueAt(rowindex,column); 329 330 } 331 /*** 332 * Ruturn value at cell in view(what you see ) 333 */ 334 public void setValueAtIndex(Object value,int row, int column) { 335 int rowindex=transRowViewToTable(row); 336 super.setValueAt(value,rowindex,column); 337 338 } 339 //auto update edu.psu.geovista.app.spreadsheet.formula value 340 public void editingStopped(ChangeEvent e) { 341 Debug.println("table.editingStopped(ChangeEvent e) "); 342 super.editingStopped(e); 343 revaluate() ; 344 } 345 346 /********************************************************************* 347 * Temp for testing event 348 ******************************************************************* 349 public void tableChanged(TableModelEvent e) { 350 Debug.println("tableChanged.e.getType():"+e.getType()); 351 super.tableChanged(e); 352 } 353 public void valueChanged(ListSelectionEvent e) { 354 Debug.println(" valueChanged "+e.toString()); 355 super.valueChanged(e); 356 } 357 public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 358 Debug.println("row,column"+row+","+column); 359 return super.prepareRenderer(renderer,row,column) ; 360 } 361 public void columnSelectionChanged(ListSelectionEvent e) { 362 } */ 363 public FormulaEditor getFormulaEditor() { 364 return fe; 365 } 366 367 public void setFormulaEditor(FormulaEditor fe) { 368 this.fe = fe; 369 } 370 371 public void reSetColumnHeader() { 372 //Reset Header 373 TableColumnModel tcm=getColumnModel() ; 374 SSTableModel tm=(SSTableModel)this.getModel() ; 375 for (int i=1;i< tcm.getColumnCount() ;i++){ 376 TableColumn tbcol=tcm.getColumn(i); 377 //String colIndex= Cell.translateColumn(i); 378 //String colIndex= translateColumn(i); 379 //String colname=tm.getColumnName(Cell.transColViewToModel(i)) ; 380 String colname=tm.getColumnName(transColViewToModel(i)) ; 381 tbcol.setHeaderValue(colname ); 382 /* 383 if (colname!=null&&colname.trim().length() >0&& 384 !colIndex.equalsIgnoreCase(colname) ){ 385 String name="("+colIndex+")"+colname; 386 tbcol.setHeaderValue(name ); 387 } 388 else{ 389 tbcol.setHeaderValue(colIndex ); 390 } */ 391 } 392 } 393 394 395 public void revaluate(){ 396 super.repaint() ; 397 } 398 399 public Object getBean() { 400 return bean; 401 } 402 403 public void setBean(Object bean) { 404 this.bean = bean; 405 } 406 /********************************************************************* 407 * Model <=> View 408 ********************************************************************/ 409 public int transColModelToView(int modelCol){ 410 //JTable tb=SpreadSheetBean.getTableInstance() ; 411 //JTable tb=this.getDataModel().getTable() ; 412 //TableColumnModel tbm=tb.getColumnModel(); 413 TableColumnModel tbm=getColumnModel(); 414 Enumeration e=tbm.getColumns() ; 415 int i=0; 416 while(e.hasMoreElements() ){ 417 TableColumn tbcol=(TableColumn)e.nextElement() ; 418 if (tbcol.getModelIndex() ==modelCol){ 419 return i; 420 } 421 i++; 422 } 423 return i; 424 //return modelCol- edu.psu.geovista.app.spreadsheet.model.SSTableModel.BASE_COL +1 ;//jin 425 } 426 427 /*** 428 * Translate Column address from view(Table) to Model(TableModel) 429 * e.g. Table's row 1 => TableModel's row 0 . 1=>0 430 */ 431 public int transColViewToModel(int viewCol){ 432 //JTable tb=SpreadSheetBean.getTableInstance() ; 433 //JTable tb=this.getDataModel().getTable() ; 434 //TableColumnModel tbm=tb.getColumnModel(); 435 TableColumnModel tbm=getColumnModel(); 436 TableColumn tbcol=tbm.getColumn(viewCol); 437 int col=tbcol.getModelIndex() ; 438 return col; 439 //return viewCol + edu.psu.geovista.app.spreadsheet.model.SSTableModel.BASE_COL -1 ;//jin 440 } 441 /*** 442 * This translates the string form of column into column number ('A' -> 1) 443 * View->Model 444 * @param column the string value of the column to be converted 445 * @return the int column address in Model(TableModel) 446 */ 447 public int translateColumn(String column) { 448 int col=SSTable.translateVVColumn(column); 449 return transColViewToModel(col); 450 } 451 /*** 452 * This translates the int form of column into column string (1 -> 'A') 453 * 454 * @param column the col address in Model 455 * @return the string value of the column 456 */ 457 public String translateColumn(int column) { 458 //int col=transColModelToView(column); 459 int col=transColModelToView(column); 460 return SSTable.translateVVColumn(column); 461 } 462 /*** 463 * Translate Row address from Model(TableModel) to view(Table) 464 * e.g. TableModel's row 0 => Table's row0=> Table's row index 1 . 1=>0 465 */ 466 public static int transRowTableToView(int modelRow){ 467 return modelRow- SSTableModel.BASE_ROW+1 ;//jin 468 469 } 470 471 /*** 472 * Translate Row address from view(Index) to Table 473 * e.g. Table's row index 1=> Table's row 0 474 */ 475 public static int transRowViewToTable(int viewRow){ 476 return viewRow + SSTableModel.BASE_ROW-1 ;//jin 477 } 478 479 /*** 480 * This translates the string form of column into column number ('A' -> 1) 481 * 482 * @param column the string value of the column to be converted 483 * @return the int column address in view(Table) 484 */ 485 public static int translateVVColumn(String column) { 486 int col = 0; 487 488 for (int i = 0; i < column.length(); i++) { 489 col = col * 26 + (column.charAt(i) - 'A' + 1); 490 } 491 return col; 492 //return col + baseCol -1; 493 //return col+edu.psu.geovista.app.spreadsheet.model.SSTableModel.BASE_COL ; 494 } 495 496 /*** 497 * This translates the int form of column into column string (1 -> 'A') 498 * 499 * @param column the col address in view 500 * @return the string value of the column 501 */ 502 public static String translateVVColumn(int column) { 503 504 //column = column - baseCol +1; 505 //column = column - edu.psu.geovista.app.spreadsheet.model.SSTableModel.BASE_COL + 1; 506 if (column < 1) 507 return null; 508 509 StringBuffer buf = new StringBuffer(); 510 511 int div = 1; 512 513 while (div > 0) { 514 div = (column - 1) / 26; 515 buf.insert(0, (char) ('A' + (column - 1 - div * 26))); 516 column = div; 517 } 518 519 return buf.toString(); 520 } 521 522 public FunctionManager getFunManager() { 523 return funManager; 524 } 525 526 public void setFunManager(FunctionManager funManager) { 527 this.funManager = funManager; 528 } 529 }

This page was automatically generated by Maven