[29] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * ResultSetTable.java |
---|
| 19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui.sql; |
---|
| 24 | |
---|
| 25 | import weka.gui.JTableHelper; |
---|
| 26 | |
---|
| 27 | import java.awt.event.MouseAdapter; |
---|
| 28 | import java.awt.event.MouseEvent; |
---|
| 29 | |
---|
| 30 | import javax.swing.JTable; |
---|
| 31 | import javax.swing.table.TableColumnModel; |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * Represents an extended JTable, containing a table model based on a ResultSet |
---|
| 35 | * and the corresponding query. |
---|
| 36 | * |
---|
| 37 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
| 38 | * @version $Revision: 1.2 $ |
---|
| 39 | */ |
---|
| 40 | public class ResultSetTable |
---|
| 41 | extends JTable { |
---|
| 42 | |
---|
| 43 | /** for serialization */ |
---|
| 44 | private static final long serialVersionUID = -3391076671854464137L; |
---|
| 45 | |
---|
| 46 | /** the query the table model is based on */ |
---|
| 47 | protected String m_Query; |
---|
| 48 | |
---|
| 49 | /** the connect string with which the query was run */ |
---|
| 50 | protected String m_URL; |
---|
| 51 | |
---|
| 52 | /** the user that was used to connect to the DB */ |
---|
| 53 | protected String m_User; |
---|
| 54 | |
---|
| 55 | /** the password that was used to connect to the DB */ |
---|
| 56 | protected String m_Password; |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * initializes the table |
---|
| 60 | * @param url the database URL |
---|
| 61 | * @param user the database user |
---|
| 62 | * @param pw the database password |
---|
| 63 | * @param query the query |
---|
| 64 | */ |
---|
| 65 | public ResultSetTable(String url, String user, String pw, String query, |
---|
| 66 | ResultSetTableModel model) { |
---|
| 67 | super(model); |
---|
| 68 | |
---|
| 69 | m_URL = url; |
---|
| 70 | m_User = user; |
---|
| 71 | m_Password = pw; |
---|
| 72 | m_Query = query; |
---|
| 73 | |
---|
| 74 | setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
---|
| 75 | |
---|
| 76 | // optimal colwidths (only according to header!)/cell renderer |
---|
| 77 | for (int i = 0; i < getColumnCount(); i++) { |
---|
| 78 | JTableHelper.setOptimalHeaderWidth(this, i); |
---|
| 79 | getColumnModel().getColumn(i).setCellRenderer( |
---|
| 80 | new ResultSetTableCellRenderer()); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // double click on column displays optimal colwidth |
---|
| 84 | final JTable table = this; |
---|
| 85 | getTableHeader().addMouseListener(new MouseAdapter() { |
---|
| 86 | public void mouseClicked(MouseEvent e) { |
---|
| 87 | TableColumnModel columnModel = getColumnModel(); |
---|
| 88 | int viewColumn = columnModel.getColumnIndexAtX(e.getX()); |
---|
| 89 | int column = convertColumnIndexToModel(viewColumn); |
---|
| 90 | if ( (e.getButton() == MouseEvent.BUTTON1) |
---|
| 91 | && (e.getClickCount() == 2) |
---|
| 92 | && (column != -1) ) |
---|
| 93 | JTableHelper.setOptimalColumnWidth(table, column); |
---|
| 94 | } |
---|
| 95 | }); |
---|
| 96 | getTableHeader().setToolTipText("double left click on column displays the column with optimal width"); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * returns the database URL that produced the table model |
---|
| 101 | */ |
---|
| 102 | public String getURL() { |
---|
| 103 | return m_URL; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * returns the user that produced the table model |
---|
| 108 | */ |
---|
| 109 | public String getUser() { |
---|
| 110 | return m_User; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * returns the password that produced the table model |
---|
| 115 | */ |
---|
| 116 | public String getPassword() { |
---|
| 117 | return m_Password; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * returns the query that produced the table model |
---|
| 122 | */ |
---|
| 123 | public String getQuery() { |
---|
| 124 | return m_Query; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * frees up the memory |
---|
| 129 | */ |
---|
| 130 | public void finalize() throws Throwable { |
---|
| 131 | if (getModel() != null) |
---|
| 132 | ((ResultSetTableModel) getModel()).finalize(); |
---|
| 133 | |
---|
| 134 | super.finalize(); |
---|
| 135 | |
---|
| 136 | System.gc(); |
---|
| 137 | } |
---|
| 138 | } |
---|