1 16 package org.apache.log4j.lf5.viewer; 17 18 import org.apache.log4j.lf5.LogLevel; 19 import org.apache.log4j.lf5.LogRecord; 20 import org.apache.log4j.lf5.LogRecordFilter; 21 import org.apache.log4j.lf5.util.DateFormatManager; 22 import org.apache.log4j.lf5.util.LogFileParser; 23 import org.apache.log4j.lf5.util.StreamUtils; 24 import org.apache.log4j.lf5.viewer.categoryexplorer.CategoryExplorerTree; 25 import org.apache.log4j.lf5.viewer.categoryexplorer.CategoryPath; 26 import org.apache.log4j.lf5.viewer.configure.ConfigurationManager; 27 import org.apache.log4j.lf5.viewer.configure.MRUFileManager; 28 29 import javax.swing.*; 30 import java.awt.*; 31 import java.awt.event.ActionEvent ; 32 import java.awt.event.ActionListener ; 33 import java.awt.event.WindowAdapter ; 34 import java.awt.event.WindowEvent ; 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.net.MalformedURLException ; 39 import java.net.URL ; 40 import java.util.*; 41 import java.util.List ; 42 43 53 54 56 public class LogBrokerMonitor { 57 61 public static final String DETAILED_VIEW = "Detailed"; 62 protected JFrame _logMonitorFrame; 68 protected int _logMonitorFrameWidth = 550; 69 protected int _logMonitorFrameHeight = 500; 70 protected LogTable _table; 71 protected CategoryExplorerTree _categoryExplorerTree; 72 protected String _searchText; 73 protected String _NDCTextFilter = ""; 74 protected LogLevel _leastSevereDisplayedLogLevel = LogLevel.DEBUG; 75 76 protected JScrollPane _logTableScrollPane; 77 protected JLabel _statusLabel; 78 protected Object _lock = new Object (); 79 protected JComboBox _fontSizeCombo; 80 81 protected int _fontSize = 10; 82 protected String _fontName = "Dialog"; 83 protected String _currentView = DETAILED_VIEW; 84 85 protected boolean _loadSystemFonts = false; 86 protected boolean _trackTableScrollPane = true; 87 protected Dimension _lastTableViewportSize; 88 protected boolean _callSystemExitOnClose = false; 89 protected List _displayedLogBrokerProperties = new Vector(); 90 91 protected Map _logLevelMenuItems = new HashMap(); 92 protected Map _logTableColumnMenuItems = new HashMap(); 93 94 protected List _levels = null; 95 protected List _columns = null; 96 protected boolean _isDisposed = false; 97 98 protected ConfigurationManager _configurationManager = null; 99 protected MRUFileManager _mruFileManager = null; 100 protected File _fileLocation = null; 101 102 106 110 113 public LogBrokerMonitor(List logLevels) { 114 115 _levels = logLevels; 116 _columns = LogTableColumn.getLogTableColumns(); 117 120 String callSystemExitOnClose = 121 System.getProperty("monitor.exit"); 122 if (callSystemExitOnClose == null) { 123 callSystemExitOnClose = "false"; 124 } 125 callSystemExitOnClose = callSystemExitOnClose.trim().toLowerCase(); 126 127 if (callSystemExitOnClose.equals("true")) { 128 _callSystemExitOnClose = true; 129 } 130 131 initComponents(); 132 133 134 _logMonitorFrame.addWindowListener( 135 new LogBrokerMonitorWindowAdaptor(this)); 136 137 } 138 139 143 147 public void show(final int delay) { 148 if (_logMonitorFrame.isVisible()) { 149 return; 150 } 151 SwingUtilities.invokeLater(new Runnable () { 153 public void run() { 154 Thread.yield(); 155 pause(delay); 156 _logMonitorFrame.setVisible(true); 157 } 158 }); 159 } 160 161 public void show() { 162 show(0); 163 } 164 165 168 public void dispose() { 169 _logMonitorFrame.dispose(); 170 _isDisposed = true; 171 172 if (_callSystemExitOnClose == true) { 173 System.exit(0); 174 } 175 } 176 177 180 public void hide() { 181 _logMonitorFrame.setVisible(false); 182 } 183 184 187 public DateFormatManager getDateFormatManager() { 188 return _table.getDateFormatManager(); 189 } 190 191 194 public void setDateFormatManager(DateFormatManager dfm) { 195 _table.setDateFormatManager(dfm); 196 } 197 198 202 public boolean getCallSystemExitOnClose() { 203 return _callSystemExitOnClose; 204 } 205 206 210 public void setCallSystemExitOnClose(boolean callSystemExitOnClose) { 211 _callSystemExitOnClose = callSystemExitOnClose; 212 } 213 214 219 public void addMessage(final LogRecord lr) { 220 if (_isDisposed == true) { 221 return; 224 } 225 226 SwingUtilities.invokeLater(new Runnable () { 227 public void run() { 228 _categoryExplorerTree.getExplorerModel().addLogRecord(lr); 229 _table.getFilteredLogTableModel().addLogRecord(lr); updateStatusLabel(); } 232 }); 233 } 234 235 public void setMaxNumberOfLogRecords(int maxNumberOfLogRecords) { 236 _table.getFilteredLogTableModel().setMaxNumberOfLogRecords(maxNumberOfLogRecords); 237 } 238 239 public JFrame getBaseFrame() { 240 return _logMonitorFrame; 241 } 242 243 public void setTitle(String title) { 244 _logMonitorFrame.setTitle(title + " - LogFactor5"); 245 } 246 247 public void setFrameSize(int width, int height) { 248 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 249 if (0 < width && width < screen.width) { 250 _logMonitorFrameWidth = width; 251 } 252 if (0 < height && height < screen.height) { 253 _logMonitorFrameHeight = height; 254 } 255 updateFrameSize(); 256 } 257 258 public void setFontSize(int fontSize) { 259 changeFontSizeCombo(_fontSizeCombo, fontSize); 260 } 263 264 public void addDisplayedProperty(Object messageLine) { 265 _displayedLogBrokerProperties.add(messageLine); 266 } 267 268 public Map getLogLevelMenuItems() { 269 return _logLevelMenuItems; 270 } 271 272 public Map getLogTableColumnMenuItems() { 273 return _logTableColumnMenuItems; 274 } 275 276 public JCheckBoxMenuItem getTableColumnMenuItem(LogTableColumn column) { 277 return getLogTableColumnMenuItem(column); 278 } 279 280 public CategoryExplorerTree getCategoryExplorerTree() { 281 return _categoryExplorerTree; 282 } 283 284 public String getNDCTextFilter() { 287 return _NDCTextFilter; 288 } 289 290 public void setNDCLogRecordFilter(String textFilter) { 294 _table.getFilteredLogTableModel(). 295 setLogRecordFilter(createNDCLogRecordFilter(textFilter)); 296 } 297 301 protected void setSearchText(String text) { 302 _searchText = text; 303 } 304 305 protected void setNDCTextFilter(String text) { 307 if (text == null) { 310 _NDCTextFilter = ""; 311 } else { 312 _NDCTextFilter = text; 313 } 314 } 315 316 protected void sortByNDC() { 320 String text = _NDCTextFilter; 321 if (text == null || text.length() == 0) { 322 return; 323 } 324 325 _table.getFilteredLogTableModel(). 327 setLogRecordFilter(createNDCLogRecordFilter(text)); 328 } 329 330 protected void findSearchText() { 331 String text = _searchText; 332 if (text == null || text.length() == 0) { 333 return; 334 } 335 int startRow = getFirstSelectedRow(); 336 int foundRow = findRecord( 337 startRow, 338 text, 339 _table.getFilteredLogTableModel().getFilteredRecords() 340 ); 341 selectRow(foundRow); 342 } 343 344 protected int getFirstSelectedRow() { 345 return _table.getSelectionModel().getMinSelectionIndex(); 346 } 347 348 protected void selectRow(int foundRow) { 349 if (foundRow == -1) { 350 String message = _searchText + " not found."; 351 JOptionPane.showMessageDialog( 352 _logMonitorFrame, 353 message, 354 "Text not found", 355 JOptionPane.INFORMATION_MESSAGE 356 ); 357 return; 358 } 359 LF5SwingUtils.selectRow(foundRow, _table, _logTableScrollPane); 360 } 361 362 protected int findRecord( 363 int startRow, 364 String searchText, 365 List records 366 ) { 367 if (startRow < 0) { 368 startRow = 0; } else { 370 startRow++; } 372 int len = records.size(); 373 374 for (int i = startRow; i < len; i++) { 375 if (matches((LogRecord) records.get(i), searchText)) { 376 return i; } 378 } 379 len = startRow; 381 for (int i = 0; i < len; i++) { 382 if (matches((LogRecord) records.get(i), searchText)) { 383 return i; } 385 } 386 return -1; 388 } 389 390 394 protected boolean matches(LogRecord record, String text) { 395 String message = record.getMessage(); 396 String NDC = record.getNDC(); 397 398 if (message == null && NDC == null || text == null) { 399 return false; 400 } 401 if (message.toLowerCase().indexOf(text.toLowerCase()) == -1 && 402 NDC.toLowerCase().indexOf(text.toLowerCase()) == -1) { 403 return false; 404 } 405 406 return true; 407 } 408 409 414 protected void refresh(JTextArea textArea) { 415 String text = textArea.getText(); 416 textArea.setText(""); 417 textArea.setText(text); 418 } 419 420 protected void refreshDetailTextArea() { 421 refresh(_table._detailTextArea); 422 } 423 424 protected void clearDetailTextArea() { 425 _table._detailTextArea.setText(""); 426 } 427 428 433 protected int changeFontSizeCombo(JComboBox box, int requestedSize) { 434 int len = box.getItemCount(); 435 int currentValue; 436 Object currentObject; 437 Object selectedObject = box.getItemAt(0); 438 int selectedValue = Integer.parseInt(String.valueOf(selectedObject)); 439 for (int i = 0; i < len; i++) { 440 currentObject = box.getItemAt(i); 441 currentValue = Integer.parseInt(String.valueOf(currentObject)); 442 if (selectedValue < currentValue && currentValue <= requestedSize) { 443 selectedValue = currentValue; 444 selectedObject = currentObject; 445 } 446 } 447 box.setSelectedItem(selectedObject); 448 return selectedValue; 449 } 450 451 454 protected void setFontSizeSilently(int fontSize) { 455 _fontSize = fontSize; 456 setFontSize(_table._detailTextArea, fontSize); 457 selectRow(0); 458 setFontSize(_table, fontSize); 459 } 460 461 protected void setFontSize(Component component, int fontSize) { 462 Font oldFont = component.getFont(); 463 Font newFont = 464 new Font(oldFont.getFontName(), oldFont.getStyle(), fontSize); 465 component.setFont(newFont); 466 } 467 468 protected void updateFrameSize() { 469 _logMonitorFrame.setSize(_logMonitorFrameWidth, _logMonitorFrameHeight); 470 centerFrame(_logMonitorFrame); 471 } 472 473 protected void pause(int millis) { 474 try { 475 Thread.sleep(millis); 476 } catch (InterruptedException e) { 477 478 } 479 } 480 481 protected void initComponents() { 482 _logMonitorFrame = new JFrame("LogFactor5"); 486 487 _logMonitorFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 488 489 String resource = 490 "/org/apache/log4j/lf5/viewer/images/lf5_small_icon.gif"; 491 URL lf5IconURL = getClass().getResource(resource); 492 493 if (lf5IconURL != null) { 494 _logMonitorFrame.setIconImage(new ImageIcon(lf5IconURL).getImage()); 495 } 496 updateFrameSize(); 497 498 JTextArea detailTA = createDetailTextArea(); 502 JScrollPane detailTAScrollPane = new JScrollPane(detailTA); 503 _table = new LogTable(detailTA); 504 setView(_currentView, _table); 505 _table.setFont(new Font(_fontName, Font.PLAIN, _fontSize)); 506 _logTableScrollPane = new JScrollPane(_table); 507 508 if (_trackTableScrollPane) { 509 _logTableScrollPane.getVerticalScrollBar().addAdjustmentListener( 510 new TrackingAdjustmentListener() 511 ); 512 } 513 514 515 518 JSplitPane tableViewerSplitPane = new JSplitPane(); 519 tableViewerSplitPane.setOneTouchExpandable(true); 520 tableViewerSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); 521 tableViewerSplitPane.setLeftComponent(_logTableScrollPane); 522 tableViewerSplitPane.setRightComponent(detailTAScrollPane); 523 tableViewerSplitPane.setDividerLocation(350); 531 532 536 _categoryExplorerTree = new CategoryExplorerTree(); 537 538 _table.getFilteredLogTableModel().setLogRecordFilter(createLogRecordFilter()); 539 540 JScrollPane categoryExplorerTreeScrollPane = 541 new JScrollPane(_categoryExplorerTree); 542 categoryExplorerTreeScrollPane.setPreferredSize(new Dimension(130, 400)); 543 544 _mruFileManager = new MRUFileManager(); 546 547 551 JSplitPane splitPane = new JSplitPane(); 552 splitPane.setOneTouchExpandable(true); 553 splitPane.setRightComponent(tableViewerSplitPane); 554 splitPane.setLeftComponent(categoryExplorerTreeScrollPane); 555 splitPane.setDividerLocation(130); 557 _logMonitorFrame.getRootPane().setJMenuBar(createMenuBar()); 562 _logMonitorFrame.getContentPane().add(splitPane, BorderLayout.CENTER); 563 _logMonitorFrame.getContentPane().add(createToolBar(), 564 BorderLayout.NORTH); 565 _logMonitorFrame.getContentPane().add(createStatusArea(), 566 BorderLayout.SOUTH); 567 568 makeLogTableListenToCategoryExplorer(); 569 addTableModelProperties(); 570 571 _configurationManager = new ConfigurationManager(this, _table); 575 576 } 577 578 protected LogRecordFilter createLogRecordFilter() { 579 LogRecordFilter result = new LogRecordFilter() { 580 public boolean passes(LogRecord record) { 581 CategoryPath path = new CategoryPath(record.getCategory()); 582 return 583 getMenuItem(record.getLevel()).isSelected() && 584 _categoryExplorerTree.getExplorerModel().isCategoryPathActive(path); 585 } 586 }; 587 return result; 588 } 589 590 protected LogRecordFilter createNDCLogRecordFilter(String text) { 593 _NDCTextFilter = text; 594 LogRecordFilter result = new LogRecordFilter() { 595 public boolean passes(LogRecord record) { 596 String NDC = record.getNDC(); 597 CategoryPath path = new CategoryPath(record.getCategory()); 598 if (NDC == null || _NDCTextFilter == null) { 599 return false; 600 } else if (NDC.toLowerCase().indexOf(_NDCTextFilter.toLowerCase()) == -1) { 601 return false; 602 } else { 603 return getMenuItem(record.getLevel()).isSelected() && 604 _categoryExplorerTree.getExplorerModel().isCategoryPathActive(path); 605 } 606 } 607 }; 608 609 return result; 610 } 611 612 613 protected void updateStatusLabel() { 614 _statusLabel.setText(getRecordsDisplayedMessage()); 615 } 616 617 protected String getRecordsDisplayedMessage() { 618 FilteredLogTableModel model = _table.getFilteredLogTableModel(); 619 return getStatusText(model.getRowCount(), model.getTotalRowCount()); 620 } 621 622 protected void addTableModelProperties() { 623 final FilteredLogTableModel model = _table.getFilteredLogTableModel(); 624 625 addDisplayedProperty(new Object () { 626 public String toString() { 627 return getRecordsDisplayedMessage(); 628 } 629 }); 630 addDisplayedProperty(new Object () { 631 public String toString() { 632 return "Maximum number of displayed LogRecords: " 633 + model._maxNumberOfLogRecords; 634 } 635 }); 636 } 637 638 protected String getStatusText(int displayedRows, int totalRows) { 639 StringBuffer result = new StringBuffer (); 640 result.append("Displaying: "); 641 result.append(displayedRows); 642 result.append(" records out of a total of: "); 643 result.append(totalRows); 644 result.append(" records."); 645 return result.toString(); 646 } 647 648 protected void makeLogTableListenToCategoryExplorer() { 649 ActionListener listener = new ActionListener () { 650 public void actionPerformed(ActionEvent e) { 651 _table.getFilteredLogTableModel().refresh(); 652 updateStatusLabel(); 653 } 654 }; 655 _categoryExplorerTree.getExplorerModel().addActionListener(listener); 656 } 657 658 protected JPanel createStatusArea() { 659 JPanel statusArea = new JPanel(); 660 JLabel status = 661 new JLabel("No log records to display."); 662 _statusLabel = status; 663 status.setHorizontalAlignment(JLabel.LEFT); 664 665 statusArea.setBorder(BorderFactory.createEtchedBorder()); 666 statusArea.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); 667 statusArea.add(status); 668 669 return (statusArea); 670 } 671 672 protected JTextArea createDetailTextArea() { 673 JTextArea detailTA = new JTextArea(); 674 detailTA.setFont(new Font("Monospaced", Font.PLAIN, 14)); 675 detailTA.setTabSize(3); 676 detailTA.setLineWrap(true); 677 detailTA.setWrapStyleWord(false); 678 return (detailTA); 679 } 680 681 protected JMenuBar createMenuBar() { 682 JMenuBar menuBar = new JMenuBar(); 683 menuBar.add(createFileMenu()); 684 menuBar.add(createEditMenu()); 685 menuBar.add(createLogLevelMenu()); 686 menuBar.add(createViewMenu()); 687 menuBar.add(createConfigureMenu()); 688 menuBar.add(createHelpMenu()); 689 690 return (menuBar); 691 } 692 693 protected JMenu createLogLevelMenu() { 694 JMenu result = new JMenu("Log Level"); 695 result.setMnemonic('l'); 696 Iterator levels = getLogLevels(); 697 while (levels.hasNext()) { 698 result.add(getMenuItem((LogLevel) levels.next())); 699 } 700 701 result.addSeparator(); 702 result.add(createAllLogLevelsMenuItem()); 703 result.add(createNoLogLevelsMenuItem()); 704 result.addSeparator(); 705 result.add(createLogLevelColorMenu()); 706 result.add(createResetLogLevelColorMenuItem()); 707 708 return result; 709 } 710 711 protected JMenuItem createAllLogLevelsMenuItem() { 712 JMenuItem result = new JMenuItem("Show all LogLevels"); 713 result.setMnemonic('s'); 714 result.addActionListener(new ActionListener () { 715 public void actionPerformed(ActionEvent e) { 716 selectAllLogLevels(true); 717 _table.getFilteredLogTableModel().refresh(); 718 updateStatusLabel(); 719 } 720 }); 721 return result; 722 } 723 724 protected JMenuItem createNoLogLevelsMenuItem() { 725 JMenuItem result = new JMenuItem("Hide all LogLevels"); 726 result.setMnemonic('h'); 727 result.addActionListener(new ActionListener () { 728 public void actionPerformed(ActionEvent e) { 729 selectAllLogLevels(false); 730 _table.getFilteredLogTableModel().refresh(); 731 updateStatusLabel(); 732 } 733 }); 734 return result; 735 } 736 737 protected JMenu createLogLevelColorMenu() { 738 JMenu colorMenu = new JMenu("Configure LogLevel Colors"); 739 colorMenu.setMnemonic('c'); 740 Iterator levels = getLogLevels(); 741 while (levels.hasNext()) { 742 colorMenu.add(createSubMenuItem((LogLevel) levels.next())); 743 } 744 745 return colorMenu; 746 } 747 748 protected JMenuItem createResetLogLevelColorMenuItem() { 749 JMenuItem result = new JMenuItem("Reset LogLevel Colors"); 750 result.setMnemonic('r'); 751 result.addActionListener(new ActionListener () { 752 public void actionPerformed(ActionEvent e) { 753 LogLevel.resetLogLevelColorMap(); 755 756 _table.getFilteredLogTableModel().refresh(); 758 } 759 }); 760 return result; 761 } 762 763 protected void selectAllLogLevels(boolean selected) { 764 Iterator levels = getLogLevels(); 765 while (levels.hasNext()) { 766 getMenuItem((LogLevel) levels.next()).setSelected(selected); 767 } 768 } 769 770 protected JCheckBoxMenuItem getMenuItem(LogLevel level) { 771 JCheckBoxMenuItem result = (JCheckBoxMenuItem) (_logLevelMenuItems.get(level)); 772 if (result == null) { 773 result = createMenuItem(level); 774 _logLevelMenuItems.put(level, result); 775 } 776 return result; 777 } 778 779 protected JMenuItem createSubMenuItem(LogLevel level) { 780 final JMenuItem result = new JMenuItem(level.toString()); 781 final LogLevel logLevel = level; 782 result.setMnemonic(level.toString().charAt(0)); 783 result.addActionListener(new ActionListener () { 784 public void actionPerformed(ActionEvent e) { 785 showLogLevelColorChangeDialog(result, logLevel); 786 } 787 }); 788 789 return result; 790 791 } 792 793 protected void showLogLevelColorChangeDialog(JMenuItem result, LogLevel level) { 794 JMenuItem menuItem = result; 795 Color newColor = JColorChooser.showDialog( 796 _logMonitorFrame, 797 "Choose LogLevel Color", 798 result.getForeground()); 799 800 if (newColor != null) { 801 level.setLogLevelColorMap(level, newColor); 803 _table.getFilteredLogTableModel().refresh(); 804 } 805 806 } 807 808 protected JCheckBoxMenuItem createMenuItem(LogLevel level) { 809 JCheckBoxMenuItem result = new JCheckBoxMenuItem(level.toString()); 810 result.setSelected(true); 811 result.setMnemonic(level.toString().charAt(0)); 812 result.addActionListener(new ActionListener () { 813 public void actionPerformed(ActionEvent e) { 814 _table.getFilteredLogTableModel().refresh(); 815 updateStatusLabel(); 816 } 817 }); 818 return result; 819 } 820 821 protected JMenu createViewMenu() { 823 JMenu result = new JMenu("View"); 824 result.setMnemonic('v'); 825 Iterator columns = getLogTableColumns(); 826 while (columns.hasNext()) { 827 result.add(getLogTableColumnMenuItem((LogTableColumn) columns.next())); 828 } 829 830 result.addSeparator(); 831 result.add(createAllLogTableColumnsMenuItem()); 832 result.add(createNoLogTableColumnsMenuItem()); 833 return result; 834 } 835 836 protected JCheckBoxMenuItem getLogTableColumnMenuItem(LogTableColumn column) { 837 JCheckBoxMenuItem result = (JCheckBoxMenuItem) (_logTableColumnMenuItems.get(column)); 838 if (result == null) { 839 result = createLogTableColumnMenuItem(column); 840 _logTableColumnMenuItems.put(column, result); 841 } 842 return result; 843 } 844 845 protected JCheckBoxMenuItem createLogTableColumnMenuItem(LogTableColumn column) { 846 JCheckBoxMenuItem result = new JCheckBoxMenuItem(column.toString()); 847 848 result.setSelected(true); 849 result.setMnemonic(column.toString().charAt(0)); 850 result.addActionListener(new ActionListener () { 851 public void actionPerformed(ActionEvent e) { 852 List selectedColumns = updateView(); 854 _table.setView(selectedColumns); 855 } 856 }); 857 return result; 858 } 859 860 protected List updateView() { 861 ArrayList updatedList = new ArrayList(); 862 Iterator columnIterator = _columns.iterator(); 863 while (columnIterator.hasNext()) { 864 LogTableColumn column = (LogTableColumn) columnIterator.next(); 865 JCheckBoxMenuItem result = getLogTableColumnMenuItem(column); 866 if (result.isSelected()) { 868 updatedList.add(column); 869 } 870 } 871 872 return updatedList; 873 } 874 875 protected JMenuItem createAllLogTableColumnsMenuItem() { 876 JMenuItem result = new JMenuItem("Show all Columns"); 877 result.setMnemonic('s'); 878 result.addActionListener(new ActionListener () { 879 public void actionPerformed(ActionEvent e) { 880 selectAllLogTableColumns(true); 881 List selectedColumns = updateView(); 883 _table.setView(selectedColumns); 884 } 885 }); 886 return result; 887 } 888 889 protected JMenuItem createNoLogTableColumnsMenuItem() { 890 JMenuItem result = new JMenuItem("Hide all Columns"); 891 result.setMnemonic('h'); 892 result.addActionListener(new ActionListener () { 893 public void actionPerformed(ActionEvent e) { 894 selectAllLogTableColumns(false); 895 List selectedColumns = updateView(); 897 _table.setView(selectedColumns); 898 } 899 }); 900 return result; 901 } 902 903 protected void selectAllLogTableColumns(boolean selected) { 904 Iterator columns = getLogTableColumns(); 905 while (columns.hasNext()) { 906 getLogTableColumnMenuItem((LogTableColumn) columns.next()).setSelected(selected); 907 } 908 } 909 910 protected JMenu createFileMenu() { 911 JMenu fileMenu = new JMenu("File"); 912 fileMenu.setMnemonic('f'); 913 JMenuItem exitMI; 914 fileMenu.add(createOpenMI()); 915 fileMenu.add(createOpenURLMI()); 916 fileMenu.addSeparator(); 917 fileMenu.add(createCloseMI()); 918 createMRUFileListMI(fileMenu); 919 fileMenu.addSeparator(); 920 fileMenu.add(createExitMI()); 921 return fileMenu; 922 } 923 924 928 protected JMenuItem createOpenMI() { 929 JMenuItem result = new JMenuItem("Open..."); 930 result.setMnemonic('o'); 931 result.addActionListener(new ActionListener () { 932 public void actionPerformed(ActionEvent e) { 933 requestOpen(); 934 } 935 }); 936 return result; 937 } 938 939 943 protected JMenuItem createOpenURLMI() { 944 JMenuItem result = new JMenuItem("Open URL..."); 945 result.setMnemonic('u'); 946 result.addActionListener(new ActionListener () { 947 public void actionPerformed(ActionEvent e) { 948 requestOpenURL(); 949 } 950 }); 951 return result; 952 } 953 954 protected JMenuItem createCloseMI() { 955 JMenuItem result = new JMenuItem("Close"); 956 result.setMnemonic('c'); 957 result.setAccelerator(KeyStroke.getKeyStroke("control Q")); 958 result.addActionListener(new ActionListener () { 959 public void actionPerformed(ActionEvent e) { 960 requestClose(); 961 } 962 }); 963 return result; 964 } 965 966 970 protected void createMRUFileListMI(JMenu menu) { 971 972 String [] files = _mruFileManager.getMRUFileList(); 973 974 if (files != null) { 975 menu.addSeparator(); 976 for (int i = 0; i < files.length; i++) { 977 JMenuItem result = new JMenuItem((i + 1) + " " + files[i]); 978 result.setMnemonic(i + 1); 979 result.addActionListener(new ActionListener () { 980 public void actionPerformed(ActionEvent e) { 981 requestOpenMRU(e); 982 } 983 }); 984 menu.add(result); 985 } 986 } 987 } 988 989 protected JMenuItem createExitMI() { 990 JMenuItem result = new JMenuItem("Exit"); 991 result.setMnemonic('x'); 992 result.addActionListener(new ActionListener () { 993 public void actionPerformed(ActionEvent e) { 994 requestExit(); 995 } 996 }); 997 return result; 998 } 999 1000 protected JMenu createConfigureMenu() { 1001 JMenu configureMenu = new JMenu("Configure"); 1002 configureMenu.setMnemonic('c'); 1003 configureMenu.add(createConfigureSave()); 1004 configureMenu.add(createConfigureReset()); 1005 configureMenu.add(createConfigureMaxRecords()); 1006 1007 return configureMenu; 1008 } 1009 1010 protected JMenuItem createConfigureSave() { 1011 JMenuItem result = new JMenuItem("Save"); 1012 result.setMnemonic('s'); 1013 result.addActionListener(new ActionListener () { 1014 public void actionPerformed(ActionEvent e) { 1015 saveConfiguration(); 1016 } 1017 }); 1018 1019 return result; 1020 } 1021 1022 protected JMenuItem createConfigureReset() { 1023 JMenuItem result = new JMenuItem("Reset"); 1024 result.setMnemonic('r'); 1025 result.addActionListener(new ActionListener () { 1026 public void actionPerformed(ActionEvent e) { 1027 resetConfiguration(); 1028 } 1029 }); 1030 1031 return result; 1032 } 1033 1034 protected JMenuItem createConfigureMaxRecords() { 1035 JMenuItem result = new JMenuItem("Set Max Number of Records"); 1036 result.setMnemonic('m'); 1037 result.addActionListener(new ActionListener () { 1038 public void actionPerformed(ActionEvent e) { 1039 setMaxRecordConfiguration(); 1040 } 1041 }); 1042 1043 return result; 1044 } 1045 1046 1047 protected void saveConfiguration() { 1048 _configurationManager.save(); 1049 } 1050 1051 protected void resetConfiguration() { 1052 _configurationManager.reset(); 1053 } 1054 1055 protected void setMaxRecordConfiguration() { 1056 LogFactor5InputDialog inputDialog = new LogFactor5InputDialog( 1057 getBaseFrame(), "Set Max Number of Records", "", 10); 1058 1059 String temp = inputDialog.getText(); 1060 1061 if (temp != null) { 1062 try { 1063 setMaxNumberOfLogRecords(Integer.parseInt(temp)); 1064 } catch (NumberFormatException e) { 1065 LogFactor5ErrorDialog error = new LogFactor5ErrorDialog( 1066 getBaseFrame(), 1067 "'" + temp + "' is an invalid parameter.\nPlease try again."); 1068 setMaxRecordConfiguration(); 1069 } 1070 } 1071 } 1072 1073 1074 protected JMenu createHelpMenu() { 1075 JMenu helpMenu = new JMenu("Help"); 1076 helpMenu.setMnemonic('h'); 1077 helpMenu.add(createHelpProperties()); 1078 return helpMenu; 1079 } 1080 1081 protected JMenuItem createHelpProperties() { 1082 final String title = "LogFactor5 Properties"; 1083 final JMenuItem result = new JMenuItem(title); 1084 result.setMnemonic('l'); 1085 result.addActionListener(new ActionListener () { 1086 public void actionPerformed(ActionEvent e) { 1087 showPropertiesDialog(title); 1088 } 1089 }); 1090 return result; 1091 } 1092 1093 protected void showPropertiesDialog(String title) { 1094 JOptionPane.showMessageDialog( 1095 _logMonitorFrame, 1096 _displayedLogBrokerProperties.toArray(), 1097 title, 1098 JOptionPane.PLAIN_MESSAGE 1099 ); 1100 } 1101 1102 protected JMenu createEditMenu() { 1103 JMenu editMenu = new JMenu("Edit"); 1104 editMenu.setMnemonic('e'); 1105 editMenu.add(createEditFindMI()); 1106 editMenu.add(createEditFindNextMI()); 1107 editMenu.addSeparator(); 1108 editMenu.add(createEditSortNDCMI()); 1109 editMenu.add(createEditRestoreAllNDCMI()); 1110 return editMenu; 1111 } 1112 1113 protected JMenuItem createEditFindNextMI() { 1114 JMenuItem editFindNextMI = new JMenuItem("Find Next"); 1115 editFindNextMI.setMnemonic('n'); 1116 editFindNextMI.setAccelerator(KeyStroke.getKeyStroke("F3")); 1117 editFindNextMI.addActionListener(new ActionListener () { 1118 public void actionPerformed(ActionEvent e) { 1119 findSearchText(); 1120 } 1121 }); 1122 return editFindNextMI; 1123 } 1124 1125 protected JMenuItem createEditFindMI() { 1126 JMenuItem editFindMI = new JMenuItem("Find"); 1127 editFindMI.setMnemonic('f'); 1128 editFindMI.setAccelerator(KeyStroke.getKeyStroke("control F")); 1129 1130 editFindMI.addActionListener( 1131 new ActionListener () { 1132 public void actionPerformed(ActionEvent e) { 1133 String inputValue = 1134 JOptionPane.showInputDialog( 1135 _logMonitorFrame, 1136 "Find text: ", 1137 "Search Record Messages", 1138 JOptionPane.QUESTION_MESSAGE 1139 ); 1140 setSearchText(inputValue); 1141 findSearchText(); 1142 } 1143 } 1144 1145 ); 1146 return editFindMI; 1147 } 1148 1149 protected JMenuItem createEditSortNDCMI() { 1153 JMenuItem editSortNDCMI = new JMenuItem("Sort by NDC"); 1154 editSortNDCMI.setMnemonic('s'); 1155 editSortNDCMI.addActionListener( 1156 new ActionListener () { 1157 public void actionPerformed(ActionEvent e) { 1158 String inputValue = 1159 JOptionPane.showInputDialog( 1160 _logMonitorFrame, 1161 "Sort by this NDC: ", 1162 "Sort Log Records by NDC", 1163 JOptionPane.QUESTION_MESSAGE 1164 ); 1165 setNDCTextFilter(inputValue); 1166 sortByNDC(); 1167 _table.getFilteredLogTableModel().refresh(); 1168 updateStatusLabel(); 1169 } 1170 } 1171 1172 ); 1173 return editSortNDCMI; 1174 } 1175 1176 protected JMenuItem createEditRestoreAllNDCMI() { 1179 JMenuItem editRestoreAllNDCMI = new JMenuItem("Restore all NDCs"); 1180 editRestoreAllNDCMI.setMnemonic('r'); 1181 editRestoreAllNDCMI.addActionListener( 1182 new ActionListener () { 1183 public void actionPerformed(ActionEvent e) { 1184 _table.getFilteredLogTableModel().setLogRecordFilter(createLogRecordFilter()); 1185 setNDCTextFilter(""); 1187 _table.getFilteredLogTableModel().refresh(); 1188 updateStatusLabel(); 1189 } 1190 } 1191 ); 1192 return editRestoreAllNDCMI; 1193 } 1194 1195 protected JToolBar createToolBar() { 1196 JToolBar tb = new JToolBar(); 1197 tb.putClientProperty("JToolBar.isRollover", Boolean.TRUE); 1198 JComboBox fontCombo = new JComboBox(); 1199 JComboBox fontSizeCombo = new JComboBox(); 1200 _fontSizeCombo = fontSizeCombo; 1201 1202 ClassLoader cl = this.getClass().getClassLoader(); 1203 if(cl == null) { 1204 cl = ClassLoader.getSystemClassLoader(); 1205 } 1206 URL newIconURL = cl.getResource("org/apache/log4j/lf5/viewer/" + 1207 "images/channelexplorer_new.gif"); 1208 1209 ImageIcon newIcon = null; 1210 1211 if (newIconURL != null) { 1212 newIcon = new ImageIcon(newIconURL); 1213 } 1214 1215 JButton newButton = new JButton("Clear Log Table"); 1216 1217 if (newIcon != null) { 1218 newButton.setIcon(newIcon); 1219 } 1220 1221 newButton.setToolTipText("Clear Log Table."); 1222 1224 newButton.addActionListener( 1225 new ActionListener () { 1226 public void actionPerformed(ActionEvent e) { 1227 _table.clearLogRecords(); 1228 _categoryExplorerTree.getExplorerModel().resetAllNodeCounts(); 1229 updateStatusLabel(); 1230 clearDetailTextArea(); 1231 LogRecord.resetSequenceNumber(); 1232 } 1233 } 1234 ); 1235 1236 Toolkit tk = Toolkit.getDefaultToolkit(); 1237 1239 String [] fonts; 1240 1241 if (_loadSystemFonts) { 1242 fonts = GraphicsEnvironment. 1243 getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 1244 } else { 1245 fonts = tk.getFontList(); 1246 } 1247 1248 for (int j = 0; j < fonts.length; j++) { 1249 fontCombo.addItem(fonts[j]); 1250 } 1251 1252 fontCombo.setSelectedItem(_fontName); 1253 1254 fontCombo.addActionListener( 1255 1256 new ActionListener () { 1257 public void actionPerformed(ActionEvent e) { 1258 JComboBox box = (JComboBox) e.getSource(); 1259 String font = (String ) box.getSelectedItem(); 1260 _table.setFont(new Font(font, Font.PLAIN, _fontSize)); 1261 _fontName = font; 1262 } 1263 } 1264 ); 1265 1266 fontSizeCombo.addItem("8"); 1267 fontSizeCombo.addItem("9"); 1268 fontSizeCombo.addItem("10"); 1269 fontSizeCombo.addItem("12"); 1270 fontSizeCombo.addItem("14"); 1271 fontSizeCombo.addItem("16"); 1272 fontSizeCombo.addItem("18"); 1273 fontSizeCombo.addItem("24"); 1274 1275 fontSizeCombo.setSelectedItem(String.valueOf(_fontSize)); 1276 fontSizeCombo.addActionListener( 1277 new ActionListener () { 1278 public void actionPerformed(ActionEvent e) { 1279 JComboBox box = (JComboBox) e.getSource(); 1280 String size = (String ) box.getSelectedItem(); 1281 int s = Integer.valueOf(size).intValue(); 1282 1283 setFontSizeSilently(s); 1284 refreshDetailTextArea(); 1285 _fontSize = s; 1286 } 1287 } 1288 ); 1289 1290 tb.add(new JLabel(" Font: ")); 1291 tb.add(fontCombo); 1292 tb.add(fontSizeCombo); 1293 tb.addSeparator(); 1294 tb.addSeparator(); 1295 tb.add(newButton); 1296 1297 newButton.setAlignmentY(0.5f); 1298 newButton.setAlignmentX(0.5f); 1299 1300 fontCombo.setMaximumSize(fontCombo.getPreferredSize()); 1301 fontSizeCombo.setMaximumSize( 1302 fontSizeCombo.getPreferredSize()); 1303 1304 return (tb); 1305 } 1306 1307 1321 protected void setView(String viewString, LogTable table) { 1322 if (DETAILED_VIEW.equals(viewString)) { 1323 table.setDetailedView(); 1324 } else { 1325 String message = viewString + "does not match a supported view."; 1326 throw new IllegalArgumentException (message); 1327 } 1328 _currentView = viewString; 1329 } 1330 1331 protected JComboBox createLogLevelCombo() { 1332 JComboBox result = new JComboBox(); 1333 Iterator levels = getLogLevels(); 1334 while (levels.hasNext()) { 1335 result.addItem(levels.next()); 1336 } 1337 result.setSelectedItem(_leastSevereDisplayedLogLevel); 1338 1339 result.addActionListener(new ActionListener () { 1340 public void actionPerformed(ActionEvent e) { 1341 JComboBox box = (JComboBox) e.getSource(); 1342 LogLevel level = (LogLevel) box.getSelectedItem(); 1343 setLeastSevereDisplayedLogLevel(level); 1344 } 1345 }); 1346 result.setMaximumSize(result.getPreferredSize()); 1347 return result; 1348 } 1349 1350 protected void setLeastSevereDisplayedLogLevel(LogLevel level) { 1351 if (level == null || _leastSevereDisplayedLogLevel == level) { 1352 return; } 1354 _leastSevereDisplayedLogLevel = level; 1355 _table.getFilteredLogTableModel().refresh(); 1356 updateStatusLabel(); 1357 } 1358 1359 1369 protected void trackTableScrollPane() { 1370 } 1372 1373 protected void centerFrame(JFrame frame) { 1374 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 1375 Dimension comp = frame.getSize(); 1376 1377 frame.setLocation(((screen.width - comp.width) / 2), 1378 ((screen.height - comp.height) / 2)); 1379 1380 } 1381 1382 1386 protected void requestOpen() { 1387 JFileChooser chooser; 1388 1389 if (_fileLocation == null) { 1390 chooser = new JFileChooser(); 1391 } else { 1392 chooser = new JFileChooser(_fileLocation); 1393 } 1394 1395 int returnVal = chooser.showOpenDialog(_logMonitorFrame); 1396 if (returnVal == JFileChooser.APPROVE_OPTION) { 1397 File f = chooser.getSelectedFile(); 1398 if (loadLogFile(f)) { 1399 _fileLocation = chooser.getSelectedFile(); 1400 _mruFileManager.set(f); 1401 updateMRUList(); 1402 } 1403 } 1404 } 1405 1406 1410 protected void requestOpenURL() { 1411 LogFactor5InputDialog inputDialog = new LogFactor5InputDialog( 1412 getBaseFrame(), "Open URL", "URL:"); 1413 String temp = inputDialog.getText(); 1414 1415 if (temp != null) { 1416 if (temp.indexOf("://") == -1) { 1417 temp = "http://" + temp; 1418 } 1419 1420 try { 1421 URL url = new URL (temp); 1422 if (loadLogFile(url)) { 1423 _mruFileManager.set(url); 1424 updateMRUList(); 1425 } 1426 } catch (MalformedURLException e) { 1427 LogFactor5ErrorDialog error = new LogFactor5ErrorDialog( 1428 getBaseFrame(), "Error reading URL."); 1429 } 1430 } 1431 } 1432 1433 1437 protected void updateMRUList() { 1438 JMenu menu = _logMonitorFrame.getJMenuBar().getMenu(0); 1439 menu.removeAll(); 1440 menu.add(createOpenMI()); 1441 menu.add(createOpenURLMI()); 1442 menu.addSeparator(); 1443 menu.add(createCloseMI()); 1444 createMRUFileListMI(menu); 1445 menu.addSeparator(); 1446 menu.add(createExitMI()); 1447 } 1448 1449 protected void requestClose() { 1450 setCallSystemExitOnClose(false); 1451 closeAfterConfirm(); 1452 } 1453 1454 1457 protected void requestOpenMRU(ActionEvent e) { 1458 String file = e.getActionCommand(); 1459 StringTokenizer st = new StringTokenizer(file); 1460 String num = st.nextToken().trim(); 1461 file = st.nextToken("\n"); 1462 1463 try { 1464 int index = Integer.parseInt(num) - 1; 1465 1466 InputStream in = _mruFileManager.getInputStream(index); 1467 LogFileParser lfp = new LogFileParser(in); 1468 lfp.parse(this); 1469 1470 _mruFileManager.moveToTop(index); 1471 updateMRUList(); 1472 1473 } catch (Exception me) { 1474 LogFactor5ErrorDialog error = new LogFactor5ErrorDialog( 1475 getBaseFrame(), "Unable to load file " + file); 1476 } 1477 1478 } 1479 1480 protected void requestExit() { 1481 _mruFileManager.save(); 1482 setCallSystemExitOnClose(true); 1483 closeAfterConfirm(); 1484 } 1485 1486 protected void closeAfterConfirm() { 1487 StringBuffer message = new StringBuffer (); 1488 1489 if (_callSystemExitOnClose == false) { 1490 message.append("Are you sure you want to close the logging "); 1491 message.append("console?\n"); 1492 message.append("(Note: This will not shut down the Virtual Machine,\n"); 1493 message.append("or the Swing event thread.)"); 1494 } else { 1495 message.append("Are you sure you want to exit?\n"); 1496 message.append("This will shut down the Virtual Machine.\n"); 1497 } 1498 1499 String title = 1500 "Are you sure you want to dispose of the Logging Console?"; 1501 1502 if (_callSystemExitOnClose == true) { 1503 title = "Are you sure you want to exit?"; 1504 } 1505 int value = JOptionPane.showConfirmDialog( 1506 _logMonitorFrame, 1507 message.toString(), 1508 title, 1509 JOptionPane.OK_CANCEL_OPTION, 1510 JOptionPane.QUESTION_MESSAGE, 1511 null 1512 ); 1513 1514 if (value == JOptionPane.OK_OPTION) { 1515 dispose(); 1516 } 1517 } 1518 1519 protected Iterator getLogLevels() { 1520 return _levels.iterator(); 1521 } 1522 1523 protected Iterator getLogTableColumns() { 1524 return _columns.iterator(); 1525 } 1526 1527 1530 protected boolean loadLogFile(File file) { 1531 boolean ok = false; 1532 try { 1533 LogFileParser lfp = new LogFileParser(file); 1534 lfp.parse(this); 1535 ok = true; 1536 } catch (IOException e) { 1537 LogFactor5ErrorDialog error = new LogFactor5ErrorDialog( 1538 getBaseFrame(), "Error reading " + file.getName()); 1539 } 1540 1541 return ok; 1542 } 1543 1544 1547 protected boolean loadLogFile(URL url) { 1548 boolean ok = false; 1549 try { 1550 LogFileParser lfp = new LogFileParser(url.openStream()); 1551 lfp.parse(this); 1552 ok = true; 1553 } catch (IOException e) { 1554 LogFactor5ErrorDialog error = new LogFactor5ErrorDialog( 1555 getBaseFrame(), "Error reading URL:" + url.getFile()); 1556 } 1557 return ok; 1558 } 1559 1563 1567 class LogBrokerMonitorWindowAdaptor extends WindowAdapter { 1568 protected LogBrokerMonitor _monitor; 1569 1570 public LogBrokerMonitorWindowAdaptor(LogBrokerMonitor monitor) { 1571 _monitor = monitor; 1572 } 1573 1574 public void windowClosing(WindowEvent ev) { 1575 _monitor.requestClose(); 1576 } 1577 } 1578} 1579 1580 1581 | Popular Tags |