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.awt.BorderLayout;
24  import java.awt.Dimension;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  
28  import java.io.*;
29  import javax.swing.*;
30  import javax.swing.table.*;
31  import javax.swing.event.*;
32  import java.awt.event.*;
33  
34  import javax.swing.*;
35  import javax.swing.BoxLayout;
36  import javax.swing.JButton;
37  import javax.swing.JPanel;
38  import javax.swing.JScrollPane;
39  import javax.swing.JTable;
40  import javax.swing.border.EmptyBorder;
41  import javax.swing.event.TableModelEvent;
42  import javax.swing.table.AbstractTableModel;
43  import java.awt.Component;
44  import javax.swing.table.DefaultTableCellRenderer;
45  
46  import net.sf.jour.rt.agent.EventProcessor;
47  import net.sf.jour.rt.agent.Monitor;
48  import net.sf.jour.rt.swingmonitor.ui.FDouble;
49  /***
50   * @author michaellif
51   *
52   * TODO To change the template for this generated type comment go to
53   * Window - Preferences - Java - Code Style - Code Templates
54   */
55  
56  public class MethodExecutionPanel extends MonitoringJPanel
57  	implements ActionListener, ListSelectionListener{
58  
59      private SwingMonitor papa;
60      
61      /***   DOCUMENT ME!   */
62      private JButton updateButton;
63  
64      /***   DOCUMENT ME!   */
65      private JButton resetButton;
66      
67      private JButton saveButton;
68  
69      /***   DOCUMENT ME!   */
70      private MethodExecutionTableModel methodExecutionTM;
71  
72      /***   DOCUMENT ME!   */
73      private MethodExceutionStatistics methodStat;
74  
75      private JTable table;
76      
77      private Object selectedRow;
78      
79      /***
80       * Creates a new InstanceCounterPanel object.
81       *
82       * @param instanceCounter DOCUMENT ME!
83       */
84      public MethodExecutionPanel(SwingMonitor thePapa, MethodExceutionStatistics methodStat) {
85          setLayout(new BorderLayout());
86          this.papa = thePapa;
87          this.methodStat = methodStat;
88          methodExecutionTM = new MethodExecutionTableModel();
89  
90          TableSorter sorter = new TableSorter(methodExecutionTM);
91          table = new JTable(sorter);
92          sorter.setTableHeader(table.getTableHeader());
93  
94          table.setPreferredScrollableViewportSize(new Dimension(600, 400));
95  		table.setDefaultRenderer(Object.class, new DoubleCellRenderer());
96  
97  		ListSelectionModel listMod =  table.getSelectionModel();
98  	    listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
99  	    listMod.addListSelectionListener(this);
100 
101 
102         //Create the scroll pane and add the table to it.
103         JScrollPane scrollPane = new JScrollPane(table);
104 
105         updateButton = new JButton("Update");
106         updateButton.addActionListener(this);
107         updateButton.setPreferredSize(new Dimension(100, 25));
108 
109         resetButton = new JButton("Reset");
110         resetButton.addActionListener(this);
111         resetButton.setPreferredSize(new Dimension(100, 25));
112 
113         saveButton = new JButton("Save");
114         saveButton.addActionListener(this);
115         saveButton.setPreferredSize(new Dimension(100, 25));
116         
117         JPanel controlPane = new JPanel();
118         controlPane.setBorder(new EmptyBorder(4, 4, 4, 4));
119         controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.X_AXIS));
120         controlPane.add(Box.createGlue());
121         controlPane.add(updateButton);
122         controlPane.add(Box.createRigidArea(new Dimension(5, 1)));
123         controlPane.add(resetButton);
124         controlPane.add(Box.createGlue());
125         controlPane.add(saveButton);
126         controlPane.add(Box.createGlue());
127 
128         //Add the scroll pane to this panel.
129         add(scrollPane, BorderLayout.CENTER);
130         add(controlPane, BorderLayout.SOUTH);
131 
132         table.addMouseListener(new MouseAdapter() {
133             public void mouseClicked(MouseEvent e) {
134                 if (e.getClickCount() == 2) {
135                     // double click
136                     papa.showOneMethodStat(selectedRow);
137                 }
138             }
139         });
140 
141         updateTable();
142     }
143     
144     class DoubleCellRenderer extends DefaultTableCellRenderer {
145 
146         public Component getTableCellRendererComponent(
147             JTable table,
148             Object value,
149             boolean isSelected,
150             boolean hasFocus,
151             int row,
152             int column) {
153             super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
154             if (value instanceof FDouble) {
155                 setHorizontalAlignment(RIGHT);
156             } else {
157 				setHorizontalAlignment(LEFT);
158             }
159             return this;
160         }
161 
162     }
163 
164     /***
165      * DOCUMENT ME!
166      */
167     public void updateTable() {
168         Object[][] data = methodStat.getReport();
169         methodExecutionTM.setData(data);
170     }
171 
172     public boolean canCloseTab() {
173         return false;
174     }
175     
176     public void updateData() {
177         updateTable();
178     }
179     
180     /***
181      * DOCUMENT ME!
182      */
183     public void resetTable() {
184     	Monitor.reset();
185         //methodStat.reset();
186     	EventProcessor.getInstance().reset();
187         updateTable();
188     }
189     
190     public void saveToFile() {
191         String filename = "statistic.csv";
192         JFileChooser fc = new JFileChooser(new File("."));
193         fc.setSelectedFile(new File(".", filename));
194         // Show save dialog; this method does not return until the dialog is closed
195         if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
196             if (!methodStat.saveReport(fc.getSelectedFile(), methodExecutionTM.getData())) {
197                 JOptionPane.showMessageDialog(this, "Error while writing to file");                
198             }
199         }
200 
201     }
202 
203     public void valueChanged(ListSelectionEvent e) {
204         if (!e.getValueIsAdjusting()) {        
205             int[] selRows = table.getSelectedRows();
206             if (selRows.length > 0) {
207                   TableModel tm = table.getModel();
208                   selectedRow = tm.getValueAt(selRows[0], -1);
209             }
210 
211         }
212     }
213     
214     /***
215      * DOCUMENT ME!
216      *
217      * @param event DOCUMENT ME!
218      */
219     public void actionPerformed(ActionEvent event) {
220         if (event.getSource() == updateButton) {
221             updateTable();
222         } else if (event.getSource() == resetButton) {
223             resetTable();
224         } else if (event.getSource() == saveButton) {
225             saveToFile();
226         }
227     }
228 
229     /***
230      * DOCUMENT ME!
231      *
232      * @author $author$
233      * @version $Revision: 1.11 $
234      */
235     class MethodExecutionTableModel extends AbstractTableModel {
236 
237         /***   DOCUMENT ME!   */
238         private Object[][] data = {};
239 
240         /***
241          * DOCUMENT ME!
242          *
243          * @param data DOCUMENT ME!
244          */
245         public void setData(Object[][] data) {
246             this.data = data;
247             fireTableChanged(new TableModelEvent(this));
248         }
249         
250         public Object[][] getData() {
251             return this.data;
252         }
253 
254         /***
255          * DOCUMENT ME!
256          *
257          * @return DOCUMENT ME!
258          */
259         public int getColumnCount() {
260             return MethodExceutionStatistics.columnNames.length;
261         }
262 
263         /***
264          * DOCUMENT ME!
265          *
266          * @return DOCUMENT ME!
267          */
268         public int getRowCount() {
269             return data.length;
270         }
271 
272         /***
273          * DOCUMENT ME!
274          *
275          * @param col DOCUMENT ME!
276          *
277          * @return DOCUMENT ME!
278          */
279         public String getColumnName(int col) {
280             return MethodExceutionStatistics.columnNames[col];
281         }
282 
283         /***
284          * DOCUMENT ME!
285          *
286          * @param row DOCUMENT ME!
287          * @param col DOCUMENT ME!
288          *
289          * @return DOCUMENT ME!
290          */
291         public Object getValueAt(int row, int col) {
292             return data[row][col + 1];
293         }
294 
295         /***
296          * DOCUMENT ME!
297          *
298          * @param c DOCUMENT ME!
299          *
300          * @return DOCUMENT ME!
301          */
302         public Class getColumnClass(int c) {
303             return getValueAt(0, c).getClass();
304         }
305 
306         /***
307          * DOCUMENT ME!
308          *
309          * @param row DOCUMENT ME!
310          * @param col DOCUMENT ME!
311          *
312          * @return DOCUMENT ME!
313          */
314         public boolean isCellEditable(int row, int col) {
315             return false;
316         }
317 
318         /***
319          * DOCUMENT ME!
320          *
321          * @param value DOCUMENT ME!
322          * @param row DOCUMENT ME!
323          * @param col DOCUMENT ME!
324          */
325         public void setValueAt(Object value, int row, int col) {
326             data[row][col] = value;
327             fireTableCellUpdated(row, col);
328         }
329     }
330 }