View Javadoc
1 package edu.psu.geovista.app.table; 2 3 4 import javax.swing.*; 5 import java.io.*; 6 import java.awt.*; 7 import java.awt.event.*; 8 import javax.swing.table.*; 9 import java.util.*; 10 import javax.swing.event.*; 11 import javax.swing.filechooser.*; 12 import java.net.*; 13 import java.awt.print.*; 14 import java.io.Serializable; 15 16 import edu.psu.geovista.data.geog.*; 17 import edu.psu.geovista.ui.event.*; 18 19 public class TableBrowser extends JPanel 20 implements ChangeListener,Serializable, 21 SelectionListener, DataSetListener 22 { 23 24 private Container contentPane= this; 25 // currentTable is the current working table 26 private JTable currentTable; 27 private JInternalFrame currentFrame=new JInternalFrame(); 28 private int model=0; 29 // control pane is defined providing the manipulations on the current table 30 private ControlPanel myControl=null; 31 private String currentTableTitle=""; 32 private MxmTableModel tableModel; 33 private JScrollPane struc ; 34 private Object[] columnName; 35 private int[] select; 36 protected transient EventListenerList listenerList = new EventListenerList(); 37 38 // initialization of the table browser 39 public TableBrowser() 40 { 41 42 //tableModel = new MxmTableModel(10,5); 43 //settableProperties(); 44 // initailized the GUI 45 //buildGUI(); 46 } 47 48 public void selectionChanged(SelectionEvent e){ 49 this.setSelectedIndex(e.getSelection()); 50 } 51 52 public void dataSetChanged(DataSetEvent e){ 53 this.setData(e.getDataSet()); 54 } 55 56 public void setColumnNames(Vector column) 57 { 58 Object[] col = new Object[column.size()]; 59 for (int i = 0; i< column.size(); i++) 60 { 61 col[i] = ((Object)column.elementAt(i)); 62 } 63 columnName = col; 64 //tableModel.setColumnIdentifiers(column); 65 } 66 public void setColumnNames(Object column) 67 { 68 Object[] column1 = (Object[]) column; 69 //System.out.println(column1.length + " is the number of columns"); 70 columnName = new Object[column1.length]; 71 columnName = column1; 72 //tableModel.setColumnIdentifiers(columnName); 73 } 74 public void setDataReverse(Vector data) 75 { 76 int rows = ((Vector)data.elementAt(0)).size(); 77 int col = data.size(); 78 Vector tmpVect = new Vector(); 79 Object[][] dataRev = new Object[rows][col]; 80 for (int j = 0; j<col ; j++) 81 { 82 tmpVect = (Vector)data.elementAt(j); 83 for (int i = 0 ; i < rows ; i++) 84 { 85 dataRev[i][j] = tmpVect.elementAt(i); 86 } 87 } 88 tableModel = new MxmTableModel(dataRev,columnName); 89 settableProperties(); 90 buildGUI(); 91 } 92 public void setData(Vector data,Vector column) 93 { 94 95 tableModel = new MxmTableModel(data,column); 96 tableModel.setDataVector(data,column); 97 settableProperties(); 98 buildGUI(); 99 } 100 public void setData(Vector data,Vector column,int mod) 101 { 102 model= mod; 103 tableModel = new MxmTableModel(data,column); 104 tableModel.setDataVector(data,column); 105 settableProperties(); 106 buildGUI(); 107 } 108 public void setData(Object[][] data,Object[] column) 109 { 110 columnName = column; 111 tableModel = new MxmTableModel(data,columnName); 112 tableModel.setDataVector(data,column); 113 settableProperties(); 114 buildGUI(); 115 } 116 /*for use with coordinator 117 */ 118 public void setData(Object[] data) { 119 120 //Object[] attributeNames = (Object[])data[0]; 121 /*Vector names = new Vector(); 122 123 for (int i = 0; i < attributeNames.length; i++){ 124 names.add(attributeNames[i]); 125 }*/ 126 127 128 DataSetForApps dataSet = new DataSetForApps(); 129 dataSet.setDataObject(data); 130 String[] attributeNames = dataSet.getAttributeNamesOriginal(); 131 Object[] allData = dataSet.getDataSetFull(); 132 int numObs = dataSet.getNumObservations(); 133 Object[][] tablesData = new Object[numObs][attributeNames.length]; 134 135 double[] doubleData = null; 136 int[] intData = null; 137 String[] stringData = null; 138 boolean[] boolData = null; 139 140 for (int column = 0; column < attributeNames.length; column++) { 141 Object datum = data[column+1]; 142 143 if (datum instanceof double[]) { 144 doubleData = (double[])datum; 145 for (int row = 0; row < numObs; row++) { 146 double dat = doubleData[row]; 147 Double d = new Double(dat); 148 tablesData[row][column] = d; 149 } 150 } 151 152 if (datum instanceof int[]) { 153 intData = (int[])datum; 154 for (int row = 0; row < numObs; row++) { 155 int dat = intData[row]; 156 Integer d = new Integer(dat); 157 tablesData[row][column] = d; 158 } 159 } 160 161 if (datum instanceof String[]) { 162 163 stringData = (String[])datum; 164 for (int row = 0; row < numObs; row++) { 165 String dat = stringData[row]; 166 tablesData[row][column] = dat; 167 } 168 } 169 170 if (datum instanceof boolean[]) { 171 boolData = (boolean[])datum; 172 for (int row = 0; row < numObs; row++) { 173 boolean dat = boolData[row]; 174 Boolean d = new Boolean(dat); 175 tablesData[row][column] = d; 176 } 177 } 178 179 180 } 181 182 this.setData(tablesData,attributeNames); 183 184 } 185 186 187 public void setData(double[][] data) 188 { 189 int len = data.length; 190 int wid = data[0].length; 191 //System.out.println("________________"); 192 //System.out.println("length" + len); 193 //System.out.println("width" + wid); 194 int x = 0; 195 Object[][] realdata = new Object[len][wid]; 196 197 for (int i =0 ; i< len ; i++) 198 { 199 for (int j=0; j< wid ; j++) 200 { 201 Double dob = null; 202 try 203 { 204 dob = new Double(data[i][j]); 205 } 206 catch(Exception e) 207 { 208 x = x+ 1; 209 210 } 211 212 213 if (dob != null) 214 realdata[i][j] = (Object)(dob.toString()); 215 else 216 realdata[i][j] = (Object)""; 217 } 218 219 } 220 //System.out.println("count =" + x); 221 //put a check for the data & column names 222 tableModel = new MxmTableModel(realdata,columnName); 223 settableProperties(); 224 buildGUI(); 225 } 226 public void addRow(Object[] data) 227 { 228 tableModel.addRow(data); 229 } 230 public void addRow(Vector data) 231 { 232 tableModel.addRow(data); 233 } 234 public TableBrowser(Vector column, int numOfRows) 235 { 236 tableModel = new MxmTableModel(column,numOfRows); 237 settableProperties(); 238 // initailized the GUI 239 buildGUI(); 240 } 241 /* public TableBrowser(int column, int numOfRows) 242 { 243 tableModel = new MxmTableModel(column,numOfRows); 244 settableProperties(); 245 // initailized the GUI 246 buildGUI(); 247 }*/ 248 public TableBrowser(Vector column) 249 { 250 tableModel = new MxmTableModel(column,5); 251 settableProperties(); 252 // initailized the GUI 253 buildGUI(); 254 } 255 public TableBrowser(Vector data, Vector column) 256 { 257 tableModel = new MxmTableModel(data,column); 258 settableProperties(); 259 // initailized the GUI 260 buildGUI(); 261 } 262 public TableBrowser(Object[] columnNames, int numRows) 263 { 264 tableModel = new MxmTableModel(columnNames,numRows); 265 settableProperties(); 266 // initailized the GUI 267 buildGUI(); 268 } 269 public TableBrowser(Object[] columnNames) 270 { 271 tableModel = new MxmTableModel(columnNames,5); 272 settableProperties(); 273 // initailized the GUI 274 buildGUI(); 275 } 276 public TableBrowser(Object[][] data, Object[] columnNames) 277 { 278 tableModel = new MxmTableModel(data,columnNames); 279 settableProperties(); 280 // initailized the GUI 281 buildGUI(); 282 } 283 284 public int[] sendSelectedIndex() 285 { 286 //System.out.println(currentTable.getSelectedRows()[1]); 287 return currentTable.getSelectedRows(); 288 } 289 public void setSelectedIndex(int[] selectedRows) 290 { 291 //currentTable.setSelectionBackground(new Color(100,100,100)); 292 //currentTable.setSelectionForeground(new Color(100,100,100)); 293 currentTable.setSelectionMode(2); 294 if (selectedRows == null){ 295 return; 296 } 297 int len = selectedRows.length; 298 for (int i = 0 ; i<len ; i++){ 299 //System.out.println(currentTable.isRowSelected(i)); 300 currentTable.addRowSelectionInterval(selectedRows[i],selectedRows[i] ); 301 302 } 303 for (int i = 0 ; i<len ; i++){ 304 int[] x = currentTable.getSelectedRows(); 305 //System.out.println(x[i]); 306 contentPane.repaint(); 307 } 308 } 309 310 private void settableProperties() 311 { 312 try 313 { 314 //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 315 } 316 catch (Exception e) 317 { 318 System.out.println("Windows Look/feel failed"); 319 System.out.println(e.getMessage()); 320 } 321 322 currentTable = new JTable(tableModel); 323 //currentTable.setPreferredSize(new Dimension(800,600)); 324 if (model==1) 325 { 326 MxmTableCellRenderer renderer = new MxmTableCellRenderer( JLabel.LEFT ); 327 int columnCount = currentTable.getColumnCount(); 328 for( int i = 0 ; i < columnCount ; i++ ) 329 { 330 String id = currentTable.getColumnName( i ) ; 331 javax.swing.table.TableColumn col = currentTable.getColumn( id ) ; 332 col.setCellRenderer( renderer ) ; 333 } 334 } 335 struc = new JScrollPane(currentTable); 336 struc.setAutoscrolls(false); 337 if (columnName != null) 338 if (columnName.length >10) 339 currentTable.setAutoResizeMode(0); 340 //super.setTitle("Table Browser"); 341 super.setVisible(true); 342 //contentPane.setSize(800,400); 343 } 344 public ControlPanel getMyControl() 345 { 346 return myControl; 347 } 348 349 public JInternalFrame getCurrentFrame() 350 { 351 return currentFrame; 352 } 353 public void setCurrentFrame(JInternalFrame fr) 354 { 355 this.currentFrame=fr; 356 } 357 public JTable getCurrentTable() 358 { 359 return currentTable; 360 } 361 public void setCurrentTable(JTable tb) 362 { 363 this.currentTable=tb; 364 } 365 366 private void buildGUI() 367 { 368 contentPane.removeAll(); 369 //initialize the menu pane 370 buildMenue(); 371 //initialize the control pane 372 buildControlPanel(); 373 // initialize the desktop pane 374 desktopGUI(); 375 //this.pack(); 376 contentPane.repaint(); 377 378 } 379 380 private void buildControlPanel() 381 { 382 //pass a reference of this class to the control pane 383 //which is defined as the control pane class 384 385 myControl=new ControlPanel(this); 386 contentPane.add(myControl,BorderLayout.NORTH); 387 388 } 389 //end of the buildControlPanel 390 391 392 private void desktopGUI() 393 { 394 // System.out.println("repainted"); 395 396 contentPane.add(struc,BorderLayout.CENTER); 397 //System.out.println(contentPane.getComponentCount()); 398 //###contentPane.setSize(800,600); 399 } 400 private void buildMenue() 401 { 402 403 Action[] actions = { 404 new NewAction(), 405 new OpenAction(), 406 new PrintAction(), 407 new CutAction(), 408 new CopyAction(), 409 new PasteAction(), 410 new ExitAction() 411 }; 412 413 JMenuBar menubar = new JMenuBar(); 414 JMenu fileMenu = new JMenu("File"); 415 416 for(int i=0; i < actions.length; ++i) { 417 fileMenu.add(actions[i]); 418 419 if(i == 2 || i == actions.length-2){ 420 fileMenu.addSeparator(); 421 } 422 } 423 menubar.add(fileMenu); 424 Action[] actions1 = { 425 new PropertiesAction() 426 }; 427 JMenu option = new JMenu("Option"); 428 for(int i=0; i < actions1.length; ++i) { 429 option.add(actions1[i]); 430 } 431 menubar.add(option); 432 JButton helpButton=new JButton("help"); 433 menubar.add(new JMenu("Help")); 434 menubar.add(Box.createHorizontalGlue()); 435 436 437 ImageIcon iconIDL=null; 438 /* 439 try{ 440 iconIDL=new ImageIcon(new URL(codebaseURL,"idl.gif")); 441 }catch(Exception ed) 442 { 443 System.out.println("can not find the image"); 444 }*/ 445 JLabel IDLLabel = new JLabel(iconIDL); 446 menubar.add(IDLLabel); 447 448 //this.setJMenuBar(menubar); 449 450 } 451 452 void showStatus(String s) 453 { 454 //do nothing now 455 } 456 457 class NewAction extends AbstractAction { 458 public NewAction() { 459 super("New ..."); 460 } 461 public void actionPerformed(ActionEvent event) { 462 showStatus("new"); 463 } 464 } 465 466 class OpenAction extends AbstractAction { 467 public OpenAction() { 468 super("Open ..."); 469 } 470 public void actionPerformed(ActionEvent event) { 471 showStatus("open"); 472 } 473 } 474 class CutAction extends AbstractAction { 475 public CutAction() { 476 super("Cut"); 477 } 478 public void actionPerformed(ActionEvent event) { 479 showStatus("cut"); 480 } 481 } 482 class CopyAction extends AbstractAction { 483 public CopyAction() { 484 super("Copy"); 485 } 486 public void actionPerformed(ActionEvent event) { 487 showStatus("copy"); 488 } 489 } 490 class PasteAction extends AbstractAction { 491 public PasteAction() { 492 super("Paste"); 493 } 494 public void actionPerformed(ActionEvent event) { 495 showStatus("paste"); 496 } 497 } 498 class ExitAction extends AbstractAction { 499 public ExitAction() { 500 super("Exit"); 501 } 502 public void actionPerformed(ActionEvent event) { 503 //dispose(); 504 } 505 } 506 class PrintAction extends AbstractAction { 507 public PrintAction() { 508 super("Print"); 509 } 510 public void actionPerformed(ActionEvent event) { 511 //PrintUtilities.printComponent(new JLabel("Hello, world")); 512 } 513 } 514 class PropertiesAction extends AbstractAction { 515 public PropertiesAction() { 516 super("Property"); 517 } 518 public void actionPerformed(ActionEvent event) { 519 //System.out.println("Hai"); 520 521 JFrame fr = new JFrame(); 522 JTabbedPane pane = new JTabbedPane(); 523 524 525 pane.add(new JPanel()); 526 pane.setTitleAt(0,"Hai"); 527 pane.add(new JPanel()); 528 pane.setTitleAt(1,"Hai"); 529 pane.add(new JPanel()); 530 pane.setTitleAt(2,"Hai"); 531 fr.getContentPane().add(pane); 532 fr.setTitle("Property window"); 533 fr.setSize(200,350); 534 fr.setResizable(false); 535 fr.setVisible(true); 536 537 } 538 } 539 //end of the initialization for the menu pane 540 public void resizeRowOfCurrentTable(int amplify,int shrink) 541 { 542 int oldY=getScrolRow(); 543 currentTable.setRowHeight(currentTable.getRowHeight()*amplify/shrink); 544 ScrollTable(0,oldY,amplify,shrink); 545 } 546 547 public void resizeColumnOfCurrentTable(int amplify,int shrink) 548 { 549 int oldX=getScrolColumn(); 550 TableColumnModel tcm = currentTable.getColumnModel(); 551 int w=0; 552 for(int i=0; i < tcm.getColumnCount(); i++) { 553 TableColumn column = tcm.getColumn(i); 554 w=column.getWidth(); 555 column.setPreferredWidth((int)(w*amplify/shrink)); 556 column.setWidth((int)(w*amplify/shrink)); 557 } 558 ScrollTable(oldX,0,amplify,shrink); 559 560 561 562 } 563 564 public void setTableMode() 565 { 566 JCheckBox[] checkBox=this.getMyControl().getTableMode(); 567 currentTable.setRowSelectionAllowed(checkBox[0].isSelected()); 568 currentTable.setColumnSelectionAllowed(checkBox[1].isSelected()); 569 currentTable.setCellSelectionEnabled(checkBox[2].isSelected()); 570 } 571 public void setTableMode(JTable tb) 572 { 573 JCheckBox[] checkBox=this.getMyControl().getTableMode(); 574 575 tb.setRowSelectionAllowed(checkBox[0].isSelected()); 576 577 tb.setColumnSelectionAllowed(checkBox[1].isSelected()); 578 579 tb.setCellSelectionEnabled(checkBox[2].isSelected()); 580 581 582 } 583 584 585 private int getScrolRow() 586 { 587 int moveNum=currentTable.getSelectedRow(); 588 return currentTable.getRowHeight()*moveNum; 589 590 591 592 } 593 594 private int getScrolColumn() 595 { 596 597 int moveNum=currentTable.getSelectedColumn(); 598 int oldX=0; 599 for(int i=0;i<moveNum;i++) 600 { 601 oldX+=currentTable.getColumnModel().getColumn(i).getWidth(); 602 } 603 return oldX; 604 } 605 private void ScrollTable(int row,int col,int amplify,int shrink) 606 { 607 JScrollPane myScrollPane = (JScrollPane)SwingUtilities.getAncestorOfClass( 608 JScrollPane.class, currentTable); 609 610 JViewport myViewport=myScrollPane.getViewport(); 611 Point point= myViewport.getViewPosition(); 612 613 point.x=row*amplify/shrink; 614 point.y=col*amplify/shrink; 615 if(amplify<0||shrink<0) 616 { 617 point.x=0; 618 point.y=0; 619 } 620 myViewport.setViewPosition(point); 621 myScrollPane.setViewport(null); 622 myScrollPane.setViewport(myViewport); 623 myScrollPane.setPreferredSize(new Dimension(800,600)); 624 myScrollPane.repaint(); 625 626 627 628 } 629 630 631 632 private void modifyGrideOnTable(JTable tb, boolean hor) 633 { 634 tb.setShowGrid(hor); 635 636 } 637 638 public void stateChanged(ChangeEvent e) { 639 JButton button = (JButton) e.getSource(); 640 if (button.getText() == "Send"){ 641 int[] sel = currentTable.getSelectedRows(); 642 this.fireSelectionChanged(sel); 643 } 644 } 645 public void addChangeListener(ChangeListener l) { 646 this.listenerList.add(ChangeListener.class, l); 647 } 648 649 public void removeChangeListener(ChangeListener l) { 650 this.listenerList.remove(ChangeListener.class, l); 651 } 652 653 public void fireChangeEvent () { 654 Object[] listeners = this.listenerList.getListenerList(); 655 // Process the listeners last to first, notifying 656 // those that are interested in this event 657 658 for (int i = listeners.length - 2; i >= 0; i -= 2) { 659 if (listeners[i] == ChangeListener.class) { 660 ((ChangeListener)listeners[i + 1]).stateChanged(new ChangeEvent(this)); 661 } 662 } // end for 663 664 } 665 /*** 666 * adds an SelectionListener 667 */ 668 public void addSelectionListener (SelectionListener l) { 669 listenerList.add(SelectionListener.class, l); 670 } 671 /*** 672 * removes an SelectionListener from the component 673 */ 674 public void removeSelectionListener (SelectionListener l) { 675 listenerList.remove(SelectionListener.class, l); 676 677 } 678 /*** 679 * Notify all listeners that have registered interest for 680 * notification on this event type. The event instance 681 * is lazily created using the parameters passed into 682 * the fire method. 683 * @see EventListenerList 684 */ 685 private void fireSelectionChanged (int[] newSelection) { 686 687 // Guaranteed to return a non-null array 688 Object[] listeners = listenerList.getListenerList(); 689 SelectionEvent e = null; 690 // Process the listeners last to first, notifying 691 // those that are interested in this event 692 for (int i = listeners.length - 2; i >= 0; i -= 2) { 693 if (listeners[i] == SelectionListener.class) { 694 // Lazily create the event: 695 if (e == null) { 696 e = new SelectionEvent(this, newSelection); 697 } 698 ((SelectionListener)listeners[i + 1]).selectionChanged(e); 699 } 700 }//next i 701 702 } 703 }//little edit

This page was automatically generated by Maven