001: /*
002: * MacroManagerDialog.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.macros;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dimension;
016: import java.awt.EventQueue;
017: import java.awt.FlowLayout;
018: import java.awt.Frame;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.awt.event.MouseListener;
022: import java.awt.event.WindowAdapter;
023: import java.awt.event.WindowEvent;
024: import java.awt.event.WindowListener;
025:
026: import javax.swing.ActionMap;
027: import javax.swing.InputMap;
028: import javax.swing.JButton;
029: import javax.swing.JCheckBox;
030: import javax.swing.JComponent;
031: import javax.swing.JDialog;
032: import javax.swing.JList;
033: import javax.swing.JPanel;
034: import javax.swing.border.CompoundBorder;
035: import javax.swing.border.EmptyBorder;
036: import javax.swing.border.EtchedBorder;
037: import javax.swing.event.ListSelectionListener;
038:
039: import workbench.gui.WbSwingUtilities;
040: import workbench.gui.actions.EscAction;
041: import workbench.gui.components.WbButton;
042: import workbench.gui.components.WbCheckBox;
043: import workbench.gui.sql.SqlPanel;
044: import workbench.resource.ResourceMgr;
045: import workbench.resource.Settings;
046:
047: /**
048: * A Dialog that displays a {@link MacroManagerGui} and allows to run
049: * the selected macro.
050: */
051: public class MacroManagerDialog extends JDialog implements
052: ActionListener, ListSelectionListener, MouseListener,
053: WindowListener {
054: private JPanel dummyPanel;
055: private JPanel buttonPanel;
056: private JList macroList;
057: private JButton okButton;
058: private JButton runButton;
059: private MacroManagerGui macroPanel;
060: private JButton cancelButton;
061: private boolean cancelled = true;
062: private EscAction escAction;
063: private SqlPanel client;
064: private JCheckBox replaceEditorText;
065:
066: public MacroManagerDialog(Frame parent, SqlPanel aTarget) {
067: super (parent, true);
068: this .client = aTarget;
069: this .initComponents();
070: this .initWindow(parent);
071: boolean connected = this .client.isConnected();
072: boolean busy = this .client.isBusy();
073: this .runButton.setEnabled(connected && !busy);
074: this .runButton.setVisible(connected && !busy);
075:
076: this .replaceEditorText.setVisible(connected && !busy);
077: this .replaceEditorText.setEnabled(connected && !busy);
078: this .initKeys();
079: this .addWindowListener(this );
080: }
081:
082: private void initWindow(Frame parent) {
083: if (!Settings.getInstance().restoreWindowSize(this )) {
084: this .setSize(600, 400);
085: }
086: WbSwingUtilities.center(this , parent);
087: boolean replace = Settings.getInstance().getBoolProperty(
088: "workbench.gui.macros.replaceOnRun", false);
089: this .replaceEditorText.setSelected(replace);
090: }
091:
092: private void initKeys() {
093: if (this .runButton.isEnabled()) {
094: this .getRootPane().setDefaultButton(this .runButton);
095: } else {
096: this .getRootPane().setDefaultButton(this .okButton);
097: }
098: InputMap im = this .getRootPane().getInputMap(
099: JComponent.WHEN_IN_FOCUSED_WINDOW);
100: ActionMap am = this .getRootPane().getActionMap();
101: escAction = new EscAction(this );
102: im.put(escAction.getAccelerator(), escAction.getActionName());
103: am.put(escAction.getActionName(), escAction);
104: }
105:
106: private void initComponents() {
107: macroPanel = new MacroManagerGui();
108: buttonPanel = new JPanel();
109: runButton = new WbButton(ResourceMgr.getString("LblRunMacro"));
110: runButton.setToolTipText(ResourceMgr
111: .getDescription("LblManageMacrosRun"));
112:
113: okButton = new WbButton(ResourceMgr
114: .getString(ResourceMgr.TXT_OK));
115: okButton.setToolTipText(ResourceMgr
116: .getDescription("LblManageMacrosOK"));
117:
118: cancelButton = new WbButton(ResourceMgr
119: .getString(ResourceMgr.TXT_CANCEL));
120: cancelButton.setToolTipText(ResourceMgr
121: .getDescription("LblManageMacrosCancel"));
122: dummyPanel = new JPanel();
123:
124: setTitle(ResourceMgr.getString("TxtMacroManagerWindowTitle"));
125: setModal(true);
126: setName("MacroManagerDialog");
127: addWindowListener(new WindowAdapter() {
128: public void windowClosing(WindowEvent evt) {
129: closeDialog(evt);
130: }
131: });
132:
133: macroPanel.setBorder(new CompoundBorder(new EmptyBorder(1, 1,
134: 1, 1), new EtchedBorder()));
135: getContentPane().add(macroPanel, BorderLayout.CENTER);
136:
137: this .replaceEditorText = new WbCheckBox(ResourceMgr
138: .getString("LblReplaceCurrentSql"));
139: this .replaceEditorText.setToolTipText(ResourceMgr
140: .getDescription("LblReplaceCurrentSql"));
141:
142: JPanel p = new JPanel();
143: p.setLayout(new FlowLayout(FlowLayout.LEFT));
144: p.add(this .replaceEditorText);
145: p.add(new JPanel());
146:
147: buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
148: buttonPanel.add(p);
149:
150: runButton.addActionListener(this );
151: runButton.setEnabled(false);
152: buttonPanel.add(runButton);
153:
154: okButton.addActionListener(this );
155: buttonPanel.add(okButton);
156:
157: cancelButton.addActionListener(this );
158: buttonPanel.add(cancelButton);
159:
160: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
161:
162: dummyPanel.setMaximumSize(new Dimension(2, 2));
163: dummyPanel.setMinimumSize(new Dimension(1, 1));
164: dummyPanel.setPreferredSize(new Dimension(2, 2));
165: getContentPane().add(dummyPanel, BorderLayout.NORTH);
166:
167: this .macroList = macroPanel.getMacroList();
168: this .macroList.addMouseListener(this );
169: this .macroList.addListSelectionListener(this );
170: }
171:
172: public void actionPerformed(ActionEvent e) {
173: if (e.getSource() == okButton) {
174: okButtonActionPerformed(e);
175: }
176: if (e.getSource() == runButton) {
177: runButtonActionPerformed(e);
178: } else if (e.getSource() == cancelButton
179: || e.getActionCommand().equals(
180: escAction.getActionName())) {
181: cancelButtonActionPerformed(e);
182: }
183: }
184:
185: private void cancelButtonActionPerformed(ActionEvent evt) {
186: this .cancelled = true;
187: this .closeDialog();
188: }
189:
190: private void okButtonActionPerformed(ActionEvent evt) {
191: try {
192: this .macroPanel.saveItem();
193: this .cancelled = false;
194: this .closeDialog();
195: } catch (Exception e) {
196: e.printStackTrace();
197: }
198: }
199:
200: private void runButtonActionPerformed(ActionEvent evt) {
201: this .runSelectedMacro();
202: }
203:
204: private void runSelectedMacro() {
205: try {
206: this .macroPanel.saveItem();
207: this .cancelled = false;
208: this .closeDialog();
209: if (this .client != null) {
210: String name = this .macroPanel.getSelectedMacroName();
211: this .client.executeMacro(name, this .replaceEditorText
212: .isSelected());
213: }
214: } catch (Exception e) {
215: e.printStackTrace();
216: }
217: }
218:
219: public boolean isCancelled() {
220: return this .cancelled;
221: }
222:
223: /** Closes the dialog */
224: private void closeDialog(WindowEvent evt) {
225: this .closeDialog();
226: }
227:
228: public void closeDialog() {
229: Settings.getInstance().storeWindowSize(this );
230: macroPanel.saveSettings();
231: Settings.getInstance().setProperty(
232: "workbench.gui.macros.replaceOnRun",
233: this .replaceEditorText.isSelected());
234: setVisible(false);
235: }
236:
237: public void valueChanged(javax.swing.event.ListSelectionEvent e) {
238: if (e.getValueIsAdjusting())
239: return;
240: boolean selected = (e.getFirstIndex() > -1)
241: && this .macroList.getModel().getSize() > 0;
242: //this.okButton.setEnabled(selected);
243: this .runButton.setEnabled(selected);
244: }
245:
246: public void mouseClicked(java.awt.event.MouseEvent e) {
247: if (e.getSource() == this .macroList) {
248: if (e.getClickCount() == 2 && this .runButton.isEnabled()) {
249: this .runSelectedMacro();
250: }
251: }
252: }
253:
254: public void mouseEntered(java.awt.event.MouseEvent e) {
255: }
256:
257: public void mouseExited(java.awt.event.MouseEvent e) {
258: }
259:
260: public void mousePressed(java.awt.event.MouseEvent e) {
261: }
262:
263: public void mouseReleased(java.awt.event.MouseEvent e) {
264: }
265:
266: public void windowOpened(WindowEvent windowEvent) {
267: // Fix for JDK 6
268: // It seems that the macro editor is not repainted
269: // correctly if we load the macro text while
270: // it's not visible
271: EventQueue.invokeLater(new Runnable() {
272: public void run() {
273: macroPanel.restoreSettings();
274: }
275: });
276: }
277:
278: public void windowClosing(WindowEvent windowEvent) {
279: }
280:
281: public void windowClosed(WindowEvent windowEvent) {
282: }
283:
284: public void windowIconified(WindowEvent windowEvent) {
285: }
286:
287: public void windowDeiconified(WindowEvent windowEvent) {
288: }
289:
290: public void windowActivated(WindowEvent windowEvent) {
291: }
292:
293: public void windowDeactivated(WindowEvent windowEvent) {
294: }
295:
296: }
|