📄 erdtable.java
字号:
/* * ErdTable.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * 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 any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * 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. * */package org.executequery.gui.erd;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.event.MouseEvent;import java.io.Serializable;import java.util.Hashtable;import java.util.Vector;import javax.swing.SwingUtilities;import org.executequery.gui.browser.ColumnData;import org.executequery.gui.scriptgenerators.ScriptGenerationUtils;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * * @author Takis Diakoumis * @version $Revision: 1.5 $ * @date $Date: 2006/07/16 15:47:26 $ */public class ErdTable extends ErdMoveableComponent implements Serializable { /** The table name displayed */ private String tableName; /** The table's columns */ private ColumnData[] columns; /** The table's original columns */ private ColumnData[] originalData; /** The CREATE TABLE script for a new table */ private String createTableScript; /** The ALTER TABLE script for a definition change */ private String alterTableScript; /** The ALTER TABLE script for a constraint change */ private String addConstraintScript; /** The ALTER TABLE script for a constraint drop */ private String dropConstraintScript; /** <code>Hashtable</code> containing table modifications */ private Hashtable alterTableHash; /** Whether this is a new table */ private boolean newTable; private boolean editable; /** The table's background colour */ private Color tableBackground; /** This components calculated width */ private int FINAL_WIDTH = -1; /** This components calculated height */ private int FINAL_HEIGHT = -1; /** The height of the title bar */ private static int TITLE_BAR_HEIGHT = 20; private boolean displayReferencedKeysOnly; private static final String PRIMARY = "(PK) "; private static final String FOREIGN = "(FK)"; private static final Color TITLE_BAR_BG_COLOR = new Color(255,251,203); private ErdTableConnectionPoint[] verticalLeftJoins; private ErdTableConnectionPoint[] verticalRightJoins; private ErdTableConnectionPoint[] horizontalTopJoins; private ErdTableConnectionPoint[] horizontalBottomJoins; protected static final int LEFT_JOIN = 0; protected static final int RIGHT_JOIN = 1; protected static final int TOP_JOIN = 2; protected static final int BOTTOM_JOIN = 3; /** <p>Constructs a new instance with the specified * table name and <code>ErdViewerPanel</code> as the * paent controller object. * * @param the table name displayed * @param the <code>ErdViewerPanel</code> controller object */ public ErdTable(String tableName, ColumnData[] columns, ErdViewerPanel parent) { super(parent); this.columns = columns; this.tableName = tableName.toUpperCase(); newTable = false; editable = false; displayReferencedKeysOnly = true; tableBackground = Color.WHITE; try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private int dataTypeOffset; private int keyLabelOffset; /** <p>Initialises the state of this instance. */ private void jbInit() throws Exception { Font tableNameFont = parent.getTableNameFont(); Font columnNameFont = parent.getColumnNameFont(); FontMetrics fmColumns = getFontMetrics(columnNameFont); FontMetrics fmTitle = getFontMetrics(tableNameFont); if (columns != null) { for (int i = 0; i < columns.length; i++) { ColumnData column = columns[i]; int valueWidth = fmColumns.stringWidth(column.getColumnName()); dataTypeOffset = Math.max(dataTypeOffset, valueWidth); valueWidth = fmColumns.stringWidth(column.getFormattedDataType()); keyLabelOffset = Math.max(keyLabelOffset, valueWidth); } } // add a further offset to the data type and key label offsets dataTypeOffset += 10; keyLabelOffset += 2; int keyWidth = fmColumns.stringWidth(PRIMARY + FOREIGN); int maxWordLength = dataTypeOffset + keyLabelOffset + keyWidth + 10; // compare to the title length maxWordLength = Math.max(fmTitle.stringWidth(tableName), maxWordLength); // add 20px to the final width FINAL_WIDTH = maxWordLength;// + 20; // minimum width is 130px // if (FINAL_WIDTH < 130) // FINAL_WIDTH = 130; TITLE_BAR_HEIGHT = fmTitle.getHeight() + 5; int keysCount = 0; for (int i = 0; i < columns.length; i++) { if (columns[i].isKey()) { keysCount++; } } if (columns.length > 0) { if (displayReferencedKeysOnly) { if (keysCount > 0) { FINAL_HEIGHT = (fmColumns.getHeight() * keysCount) + TITLE_BAR_HEIGHT + 10; } else { FINAL_HEIGHT = fmColumns.getHeight() + TITLE_BAR_HEIGHT + 8; } } else { FINAL_HEIGHT = (fmColumns.getHeight() * columns.length) + TITLE_BAR_HEIGHT + 10; } } else { // have one blank row (column) on the table FINAL_HEIGHT = fmColumns.getHeight() + TITLE_BAR_HEIGHT + 10; } int joinSpacing = 10; int vertSize = (FINAL_HEIGHT / joinSpacing) - 1; int horizSize = (FINAL_WIDTH / joinSpacing) - 1; verticalLeftJoins = new ErdTableConnectionPoint[vertSize]; verticalRightJoins = new ErdTableConnectionPoint[vertSize]; horizontalTopJoins = new ErdTableConnectionPoint[horizSize]; horizontalBottomJoins = new ErdTableConnectionPoint[horizSize]; int midPointVert = FINAL_HEIGHT / 2; int midPointHoriz = FINAL_WIDTH / 2; int aboveMidPoint = midPointHoriz; int belowMidPoint = midPointHoriz; for (int i = 0; i < horizontalTopJoins.length; i++) { horizontalTopJoins[i] = new ErdTableConnectionPoint(TOP_JOIN); horizontalBottomJoins[i] = new ErdTableConnectionPoint(BOTTOM_JOIN); if (i == 0) { horizontalTopJoins[i].setPosition(midPointHoriz); horizontalBottomJoins[i].setPosition(midPointHoriz); } else if (i % 2 == 0) { belowMidPoint -= joinSpacing; if (belowMidPoint > 10) { horizontalTopJoins[i].setPosition(belowMidPoint); horizontalBottomJoins[i].setPosition(belowMidPoint); } } else { aboveMidPoint += joinSpacing; if (aboveMidPoint < FINAL_WIDTH - 10) { horizontalTopJoins[i].setPosition(belowMidPoint); horizontalBottomJoins[i].setPosition(belowMidPoint); } horizontalTopJoins[i].setPosition(aboveMidPoint); horizontalBottomJoins[i].setPosition(aboveMidPoint); } } aboveMidPoint = midPointVert; belowMidPoint = midPointVert; for (int i = 0; i < verticalLeftJoins.length; i++) { verticalLeftJoins[i] = new ErdTableConnectionPoint(LEFT_JOIN); verticalRightJoins[i] = new ErdTableConnectionPoint(RIGHT_JOIN); if (i == 0) { verticalLeftJoins[i].setPosition(midPointVert); verticalRightJoins[i].setPosition(midPointVert); } else if (i % 2 == 0) { belowMidPoint -= joinSpacing; if (belowMidPoint < FINAL_HEIGHT - 10) { verticalLeftJoins[i].setPosition(belowMidPoint); verticalRightJoins[i].setPosition(belowMidPoint); } } else { aboveMidPoint += joinSpacing; if (aboveMidPoint > 10) { verticalLeftJoins[i].setPosition(aboveMidPoint); verticalRightJoins[i].setPosition(aboveMidPoint); } } } SwingUtilities.invokeLater(new Runnable() { public void run() { originalData = new ColumnData[columns.length]; for (int i = 0; i < columns.length; i++) { originalData[i] = new ColumnData(); originalData[i].setValues(columns[i]); } } }); } public boolean isEditable() { return editable; } /** * Sets whether this table is editable or not. * @param editable <code>true</code> | <code>false</coe> */ public void setEditable(boolean editable) { this.editable = editable; } public void setDisplayReferencedKeysOnly(boolean display) { displayReferencedKeysOnly = display; } public void tableColumnsChanged() { resetAllJoins(); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } repaint(); revalidate(); } /** <p>Returns the <code>Hashtable</code> containing * ALTER TABLE SQL script changes for this table. * * @return the ALTER TABLE <code>Hashtable</code> */ public Hashtable getAlterTableHash() { return alterTableHash; } /** <p>Sets the <code>Hashtable</code> containing * ALTER TABLE SQL script changes for this table. * * @return the ALTER TABLE <code>Hashtable</code> */ public void setAlterTableHash(Hashtable alterTableHash) { this.alterTableHash = alterTableHash; } /** <p>Returns a concatenation of all SQL scipts * generated for this table, if any. The order of * the scripts returned is CREATE TABLE, ALTER TABLE * (table definition), ALTER TABLE (table constraints). * * @return all this table's generated SQL scripts */ public String getAllSQLScripts() { String EMPTY = ""; return (createTableScript == null ? EMPTY : createTableScript) + (alterTableScript == null ? EMPTY : alterTableScript) + (addConstraintScript == null ? EMPTY : addConstraintScript) + (dropConstraintScript == null ? EMPTY : dropConstraintScript); } /** <p>Returns whether this table having changes * made to its definition has an SQL script. * * @return <code>true</code> if a script is available | * <code>false</code> otherwise */ public boolean hasSQLScripts() { return createTableScript != null || alterTableScript != null || addConstraintScript != null || dropConstraintScript != null; } /** <p>Returns the ALTER TABLE script for this table * for table definition changes only - ie. column name, * datatype changes and so forth. * * @return the ALTER TABLE script */ public String getAlterTableScript() { return alterTableScript; } /** <p>Sets the ALTER TABLE script for this table * for table definition changes only - ie. column name, * datatype changes and so forth. * * @return the ALTER TABLE script */ public void setAlterTableScript(String alterTableScript) { this.alterTableScript = alterTableScript; } /** <p>Returns the ALTER TABLE script for this table * for relationship/constraint changes only. * * @return the ALTER TABLE script */ public String getAddConstraintsScript() { return addConstraintScript; } /** <p>Sets the ALTER TABLE script for this table * for relationship/constraint changes only. * * @return the ALTER TABLE script */ public void setAddConstraintsScript(String addConstraintScript) { if (this.addConstraintScript == null) this.addConstraintScript = addConstraintScript; else this.addConstraintScript += addConstraintScript; } /** <p>Returns the ALTER TABLE script for this table * for relationship/constraint drops only. * * @return the ALTER TABLE script */ public String getDropConstraintsScript() { return dropConstraintScript; } /** <p>Sets the ALTER TABLE script for this table * for relationship/constraint drop only. * * @return the ALTER TABLE script */ public void setDropConstraintsScript(String dropConstraintScript) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -