View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.swingmonitor;
22  
23  import java.net.*;
24  import java.awt.*;
25  import javax.swing.UIManager;
26  
27  import javax.swing.*;
28  
29  import org.apache.log4j.Logger;
30  
31  import net.sf.jour.rt.agent.*;
32  import net.sf.jour.rt.swingmonitor.ui.*;
33  import net.sf.jour.rt.swingmonitor.ui.action.*;
34  
35  import net.sf.jour.util.*;
36  
37  /***
38   * @author Misha TODO To change the template for this generated type comment go
39   *         to Window - Preferences - Java - Code Style - Code Templates
40   */
41  public class SwingMonitor extends JFrame {
42      
43      private static final Logger log = Logger.getLogger(SwingMonitor.class);
44  
45      private JMenuBar menuBar = new JMenuBar();
46  
47      private JMenu fileMenu = new JMenu("File");
48  
49      private JMenu injectMenu = new JMenu("Instrument");
50  
51      private JMenu tabsMenu = new JMenu("Tabs");
52  
53      private JMenu helpMenu = new JMenu("Help");
54  
55      private StatusBar statusBar = new StatusBar();
56      
57      private EventProcessor processor;
58      
59      //private static EventLogger eventLogger = EventQueueLogger.getInstance();
60      JTabbedPane tabbedPane;
61  
62      /***
63       * Creates a new SwingMonitor object.
64       *
65       * @param instanceCounter DOCUMENT ME!
66       */
67      public SwingMonitor(String title, boolean isRuntime) {
68          
69          try {
70              URL url = getClass().getResource("jour.gif");
71              if (url == null) {
72                  url = FileUtil.getFileLocation("jour.gif");
73              }
74              if (url != null) {
75                  setIconImage(new ImageIcon(url).getImage());
76              }
77              //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
78              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
79          } catch (Throwable e) {
80              log.warn("Error", e);
81          }
82          
83          processor = EventProcessor.getInstance();
84          MethodExceutionStatistics mes = processor.getMethodExecutionStatistics();
85  
86          InstanceCounterPanel icPanel = new InstanceCounterPanel(processor.getInstanceCounter());
87          MethodExecutionPanel mePanel = new MethodExecutionPanel(this, mes);
88          JVMRuntimeChartPanel jvms = new JVMRuntimeChartPanel(processor.getJvmInfo());
89  
90          MethodsChartPanel mePanelTotals = new MethodsChartPanel(mes.getTotals(), mes);
91          
92          // Get memory usage every 5 sec;
93          JPanel mu = new MonitorMemoryUsagePanel(20, 5000);
94          
95          tabbedPane = new JTabbedPane();
96          //tabbedPane.setTabPlacement(JTabbedPane.TOP);
97          tabbedPane.addTab("Methods", mePanel);
98          tabbedPane.addTab("Total", mePanelTotals);
99          tabbedPane.addTab("Instances", icPanel);
100         tabbedPane.addTab("Memory", jvms);
101         tabbedPane.addTab("Analyzer", mu);
102 
103         Container pane = getContentPane();
104         pane.setLayout(new BorderLayout());
105         pane.add(statusBar, BorderLayout.SOUTH);
106         pane.add(tabbedPane, BorderLayout.CENTER);
107         addMenuBar();
108 
109         //JLabel instructions = new JLabel("<html><ul></html>");
110         //this.getContentPane().add(instructions, BorderLayout.NORTH);
111         //this.getContentPane().add(tabbedPane, BorderLayout.CENTER);
112 
113         String systemTitle = System.getProperty("java.security.policy");
114         if (systemTitle != null && systemTitle.toLowerCase().trim().indexOf("weblogic") > -1) {
115             systemTitle = "Weblogic Server";
116         } else if (systemTitle!=null && systemTitle.toLowerCase().trim().indexOf("jrun") > -1) {
117             systemTitle = "JRun Server";
118         } else if (title != null)  {
119             systemTitle = title;
120         } else {
121             systemTitle = "Runtime";
122         }
123 
124         this.setTitle("Jour - " + systemTitle);
125         this.pack();
126         
127         icPanel.requestFocus(); // Give the panel focus.
128         
129         if (!isRuntime) {
130             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
131         } else {
132             this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
133         }
134         this.setVisible(true);
135     }
136     
137     
138     public void showOneMethodStat(Object statItem) {
139         MethodsChartPanel chartPanel = new MethodsChartPanel(statItem, processor.getMethodExecutionStatistics());
140         tabbedPane.addTab(chartPanel.getTabTitle(), chartPanel);
141         tabbedPane.setSelectedComponent(chartPanel);
142     }
143     
144     /***
145      * Method addMenuBar
146      */
147     public void addMenuBar()  {
148         JMenuItem exit = new JMenuItem("Exit");
149         fileMenu.setMnemonic('F');
150         
151         JMenuItem open = new JMenuItem("Monitor data folder");
152         fileMenu.add(open);
153         open.setMnemonic('O');
154         open.addActionListener(new MonitorDataFolderAction(this)); 
155         
156         fileMenu.addSeparator();
157         fileMenu.add(exit);
158         
159         exit.setMnemonic('X');
160         exit.addActionListener(new ExitAction());
161         
162         JMenuItem close = new JMenuItem(new CloseCurrentTabAction(tabbedPane));
163         tabsMenu.add(close);
164         //menuBar.add(new JMenu(new UpdateCurrentTabAction(tabbedPane)));
165         
166         menuBar.add(fileMenu);
167         menuBar.add(tabsMenu);
168         
169         menuBar.add(new JMenuItem(new UpdateCurrentTabAction(tabbedPane)));
170                 
171         //menuBar.add(injectMenu);
172         //menuBar.add(helpMenu);
173         setJMenuBar(menuBar);
174     }
175 }