01: /*
02: * ImportClipboardAction.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.Toolkit;
15: import java.awt.datatransfer.Clipboard;
16: import java.awt.datatransfer.DataFlavor;
17: import java.awt.datatransfer.Transferable;
18: import java.awt.event.ActionEvent;
19: import java.util.ArrayList;
20: import java.util.List;
21: import workbench.db.ColumnIdentifier;
22: import workbench.gui.components.WbTable;
23: import workbench.gui.sql.SqlPanel;
24: import workbench.log.LogMgr;
25: import workbench.resource.ResourceMgr;
26: import workbench.util.WbStringTokenizer;
27:
28: /**
29: * Import data from the clipboard into a table
30: * @author support@sql-workbench.net
31: */
32: public class ImportClipboardAction extends WbAction {
33: private SqlPanel client;
34: private WbTable dataTable;
35:
36: public ImportClipboardAction(SqlPanel panel) {
37: this .initMenuDefinition("MnuTxtImportClip");
38: this .setMenuItemName(ResourceMgr.MNU_TXT_DATA);
39: client = panel;
40: this .setEnabled(false);
41: }
42:
43: public void setTargetTable(WbTable table) {
44: this .dataTable = table;
45: this .setEnabled(table != null);
46: }
47:
48: public boolean hasCtrlModifier() {
49: return true;
50: }
51:
52: public void executeAction(ActionEvent evt) {
53: String content = getClipboardContents();
54: if (content == null)
55: return;
56: client.importString(content, isCtrlPressed(evt));
57: }
58:
59: private String getClipboardContents() {
60: if (client == null)
61: return null;
62: Clipboard clp = Toolkit.getDefaultToolkit()
63: .getSystemClipboard();
64: Transferable content = clp.getContents(client);
65: try {
66: String s = (String) content
67: .getTransferData(DataFlavor.stringFlavor);
68: return s;
69: } catch (Throwable e) {
70: LogMgr.logError("ImportClipboardAction.checkContents()",
71: "Error accessing clipboard", e);
72: }
73: return null;
74: }
75:
76: }
|