01: /*
02: * AddMacroAction.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.event.ActionEvent;
16:
17: import workbench.gui.WbSwingUtilities;
18: import workbench.gui.sql.EditorPanel;
19: import workbench.interfaces.TextSelectionListener;
20: import workbench.resource.ResourceMgr;
21: import workbench.sql.MacroManager;
22:
23: /**
24: * Action to add a new macro.
25: * @see workbench.sql.MacroManager
26: * @author support@sql-workbench.net
27: */
28: public class AddMacroAction extends WbAction implements
29: TextSelectionListener {
30: private EditorPanel client;
31:
32: public AddMacroAction() {
33: super ();
34: this .setIcon(null);
35: this .setMenuItemName(ResourceMgr.MNU_TXT_MACRO);
36: this .initMenuDefinition("MnuTxtAddMacro", null);
37: }
38:
39: public void setClient(EditorPanel panel) {
40: if (this .client != null) {
41: this .client.removeSelectionListener(this );
42: }
43: this .client = panel;
44: this .client.addSelectionListener(this );
45: this .setEnabled(client.isTextSelected());
46: }
47:
48: public void executeAction(ActionEvent e) {
49: String text = client.getSelectedText();
50: if (text == null || text.trim().length() == 0) {
51: Toolkit.getDefaultToolkit().beep();
52: return;
53: }
54:
55: String name = WbSwingUtilities.getUserInput(client, ResourceMgr
56: .getString("TxtGetMacroNameWindowTitle"), ResourceMgr
57: .getString("TxtEmptyMacroName"));
58: if (name != null) {
59: MacroManager.getInstance().setMacro(name, text);
60: }
61: }
62:
63: public void selectionChanged(int newStart, int newEnd) {
64: boolean selected = (newStart > -1 && newEnd > newStart);
65: this.setEnabled(selected);
66: }
67: }
|