/*
 * Copyright 2008-2012 by Emeric Vernat
 *
 *     This file is part of Java Melody.
 *
 * Java Melody is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Java Melody is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Java Melody.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.bull.javamelody;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.bull.javamelody.swing.MButton;

/**
 * Panel du détail d'une requête.
 * @author Emeric Vernat
 */
class CounterRequestDetailPanel extends MelodyPanel {
	private static final long serialVersionUID = 1L;

	private final CounterRequest request;

	CounterRequestDetailPanel(RemoteCollector remoteCollector, CounterRequest request)
			throws IOException {
		super(remoteCollector);
		this.request = request;

		refresh();
	}

	final void refresh() throws IOException {
		removeAll();

		final String graphName = request.getId();
		final String graphLabel = truncate(request.getName(), 50);
		setName(graphLabel);

		final RemoteCollector remoteCollector = getRemoteCollector();
		final CounterRequestDetailTablePanel counterRequestDetailTablePanel = new CounterRequestDetailTablePanel(
				remoteCollector, request);
		add(counterRequestDetailTablePanel, BorderLayout.NORTH);

		if (CounterRequestTable.isRequestGraphDisplayed(getCounterByRequestId(request))) {
			final MButton refreshButton = createRefreshButton();
			refreshButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						getRemoteCollector().collectDataIncludingCurrentRequests();
						refresh();
					} catch (final IOException ex) {
						showException(ex);
					}
				}
			});
			final ChartPanel chartPanel = new ChartPanel(remoteCollector, graphName, graphLabel,
					refreshButton);
			add(chartPanel, BorderLayout.CENTER);
		}

		if (JdbcWrapper.SINGLETON.getSqlCounter().isRequestIdFromThisCounter(graphName)
				&& !request.getName().toLowerCase().startsWith("alter ")) {
			// inutile d'essayer d'avoir le plan d'exécution des requêtes sql
			// telles que "alter session set ..." (cf issue 152)
			final String sqlRequestExplainPlan = remoteCollector
					.collectSqlRequestExplainPlan(request.getName());
			if (sqlRequestExplainPlan != null) {
				final JPanel panel = createSqlRequestExplainPlanPanel(sqlRequestExplainPlan);
				add(panel, BorderLayout.SOUTH);
			}
		}
	}

	private JPanel createSqlRequestExplainPlanPanel(String sqlRequestExplainPlan) {
		final JTextArea textArea = new JTextArea();
		textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, textArea.getFont().getSize() - 1));
		textArea.setEditable(false);
		textArea.setCaretPosition(0);
		// background nécessaire avec la plupart des look and feels dont Nimbus,
		// sinon il reste blanc malgré editable false
		textArea.setBackground(Color.decode("#E6E6E6"));
		textArea.setText(sqlRequestExplainPlan);
		final JPanel panel = new JPanel(new BorderLayout());
		panel.setOpaque(false);
		final JLabel label = new JLabel(I18N.getString("Plan_d_execution"));
		label.setFont(label.getFont().deriveFont(Font.BOLD));
		panel.add(label, BorderLayout.NORTH);
		final JScrollPane scrollPane = new JScrollPane(textArea);
		panel.add(scrollPane, BorderLayout.CENTER);
		return panel;
	}

	private Counter getCounterByRequestId(CounterRequest counterRequest) {
		return getCollector().getCounterByRequestId(counterRequest);
	}

	private static String truncate(String string, int maxLength) {
		return string.substring(0, Math.min(string.length(), maxLength));
	}
}

@@ -1104,16 +1104,57 @@
 xtArea;%0D
+%0Aimport javax.swing.ScrollPaneConstants;%0D
 %0A%0D%0Aimpor
@@ -1791,24 +1791,350 @@
 hLabel);%0D%0A%0D%0A
+%09%09final JPanel panel = new JPanel(new BorderLayout());%0D%0A%09%09final JScrollPane scrollPane = new JScrollPane(panel);%0D%0A%09%09scrollPane.getVerticalScrollBar().setFocusable(false);%0D%0A%09%09scrollPane.getHorizontalScrollBar().setFocusable(false);%0D%0A%09%09scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);%0D%0A%0D%0A
 %09%09final Remo
@@ -2329,16 +2329,22 @@
 st);%0D%0A%09%09
+panel.
 add(coun
@@ -2961,16 +2961,22 @@
 n);%0D%0A%09%09%09
+panel.
 add(char
@@ -3456,24 +3456,28 @@
 nal JPanel p
+lanP
 anel = creat
@@ -3537,13 +3537,23 @@
 %09%09%09%09
+panel.
 add(p
+lanP
 anel
@@ -3586,16 +3586,38 @@
 %09%7D%0D%0A%09%09%7D%0D
+%0A%0D%0A%09%09add(scrollPane);%0D
 %0A%09%7D%0D%0A%0D%0A%09

fix for no scrollBar in request details when requests children are too many