/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.threadlog.ui; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.filechooser.FileNameExtensionFilter; import org.simantics.threadlog.ThreadLog; public class ThreadLogController extends JFrame { private static final long serialVersionUID = -2487997716157625672L; boolean isLogging = false; JButton logButton; JButton loadButton; public ThreadLogController() { super("Thread log controller"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); logButton = new JButton("Start logging"); logButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(isLogging) { logButton.setText("Start logging"); isLogging = false; ThreadLog log = ThreadLog.setDefaultThreadLog(null); ThreadLogVisualizer visualizer = new ThreadLogVisualizer(); visualizer.setLog(log); visualizer.setVisible(true); } else { logButton.setText("Stop logging"); isLogging = true; ThreadLog.setDefaultThreadLog(new ThreadLog()); } } }); loadButton = new JButton("Load log"); loadButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "Thread Logs (*.tlog)", "tlog", "tlog"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(ThreadLogController.this); if (returnVal != JFileChooser.APPROVE_OPTION) return; try { ThreadLog log = ThreadLog.deserialize(chooser.getSelectedFile()); ThreadLogVisualizer visualizer = new ThreadLogVisualizer(); visualizer.setLog(log); visualizer.setVisible(true); } catch (IOException ex) { ex.printStackTrace(); } } }); getContentPane().setLayout(new GridLayout(2, 1)); getContentPane().add(logButton); getContentPane().add(loadButton); setSize(200, 100); } public static void start() { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new ThreadLogController().setVisible(true); } }); } public static void main(String[] args) { start(); } }