01: /*
02: * DeleteTablesAction.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: import java.util.ArrayList;
16: import java.util.List;
17: import javax.swing.JFrame;
18: import javax.swing.ListSelectionModel;
19: import javax.swing.SwingUtilities;
20: import javax.swing.event.ListSelectionEvent;
21: import javax.swing.event.ListSelectionListener;
22: import workbench.db.DbObject;
23: import workbench.db.TableIdentifier;
24: import workbench.gui.WbSwingUtilities;
25: import workbench.gui.dbobjects.DbObjectList;
26: import workbench.gui.dbobjects.TableDeleterUI;
27: import workbench.interfaces.TableDeleteListener;
28:
29: /**
30: * @author support@sql-workbench.net
31: */
32: public class DeleteTablesAction extends WbAction implements
33: ListSelectionListener {
34: private DbObjectList source;
35: private ListSelectionModel selection;
36: private TableDeleteListener deleteListener;
37:
38: public DeleteTablesAction(DbObjectList client,
39: ListSelectionModel list, TableDeleteListener l) {
40: this .initMenuDefinition("MnuTxtDeleteTableData");
41: this .source = client;
42: this .selection = list;
43: this .deleteListener = l;
44: setEnabled(false);
45: list.addListSelectionListener(this );
46: }
47:
48: @Override
49: public void executeAction(ActionEvent e) {
50: if (!WbSwingUtilities.checkConnection(source.getComponent(),
51: source.getConnection()))
52: return;
53:
54: List<TableIdentifier> tables = getSelectedTables();
55:
56: TableDeleterUI deleter = new TableDeleterUI();
57: deleter.addDeleteListener(this .deleteListener);
58: deleter.setObjects(tables);
59: deleter.setConnection(source.getConnection());
60: JFrame f = (JFrame) SwingUtilities.getWindowAncestor(source
61: .getComponent());
62: deleter.showDialog(f);
63: deleter.removeDeleteListener(deleteListener);
64: }
65:
66: private List<TableIdentifier> getSelectedTables() {
67: List<? extends DbObject> objects = source.getSelectedObjects();
68: if (objects == null || objects.size() == 0)
69: return null;
70:
71: List<TableIdentifier> tables = new ArrayList<TableIdentifier>(
72: objects.size());
73: for (DbObject dbo : objects) {
74: if (dbo instanceof TableIdentifier) {
75: String type = dbo.getObjectType();
76: if (!"table".equalsIgnoreCase(type)
77: && !"view".equalsIgnoreCase(type))
78: continue;
79: tables.add((TableIdentifier) dbo);
80: }
81: }
82: return tables;
83: }
84:
85: public void valueChanged(ListSelectionEvent e) {
86: setEnabled(this .selection.getMinSelectionIndex() >= 0);
87: }
88:
89: }
|