/* * ResultWindow.java -- Class show results from an db query * Filemake to SQL Converter * Copyright (C) 2004 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 ResultWindow extends JDialog { /** * FM reference */ JFrame fm; /** * TableComponent instance used to display the data */ TableComponent table = new TableComponent(); /** * window title */ String title = ""; boolean update = true; int oldWidth = 0; int oldHeight = 0; static int counter = 0; /** * Constructs the result Window with the JFrame argument as parent * @param parent frame of the JDialog */ public ResultWindow(JFrame frame) { super(frame); fm = (JFrame) frame; getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); getContentPane().add(table); // getContentPane().setBackground(Color.white); addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { // System.out.println("Window resized " + getWidth() + " " + getHeight()); setTitle(title); //if(table.tableScroller.getPreferredSize().width table.table.getPreferredSize().height + size) setSize(getWidth(), table.table.getHeight() + size+5); // } // 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(false); } /** * Fills the table component of this window with new data */ public void updateResult(Vector data, Vector columnNames) { update = true; 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()); if (dim.width > getWidth()) { //table.vergroessern(getWidth(),getHeight()); oldWidth = dim.width * 3; oldHeight = (dim.height >= table.screenSize.height) ? table.screenSize.height : dim.height + table.table.getTableHeader().getHeight() * 2; //32; } else { // table.sizeToFit(getWidth(),getHeight()); // table.sizeToFit(dim.width * 3, getHeight()); /* // table.table.setSize(dim.width * 3,dim.height); table.tableScroller.setPreferredSize(new Dimension((dim.width*3), dim.height+23)); d2 = table.tableScroller.getPreferredSize(); // table.table.setPreferredScrollableViewportSize(table.table.getSize()); dim = table.table.getPreferredSize(); table.setPreferredSize(new Dimension(d2.width+10,d2.height+3)); table.tableScroller.validate(); table.validate(); // setSize(dim.width, dim.height + 40); oldWidth = d2.width+10; oldHeight=d2.height+3; System.out.println("here"+dim.width+" "+getWidth()+" "+d2.width+" "+table.table.getPreferredScrollableViewportSize()); */ oldWidth = dim.width; oldHeight = (dim.height >= table.screenSize.height) ? table.screenSize.height : dim.height + table.table.getTableHeader().getHeight() * 2; setSize(dim.width, oldHeight); } oldWidth = getWidth(); oldHeight = getHeight(); 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(""); buff.append(""); buff.append(""); buff.append(query); buff.append(""); buff.append(""); buff.append(""); buff.append("
"); buff.append("

"); buff.append(query); buff.append("

"); buff.append(table.exportToHTML()); buff.append(""); buff.append(""); 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; } }