package crushtools.plot;
/*******************************************************************************
 * Copyright (c) 2014 Attila Kovacs <attila[AT]sigmyne.com>.
 * All rights reserved. 
 * 
 * This file is part of crushtools.
 * 
 *     crushtools is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     crushtools 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 General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with crushtools.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Attila Kovacs <attila[AT]sigmyne.com> - initial API and implementation
 ******************************************************************************/
// (C)2007 Attila Kovacs <attila[AT]sigmyne.com>

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class InfoPanel extends JPanel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 2660465593795584392L;
	
	public int borderwidth = 4, spacing = 4, margin = 4;
    public Font font = new Font("SansSerif", Font.PLAIN, 13); 
    public String[] lines;
    
    public Font tabFont = new Font("SansSerif", Font.BOLD, 11); 
    int tabWidth = -1;
    public String tabText = null;
    private int tabHeight;
    
    
    private int[] x = new int[4], y = new int[4];
    
    public InfoPanel(int size) {
    	//setBorder(BorderFactory.createRaisedBevelBorder());
    	//setBorder(BorderFactory.createLineBorder(Color.black));
    	
    	//setBackground(Color.WHITE);
    	setBackground(new Color(248,248,248));
    	setFont(font);
    	lines = new String[Math.max(1, size)];
    	for(int i=0; i<lines.length; i++) lines[i] = "";
    	
    	addMouseListener(new MouseAdapter() {
    		@Override
			public void mouseClicked(MouseEvent evt) {
    			if(tabWidth < 1) return;
    			
    			int x = evt.getX();
    			int y = evt.getY();
    			if(x > getWidth() - tabWidth) if(y > getHeight() - tabHeight) tabClicked();
    		}
    		
    	});
    	
    }
    
    public void tabClicked() {}
    
    public void setTab(int width) {
    	tabWidth = width;
    }
     
    public void setPreferredSize(FontMetrics fm) {
    	int fontheight=fm.getHeight();
    	
    	int height = 2*margin + lines.length * fontheight + (lines.length-1) * spacing + borderwidth;
    	setPreferredSize(new Dimension(1, height));
    }
    
    @Override
	public void paintComponent(Graphics g) {
    	
    	FontMetrics fm = g.getFontMetrics(font);
    	int fontheight=fm.getHeight();
    	
    	//int height = 2*borderwidth + lines.length * fontheight + (lines.length-1) * spacing;
    	//setPreferredSize(new Dimension(1, height));
    	
    	Graphics2D g2 = (Graphics2D) g;
    	g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    	//g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		//g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		//g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
		
    	
    	super.paintComponent(g);
    	
    	int offset = fontheight + borderwidth - 2;

    	for(int i=0; i<lines.length; i++) {
    		if(lines[0] != null) g.drawString(lines[i], borderwidth + 5, offset);
    		offset += spacing + fontheight;
    	}
    
    	if(tabWidth > 0) {
    		fm = g.getFontMetrics(tabFont);
	    	fontheight=fm.getHeight();
    		
    		g.setColor(Color.WHITE);
    		int w = getWidth();
    		int h = getHeight();
    		tabHeight = fontheight + spacing;
    		x[0] = w - tabWidth;
    		x[1] = x[0] + tabHeight / 2;
    		x[2] = x[3] = w;
    		
    		y[0] = y[3] = h;
    		y[1] = y[2] = h - tabHeight;
    		
    		g.fillPolygon(x, y, 4);
    		
    		g.setColor(Color.BLACK);
    		g.drawLine(x[0], y[0], x[1], y[1]);
    		g.drawLine(x[1], y[1], x[2], y[2]);
    		
    		if(tabText != null) {
    			g.setFont(tabFont);
    			
    			int x0 = x[0] + (tabWidth - (int)fm.getStringBounds(tabText, g).getWidth()) / 2;
    			if(x0 < x[1]) x0 = x[1];
    			
    			g.drawString(tabText, x0, y[0] - spacing/2 - fm.getDescent());
    		}
    	}
    	
    	g.setColor(Color.GRAY);
    	
    	int bottom = getHeight() - 1;
    	//g.drawLine(0, 0, getWidth(), 0);
    	
    	int w = tabWidth > 0 ? getWidth() - tabWidth : getWidth();
    	g.drawLine(0, bottom, w, bottom);
    
    	//g.fillRect(0, 0, getWidth(), borderwidth);
    	//g.fillRect(0, bottom-borderwidth+1, getWidth(), borderwidth);
    	
    }
   
    
    public void clear() {
    	for(int i=0; i<lines.length; i++) lines[i] = "";
    	repaint();    	
    }
    
}
 