KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > sqlprofiler > gui > Main


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software
5  * License version 1.1, a copy of which has been included with this
6  * distribution in the APACHE.txt file. */

7 package org.jahia.sqlprofiler.gui;
8
9 import java.awt.BorderLayout JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.awt.event.WindowAdapter JavaDoc;
12 import java.awt.event.WindowEvent JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.Properties JavaDoc;
15 import javax.swing.BorderFactory JavaDoc;
16 import javax.swing.JFrame JavaDoc;
17 import javax.swing.JMenu JavaDoc;
18 import javax.swing.JMenuBar JavaDoc;
19 import javax.swing.JMenuItem JavaDoc;
20 import javax.swing.JOptionPane JavaDoc;
21 import javax.swing.JPanel JavaDoc;
22 import javax.swing.JScrollPane JavaDoc;
23 import javax.swing.JSplitPane JavaDoc;
24 import javax.swing.JTable JavaDoc;
25 import javax.swing.ListSelectionModel JavaDoc;
26 import javax.swing.UIManager JavaDoc;
27 import org.apache.log4j.Category;
28 import org.apache.log4j.PropertyConfigurator;
29 import javax.swing.*;
30 import java.awt.FlowLayout JavaDoc;
31 import java.awt.Paint JavaDoc;
32 import java.awt.Color JavaDoc;
33 import org.jCharts.properties.PieChart2DProperties;
34 import org.jCharts.chartData.PieChartDataSet;
35 import org.jCharts.nonAxisChart.PieChart2D;
36 import org.jCharts.properties.LegendProperties;
37 import org.jCharts.properties.ChartProperties;
38 import java.awt.Graphics2D JavaDoc;
39
40 /**
41  * The main application.
42  *
43  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
44  *
45  * 11/14/2003 : Add DML statements by Jean-Philippe VALENTIN (JPV) version 0.3
46  */

47 public class Main
48     extends JFrame JavaDoc
49 {
50     /** the default port number to listen on **/
51     private static final int DEFAULT_PORT = 4445;
52
53     /** name of property for port name **/
54     public static final String JavaDoc PORT_PROP_NAME = "chainsaw.port";
55
56     /** use to log messages **/
57     private static final Category LOG = Category.getInstance(Main.class);
58
59
60     /**
61      * Creates a new <code>Main</code> instance.
62      */

63     private Main() {
64         super("SQL Profiler v0.3"); //JPV : change version 0.2 to 0.3
65

66         // First let's use the system's default look and feel
67
try {
68             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
69         } catch (UnsupportedLookAndFeelException ulafe) {
70             LOG.error("Unsupported look & feel : ", ulafe);
71         } catch (IllegalAccessException JavaDoc iae) {
72             LOG.error("Illegal access exception : ", iae);
73         } catch (InstantiationException JavaDoc ie) {
74             LOG.error("Error : ", ie);
75         } catch (ClassNotFoundException JavaDoc cnfe) {
76             LOG.error("Error : ", cnfe);
77         }
78
79         // create the all important model
80
final LoggerTableModel loggerModel = new LoggerTableModel();
81         final ProfileStatementTableModel profileStatementTableModel = new
82             ProfileStatementTableModel(loggerModel);
83         loggerModel.setProfileStatementModel(profileStatementTableModel);
84
85         //Create the menu bar.
86
final JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
87         setJMenuBar(menuBar);
88         final JMenu JavaDoc menu = new JMenu JavaDoc("File");
89         menuBar.add(menu);
90
91         /*
92         try {
93             final LoadXMLAction lxa = new LoadXMLAction(this, loggerModel);
94             final JMenuItem loadMenuItem = new JMenuItem("Load Log4J XML file...");
95             menu.add(loadMenuItem);
96             loadMenuItem.addActionListener(lxa);
97         } catch (NoClassDefFoundError e) {
98             LOG.info("Missing classes for XML parser", e);
99             JOptionPane.showMessageDialog(
100                 this,
101                 "XML parser not in classpath - unable to load XML events.",
102                 "CHAINSAW",
103                 JOptionPane.ERROR_MESSAGE);
104         } catch (Exception e) {
105             LOG.info("Unable to create the action to load XML files", e);
106             JOptionPane.showMessageDialog(
107                 this,
108                 "Unable to create a XML parser - unable to load XML events.",
109                 "CHAINSAW",
110                 JOptionPane.ERROR_MESSAGE);
111         }
112         */

113
114         final JMenuItem JavaDoc exitMenuItem = new JMenuItem JavaDoc("Exit");
115         menu.add(exitMenuItem);
116         exitMenuItem.addActionListener(ExitAction.INSTANCE);
117
118         final MainTabbedPanel mainTabbedPanel = new MainTabbedPanel();
119
120         final JPanel JavaDoc loggerPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
121         final JPanel JavaDoc profilerPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
122         final JPanel JavaDoc analysisPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
123
124         final ToolbarPanel toolbarPanel = new ToolbarPanel(loggerModel, profileStatementTableModel);
125         getContentPane().add(toolbarPanel, BorderLayout.NORTH);
126
127         buildLoggerPanel(loggerModel, loggerPanel);
128
129         buildProfilePanel(profileStatementTableModel, profilerPanel);
130
131         buildAnalysisPanel(loggerModel, profileStatementTableModel, analysisPanel);
132
133         mainTabbedPanel.addTab("Profiler", profilerPanel, "SQL Profiler");
134         mainTabbedPanel.addTab("Logger", loggerPanel, "Logging view");
135         mainTabbedPanel.addTab("Analysis", analysisPanel, "Analysis view");
136         mainTabbedPanel.finishTabs();
137         getContentPane().add(mainTabbedPanel, BorderLayout.CENTER);
138
139         addWindowListener(new WindowAdapter JavaDoc() {
140             public void windowClosing(WindowEvent JavaDoc aEvent) {
141                 ExitAction.INSTANCE.actionPerformed(null);
142             }
143         });
144
145         Box statusBar = Box.createHorizontalBox();
146         JPanel JavaDoc messagePanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.LEFT, 0, 0));
147         messagePanel.setBorder(BorderFactory.createLoweredBevelBorder());
148         JLabel messageTextLabel = new JLabel();
149         messageTextLabel.setHorizontalAlignment(JLabel.LEFT);
150         messageTextLabel.setText("Not connected to P6Spy");
151         messagePanel.add(messageTextLabel);
152         statusBar.add(messagePanel);
153         /*
154         JPanel progressPanel = new JPanel(new BorderLayout());
155         progressPanel.setBorder(BorderFactory.createLoweredBevelBorder());
156         JProgressBar progressBar = new JProgressBar();
157         progressBar.setValue(0);
158         progressPanel.add(progressBar, BorderLayout.CENTER);
159         statusBar.add(progressPanel);
160         */

161         Box statusLabelBox = Box.createHorizontalBox();
162         StatusLabel statusLabel = new StatusLabel();
163         statusLabelBox.add(statusLabel);
164         statusBar.add(statusLabelBox);
165         getContentPane().add(statusBar, BorderLayout.SOUTH);
166
167         pack();
168         setVisible(true);
169
170         setupReceiver(loggerModel, statusLabel, messageTextLabel);
171     }
172
173     private void buildProfilePanel(ProfileStatementTableModel profileStatementTableModel,
174                                    JPanel JavaDoc profilerPanel) {
175         // Create the table
176
final JTable JavaDoc profileStatementTable = new JTable JavaDoc(profileStatementTableModel);
177         profileStatementTableModel.addMouseListenerToHeaderInTable(profileStatementTable);
178         profileStatementTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
179         profileStatementTable.getColumnModel().getColumn(0).setMinWidth(120);
180         profileStatementTable.getColumnModel().getColumn(1).setMaxWidth(70);
181         profileStatementTable.getColumnModel().getColumn(2).setMaxWidth(30);
182         profileStatementTable.getColumnModel().getColumn(3).setPreferredWidth(70);
183         profileStatementTable.getColumnModel().getColumn(4).setPreferredWidth(70);
184         profileStatementTable.getColumnModel().getColumn(5).setPreferredWidth(680-70-70-30-70-150);
185         final JScrollPane JavaDoc profileScrollPane = new JScrollPane JavaDoc(profileStatementTable);
186         profileScrollPane.setBorder(BorderFactory.createTitledBorder("SQL Statements: "));
187         profileScrollPane.setPreferredSize(new Dimension JavaDoc(700, 150));
188
189         ProfileResultTableModel profileResultModel = new ProfileResultTableModel();
190         profileStatementTableModel.setProfileResultModel(profileResultModel);
191         final JTable JavaDoc profileResultTable = new JTable JavaDoc(profileResultModel);
192         profileResultModel.addMouseListenerToHeaderInTable(profileResultTable);
193         profileResultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
194         profileResultTable.getColumnModel().getColumn(0).setMaxWidth(50);
195         profileResultTable.getColumnModel().getColumn(1).setMaxWidth(70);
196         profileResultTable.getColumnModel().getColumn(2).setMaxWidth(70);
197         profileResultTable.getColumnModel().getColumn(3).setPreferredWidth(160);
198         profileResultTable.getColumnModel().getColumn(4).setPreferredWidth(680-160-70-70-50);
199         final JScrollPane JavaDoc profileResultScrollPane = new JScrollPane JavaDoc(profileResultTable);
200         profileResultScrollPane.setBorder(BorderFactory.createTitledBorder("Profile results: "));
201         profileResultScrollPane.setPreferredSize(new Dimension JavaDoc(700, 150));
202
203         Box counterBox = Box.createHorizontalBox();
204
205         JLabel statementCountTextLabel = new JLabel(" Total statements : ");
206         counterBox.add(statementCountTextLabel);
207         JLabel statementCountValueLabel = new JLabel("0");
208         profileStatementTableModel.setStatementCountValueLabel(statementCountValueLabel);
209         counterBox.add(statementCountValueLabel);
210         counterBox.add(Box.createHorizontalStrut(10));
211         JLabel statementTimeTextLabel = new JLabel(" Total statement time [ms] : ");
212         counterBox.add(statementTimeTextLabel);
213         JLabel statementTimeValueLabel = new JLabel("0");
214         profileStatementTableModel.setStatementTimeValueLabel(statementTimeValueLabel);
215         counterBox.add(statementTimeValueLabel);
216
217         profilerPanel.add(counterBox, BorderLayout.NORTH);
218
219         // Create the details
220
final JPanel JavaDoc profileDetails = new ProfileDetailsPanel(profileStatementTable, profileStatementTableModel);
221         profileDetails.setPreferredSize(new Dimension JavaDoc(700, 150));
222
223         final JSplitPane JavaDoc profileSplitPane =
224             new JSplitPane JavaDoc(JSplitPane.VERTICAL_SPLIT, profileScrollPane, profileResultScrollPane);
225
226         final JSplitPane JavaDoc profileSplitPane2 =
227             new JSplitPane JavaDoc(JSplitPane.VERTICAL_SPLIT, profileSplitPane, profileDetails);
228
229         profilerPanel.add(profileSplitPane2, BorderLayout.CENTER);
230
231     }
232
233     private void buildLoggerPanel(LoggerTableModel loggerModel,
234                                   JPanel JavaDoc loggerPanel) {
235         // Add control panel
236
final ControlPanel cp = new ControlPanel(loggerModel);
237         loggerPanel.add(cp, BorderLayout.NORTH);
238
239         // Create the table
240
final JTable JavaDoc loggerTable = new JTable JavaDoc(loggerModel);
241         loggerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
242         final JScrollPane JavaDoc loggerScrollPane = new JScrollPane JavaDoc(loggerTable);
243         loggerScrollPane.setBorder(BorderFactory.createTitledBorder("Events: "));
244         loggerScrollPane.setPreferredSize(new Dimension JavaDoc(700, 150));
245
246         // Create the details
247
final JPanel JavaDoc loggerDetails = new DetailPanel(loggerTable, loggerModel);
248         loggerDetails.setPreferredSize(new Dimension JavaDoc(700, 150));
249
250         // Add the table and stack trace into a splitter
251
final JSplitPane JavaDoc loggerSplitPane =
252             new JSplitPane JavaDoc(JSplitPane.VERTICAL_SPLIT, loggerScrollPane, loggerDetails);
253         loggerPanel.add(loggerSplitPane, BorderLayout.CENTER);
254     }
255
256     private void buildAnalysisPanel(LoggerTableModel loggerModel, ProfileStatementTableModel profileStatementModel, JPanel JavaDoc analysisPanel) {
257
258         try {
259             QueryCountChartModel queryCountChartModel = new QueryCountChartModel(profileStatementModel);
260             ChartPanel queryCountChartPanel = new ChartPanel(queryCountChartModel);
261             loggerModel.setQueryCountChartModel(queryCountChartModel);
262             queryCountChartPanel.setPreferredSize(new Dimension JavaDoc(700, 150));
263
264             QueryTrafficChartModel queryTrafficChartModel = new QueryTrafficChartModel(profileStatementModel);
265             ChartPanel queryTrafficChartPanel = new ChartPanel(queryTrafficChartModel);
266             loggerModel.setQueryTrafficChartModel(queryTrafficChartModel);
267             queryTrafficChartPanel.setPreferredSize(new Dimension JavaDoc(700, 150));
268
269             final JSplitPane JavaDoc analysisSplitPane =
270                 new JSplitPane JavaDoc(JSplitPane.VERTICAL_SPLIT, queryCountChartPanel, queryTrafficChartPanel);
271
272             analysisPanel.add(analysisSplitPane, BorderLayout.CENTER);
273
274         } catch (Throwable JavaDoc t) {
275             LOG.error("Error while creating chart panel", t);
276         }
277
278     }
279
280     /**
281      * Setup recieving messages.
282      *
283      * @param aModel a <code>MyTableModel</code> value
284      */

285     private void setupReceiver(final LoggerTableModel aModel,
286                                final StatusLabel statusLabel,
287                                final JLabel messageTextLabel) {
288         int port = DEFAULT_PORT;
289         final String JavaDoc strRep = System.getProperty(PORT_PROP_NAME);
290         if (strRep != null) {
291             try {
292                 port = Integer.parseInt(strRep);
293             } catch (NumberFormatException JavaDoc nfe) {
294                 LOG.fatal("Unable to parse " + PORT_PROP_NAME +
295                           " property with value " + strRep + ".");
296                 JOptionPane.showMessageDialog(
297                     this,
298                     "Unable to parse port number from '" + strRep +
299                     "', quitting.",
300                     "CHAINSAW",
301                     JOptionPane.ERROR_MESSAGE);
302                 System.exit(1);
303             }
304         }
305
306         try {
307             final LoggingReceiver lr = new LoggingReceiver(aModel, port);
308             lr.addLogReceptionListener(new LogReceptionAdapter() {
309                 public void logReceptionConnected(LogReceptionEvent logReceptionEvent) {
310                     statusLabel.setStatusActivated(true);
311                     messageTextLabel.setText("Connected to P6Spy");
312                 }
313                 public void logReceptionDisconnected(LogReceptionEvent logReceptionEvent) {
314                     statusLabel.setStatusActivated(false);
315                     messageTextLabel.setText("Disconnected from P6Spy");
316                 }
317                 public void logReceptionDataReceived(LogReceptionEvent logReceptionEvent) {
318                     aModel.addEvent(logReceptionEvent.getEventDetails());
319                 }
320             });
321
322             lr.start();
323         } catch (IOException JavaDoc e) {
324             LOG.fatal("Unable to connect to socket server, quiting", e);
325             JOptionPane.showMessageDialog(
326                 this,
327                 "Unable to create socket on port " + port + ", quitting.",
328                 "CHAINSAW",
329                 JOptionPane.ERROR_MESSAGE);
330             System.exit(1);
331         }
332     }
333
334
335     ////////////////////////////////////////////////////////////////////////////
336
// static methods
337
////////////////////////////////////////////////////////////////////////////
338

339
340     /** initialise log4j **/
341     private static void initLog4J() {
342         final Properties JavaDoc props = new Properties JavaDoc();
343         props.setProperty("log4j.rootCategory", "DEBUG, A1");
344         props.setProperty("log4j.appender.A1",
345                           "org.apache.log4j.ConsoleAppender");
346         props.setProperty("log4j.appender.A1.layout",
347                           "org.apache.log4j.TTCCLayout");
348         PropertyConfigurator.configure(props);
349     }
350
351     /**
352      * The main method.
353      *
354      * @param aArgs ignored
355      */

356     public static void main(String JavaDoc[] aArgs) {
357         initLog4J();
358         new Main();
359     }
360 }
361
Popular Tags