/*
* MultiResultWindow.java -- Display results from several tables in one Window
* FileMaker to SQL Converter
* Copyright (C) 2003 Robert Gordesch (rogo@mpiwg-berlin.mpg.de)
* This program 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 2 of the License, or (at your option)
* any later version. Please read license.txt for the full details. A copy of
* the GPL may be found at http://www.gnu.org/copyleft/lgpl.html You should
* have received a copy of the GNU General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA Created on 15.09.2003 by
* rogo
*/
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import java.text.*;
import java.io.*;
/**
*
* ResultWindow - used to display the results of the query
*
* @author rogo
*/
public class MultiResultWindow extends JDialog
{
/**
* FM reference
*/
JFrame fm;
/**
* TableComponent instance used to display the data
*/
TableComponent table = new TableComponent();
/**
* Vector for mutltiple TableComponents
*/
Vector tables = new Vector();
JTabbedPane pane = new JTabbedPane();
/**
* window title
*/
String title = "";
boolean update = true;
int oldWidth = 0;
int oldHeight = 0;
int oldTabCount = 0;
static int counter = 0;
/**
* Constructs the result Window with the JFrame argument as parent
* @param parent frame of the JDialog
*/
public MultiResultWindow(JFrame frame)
{
super(frame);
fm = (JFrame) frame;
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
// pane.addTab("no Name",table);
getContentPane().add(pane);
// getContentPane().setBackground(Color.white);
addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
// System.out.println("Window resized " + getWidth() + " " + getHeight());
setTitle("show Tables");
//if(table.tableScroller.getPreferredSize().width<getWidth())
//table.repaint();
if (oldWidth != getWidth() || oldHeight != getHeight() || oldTabCount != pane.getTabRunCount())
{
for (int i = 0; i < tables.size(); ++i)
{
table = (TableComponent) tables.get(i);
table.sizeToFit(getWidth(), getHeight());
// table.setEnabled(false);
Dimension dim = table.table.getPreferredSize();
oldTabCount = pane.getTabRunCount();
// System.out.println("correction " + (pane.getTabRunCount() * 19) + " " + pane.getBoundsAt(0));
int correction = getHeight() - (pane.getTabRunCount() * pane.getBoundsAt(0).height) - new JScrollBar().getPreferredSize().height;
//table.tableScroller.setSize(new Dimension(getWidth() - 30,correction-15 ));
table.tableScroller.setPreferredSize(new Dimension(getWidth() - 30, correction));
Dimension d2 = table.tableScroller.getPreferredSize();
// System.out.println("runs faster" + pane.getTabRunCount() + " " + d2 + " " + getHeight() + " " + ((double) (d2.height) / ((double) getHeight()) * 100) + " pane height " + pane.getPreferredSize());
table.setPreferredSize(new Dimension(d2.width + 10, d2.height + 5));
table.table.revalidate();
//table.tableScroller.revalidate();
//table.revalidate();
}
pane.revalidate();
oldWidth = getWidth();
oldHeight = getHeight();
}
//System.out.println("runs slower "+ pane.getTabRunCount());
validate();
repaint();
// System.out.println("fit" + getWidth() + " " + oldWidth);
}
});
// table.setEnabled(false);
update = true;
pack();
oldWidth = getWidth();
setLocation(new Point(frame.getLocation().x, frame.getLocation().y + 50));
rootPane.setOpaque(true);
}
/**
* Fills the table component of this window with new data
*/
public void updateResult(Vector data, Vector columnNames)
{
update = true;
TableComponent table = new TableComponent();
tables.add(table);
pane.addTab(title, table);
table.tableModel.setDataVector(data, columnNames);
Dimension d2 = table.tableScroller.getPreferredSize();
Dimension dim = table.table.getPreferredSize();
// System.out.println("here"+dim.width+" "+getWidth()+" "+d2.width+" "+table.table.getScrollableTracksViewportHeight());
//pack();
// table.repaint();
}
/**
* exports the current results to HTML
* @param query that produced the result
* @return StringBuffer containing the HTML code
*/
public StringBuffer exportQueryToHTML(String query)
{
if (query == "")
query = title;
StringBuffer buff = new StringBuffer();
buff.append("<html>");
buff.append("<head>");
buff.append("<title>");
buff.append(query);
buff.append("</title>");
buff.append("</head>");
buff.append("<body>");
buff.append("<center>");
buff.append("<h3>");
buff.append(query);
buff.append("</h3>");
buff.append(table.exportToHTML());
buff.append("</body>");
buff.append("</html>");
return TableComponent.convertUml(buff);
}
/**
* Actual write the result to a file( in HTML)
*/
public String writeResult()
{
String name = "";
try
{
NumberFormat nf = new DecimalFormat("0000");
name = "./html/query" + nf.format(counter++) + ".html";
FileWriter file = new FileWriter(new File(name));
file.write(exportQueryToHTML(title).toString());
file.close();
} catch (Exception e)
{
System.out.println("Error while writing html\n" + e);
}
return name;
}
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>