001: package org.jsqltool.gui.tableplugins;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import javax.swing.event.*;
006: import javax.swing.table.*;
007: import java.awt.event.*;
008: import org.jsqltool.conn.DbConnectionUtil;
009: import org.jsqltool.model.CustomTableModel;
010: import java.sql.*;
011: import java.util.*;
012: import org.jsqltool.gui.*;
013: import org.jsqltool.*;
014: import org.jsqltool.gui.tablepanel.*;
015: import org.jsqltool.gui.panel.*;
016: import org.jsqltool.gui.graphics.SQLTextArea;
017: import org.jsqltool.utils.Options;
018:
019: /**
020: * <p>Title: JSqlTool Project</p>
021: * <p>Description: Panel that shows table SQL scripts.
022: * </p>
023: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
024: *
025: * <p> This file is part of JSqlTool project.
026: * This library is free software; you can redistribute it and/or
027: * modify it under the terms of the (LGPL) Lesser General Public
028: * License as published by the Free Software Foundation;
029: *
030: * GNU LESSER GENERAL PUBLIC LICENSE
031: * Version 2.1, February 1999
032: *
033: * This library is distributed in the hope that it will be useful,
034: * but WITHOUT ANY WARRANTY; without even the implied warranty of
035: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036: * Library General Public License for more details.
037: *
038: * You should have received a copy of the GNU Library General Public
039: * License along with this library; if not, write to the Free
040: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
041: *
042: * The author may be contacted at:
043: * maurocarniel@tin.it</p>
044: *
045: * @author Mauro Carniel
046: * @version 1.0
047: */
048: public class ScriptTablePanel extends JPanel implements TablePlugin {
049:
050: private String tableName = null;
051: private SQLTextArea sql = new SQLTextArea();
052: BorderLayout borderLayout1 = new BorderLayout();
053: private DbConnectionUtil dbConnUtil = null;
054: private JFrame parent = null;
055: GridBagLayout gridBagLayout1 = new GridBagLayout();
056:
057: public ScriptTablePanel() {
058: try {
059: jbInit();
060: } catch (Exception e) {
061: e.printStackTrace();
062: }
063: }
064:
065: private void jbInit() throws Exception {
066: this .setLayout(gridBagLayout1);
067: this .add(sql, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
068: GridBagConstraints.WEST, GridBagConstraints.BOTH,
069: new Insets(5, 5, 0, 0), 0, 0));
070: sql.setEditable(false);
071: }
072:
073: public final void resetPanel() {
074: sql.setText("");
075: }
076:
077: /**
078: * @return panel position inside the JTabbedPane related to the table detail
079: */
080: public int getTabbedPosition() {
081: return 4;
082: }
083:
084: /**
085: * @return folder name of the plugin panel, inside the JTabbedPane related to the table detail
086: */
087: public String getTabbedName() {
088: return Options.getInstance().getResource("script");
089: }
090:
091: /**
092: * This method is called from the table detail to inizialize the plugin panel
093: */
094: public void initPanel(MainFrame parent, DbConnectionUtil dbConnUtil) {
095: this .parent = parent;
096: this .dbConnUtil = dbConnUtil;
097: }
098:
099: /**
100: * This method is called from the table detail to update the plugin panel content.
101: * @param tableName table name (edventualy including catalog name) that table plugin have to show
102: */
103: public final void updateContent() {
104: this .tableName = tableName;
105: if (tableName == null || tableName.equals("")) {
106: sql.setText("");
107: } else {
108: try {
109: TableModel cols = dbConnUtil.getTableColumns(tableName);
110: String t = tableName;
111: if (t.indexOf(".") != -1)
112: t = t.substring(t.indexOf(".") + 1);
113: String text = "CREATE TABLE " + t + "\n(\n";
114: int namelen = 0;
115: int len = 0;
116: Hashtable pk = new Hashtable();
117: for (int i = 0; i < cols.getRowCount(); i++) {
118: len = cols.getValueAt(i, 0).toString().length();
119: if (len > namelen) {
120: namelen = len;
121: }
122: if (cols.getValueAt(i, 2) != null) {
123: pk.put(cols.getValueAt(i, 2), cols.getValueAt(
124: i, 0));
125: }
126: }
127: for (int i = 0; i < cols.getRowCount(); i++) {
128: text += " "
129: + rpad(cols.getValueAt(i, 0).toString(),
130: namelen + 1)
131: + cols.getValueAt(i, 1) + " ";
132: text += (cols.getValueAt(i, 4) != null ? "DEFAULT "
133: + cols.getValueAt(i, 4) + " " : "");
134: text += (((Boolean) cols.getValueAt(i, 3))
135: .booleanValue() ? "" : "NOT NULL");
136: text += ",\n";
137: }
138: if (pk.size() > 0) {
139: text += " PRIMARY KEY(";
140: for (int i = 0; i < pk.size(); i++)
141: text += pk.get(new Integer(i + 1)) + ",";
142: text = text.substring(0, text.length() - 1);
143: text += ")\n";
144: } else
145: text = text.substring(0, text.length() - 2) + "\n";
146: text += ");\n\n";
147:
148: // indexes...
149: TableModel model = dbConnUtil
150: .getTableIndexes(tableName);
151: String indexName = null;
152: boolean unique = false;
153: for (int i = 0; i < model.getRowCount(); i++) {
154: if (model.getValueAt(i, 5) == null)
155: continue;
156: unique = false;
157: if (model.getValueAt(i, 3).getClass().equals(
158: String.class))
159: unique = model.getValueAt(i, 3).toString()
160: .equals("0");
161: else if (model.getValueAt(i, 3) instanceof Number)
162: unique = ((Number) model.getValueAt(i, 3))
163: .intValue() == 0;
164:
165: if (model.getValueAt(i, 5).equals(indexName)) {
166: text += model.getValueAt(i, 8) + ",";
167: continue;
168: } else {
169: if (indexName != null) {
170: text = text.substring(0, text.length() - 1);
171: text += ");\n\n";
172: }
173: indexName = model.getValueAt(i, 5).toString();
174: }
175: text += "CREATE ";
176: if (unique)
177: text += "UNIQUE ";
178: text += "INDEX " + model.getValueAt(i, 5) + " ON "
179: + t + " (";
180: text += model.getValueAt(i, 8) + ",";
181: }
182: if (indexName != null) {
183: text = text.substring(0, text.length() - 1);
184: text += ");\n\n";
185: }
186:
187: // fk...
188: model = dbConnUtil.getCrossReference(tableName);
189: String fkName = null;
190: String pkList = "";
191: String pkTable = null;
192: for (int i = 0; i < model.getRowCount(); i++) {
193: if (model.getValueAt(i, 11) == null)
194: continue;
195:
196: if (model.getValueAt(i, 11).equals(fkName)) {
197: text += model.getValueAt(i, 7) + ",";
198: pkList += model.getValueAt(i, 3) + ",";
199: continue;
200: } else {
201: if (fkName != null) {
202: text = text.substring(0, text.length() - 1);
203: text += ")\n";
204: text += " REFERENCES " + pkTable + "\n (";
205: pkList = pkList.substring(0, pkList
206: .length() - 1);
207: text += pkList + ")\n);\n\n";
208: }
209: fkName = model.getValueAt(i, 11).toString();
210: }
211: text += "ALTER TABLE " + t + " ADD CONSTRAINT "
212: + fkName + " FOREIGN KEY \n (";
213: text += model.getValueAt(i, 7) + ",";
214: pkList = model.getValueAt(i, 3) + ",";
215: pkTable = model.getValueAt(i, 2).toString();
216: }
217: if (fkName != null) {
218: text = text.substring(0, text.length() - 1);
219: text += ")\n";
220: text += " REFERENCES " + pkTable + "\n (";
221: pkList = pkList.substring(0, pkList.length() - 1);
222: text += pkList + ")\n);\n\n";
223: }
224:
225: sql.setText(text);
226: } catch (Exception ex) {
227: ex.printStackTrace();
228: }
229:
230: }
231: }
232:
233: /**
234: * This method is called from the table detail to set entity name.
235: * @param tableName table name (edventualy including catalog name) that table plugin have to show
236: */
237: public final void setTableName(String tableName) {
238: this .tableName = tableName;
239: }
240:
241: /**
242: * @return entity name
243: */
244: public String getTableName() {
245: return tableName;
246: }
247:
248: private String rpad(String text, int len) {
249: for (int i = text.length(); i < len; i++)
250: text += " ";
251: return text;
252: }
253:
254: /**
255: * @return infos about the author of the plugin panel; "" or null does not report any info about the plugin panel
256: */
257: public String getAuthor() {
258: return "";
259: }
260:
261: /**
262: * @return plugin panel version
263: */
264: public String getVersion() {
265: return "1.0";
266: }
267:
268: /**
269: * @return plugin panel name, reported into the about window
270: */
271: public String getName() {
272: return Options.getInstance().getResource("script plugin");
273: }
274:
275: public boolean equals(Object o) {
276: return (o instanceof TablePlugin && ((TablePlugin) o).getName()
277: .equals(getName()));
278: }
279:
280: }
|