1 package com.ca.directory.jxplorer.search; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.awt.print.*; 6 import java.util.*; 7 import java.util.logging.Logger ; 8 import java.util.logging.Level ; 9 import java.io.*; 10 import javax.swing.*; 11 import javax.swing.table.*; 12 import javax.naming.*; 13 import javax.naming.directory.*; 14 15 import com.ca.commons.cbutil.*; 16 import com.ca.directory.jxplorer.*; 17 import com.ca.commons.naming.*; 18 import com.ca.directory.jxplorer.tree.SmartTree; 19 20 36 public class ReturnAttributesDisplay 37 implements DataListener 38 { 39 private static Logger log = Logger.getLogger(ReturnAttributesDisplay.class.getName()); 40 41 44 private JXplorer jx; 45 46 49 private DataSource dataSource = null; 50 51 54 private Object [][] tableData = null; 55 56 59 private String [] tableHeader = null; 60 61 67 private static ReturnAttributesGUI gui = null; 68 69 75 public ReturnAttributesDisplay(JXplorer jx, String [] tableHeader) 76 { 77 this.tableHeader = tableHeader; 78 this.jx = jx; 79 } 80 81 86 public void registerDataSource(DataSource ds) 87 { 88 dataSource = ds; 89 dataSource.addDataListener(this); 90 } 91 92 97 public void dataReady(DataQuery result) 98 { 99 int type = result.getType(); 100 101 if (result.hasException()) 102 { 103 CBUtility.error("Unable to perform " + result.getTypeString() + " operation.", result.getException()); 104 return; 105 } 106 else 107 { 108 if (type == DataQuery.SEARCH) 110 displaySearchResult(result); 111 } 112 } 113 114 120 protected void displaySearchResult(DataQuery result) 121 { 122 HashMap map = new HashMap(0); 123 124 try 125 { 126 DXNamingEnumeration myResults = result.getEnumeration(); 127 128 Object [] searchResults = myResults.toArray(); 129 130 int rows = searchResults.length; 131 int cols = tableHeader.length; 132 133 if (rows == 0) 134 { 135 tableData = new Object [0][0]; 137 } 138 else 139 { 140 tableData = new Object [rows][cols]; 142 143 String dn = ""; 144 Attribute att = null; 145 String header = ""; 146 147 int includeDNPos = -1; 149 150 for (int i = 0; i < rows; i++) 151 { 152 for (int j = 0; j < cols; j++) 153 { 154 Attributes atts = ((SearchResult) searchResults[i]).getAttributes(); 155 156 dn = ((SearchResult) searchResults[i]).getName(); 158 159 att = atts.get(tableHeader[j]); 161 162 if(tableHeader[j].equalsIgnoreCase(ReturnAttributesDialog.INCLUDE_DN)) 167 { 168 includeDNPos = j; 169 tableData[i][j] = dn; 170 } 171 else if(att == null) 172 { 173 tableData[i][j] = ""; 174 } 175 else 176 { 177 header = att.toString(); 178 tableData[i][j] = header.substring(header.indexOf(":") + 2); 179 } 180 } 181 182 map.put(String.valueOf(i), dn); 184 } 185 186 if(includeDNPos > -1) 188 tableHeader[includeDNPos] = "DN"; 189 } 190 191 if (tableData==null || tableHeader==null) 192 { 193 log.warning("Problem retrieving the search results for the Return Attributes display"); 194 return; 195 } 196 197 if (gui == null) 198 { 199 gui = new ReturnAttributesGUI(tableData, tableHeader, rows, map); 201 } 202 else 203 { 204 gui.setTableData(tableData, tableHeader, rows, map); 206 gui.setVisible(true); 207 } 208 } 209 catch (NamingException e) 210 { 211 result.setException(e); } 213 catch (ClassCastException ee) 214 { 215 log.log(Level.WARNING, "Casting problem in return attribute display ", ee); 216 } 217 218 dataSource.removeDataListener(this); 223 } 224 225 230 class ReturnAttributesGUI extends JDialog implements Printable 231 { 232 JTable table; 233 DefaultTableModel model; 234 CBTableSorter sorter; 235 CBButton btnPrint, btnClose, btnHelp, btnSave; 236 CBPanel bottomPanel; 237 CBPanel topPanel; 238 CBPanel display; 239 JScrollPane scrollPane; 240 JFileChooser chooser; 241 HashMap map; 242 243 247 private Component printComponent = null; 248 249 250 259 public ReturnAttributesGUI(Object [][] tableData, String [] tableHeader, int num, HashMap map) 260 { 261 super(jx, CBIntText.get(String.valueOf(num)+ CBIntText.get(" Search Results")), false); 262 263 this.map = map; 264 265 model = new DefaultTableModel(tableData, tableHeader); 266 267 sorter = new CBTableSorter(model); 269 270 table = new JTable(sorter); 272 sorter.addMouseListenerToHeaderInTable(table); 273 274 addMouseListenerToTable(); 276 277 scrollPane = new JScrollPane(table); 279 280 display = new CBPanel(); 282 283 topPanel = new CBPanel(); 285 topPanel.makeHeavy(); 286 topPanel.addln(scrollPane); 287 288 display.makeHeavy(); 289 display.addln(topPanel); 290 291 bottomPanel = new CBPanel(); 293 294 btnPrint = new CBButton(CBIntText.get("Print"), CBIntText.get("Print this page.")); 295 btnPrint.addActionListener(new ActionListener(){ 296 public void actionPerformed(ActionEvent e){ 297 print(); 298 }}); 299 300 btnSave = new CBButton(CBIntText.get("Save"), CBIntText.get("Save this page.")); 301 btnSave.addActionListener(new ActionListener(){ 302 public void actionPerformed(ActionEvent e){ 303 save(); 304 }}); 305 306 btnClose = new CBButton(CBIntText.get("Close"), CBIntText.get("Close this window.")); 307 btnClose.addActionListener(new ActionListener(){ 308 public void actionPerformed(ActionEvent e){ 309 close(); 310 }}); 311 312 btnHelp = new CBButton(CBIntText.get("Help"), CBIntText.get("Help for this window.")); 313 CBHelpSystem.useDefaultHelp(btnHelp, HelpIDs.SEARCH_RESULTS); 314 315 bottomPanel.makeWide(); 316 bottomPanel.add(new JLabel(" ")); 317 bottomPanel.makeLight(); 318 bottomPanel.add(btnPrint); 319 bottomPanel.add(btnSave); 320 bottomPanel.add(btnClose); 321 bottomPanel.add(btnHelp); 322 323 display.makeLight(); 324 display.addln(bottomPanel); 325 326 Container pane = getContentPane(); 328 pane.setLayout(new BorderLayout()); 329 pane.add(display); 330 331 setSize(500, 300); 332 CBUtility.center(this, jx); 333 setVisible(true); 334 } 335 336 349 public void setTableData(Object [][] tableData, String [] tableHeader, int num, HashMap map) 350 { 351 this.map = map; 352 setTitle(CBIntText.get(String.valueOf(num)+ CBIntText.get(" Search Results"))); 353 354 topPanel.remove(scrollPane); 356 357 model = new DefaultTableModel(tableData, tableHeader); 358 359 sorter = new CBTableSorter(model); 361 362 table = new JTable(sorter); 364 addMouseListenerToTable(); 365 sorter.addMouseListenerToHeaderInTable(table); 366 scrollPane = new JScrollPane(table); 367 368 topPanel.add(scrollPane); 369 370 repaint(); 371 } 372 373 378 public void addMouseListenerToTable() 379 { 380 table.addMouseListener(new MouseAdapter() 381 { 382 public void mousePressed(MouseEvent e) { if (!doPopupStuff(e)) super.mousePressed(e); } 383 384 public void mouseReleased(MouseEvent e) { if (!doPopupStuff(e)) super.mouseReleased(e); } 385 386 public boolean doPopupStuff(MouseEvent e) 387 { 388 if(!e.isPopupTrigger()) 389 return false; 390 391 setUpPopup(e.getX(), e.getY()); 392 393 return true; 394 } 395 }); 396 } 397 398 407 public void setUpPopup(int x, int y) 408 { 409 JPopupMenu pop = new JPopupMenu("Go to"); 410 final JMenuItem menuItem = new JMenuItem(CBIntText.get("Go to entry..."), 411 new ImageIcon(JXplorer.getProperty("dir.images")+"goto.gif")); 412 pop.add(menuItem); 413 414 final int selectedRow = table.rowAtPoint(new Point(x, y)); 416 417 table.changeSelection(selectedRow, table.getSelectedColumn(), false, false); 418 419 menuItem.addActionListener(new ActionListener() 420 { 421 public void actionPerformed(ActionEvent e) 422 { 423 JMenuItem src = ((JMenuItem)e.getSource()); 424 425 if (src == menuItem) 426 goToEntry(selectedRow); 427 } 428 }); 429 430 pop.show(table, x, y); 431 } 432 433 440 public void goToEntry(int selectedRow) 441 { 442 SmartTree tree = jx.getTree(); 444 445 Object temp = map.get(String.valueOf(sorter.getTrueIndex(selectedRow))); 447 tree.collapse(); 448 tree.readAndExpandDN(new DN(temp.toString())); 449 450 jx.getTreeTabPane().setSelectedComponent(jx.getExplorePanel()); 452 } 453 454 458 public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException 459 { 460 Graphics2D g2 = (Graphics2D)g; 461 462 g2.setColor(Color.black); 464 465 468 Dimension d = printComponent.getSize(); double panelWidth = d.width; double panelHeight = d.height; double pageHeight = pf.getImageableHeight(); double pageWidth = pf.getImageableWidth(); double scale = pageWidth/panelWidth; 474 int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight); 475 476 if(pageIndex >= totalNumPages) 478 { 479 return Printable.NO_SUCH_PAGE; 480 } 481 482 g2.translate(pf.getImageableX(), pf.getImageableY()); 484 485 g2.translate(0f, -pageIndex*pageHeight); 487 488 g2.scale(scale, scale); 490 491 printComponent.paint(g2); 493 494 return Printable.PAGE_EXISTS; 495 } 496 497 501 public void print() 502 { 503 final PrinterJob job = PrinterJob.getPrinterJob(); 504 job.setPrintable(this); 505 506 509 final Component printMe = table; 510 511 Thread worker = new Thread () 512 { 513 public void run() 514 { 515 if (job.printDialog()) 516 { 517 try 518 { 519 synchronized(this) 523 { 524 printComponent = printMe; 525 job.print(); 526 printComponent = null; 527 } 528 } 529 catch (Exception ex) 530 { 531 log.warning("error printing: " + ex); 532 } 533 } 534 } 535 }; 536 worker.start(); 537 } 538 539 542 public void close() 543 { 544 this.setVisible(false); 545 this.dispose(); 546 } 547 548 552 public void save() 553 { 554 chooser = new JFileChooser(JXplorer.getProperty("csv.homeDir")); 555 556 chooser.addChoosableFileFilter(new CBFileFilter(new String [] {"csv"},"CSV Files (*.csv)")); 557 558 int option = chooser.showSaveDialog(this); 559 560 if (option == JFileChooser.APPROVE_OPTION) 562 { 563 File readFile = chooser.getSelectedFile(); 564 565 if (readFile == null) 566 { 567 CBUtility.error(CBIntText.get("Please select a file")); 568 } 569 else 570 { 571 readFile = adjustFileName(readFile); 573 574 int response = -1; 575 576 if (readFile.exists()) 578 { 579 response = JOptionPane.showConfirmDialog(this, 580 CBIntText.get("The File ''{0}'' already exists. Do you want to replace it?", new String [] {readFile.toString()}), 581 CBIntText.get("Overwrite Confirmation"), JOptionPane.YES_NO_OPTION ); 582 583 if (response != JOptionPane.YES_OPTION) 584 save(); 585 } 586 587 JXplorer.setProperty("csv.homeDir", readFile.getParent()); 589 doFileWrite(readFile); 590 } 591 } 592 } 593 594 598 protected File adjustFileName(File file) 599 { 600 if (file == null) 602 return null; 603 604 if (file.exists()) 606 return file; 607 608 String name = file.getName(); 609 610 if (name.indexOf('.') != -1) 612 return file; 613 614 name = name + ".csv"; 615 616 return new File(file.getParentFile(), name); 617 } 618 619 624 protected void doFileWrite(File file) 625 { 626 if (file == null) 627 CBUtility.error(CBIntText.get("Unable to write to empty file"), null); 628 629 FileWriter fileWriter = null; 630 631 try 632 { 633 fileWriter = new FileWriter(file); 634 635 StringBuffer buffy = new StringBuffer (0); 637 638 int cols = model.getColumnCount(); 640 641 int rows = model.getRowCount(); 643 String temp = ""; 644 645 for (int i = 0; i < rows; i++) 646 { 647 for (int j = 0; j < cols; j++) 649 { 650 temp = (model.getValueAt(i, j)).toString(); 651 652 buffy.append(escapeForCSV(temp)); 654 655 if(!((j == cols - 1) == true)) 657 buffy.append(","); 658 else 659 buffy.append("\n"); 660 661 temp = ""; 662 } 663 } 664 665 fileWriter.write(buffy.toString()); 666 667 fileWriter.close(); 668 log.warning("Closed CSV file"); 669 670 chooser.setVisible(false); 671 } 672 catch (Exception e) 673 { 674 log.warning("Error writing CSV file from Return Attributes dialog "+e); 675 } 676 } 677 } 678 679 691 private String escapeForCSV(String str) 692 { 693 if(str == null) 694 return str; 695 696 str = str.trim(); 697 698 str = str.replaceAll("\"", "\"\""); 699 return "\"" + str + "\""; 700 } 701 } 702 703 | Popular Tags |