01: /*
02: * SQLeonardo :: java database frontend
03: * Copyright (C) 2004 nickyb@users.sourceforge.net
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * as published by the Free Software Foundation; either version 2
08: * of the License, or (at your option) any later version.
09: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU General Public License for more details.
14: *
15: * You should have received a copy of the GNU General Public License
16: * along with this program; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18: *
19: */
20:
21: package nickyb.sqleonardo.environment.ctrl.content;
22:
23: import java.awt.event.ActionEvent;
24: import java.sql.ResultSet;
25: import java.sql.SQLException;
26: import java.sql.Statement;
27: import java.text.NumberFormat;
28:
29: import javax.swing.AbstractAction;
30: import javax.swing.JOptionPane;
31:
32: import nickyb.sqleonardo.common.jdbc.ConnectionAssistant;
33: import nickyb.sqleonardo.common.jdbc.ConnectionHandler;
34: import nickyb.sqleonardo.environment.Application;
35: import nickyb.sqleonardo.environment.ctrl.define.TableMetaData;
36: import nickyb.sqleonardo.querybuilder.syntax.SQLFormatter;
37:
38: public abstract class AbstractActionContent extends AbstractAction {
39: protected abstract void onActionPerformed(int records, int option);
40:
41: protected abstract TableMetaData getTableMetaData();
42:
43: protected final String getDefaultMessage(int records) {
44: return this .getTableMetaData().getType() + " : '"
45: + this .getTableMetaData() + "' contains "
46: + NumberFormat.getInstance().format(records)
47: + " record(s)";
48: }
49:
50: protected int showConfirmDialog(int records) {
51: String message = this .getDefaultMessage(records)
52: + "\ndo you want continue?";
53: return JOptionPane.showConfirmDialog(Application.window,
54: message, this .getValue(NAME).toString(),
55: JOptionPane.YES_NO_OPTION);
56: }
57:
58: public void actionPerformed(ActionEvent e) {
59: if (this .getTableMetaData() == null
60: || this .getTableMetaData().getHandlerKey() == null)
61: return;
62:
63: try {
64: ConnectionHandler ch = ConnectionAssistant.getHandler(this
65: .getTableMetaData().getHandlerKey());
66: String identifierQuoteString = ConnectionAssistant
67: .getHandler(this .getTableMetaData().getHandlerKey())
68: .getObject("$identifierQuoteString").toString();
69:
70: Statement stmt = ch.get().createStatement();
71: ResultSet rs = stmt.executeQuery("SELECT count(*) FROM "
72: + SQLFormatter.ensureQuotes(this .getTableMetaData()
73: .toString(), identifierQuoteString, true));
74:
75: int records = rs.next() ? rs.getInt(1) : 0;
76:
77: rs.close();
78: stmt.close();
79:
80: onActionPerformed(records, showConfirmDialog(records));
81: } catch (SQLException sqle) {
82: Application.println(sqle, true);
83: }
84: }
85: }
|