1 7 package org.jahia.sqlprofiler.gui; 8 9 import java.awt.BorderLayout ; 10 import java.awt.Dimension ; 11 import java.awt.event.WindowAdapter ; 12 import java.awt.event.WindowEvent ; 13 import java.io.IOException ; 14 import java.util.Properties ; 15 import javax.swing.BorderFactory ; 16 import javax.swing.JFrame ; 17 import javax.swing.JMenu ; 18 import javax.swing.JMenuBar ; 19 import javax.swing.JMenuItem ; 20 import javax.swing.JOptionPane ; 21 import javax.swing.JPanel ; 22 import javax.swing.JScrollPane ; 23 import javax.swing.JSplitPane ; 24 import javax.swing.JTable ; 25 import javax.swing.ListSelectionModel ; 26 import javax.swing.UIManager ; 27 import org.apache.log4j.Category; 28 import org.apache.log4j.PropertyConfigurator; 29 import javax.swing.*; 30 import java.awt.FlowLayout ; 31 import java.awt.Paint ; 32 import java.awt.Color ; 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 ; 39 40 47 public class Main 48 extends JFrame  49 { 50 51 private static final int DEFAULT_PORT = 4445; 52 53 54 public static final String PORT_PROP_NAME = "chainsaw.port"; 55 56 57 private static final Category LOG = Category.getInstance(Main.class); 58 59 60 63 private Main() { 64 super("SQL Profiler v0.3"); 66 try { 68 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 69 } catch (UnsupportedLookAndFeelException ulafe) { 70 LOG.error("Unsupported look & feel : ", ulafe); 71 } catch (IllegalAccessException iae) { 72 LOG.error("Illegal access exception : ", iae); 73 } catch (InstantiationException ie) { 74 LOG.error("Error : ", ie); 75 } catch (ClassNotFoundException cnfe) { 76 LOG.error("Error : ", cnfe); 77 } 78 79 final LoggerTableModel loggerModel = new LoggerTableModel(); 81 final ProfileStatementTableModel profileStatementTableModel = new 82 ProfileStatementTableModel(loggerModel); 83 loggerModel.setProfileStatementModel(profileStatementTableModel); 84 85 final JMenuBar menuBar = new JMenuBar (); 87 setJMenuBar(menuBar); 88 final JMenu menu = new JMenu ("File"); 89 menuBar.add(menu); 90 91 113 114 final JMenuItem exitMenuItem = new JMenuItem ("Exit"); 115 menu.add(exitMenuItem); 116 exitMenuItem.addActionListener(ExitAction.INSTANCE); 117 118 final MainTabbedPanel mainTabbedPanel = new MainTabbedPanel(); 119 120 final JPanel loggerPanel = new JPanel (new BorderLayout ()); 121 final JPanel profilerPanel = new JPanel (new BorderLayout ()); 122 final JPanel analysisPanel = new JPanel (new BorderLayout ()); 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 () { 140 public void windowClosing(WindowEvent aEvent) { 141 ExitAction.INSTANCE.actionPerformed(null); 142 } 143 }); 144 145 Box statusBar = Box.createHorizontalBox(); 146 JPanel messagePanel = new JPanel (new FlowLayout (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 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 profilerPanel) { 175 final JTable profileStatementTable = new JTable (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 profileScrollPane = new JScrollPane (profileStatementTable); 186 profileScrollPane.setBorder(BorderFactory.createTitledBorder("SQL Statements: ")); 187 profileScrollPane.setPreferredSize(new Dimension (700, 150)); 188 189 ProfileResultTableModel profileResultModel = new ProfileResultTableModel(); 190 profileStatementTableModel.setProfileResultModel(profileResultModel); 191 final JTable profileResultTable = new JTable (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 profileResultScrollPane = new JScrollPane (profileResultTable); 200 profileResultScrollPane.setBorder(BorderFactory.createTitledBorder("Profile results: ")); 201 profileResultScrollPane.setPreferredSize(new Dimension (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 final JPanel profileDetails = new ProfileDetailsPanel(profileStatementTable, profileStatementTableModel); 221 profileDetails.setPreferredSize(new Dimension (700, 150)); 222 223 final JSplitPane profileSplitPane = 224 new JSplitPane (JSplitPane.VERTICAL_SPLIT, profileScrollPane, profileResultScrollPane); 225 226 final JSplitPane profileSplitPane2 = 227 new JSplitPane (JSplitPane.VERTICAL_SPLIT, profileSplitPane, profileDetails); 228 229 profilerPanel.add(profileSplitPane2, BorderLayout.CENTER); 230 231 } 232 233 private void buildLoggerPanel(LoggerTableModel loggerModel, 234 JPanel loggerPanel) { 235 final ControlPanel cp = new ControlPanel(loggerModel); 237 loggerPanel.add(cp, BorderLayout.NORTH); 238 239 final JTable loggerTable = new JTable (loggerModel); 241 loggerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 242 final JScrollPane loggerScrollPane = new JScrollPane (loggerTable); 243 loggerScrollPane.setBorder(BorderFactory.createTitledBorder("Events: ")); 244 loggerScrollPane.setPreferredSize(new Dimension (700, 150)); 245 246 final JPanel loggerDetails = new DetailPanel(loggerTable, loggerModel); 248 loggerDetails.setPreferredSize(new Dimension (700, 150)); 249 250 final JSplitPane loggerSplitPane = 252 new JSplitPane (JSplitPane.VERTICAL_SPLIT, loggerScrollPane, loggerDetails); 253 loggerPanel.add(loggerSplitPane, BorderLayout.CENTER); 254 } 255 256 private void buildAnalysisPanel(LoggerTableModel loggerModel, ProfileStatementTableModel profileStatementModel, JPanel 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 (700, 150)); 263 264 QueryTrafficChartModel queryTrafficChartModel = new QueryTrafficChartModel(profileStatementModel); 265 ChartPanel queryTrafficChartPanel = new ChartPanel(queryTrafficChartModel); 266 loggerModel.setQueryTrafficChartModel(queryTrafficChartModel); 267 queryTrafficChartPanel.setPreferredSize(new Dimension (700, 150)); 268 269 final JSplitPane analysisSplitPane = 270 new JSplitPane (JSplitPane.VERTICAL_SPLIT, queryCountChartPanel, queryTrafficChartPanel); 271 272 analysisPanel.add(analysisSplitPane, BorderLayout.CENTER); 273 274 } catch (Throwable t) { 275 LOG.error("Error while creating chart panel", t); 276 } 277 278 } 279 280 285 private void setupReceiver(final LoggerTableModel aModel, 286 final StatusLabel statusLabel, 287 final JLabel messageTextLabel) { 288 int port = DEFAULT_PORT; 289 final String strRep = System.getProperty(PORT_PROP_NAME); 290 if (strRep != null) { 291 try { 292 port = Integer.parseInt(strRep); 293 } catch (NumberFormatException 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 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 339 340 341 private static void initLog4J() { 342 final Properties props = new Properties (); 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 356 public static void main(String [] aArgs) { 357 initLog4J(); 358 new Main(); 359 } 360 } 361 | Popular Tags |