File:  [Repository] / FM2SQL / src / ResultWindow.java
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Fri Jan 21 11:27:03 2005 UTC (19 years, 4 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
moved java src to src folder

/*
 * 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<getWidth())

        //table.repaint();
        if (oldWidth != getWidth() || oldHeight != getHeight())
        {
          table.sizeToFit(getWidth(), getHeight());

          Dimension dim = table.table.getPreferredSize();

          table.tableScroller.setPreferredSize(new Dimension(getWidth() - 15, getHeight() - 45));
          Dimension d2 = table.tableScroller.getPreferredSize();

          oldWidth = getWidth();
          oldHeight = getHeight();
          //   System.out.println("fit" + getWidth() + " " + oldWidth);
          //table.table.setPreferredScrollableViewportSize(dim);

          table.setPreferredSize(new Dimension(d2.width + 10, d2.height + 5));
          table.table.revalidate();
          table.tableScroller.revalidate();
          table.revalidate();
          int size = (table.tableScroller.getHorizontalScrollBar().isVisible()) ? 90 : 70;
          if (getHeight() > 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("<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>