01: package net.sourceforge.squirrel_sql.client.session.action;
02:
03: /*
04: * Copyright (C) 2003 Colin Bell
05: * colbell@users.sourceforge.net
06: *
07: * Modifications Copyright (C) 2003-2004 Jason Height
08: *
09: * This library is free software; you can redistribute it and/or
10: * modify it under the terms of the GNU Lesser General Public
11: * License as published by the Free Software Foundation; either
12: * version 2.1 of the License, or (at your option) any later version.
13: *
14: * This library is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17: * Lesser General Public License for more details.
18: *
19: * You should have received a copy of the GNU Lesser General Public
20: * License along with this library; if not, write to the Free Software
21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: */
23: import java.awt.Toolkit;
24: import java.awt.datatransfer.Clipboard;
25: import java.awt.datatransfer.StringSelection;
26:
27: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
28: import net.sourceforge.squirrel_sql.fw.util.ICommand;
29:
30: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
31:
32: /**
33: * This <CODE>Action</CODE> will copy the object names of all objects
34: * currently in the object tree and place on the system clipboard.
35: *
36: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
37: */
38: public class CopyObjectNameCommand implements ICommand {
39: /** Type of copy to do. */
40: public interface ICopyTypes {
41: int SIMPLE_NAME = 0;
42: int QUALIFIED_NAME = 1;
43: }
44:
45: private IObjectTreeAPI _api;
46:
47: /** Type of copy to do. @see ICopyTypes */
48: private int _copyType;
49:
50: /**
51: * Ctor specifying copy type.
52: *
53: * @param api
54: * @param copyType Type of copy to do. @see ICopyTypes.
55: *
56: * @throws IllegalArgumentException
57: * Thrown if a <TT>null</TT> <TT>ISession</TT> or an invalid
58: <TT>copyType</TT> passed.
59: */
60: public CopyObjectNameCommand(IObjectTreeAPI api, int copyType) {
61: super ();
62: if (api == null) {
63: throw new IllegalArgumentException("IObjectTreeAPI == null");
64: }
65: if (copyType < ICopyTypes.SIMPLE_NAME
66: || copyType > ICopyTypes.QUALIFIED_NAME) {
67: throw new IllegalArgumentException("Invalid copyType of : "
68: + copyType + " passed");
69: }
70:
71: _api = api;
72: _copyType = copyType;
73: }
74:
75: /**
76: * Copy to clipboard.
77: */
78: public void execute() {
79: final StringBuffer buf = new StringBuffer(100);
80: final IDatabaseObjectInfo[] dbObjs = _api
81: .getSelectedDatabaseObjects();
82:
83: // Get all the selected object names and place in a comma separated list.
84: for (int i = 0; i < dbObjs.length; i++) {
85: final IDatabaseObjectInfo doi = dbObjs[i];
86: final String name = _copyType == ICopyTypes.SIMPLE_NAME ? doi
87: .getSimpleName()
88: : doi.getQualifiedName();
89: buf.append(name).append(", ");
90: }
91: if (buf.length() > 0) {
92: buf.setLength(buf.length() - 2); // Remove trailing ", ".
93: Clipboard clip = Toolkit.getDefaultToolkit()
94: .getSystemClipboard();
95: StringSelection data = new StringSelection(buf.toString());
96: clip.setContents(data, data);
97: }
98: }
99: }
|