001: /*
002: * DropDbObjectAction.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.actions;
013:
014: import java.awt.EventQueue;
015: import java.awt.event.ActionEvent;
016: import java.util.List;
017: import javax.swing.JFrame;
018: import javax.swing.ListSelectionModel;
019: import javax.swing.SwingUtilities;
020: import javax.swing.event.ListSelectionEvent;
021: import javax.swing.event.ListSelectionListener;
022: import workbench.db.DbObject;
023: import workbench.db.GenericObjectDropper;
024: import workbench.gui.WbSwingUtilities;
025: import workbench.gui.dbobjects.DbObjectList;
026: import workbench.gui.dbobjects.ObjectDropperUI;
027: import workbench.interfaces.ObjectDropper;
028: import workbench.interfaces.Reloadable;
029:
030: /**
031: * @author support@sql-workbench.net
032: */
033: public class DropDbObjectAction extends WbAction implements
034: ListSelectionListener {
035: private DbObjectList source;
036: private ListSelectionModel selection;
037: private ObjectDropper dropper;
038: private Reloadable data;
039: private boolean available = true;
040:
041: public DropDbObjectAction(DbObjectList client,
042: ListSelectionModel list, Reloadable r) {
043: this ("MnuTxtDropDbObject", client, list, r);
044: }
045:
046: public DropDbObjectAction(String labelKey, DbObjectList client,
047: ListSelectionModel list, Reloadable r) {
048: this .initMenuDefinition(labelKey);
049: this .source = client;
050: this .selection = list;
051: this .data = r;
052: setEnabled(false);
053: list.addListSelectionListener(this );
054: }
055:
056: @Override
057: public void executeAction(ActionEvent e) {
058: dropObjects();
059: }
060:
061: public void setAvailable(boolean flag) {
062: this .available = flag;
063: if (!available)
064: this .setEnabled(false);
065: }
066:
067: public void setDropper(ObjectDropper dropperToUse) {
068: this .dropper = dropperToUse;
069: }
070:
071: private void dropObjects() {
072: if (!WbSwingUtilities.checkConnection(source.getComponent(),
073: source.getConnection()))
074: return;
075:
076: List<? extends DbObject> objects = source.getSelectedObjects();
077: if (objects == null || objects.size() == 0)
078: return;
079:
080: ObjectDropper dropperToUse = (this .dropper != null ? this .dropper
081: : new GenericObjectDropper());
082: dropperToUse.setObjects(objects);
083: dropperToUse.setConnection(source.getConnection());
084: dropperToUse.setObjectTable(source.getObjectTable());
085:
086: ObjectDropperUI dropperUI = new ObjectDropperUI(dropperToUse);
087:
088: JFrame f = (JFrame) SwingUtilities.getWindowAncestor(source
089: .getComponent());
090: dropperUI.showDialog(f);
091:
092: if (!dropperUI.dialogWasCancelled() && data != null) {
093: EventQueue.invokeLater(new Runnable() {
094: public void run() {
095: data.reload();
096: }
097: });
098: }
099: }
100:
101: public void valueChanged(ListSelectionEvent e) {
102: setEnabled(this .available
103: && this .selection.getMinSelectionIndex() >= 0);
104: }
105:
106: }
|