001: package org.jsqltool.jgraph.gui;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import org.jsqltool.conn.DbConnectionUtil;
006: import javax.swing.table.TableModel;
007: import java.util.*;
008:
009: /**
010: * <p>Title: JSqlTool Project</p>
011: * <p>Description: Panel used to represent a database entity (table/view/synonymn).
012: * Used inside a Vertex View.
013: * </p>
014: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
015: *
016: * <p> This file is part of JSqlTool project.
017: * This library is free software; you can redistribute it and/or
018: * modify it under the terms of the (LGPL) Lesser General Public
019: * License as published by the Free Software Foundation;
020: *
021: * GNU LESSER GENERAL PUBLIC LICENSE
022: * Version 2.1, February 1999
023: *
024: * This library is distributed in the hope that it will be useful,
025: * but WITHOUT ANY WARRANTY; without even the implied warranty of
026: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
027: * Library General Public License for more details.
028: *
029: * You should have received a copy of the GNU Library General Public
030: * License along with this library; if not, write to the Free
031: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
032: *
033: * The author may be contacted at:
034: * maurocarniel@tin.it</p>
035: *
036: * @author Mauro Carniel
037: * @version 1.0
038: */
039: public class EntityPanel extends JPanel {
040:
041: GridBagLayout gridBagLayout1 = new GridBagLayout();
042: JPanel pkPanel = new JPanel();
043: JPanel attrsPanel = new JPanel();
044: JPanel namePanel = new JPanel();
045: JLabel nameLabel = new JLabel();
046: JTextArea pks = new JTextArea();
047: JTextArea attrs = new JTextArea();
048: GridBagLayout gridBagLayout2 = new GridBagLayout();
049: GridBagLayout gridBagLayout3 = new GridBagLayout();
050: GridBagLayout gridBagLayout4 = new GridBagLayout();
051:
052: public EntityPanel(DbConnectionUtil dbUtil, String tableName) {
053: try {
054: jbInit();
055: init(dbUtil, tableName);
056: } catch (Exception e) {
057: e.printStackTrace();
058: }
059: }
060:
061: /**
062: * Costructor not used.
063: */
064: public EntityPanel() {
065: try {
066: jbInit();
067: } catch (Exception e) {
068: e.printStackTrace();
069: }
070: }
071:
072: /**
073: * Initialize the panel content.
074: */
075: private void init(DbConnectionUtil dbUtil, String tableName) {
076: try {
077: nameLabel.setText(tableName);
078: TableModel model = dbUtil.getTableColumns(tableName);
079: // column, data type, pk, null?, default...
080: Hashtable primaryKeys = new Hashtable();
081: FontMetrics fm = nameLabel.getFontMetrics(nameLabel
082: .getFont());
083: int h = fm.getHeight();
084: int len, maxLen = 0;
085: String line = null;
086: for (int i = 0; i < model.getRowCount(); i++) {
087: if (model.getValueAt(i, 2) == null) {
088: // column is NOT a pk...
089: line = model.getValueAt(i, 0) + " : "
090: + model.getValueAt(i, 1);
091: if (model.getValueAt(i, 3) != null
092: && !((Boolean) model.getValueAt(i, 3))
093: .booleanValue()) {
094: line += " NOT NULL";
095: }
096: line += "\n";
097: len = fm.stringWidth(line);
098: if (len > maxLen)
099: maxLen = len;
100: attrs.append(line);
101: } else {
102: // column is pk...
103: primaryKeys.put(model.getValueAt(i, 2),
104: new Integer(i));
105: }
106: }
107: if (attrs.getText().length() > 0) {
108: attrs.setText(attrs.getText().substring(0,
109: attrs.getText().length() - 1));
110: }
111: int j = -1;
112: for (int i = 0; i < primaryKeys.size(); i++) {
113: j = ((Integer) primaryKeys.get(new Integer(i + 1)))
114: .intValue();
115: line = model.getValueAt(i, 0) + " : "
116: + model.getValueAt(i, 1);
117: if (model.getValueAt(i, 3) != null
118: && !((Boolean) model.getValueAt(i, 3))
119: .booleanValue()) {
120: line += " NOT NULL";
121: }
122: line += "\n";
123: len = fm.stringWidth(line);
124: if (len > maxLen)
125: maxLen = len;
126: pks.append(line);
127: }
128: if (pks.getText().length() > 0) {
129: pks.setText(pks.getText().substring(0,
130: pks.getText().length() - 1));
131: }
132: if (primaryKeys.size() == 0) {
133: pkPanel.setPreferredSize(new Dimension(0, 0));
134: pkPanel.revalidate();
135: }
136: setSize(maxLen + 20, model.getRowCount() * (h) + 25 + h
137: + (primaryKeys.size() > 0 ? 10 + h : 0));
138: } catch (Exception ex) {
139: ex.printStackTrace();
140: }
141:
142: }
143:
144: private void jbInit() throws Exception {
145: this .setLayout(gridBagLayout1);
146: attrsPanel.setBackground(Color.white);
147: attrsPanel.setBorder(BorderFactory
148: .createLineBorder(Color.black));
149: attrsPanel.setLayout(gridBagLayout3);
150: pkPanel.setBackground(Color.white);
151: pkPanel.setBorder(BorderFactory.createLineBorder(Color.black));
152: pkPanel.setLayout(gridBagLayout2);
153: this .setOpaque(false);
154: namePanel
155: .setBorder(BorderFactory.createLineBorder(Color.black));
156: namePanel.setOpaque(false);
157: namePanel.setLayout(gridBagLayout4);
158: attrs.setOpaque(false);
159: attrs.setEditable(false);
160: attrs.setText("");
161: attrs.setFont(nameLabel.getFont());
162: pks.setOpaque(false);
163: pks.setEditable(false);
164: pks.setText("");
165: pks.setFont(nameLabel.getFont());
166: nameLabel.setBorder(null);
167: this .add(pkPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0,
168: GridBagConstraints.NORTHWEST,
169: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
170: 0, 0));
171: pkPanel.add(pks, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
172: GridBagConstraints.CENTER, GridBagConstraints.BOTH,
173: new Insets(5, 5, 5, 5), 0, 0));
174: this .add(attrsPanel, new GridBagConstraints(0, 2, 1, 1, 1.0,
175: 1.0, GridBagConstraints.NORTHWEST,
176: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
177: attrsPanel.add(attrs, new GridBagConstraints(0, 0, 1, 1, 1.0,
178: 1.0, GridBagConstraints.CENTER,
179: GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
180: this
181: .add(namePanel, new GridBagConstraints(0, 0, 1, 1, 0.0,
182: 0.0, GridBagConstraints.NORTHWEST,
183: GridBagConstraints.NONE,
184: new Insets(0, 0, 0, 0), 10, 0));
185: namePanel.add(nameLabel, new GridBagConstraints(0, 0,
186: GridBagConstraints.REMAINDER,
187: GridBagConstraints.REMAINDER, 1.0, 0.0,
188: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
189: new Insets(5, 5, 5, 5), 0, 0));
190: }
191:
192: public String getEntityName() {
193: return nameLabel.getText();
194: }
195:
196: }
|