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.utils.Options;
017:
018: /**
019: * <p>Title: JSqlTool Project</p>
020: * <p>Description: Panel that shows table columns.
021: * </p>
022: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
023: *
024: * <p> This file is part of JSqlTool project.
025: * This library is free software; you can redistribute it and/or
026: * modify it under the terms of the (LGPL) Lesser General Public
027: * License as published by the Free Software Foundation;
028: *
029: * GNU LESSER GENERAL PUBLIC LICENSE
030: * Version 2.1, February 1999
031: *
032: * This library is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
035: * Library General Public License for more details.
036: *
037: * You should have received a copy of the GNU Library General Public
038: * License along with this library; if not, write to the Free
039: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
040: *
041: * The author may be contacted at:
042: * maurocarniel@tin.it</p>
043: *
044: * @author Mauro Carniel
045: * @version 1.0
046: */
047: public class ColumnsTablePanel extends JPanel implements TablePlugin {
048:
049: private String tableName = null;
050: private Hashtable pk = null;
051: JPanel buttonsPanel = new JPanel();
052: FlowLayout flowLayout1 = new FlowLayout();
053: DataPanel dataPanel = null;
054: BorderLayout borderLayout1 = new BorderLayout();
055: private DbConnectionUtil dbConnUtil = null;
056: private JFrame parent = null;
057: GridBagLayout gridBagLayout1 = new GridBagLayout();
058:
059: public ColumnsTablePanel() {
060: try {
061: jbInit();
062: } catch (Exception e) {
063: e.printStackTrace();
064: }
065: }
066:
067: private void jbInit() throws Exception {
068: this .setLayout(gridBagLayout1);
069: flowLayout1.setVgap(0);
070: this .add(buttonsPanel, new GridBagConstraints(0, 0, 1, 1, 1.0,
071: 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH,
072: new Insets(5, 5, 0, 0), 0, 0));
073: buttonsPanel.setLayout(flowLayout1);
074: flowLayout1.setAlignment(FlowLayout.LEFT);
075: flowLayout1.setHgap(0);
076: }
077:
078: public final void resetPanel() {
079: dataPanel.resetPanel();
080: }
081:
082: /**
083: * @return panel position inside the JTabbedPane related to the table detail
084: */
085: public int getTabbedPosition() {
086: return 0;
087: }
088:
089: /**
090: * @return folder name of the plugin panel, inside the JTabbedPane related to the table detail
091: */
092: public String getTabbedName() {
093: return Options.getInstance().getResource("columns");
094: }
095:
096: /**
097: * This method is called from the table detail to inizialize the plugin panel
098: */
099: public void initPanel(MainFrame parent, DbConnectionUtil dbConnUtil) {
100: this .parent = parent;
101: this .dbConnUtil = dbConnUtil;
102: this .dataPanel = new DataPanel(dbConnUtil,
103: new TableModelListener() {
104: public void tableChanged(TableModelEvent e) {
105: }
106: });
107: this .dataPanel.getTable().setAutoResizeMode(
108: JTable.AUTO_RESIZE_OFF);
109: this .add(dataPanel, new GridBagConstraints(0, 1, 1, 1, 1.0,
110: 1.0, GridBagConstraints.CENTER,
111: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
112: init();
113: }
114:
115: private void init() {
116: // pop-up menu creation...
117: final JPopupMenu tableMenu = new JPopupMenu();
118: JMenuItem addColMenu = new JMenuItem(Options.getInstance()
119: .getResource("add column"));
120: JMenuItem dropColMenu = new JMenuItem(Options.getInstance()
121: .getResource("drop column"));
122: JMenuItem dropTableMenu = new JMenuItem(Options.getInstance()
123: .getResource("drop table"));
124: addColMenu.addActionListener(new ActionListener() {
125: public void actionPerformed(ActionEvent e) {
126: new AddColumnDialog(parent, tableName, dbConnUtil);
127: updateContent();
128: }
129: });
130:
131: dropColMenu.addActionListener(new ActionListener() {
132: public void actionPerformed(ActionEvent e) {
133: if (dataPanel.getTable().getSelectedRow() != -1)
134: try {
135: PreparedStatement pstmt = dbConnUtil
136: .getConn()
137: .prepareStatement(
138: "ALTER TABLE "
139: + tableName
140: + " DROP COLUMN "
141: + dataPanel
142: .getTable()
143: .getModel()
144: .getValueAt(
145: dataPanel
146: .getTable()
147: .getSelectedRow(),
148: 0));
149: pstmt.execute();
150: pstmt.close();
151: updateContent();
152: } catch (Exception ex) {
153: JOptionPane.showMessageDialog(parent, Options
154: .getInstance().getResource(
155: "error while dropping table:")
156: + "\n" + ex.getMessage(), Options
157: .getInstance().getResource("error"),
158: JOptionPane.ERROR_MESSAGE);
159: }
160:
161: }
162: });
163:
164: dropTableMenu.addActionListener(new ActionListener() {
165: public void actionPerformed(ActionEvent e) {
166: if (JOptionPane.showConfirmDialog(parent, Options
167: .getInstance().getResource(
168: "confirm drop table?"), Options
169: .getInstance().getResource("attention"),
170: JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
171:
172: try {
173: PreparedStatement pstmt = dbConnUtil.getConn()
174: .prepareStatement(
175: "DROP TABLE " + tableName);
176: pstmt.execute();
177: pstmt.close();
178: tableName = null;
179: updateContent();
180: } catch (Exception ex) {
181: JOptionPane.showMessageDialog(parent, Options
182: .getInstance().getResource(
183: "error while dropping table:")
184: + "\n" + ex.getMessage(), Options
185: .getInstance().getResource("error"),
186: JOptionPane.ERROR_MESSAGE);
187: }
188:
189: }
190: }
191: });
192:
193: tableMenu.add(addColMenu);
194: tableMenu.add(dropColMenu);
195: tableMenu.add(dropTableMenu);
196:
197: dataPanel.getTable().addMouseListener(new MouseAdapter() {
198: public void mouseClicked(MouseEvent e) {
199: if (SwingUtilities.isRightMouseButton(e)) { // right mouse click
200: // show pop-up menu...
201: tableMenu
202: .show(e.getComponent(), e.getX(), e.getY());
203: }
204: }
205: });
206: }
207:
208: /**
209: * This method is called from the table detail to update the plugin panel content.
210: * @param tableName table name (edventualy including catalog name) that table plugin have to show
211: */
212: public final void updateContent() {
213: if (tableName == null || tableName.equals("")) {
214: dataPanel.getTable().setModel(
215: new DefaultTableModel(
216: new String[] {
217: Options.getInstance().getResource(
218: "column"),
219: Options.getInstance().getResource(
220: "data type"),
221: Options.getInstance().getResource(
222: "pK"),
223: Options.getInstance().getResource(
224: "null?") }, 0));
225: } else {
226: dataPanel.getTable().setModel(
227: dbConnUtil.getTableColumns(tableName));
228: }
229: dataPanel.getTable().getColumnModel().getColumn(0)
230: .setPreferredWidth(200);
231: dataPanel.getTable().getColumnModel().getColumn(1)
232: .setPreferredWidth(140);
233: dataPanel.getTable().getColumnModel().getColumn(2)
234: .setPreferredWidth(50);
235: dataPanel.getTable().getColumnModel().getColumn(3)
236: .setPreferredWidth(50);
237: dataPanel.getTable().getColumnModel().getColumn(4)
238: .setPreferredWidth(50);
239: // dataPanel.getTable().setColumnSelectionAllowed(false);
240: // dataPanel.getTable().setRowSelectionAllowed(false);
241: }
242:
243: /**
244: * This method is called from the table detail to set entity name.
245: * @param tableName table name (edventualy including catalog name) that table plugin have to show
246: */
247: public final void setTableName(String tableName) {
248: this .tableName = tableName;
249: }
250:
251: /**
252: * @return entity name
253: */
254: public String getTableName() {
255: return tableName;
256: }
257:
258: /**
259: * @return infos about the author of the plugin panel; "" or null does not report any info about the plugin panel
260: */
261: public String getAuthor() {
262: return "";
263: }
264:
265: /**
266: * @return plugin panel version
267: */
268: public String getVersion() {
269: return "1.0";
270: }
271:
272: /**
273: * @return plugin panel name, reported into the about window
274: */
275: public String getName() {
276: return Options.getInstance()
277: .getResource("table columns plugin");
278: }
279:
280: public boolean equals(Object o) {
281: return (o instanceof TablePlugin && ((TablePlugin) o).getName()
282: .equals(getName()));
283: }
284:
285: }
|