001: /*
002: * DwUpdatePreview.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.Component;
015: import java.awt.Dimension;
016: import java.awt.Window;
017: import java.sql.SQLException;
018: import java.util.List;
019: import javax.swing.JScrollPane;
020: import javax.swing.SwingUtilities;
021: import workbench.db.WbConnection;
022: import workbench.gui.WbSwingUtilities;
023: import workbench.log.LogMgr;
024: import workbench.resource.ResourceMgr;
025: import workbench.resource.Settings;
026: import workbench.storage.DataStore;
027: import workbench.storage.DmlStatement;
028: import workbench.storage.SqlLiteralFormatter;
029: import workbench.util.ExceptionUtil;
030: import workbench.util.MessageBuffer;
031:
032: /**
033: * @author support@sql-workbench.net
034: */
035: public class DwUpdatePreview {
036:
037: public DwUpdatePreview() {
038: }
039:
040: public boolean confirmUpdate(Component caller, DataStore ds,
041: WbConnection dbConn) {
042: boolean doSave = true;
043:
044: Window win = SwingUtilities.getWindowAncestor(caller);
045: try {
046: List<DmlStatement> stmts = ds.getUpdateStatements(dbConn);
047: if (stmts.isEmpty())
048: return true;
049:
050: Dimension max = new Dimension(800, 600);
051: Dimension pref = new Dimension(400, 300);
052: final EditorPanel preview = EditorPanel.createSqlEditor();
053: preview.setEditable(false);
054: preview.showFindOnPopupMenu();
055: preview.setBorder(WbSwingUtilities.EMPTY_BORDER);
056: preview.setPreferredSize(pref);
057: preview.setMaximumSize(max);
058: JScrollPane scroll = new JScrollPane(preview);
059: scroll.setMaximumSize(max);
060: int maxStatements = Settings.getInstance().getIntProperty(
061: "workbench.db.previewsql.maxstatements", 5000);
062: if (maxStatements < stmts.size()) {
063: LogMgr
064: .logWarning(
065: "DwUpdatePreview.confirmUpdate()",
066: "Only "
067: + maxStatements
068: + " of "
069: + stmts.size()
070: + " statments displayed. To view all statements increase the value of the property 'workbench.db.previewsql.maxstatements'");
071: }
072: MessageBuffer buffer = new MessageBuffer(maxStatements);
073:
074: SqlLiteralFormatter f = new SqlLiteralFormatter(dbConn);
075:
076: for (DmlStatement dml : stmts) {
077: buffer.append(dml.getExecutableStatement(f));
078: buffer.append(";");
079: buffer.appendNewLine();
080: }
081:
082: final String text = buffer.getBuffer().toString();
083:
084: WbSwingUtilities.invoke(new Runnable() {
085: public void run() {
086: preview.setText(text.toString());
087: preview.setCaretPosition(0);
088: preview.repaint();
089: }
090: });
091:
092: WbSwingUtilities.showDefaultCursor(caller);
093: Runnable painter = new Runnable() {
094: public void run() {
095: preview.repaint();
096: }
097: };
098: doSave = WbSwingUtilities.getOKCancel(ResourceMgr
099: .getString("MsgConfirmUpdates"), win, scroll,
100: painter);
101: } catch (SQLException e) {
102: LogMgr.logError("DwUpdatePreview.confirmUpdate()",
103: "Error generating preview", e);
104: String msg = ExceptionUtil.getDisplay(e);
105: WbSwingUtilities.showErrorMessage(win, msg);
106: return false;
107: } catch (OutOfMemoryError mem) {
108: String msg = ResourceMgr
109: .getString("MsgOutOfMemorySQLPreview");
110: WbSwingUtilities.showErrorMessage(caller, msg);
111: return false;
112: } catch (Throwable th) {
113: LogMgr.logError("DwUpdatePreview.confirmUpdate()",
114: "Error when previewing SQL", th);
115: }
116: return doSave;
117:
118: }
119: }
|