001: /*
002: * Created on Sep 27, 2004
003: *
004: * @author ctaylor
005: */
006: package com.pk.script.ui;
007:
008: import java.awt.FileDialog;
009: import java.awt.FlowLayout;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import java.io.IOException;
013: import java.sql.Connection;
014: import java.sql.SQLException;
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import javax.swing.JDialog;
019: import javax.swing.JFrame;
020: import javax.swing.JOptionPane;
021: import javax.swing.JPanel;
022: import javax.swing.ListSelectionModel;
023: import javax.swing.event.ListSelectionEvent;
024: import javax.swing.event.ListSelectionListener;
025:
026: import com.pk.Config;
027: import com.pk.ConnectionDialog;
028: import com.pk.script.Script;
029: import com.pk.script.ScriptCommand;
030:
031: /**
032: * @author ctaylor
033: *
034: */
035: public class ScriptPanel extends JPanel implements ActionListener,
036: ListSelectionListener {
037: /**
038: *
039: */
040: private static final long serialVersionUID = 6870801579299984459L;
041: private ConnectionPanel connectionPanel = null;
042: private PasteScriptPanel pasteScriptPanel = null;
043: private CommandTablePanel commandTablePanel = null;
044: private DetailPanel detailPanel = null;
045: private JDialog pasteScriptJDialog = null;
046: private Script script = null;
047:
048: private JFrame frame = null;
049: private String delimiter = null;
050:
051: private ConnectionDialog connectionDialog = null;
052:
053: private Config config = null;
054:
055: /* (non-Javadoc)
056: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
057: */
058: public void actionPerformed(ActionEvent arg0) {
059: Object source = arg0.getSource();
060: if (source == getDetailPanel().getApplyButton()) {
061: ScriptCommand tmpCommand = getDetailPanel()
062: .getSelectedScriptCommand();
063: tmpCommand.setCommand(getDetailPanel().getCommandText()
064: .getText());
065: tmpCommand.setRun(getDetailPanel().getRunCheckBox()
066: .isSelected());
067: getCommandTablePanel().getCommandTable().updateUI();
068: } else if (source == getDetailPanel().getRemoveCommandButton()) {
069: ScriptCommand tmpScriptCommand = getDetailPanel()
070: .getSelectedScriptCommand();
071: if (tmpScriptCommand != null) {
072: script.getCommands().remove(tmpScriptCommand);
073: commandTablePanel.getCommandTable().updateUI();
074: }
075: } else if (source == getDetailPanel().getShowErrorButton()) {
076: JOptionPane.showMessageDialog(this , getDetailPanel()
077: .getSelectedScriptCommand().getErrorMessage());
078: } else if (source == getConnectionPanel()
079: .getNewConnectionButton()) {
080: if (connectionDialog == null) {
081: connectionDialog = new ConnectionDialog(config);
082: connectionDialog.setModal(true);
083: }
084: connectionDialog.show();
085: if (connectionDialog.getConnection() != null) {
086: script.setConnection(connectionDialog.getConnection());
087: script.setConnectionInformation(connectionDialog
088: .getConnectionInformation());
089: getConnectionPanel().getUserNameLabel().setText(
090: script.getConnectionInformation().getUserID());
091: getConnectionPanel().getConnNameLabel().setText(
092: script.getConnectionInformation()
093: .getConnectionName());
094: getDetailPanel().setKeywords(
095: script.getConnectionInformation()
096: .getDatabaseDialect().getKeywords());
097: }
098:
099: } else if (source == getConnectionPanel().getPasteButton()) {
100: if (pasteScriptPanel == null) {
101: pasteScriptPanel = new PasteScriptPanel(this );
102: pasteScriptJDialog = new JDialog();
103: // pasteScriptJDialog.setModal(true);
104: pasteScriptJDialog.setTitle("Paste Script");
105: pasteScriptJDialog.getContentPane().add(
106: pasteScriptPanel);
107: pasteScriptJDialog.setSize(600, 500);
108: }
109: pasteScriptPanel.getScriptTextArea().setText("");
110:
111: pasteScriptJDialog.show();
112: pasteScriptPanel.getScriptTextArea().requestFocus();
113: } else if (source == connectionPanel.getCloseButton()) {
114: Connection tmpConn = script.getConnection();
115: if (tmpConn != null) {
116: try {
117: if (!tmpConn.isClosed()) {
118: tmpConn.close();
119: }
120: } catch (SQLException ex) {
121: ex.printStackTrace();
122: }
123: script.setConnection(null);
124:
125: connectionPanel.getConnNameLabel().setText("");
126: connectionPanel.getUserNameLabel().setText("");
127: }
128: script.setCommands(new ArrayList());
129: frame.setVisible(false);
130: } else if (pasteScriptPanel != null
131: && source == pasteScriptPanel.getOkButton()) {
132: pasteScriptJDialog.setVisible(false);
133: script.parseScriptFromClipBoard(pasteScriptPanel
134: .getScriptTextArea().getText(), "/",
135: pasteScriptPanel.getInsertMode(),
136: getCommandTablePanel().getCommandTable()
137: .getSelectedRow());
138: this .commandTablePanel.getCommandTable().updateUI();
139: this .commandTablePanel.getCommandTable().requestFocus();
140: } else if (pasteScriptPanel != null
141: && source == pasteScriptPanel.getCancelButton()) {
142: pasteScriptJDialog.setVisible(false);
143: this .commandTablePanel.getCommandTable().updateUI();
144: this .commandTablePanel.getCommandTable().requestFocus();
145: } else if (source == connectionPanel.getRunButton()) {
146: if (script.getConnection() == null) {
147: JOptionPane
148: .showMessageDialog(this ,
149: "You must create a connection before running a script.");
150: return;
151: }
152: this .script.run();
153: this .commandTablePanel.getCommandTable().updateUI();
154: } else if (source == connectionPanel.getLoadButton()) {
155: FileDialog exportFile = new FileDialog(frame,
156: "Load Script", FileDialog.LOAD);
157: //TODO set drop down list to .sql
158: // java.awt.dnd.DropTarget tre = new java.awt.dnd.DropTarget();
159:
160: exportFile.show();
161: String curFile = exportFile.getFile();
162: String filename = exportFile.getDirectory() + curFile;
163: if (curFile == null) {
164: return;
165: }
166:
167: script.parseScriptFile(filename, delimiter);
168: commandTablePanel.getCommandTable().updateUI();
169:
170: } else if (source == connectionPanel.getSaveButton()) {
171: java.io.PrintWriter exportFile = null;
172: FileDialog importFile = new FileDialog(frame,
173: "Save Script", FileDialog.SAVE);
174: // TODO set drop down list to .sql
175: // java.awt.dnd.DropTarget tre = new java.awt.dnd.DropTarget();
176:
177: importFile.show();
178: String curFile = importFile.getFile();
179: String filename = importFile.getDirectory() + curFile;
180: if (curFile == null) {
181: return;
182: }
183: if (curFile.lastIndexOf(".sql") == -1) {
184: curFile += ".sql";
185: }
186: try {
187: exportFile = new java.io.PrintWriter(
188: new java.io.BufferedWriter(
189: new java.io.FileWriter(filename, true)));
190: List commands = getScript().getCommands();
191: int size = commands.size();
192: ScriptCommand scriptCommand = null;
193: for (int x = 0; x < size; x++) {
194: scriptCommand = (ScriptCommand) commands.get(x);
195: exportFile.println(scriptCommand.getCommand());
196: exportFile.println(delimiter);
197: }
198: } catch (IOException e) {
199: JOptionPane.showMessageDialog(this , e.getMessage());
200: e.printStackTrace();
201: } finally {
202: if (exportFile != null) {
203: exportFile.close();
204: }
205: }
206: } else if (source == getConnectionPanel().getCommitButton()) {
207: if (script.getConnection() != null) {
208: try {
209: script.getConnection().commit();
210: JOptionPane.showMessageDialog(this ,
211: "Transaction committed.");
212: } catch (SQLException e) {
213: JOptionPane.showMessageDialog(this , e.getMessage());
214: e.printStackTrace();
215: }
216: }
217: } else if (source == getConnectionPanel().getRollbackButton()) {
218: if (script.getConnection() != null) {
219: try {
220: script.getConnection().rollback();
221: JOptionPane.showMessageDialog(this ,
222: "Transaction rolled back.");
223: } catch (SQLException e) {
224: JOptionPane.showMessageDialog(this , e.getMessage());
225: e.printStackTrace();
226: }
227: }
228: }
229:
230: }
231:
232: /**
233: * @return Returns the connectionPanel.
234: */
235: public ConnectionPanel getConnectionPanel() {
236: return connectionPanel;
237: }
238:
239: /**
240: * @param connectionPanel The connectionPanel to set.
241: */
242: public void setConnectionPanel(ConnectionPanel connectionPanel) {
243: this .connectionPanel = connectionPanel;
244: }
245:
246: /**
247: * @return Returns the script.
248: */
249: public Script getScript() {
250: return script;
251: }
252:
253: /**
254: * @param script The script to set.
255: */
256: public void setScript(Script script) {
257: this .script = script;
258: }
259:
260: /**
261: * @return Returns the commandTablePanel.
262: */
263: public CommandTablePanel getCommandTablePanel() {
264: return commandTablePanel;
265: }
266:
267: /**
268: * @param commandTablePanel The commandTablePanel to set.
269: */
270: public void setCommandTablePanel(CommandTablePanel commandTablePanel) {
271: this .commandTablePanel = commandTablePanel;
272: }
273:
274: /**
275: * @return Returns the frame.
276: */
277: public JFrame getFrame() {
278: return frame;
279: }
280:
281: /**
282: * @param frame The frame to set.
283: */
284: public void setFrame(JFrame frame) {
285: if (frame != null) {
286: frame.setTitle("Script Runner");
287: }
288: this .frame = frame;
289: }
290:
291: /**
292: * @return Returns the delimiter.
293: */
294: public String getDelimiter() {
295: return delimiter;
296: }
297:
298: /**
299: * @param delimiter The delimiter to set.
300: */
301: public void setDelimiter(String delimiter) {
302: this .delimiter = delimiter;
303: }
304:
305: /* (non-Javadoc)
306: * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
307: */
308: public void valueChanged(ListSelectionEvent argEvent) {
309: ListSelectionModel listSelectionModel = (ListSelectionModel) argEvent
310: .getSource();
311: if (argEvent.getValueIsAdjusting()
312: || listSelectionModel.isSelectionEmpty()) {
313: return;
314: }
315: detailPanel.setSelectedScriptCommand(getScript().getCommand(
316: listSelectionModel.getMinSelectionIndex()));
317: }
318:
319: /**
320: * @return Returns the detailPanel.
321: */
322: public DetailPanel getDetailPanel() {
323: return detailPanel;
324: }
325:
326: /**
327: * @param detailPanel The detailPanel to set.
328: */
329: public void setDetailPanel(DetailPanel detailPanel) {
330: this .detailPanel = detailPanel;
331: }
332:
333: public void init(Config argConfig) {
334: config = argConfig;
335: script = new Script();
336: frame = new JFrame("Script Runner");
337: setConnectionPanel(new ConnectionPanel(this ));
338: add(getConnectionPanel());
339:
340: setDelimiter(config.getScriptLineDelimiter());
341:
342: FlowLayout layout = new FlowLayout();
343: layout.setAlignment(FlowLayout.CENTER);
344: frame.getContentPane().setLayout(layout);
345: frame.getContentPane().add(this );
346:
347: setCommandTablePanel(new CommandTablePanel(this ));
348:
349: commandTablePanel.setScript(getScript());
350: frame.getContentPane().add(commandTablePanel);
351: DetailPanel detailPanel = new DetailPanel(this, null);
352: setDetailPanel(detailPanel);
353: frame.getContentPane().add(detailPanel);
354: }
355: }
|