01: /*
02: * CreateDummySqlAction.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.ListSelectionModel;
18: import javax.swing.SwingUtilities;
19: import javax.swing.event.ListSelectionEvent;
20: import javax.swing.event.ListSelectionListener;
21: import workbench.db.DbObject;
22: import workbench.db.DummyInsert;
23: import workbench.db.DummySelect;
24: import workbench.db.ObjectScripter;
25: import workbench.db.TableIdentifier;
26: import workbench.gui.dbobjects.DbObjectList;
27: import workbench.gui.dbobjects.ObjectScripterUI;
28:
29: /**
30: * @author support@sql-workbench.net
31: */
32: public class CreateDummySqlAction extends WbAction implements
33: ListSelectionListener {
34: private DbObjectList source;
35: private ListSelectionModel selection;
36: private String scriptType;
37:
38: public static CreateDummySqlAction createDummyInsertAction(
39: DbObjectList client, ListSelectionModel list) {
40: return new CreateDummySqlAction("MnuTxtCreateDummyInsert",
41: client, list, ObjectScripter.TYPE_INSERT);
42: }
43:
44: public static CreateDummySqlAction createDummySelectAction(
45: DbObjectList client, ListSelectionModel list) {
46: return new CreateDummySqlAction("MnuTxtCreateDefaultSelect",
47: client, list, ObjectScripter.TYPE_SELECT);
48: }
49:
50: private CreateDummySqlAction(String key, DbObjectList client,
51: ListSelectionModel list, String type) {
52: this .initMenuDefinition(key);
53: this .source = client;
54: this .selection = list;
55: this .scriptType = type;
56: setEnabled(false);
57: list.addListSelectionListener(this );
58: }
59:
60: @Override
61: public void executeAction(ActionEvent e) {
62: List<? extends DbObject> objects = source.getSelectedObjects();
63: List<DbObject> dummy = new ArrayList<DbObject>(objects.size());
64: for (DbObject dbo : objects) {
65: if (dbo instanceof TableIdentifier) {
66: TableIdentifier tbl = (TableIdentifier) dbo;
67: if (scriptType.equalsIgnoreCase("select")) {
68: dummy.add(new DummySelect(tbl));
69: } else {
70: dummy.add(new DummyInsert(tbl));
71: }
72: }
73: }
74: ObjectScripter s = new ObjectScripter(dummy, source
75: .getConnection());
76: ObjectScripterUI scripterUI = new ObjectScripterUI(s);
77: scripterUI.show(SwingUtilities.getWindowAncestor(source
78: .getComponent()));
79: }
80:
81: public void valueChanged(ListSelectionEvent e) {
82: setEnabled(this .selection.getMinSelectionIndex() >= 0);
83: }
84:
85: }
|