001: /*
002: * ObjectScripterUI.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.dbobjects;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dimension;
016: import java.awt.EventQueue;
017: import java.awt.Window;
018: import java.awt.event.WindowListener;
019:
020: import javax.swing.JFrame;
021: import javax.swing.JLabel;
022: import javax.swing.JPanel;
023: import javax.swing.border.CompoundBorder;
024: import javax.swing.border.EmptyBorder;
025: import javax.swing.border.EtchedBorder;
026:
027: import workbench.gui.WbSwingUtilities;
028: import workbench.gui.actions.CreateSnippetAction;
029: import workbench.gui.sql.EditorPanel;
030: import workbench.interfaces.ScriptGenerationMonitor;
031: import workbench.interfaces.Scripter;
032: import workbench.resource.ResourceMgr;
033: import workbench.resource.Settings;
034: import workbench.util.StringUtil;
035: import workbench.util.WbThread;
036:
037: /**
038: *
039: * @author support@sql-workbench.net
040: */
041: public class ObjectScripterUI extends JPanel implements WindowListener,
042: ScriptGenerationMonitor {
043: protected Scripter scripter;
044: protected JLabel statusMessage;
045: protected EditorPanel editor;
046: protected JFrame window;
047: private boolean isRunning;
048: private Object runMonitor = new Object();
049:
050: public ObjectScripterUI(Scripter scripter) {
051: super ();
052: this .scripter = scripter;
053: this .scripter.setProgressMonitor(this );
054:
055: this .statusMessage = new JLabel("");
056: this .statusMessage.setBorder(new CompoundBorder(
057: new EtchedBorder(), new EmptyBorder(0, 2, 0, 0)));
058: this .statusMessage.setMaximumSize(new Dimension(32768, 22));
059: this .statusMessage.setMinimumSize(new Dimension(10, 22));
060: this .statusMessage.setPreferredSize(new Dimension(60, 22));
061: this .setLayout(new BorderLayout());
062: this .add(this .statusMessage, BorderLayout.SOUTH);
063: this .editor = EditorPanel.createSqlEditor();
064: CreateSnippetAction create = new CreateSnippetAction(
065: this .editor);
066: this .editor.addPopupMenuItem(create, true);
067: this .add(this .editor, BorderLayout.CENTER);
068: }
069:
070: private void setRunning(boolean flag) {
071: synchronized (runMonitor) {
072: this .isRunning = flag;
073: }
074: }
075:
076: private boolean isRunning() {
077: synchronized (runMonitor) {
078: return this .isRunning;
079: }
080: }
081:
082: private void startScripting() {
083: if (this .isRunning())
084: return;
085: WbThread t = new WbThread("ObjectScripter Thread") {
086: public void run() {
087: try {
088: setRunning(true);
089: scripter.generateScript();
090: if (!scripter.isCancelled()) {
091: editor.setText(scripter.getScript().toString());
092: editor.setCaretPosition(0);
093: }
094: } finally {
095: setRunning(false);
096: EventQueue.invokeLater(new Runnable() {
097: public void run() {
098: statusMessage
099: .setText(StringUtil.EMPTY_STRING);
100: }
101: });
102: }
103: }
104: };
105: t.start();
106: }
107:
108: public void setCurrentObject(String aTableName) {
109: this .statusMessage.setText(aTableName);
110: this .statusMessage.repaint();
111: }
112:
113: public void show(Window aParent) {
114: if (this .window == null) {
115: this .window = new JFrame(ResourceMgr
116: .getString("TxtWindowTitleGeneratedScript"));
117: this .window.getContentPane().setLayout(new BorderLayout());
118: this .window.getContentPane().add(this , BorderLayout.CENTER);
119: this .window.setIconImage(ResourceMgr.getImage("script")
120: .getImage());
121: if (!Settings.getInstance().restoreWindowSize(this .window,
122: ObjectScripterUI.class.getName())) {
123: this .window.setSize(500, 400);
124: }
125:
126: if (!Settings.getInstance().restoreWindowPosition(
127: this .window, ObjectScripterUI.class.getName())) {
128: WbSwingUtilities.center(this .window, aParent);
129: }
130: this .window.addWindowListener(this );
131: }
132: this .window.setVisible(true);
133: this .startScripting();
134: }
135:
136: public void windowActivated(java.awt.event.WindowEvent e) {
137: }
138:
139: public void windowClosed(java.awt.event.WindowEvent e) {
140: }
141:
142: private void cancel() {
143: WbThread t = new WbThread("Scripter Cancel") {
144: public void run() {
145: try {
146: WbSwingUtilities.showWaitCursor(window);
147: statusMessage.setText(ResourceMgr
148: .getString("MsgCancelling"));
149: Thread.yield();
150: scripter.cancel();
151: } catch (Throwable ex) {
152: ex.printStackTrace();
153: } finally {
154: WbSwingUtilities.showDefaultCursor(window);
155: }
156: scripter = null;
157: setRunning(false);
158: closeWindow();
159: }
160: };
161: t.start();
162: }
163:
164: protected void closeWindow() {
165: if (isRunning())
166: return;
167: Settings.getInstance().storeWindowPosition(this .window,
168: ObjectScripterUI.class.getName());
169: Settings.getInstance().storeWindowSize(this .window,
170: ObjectScripterUI.class.getName());
171: this .window.setVisible(false);
172: this .window.dispose();
173: }
174:
175: public void windowClosing(java.awt.event.WindowEvent e) {
176: if (this .isRunning()) {
177: cancel();
178: return;
179: }
180: closeWindow();
181: }
182:
183: public void windowDeactivated(java.awt.event.WindowEvent e) {
184: }
185:
186: public void windowDeiconified(java.awt.event.WindowEvent e) {
187: }
188:
189: public void windowIconified(java.awt.event.WindowEvent e) {
190: }
191:
192: public void windowOpened(java.awt.event.WindowEvent e) {
193: }
194:
195: }
|