01: /*
02: * CreateDeleteScriptAction.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.actions;
13:
14: import java.awt.event.ActionEvent;
15:
16: import javax.swing.event.ListSelectionListener;
17: import workbench.db.DeleteScriptGenerator;
18: import workbench.db.WbConnection;
19: import workbench.gui.components.WbTable;
20: import workbench.log.LogMgr;
21: import workbench.resource.ResourceMgr;
22:
23: /**
24: * Create a SQL script to delete the selected row from a WbTable.
25: * @see workbench.db.DeleteScriptGenerator
26: * @author support@sql-workbench.net
27: */
28: public class CreateDeleteScriptAction extends WbAction implements
29: ListSelectionListener {
30: private WbTable client;
31:
32: public CreateDeleteScriptAction(WbTable aClient) {
33: super ();
34: this .initMenuDefinition("MnuTxtCreateDeleteScript", null);
35: this .setMenuItemName(ResourceMgr.MNU_TXT_DATA);
36: setClient(aClient);
37: }
38:
39: public void executeAction(ActionEvent e) {
40: WbConnection con = client.getDataStore()
41: .getOriginalConnection();
42: if (con.isBusy())
43: return;
44:
45: try {
46: boolean hasPK = client.checkPkColumns(true);
47: if (!hasPK)
48: return;
49: DeleteScriptGenerator gen = new DeleteScriptGenerator(con);
50: gen.setSource(client);
51: gen.startGenerate();
52: } catch (Exception ex) {
53: LogMgr.logError("SqlPanel.generateDeleteScript()",
54: "Error initializing DeleteScriptGenerator", ex);
55: }
56: }
57:
58: public void valueChanged(javax.swing.event.ListSelectionEvent e) {
59: if (e.getValueIsAdjusting())
60: return;
61: checkSelection();
62: }
63:
64: private void checkSelection() {
65: if (this .client == null)
66: return;
67: int rows = this .client.getSelectedRowCount();
68: this .setEnabled(rows > 0);
69: }
70:
71: public void setClient(WbTable w) {
72: if (this.client != null) {
73: this.client.getSelectionModel()
74: .removeListSelectionListener(this);
75: }
76: this.client = w;
77: if (this.client != null) {
78: this.client.getSelectionModel().addListSelectionListener(
79: this);
80: checkSelection();
81: }
82: this.setEnabled(this.client != null);
83: checkSelection();
84: }
85: }
|