001: /*
002: * ExecuteSqlDialog.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.sql;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dimension;
016: import java.awt.EventQueue;
017: import java.awt.FlowLayout;
018: import java.awt.Frame;
019: import java.awt.event.ActionListener;
020: import java.awt.event.WindowListener;
021: import java.sql.Statement;
022:
023: import javax.swing.JDialog;
024: import javax.swing.JLabel;
025: import javax.swing.JPanel;
026: import javax.swing.border.EtchedBorder;
027:
028: import workbench.db.WbConnection;
029: import workbench.util.ExceptionUtil;
030: import workbench.gui.WbSwingUtilities;
031: import workbench.gui.components.WbButton;
032: import workbench.resource.ResourceMgr;
033: import workbench.resource.Settings;
034: import workbench.util.WbThread;
035:
036: /**
037: *
038: * @author support@sql-workbench.net
039: */
040: public class ExecuteSqlDialog extends JDialog implements
041: ActionListener, WindowListener {
042: protected WbConnection dbConn;
043: protected EditorPanel sqlEditor;
044: protected WbButton startButton = new WbButton(ResourceMgr
045: .getString("LblStartSql"));
046: protected WbButton closeButton = new WbButton(ResourceMgr
047: .getString("LblClose"));
048: protected JLabel statusMessage;
049: private Thread worker;
050:
051: /** Creates a new instance of ExecuteSqlDialog */
052: public ExecuteSqlDialog(Frame owner, String aTitle, String sql,
053: final String highlight, WbConnection aConnection) {
054: super (owner, aTitle, true);
055: this .dbConn = aConnection;
056: this .getContentPane().setLayout(new BorderLayout());
057: JPanel buttonPanel = new JPanel();
058: buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
059: this .startButton.addActionListener(this );
060: this .closeButton.addActionListener(this );
061: buttonPanel.add(startButton);
062: buttonPanel.add(closeButton);
063: JPanel mainPanel = new JPanel();
064: sqlEditor = EditorPanel.createSqlEditor();
065: sqlEditor.showFormatSql();
066: mainPanel.setLayout(new BorderLayout());
067: mainPanel.add(sqlEditor, BorderLayout.CENTER);
068: sqlEditor.setDatabaseConnection(this .dbConn);
069: sqlEditor.setText(sql);
070:
071: this .statusMessage = new JLabel("");
072: this .statusMessage.setBorder(new EtchedBorder());
073: this .statusMessage.setMaximumSize(new Dimension(32768, 22));
074: this .statusMessage.setMinimumSize(new Dimension(10, 22));
075: this .statusMessage.setPreferredSize(new Dimension(60, 22));
076: mainPanel.add(this .statusMessage, BorderLayout.SOUTH);
077: this .getContentPane().add(mainPanel, BorderLayout.CENTER);
078: this .getContentPane().add(buttonPanel, BorderLayout.SOUTH);
079: if (!Settings.getInstance().restoreWindowSize(this ,
080: this .getClass().getName())) {
081: this .setSize(500, 400);
082: }
083: this .addWindowListener(this );
084:
085: WbSwingUtilities.center(this , owner);
086: EventQueue.invokeLater(new Runnable() {
087: public void run() {
088: highlightText(highlight);
089: }
090: });
091: }
092:
093: public void setStartButtonText(String aText) {
094: this .startButton.setText(aText);
095: }
096:
097: private void highlightText(String aText) {
098: if (aText == null || aText.trim().length() == 0)
099: return;
100: sqlEditor.reformatSql();
101: sqlEditor.setCaretPosition(0);
102: int start = sqlEditor.getReplacer().findFirst(aText, true,
103: true, false);
104: int end = start + aText.length();
105: if (start > -1) {
106: sqlEditor.select(start, end);
107: }
108: sqlEditor.requestFocusInWindow();
109: }
110:
111: public void closeWindow() {
112: if (this .worker != null) {
113: this .worker.interrupt();
114: this .worker = null;
115: }
116: Settings.getInstance().storeWindowSize(this ,
117: this .getClass().getName());
118: this .setVisible(false);
119: this .dispose();
120: }
121:
122: private void startCreate() {
123: if (this .dbConn == null)
124: return;
125:
126: if (this .dbConn.isBusy()) {
127: WbSwingUtilities.showMessageKey(this , "ErrConnectionBusy");
128: return;
129: }
130: this .statusMessage.setText(ResourceMgr
131: .getString("MsgCreatingIndex"));
132:
133: this .worker = new WbThread("Create index thread") {
134: public void run() {
135: Statement stmt = null;
136: try {
137: String sql = sqlEditor.getText().trim().replaceAll(
138: ";", "");
139: dbConn.setBusy(true);
140: stmt = dbConn.createStatement();
141: stmt.executeUpdate(sql);
142: statusMessage.setText(ResourceMgr
143: .getString("MsgIndexCreated"));
144: if (dbConn.shouldCommitDDL()) {
145: dbConn.commit();
146: }
147: EventQueue.invokeLater(new Runnable() {
148: public void run() {
149: createSuccess();
150: }
151: });
152: } catch (Exception e) {
153: if (dbConn.shouldCommitDDL()) {
154: try {
155: dbConn.rollback();
156: } catch (Throwable th) {
157: }
158: }
159: createFailure(e);
160: } finally {
161: dbConn.setBusy(false);
162: try {
163: stmt.close();
164: } catch (Throwable th) {
165: }
166: }
167: }
168: };
169: WbSwingUtilities.showWaitCursor(this );
170: this .startButton.setEnabled(false);
171: this .closeButton.setEnabled(false);
172: this .sqlEditor.setEnabled(false);
173: this .worker.start();
174: }
175:
176: protected void createSuccess() {
177: createFinished();
178: WbSwingUtilities.showMessage(this , ResourceMgr
179: .getString("MsgIndexCreated"));
180: this .closeWindow();
181: }
182:
183: protected void createFinished() {
184: WbSwingUtilities.showDefaultCursor(this );
185: this .startButton.setEnabled(true);
186: this .closeButton.setEnabled(true);
187: this .sqlEditor.setEnabled(true);
188: this .worker = null;
189: }
190:
191: protected void createFailure(Exception e) {
192: createFinished();
193: String error = ExceptionUtil.getDisplay(e);
194: statusMessage.setText(error);
195: String msg = ResourceMgr.getString("MsgCreateIndexError")
196: + "\n" + error;
197: WbSwingUtilities.showErrorMessage(this , msg);
198: }
199:
200: public void actionPerformed(java.awt.event.ActionEvent e) {
201: if (this .worker != null)
202: return;
203: if (e.getSource() == this .closeButton) {
204: this .closeWindow();
205: } else if (e.getSource() == this .startButton) {
206: this .startCreate();
207: }
208: }
209:
210: public void windowActivated(java.awt.event.WindowEvent e) {
211: }
212:
213: public void windowClosed(java.awt.event.WindowEvent e) {
214: }
215:
216: public void windowClosing(java.awt.event.WindowEvent e) {
217: if (this .worker == null)
218: this .closeWindow();
219: }
220:
221: public void windowDeactivated(java.awt.event.WindowEvent e) {
222: }
223:
224: public void windowDeiconified(java.awt.event.WindowEvent e) {
225: }
226:
227: public void windowIconified(java.awt.event.WindowEvent e) {
228: }
229:
230: public void windowOpened(java.awt.event.WindowEvent e) {
231: }
232:
233: }
|