001: /*
002: * EditConnectScriptsPanel.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.profiles;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dialog;
016: import java.awt.Dimension;
017: import java.awt.EventQueue;
018: import java.awt.FlowLayout;
019: import java.awt.GridBagConstraints;
020: import java.awt.GridBagLayout;
021: import java.awt.Insets;
022: import javax.swing.JLabel;
023: import javax.swing.JPanel;
024: import javax.swing.JTextField;
025: import workbench.db.ConnectionProfile;
026: import workbench.db.KeepAliveDaemon;
027: import workbench.gui.WbSwingUtilities;
028: import workbench.gui.components.ValidatingDialog;
029: import workbench.gui.components.WbTraversalPolicy;
030: import workbench.gui.sql.EditorPanel;
031: import workbench.interfaces.ValidatingComponent;
032: import workbench.resource.ResourceMgr;
033: import workbench.resource.Settings;
034: import workbench.util.StringUtil;
035:
036: /**
037: * @author support@sql-workbench.net
038: */
039: public class EditConnectScriptsPanel extends JPanel implements
040: ValidatingComponent {
041: private EditorPanel postConnectEditor;
042: private EditorPanel preDisconnectEditor;
043: private EditorPanel keepAliveScriptEditor;
044: private JTextField keepAliveInterval;
045:
046: public EditConnectScriptsPanel(ConnectionProfile profile) {
047: if (profile == null)
048: throw new NullPointerException("Null profile specified!");
049:
050: this .setLayout(new GridBagLayout());
051:
052: JPanel p1 = new JPanel(new BorderLayout(0, 5));
053: JLabel l1 = new JLabel(ResourceMgr
054: .getString("LblPostConnectScript"));
055: postConnectEditor = EditorPanel.createSqlEditor();
056: postConnectEditor.setText(profile.getPostConnectScript());
057: Dimension d = new Dimension(200, 200);
058: postConnectEditor.setPreferredSize(d);
059: p1.add(l1, BorderLayout.NORTH);
060: p1.add(postConnectEditor, BorderLayout.CENTER);
061:
062: JPanel p2 = new JPanel(new BorderLayout(0, 5));
063: JLabel l2 = new JLabel(ResourceMgr
064: .getString("LblPreDisconnectScript"));
065: preDisconnectEditor = EditorPanel.createSqlEditor();
066: preDisconnectEditor.setText(profile.getPreDisconnectScript());
067: preDisconnectEditor.setPreferredSize(d);
068: preDisconnectEditor.setCaretVisible(false);
069: p2.add(preDisconnectEditor, BorderLayout.CENTER);
070: p2.add(l2, BorderLayout.NORTH);
071:
072: JPanel p3 = new JPanel(new BorderLayout(0, 5));
073: JLabel l3 = new JLabel(ResourceMgr.getString("LblIdleScript"));
074: keepAliveScriptEditor = EditorPanel.createSqlEditor();
075: keepAliveScriptEditor.setText(profile.getIdleScript());
076: keepAliveScriptEditor.setPreferredSize(d);
077: keepAliveScriptEditor.setCaretVisible(false);
078: p3.add(keepAliveScriptEditor, BorderLayout.CENTER);
079: p3.add(l3, BorderLayout.NORTH);
080: JPanel p4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
081: JLabel time = new JLabel(ResourceMgr
082: .getString("LblIdleScriptTime"));
083: time.setToolTipText(ResourceMgr
084: .getDescription("LblIdleScriptTime"));
085: p4.add(time);
086:
087: keepAliveInterval = new JTextField(8);
088: keepAliveInterval.setText(KeepAliveDaemon
089: .getTimeDisplay(profile.getIdleTime()));
090: p4.add(keepAliveInterval);
091: p3.add(p4, BorderLayout.SOUTH);
092:
093: GridBagConstraints c = new GridBagConstraints();
094: c.gridx = 0;
095: c.gridy = 0;
096: c.weightx = 1.0;
097: c.weighty = 0.5;
098: c.fill = GridBagConstraints.BOTH;
099: this .add(p1, c);
100:
101: c.gridy++;
102: c.insets = new Insets(10, 0, 0, 0);
103: this .add(p2, c);
104:
105: c.gridy++;
106: this .add(p3, c);
107:
108: WbTraversalPolicy pol = new WbTraversalPolicy();
109: pol.addComponent(this .postConnectEditor);
110: pol.addComponent(this .preDisconnectEditor);
111: pol.addComponent(this .keepAliveScriptEditor);
112: pol.addComponent(this .keepAliveInterval);
113: pol.setDefaultComponent(this .postConnectEditor);
114: this .setFocusTraversalPolicy(pol);
115: this .setFocusCycleRoot(false);
116: }
117:
118: public String getKeepAliveScript() {
119: return keepAliveScriptEditor.getText().trim();
120: }
121:
122: private long getIdleTime() {
123: String s = this .keepAliveInterval.getText().trim()
124: .toLowerCase();
125: if (s.length() == 0)
126: return 0;
127: long result = 0;
128:
129: if (s.endsWith("s")) {
130: s = s.substring(0, s.length() - 1);
131: result = StringUtil.getLongValue(s, 0) * 1000;
132: } else if (s.endsWith("m")) {
133: s = s.substring(0, s.length() - 1);
134: result = StringUtil.getLongValue(s, 0) * 1000 * 60;
135: } else {
136: result = StringUtil.getLongValue(s, 0);
137: }
138: return result;
139: }
140:
141: public String getPostConnectScript() {
142: return postConnectEditor.getText().trim();
143: }
144:
145: public String getPreDisconnectScript() {
146: return preDisconnectEditor.getText().trim();
147: }
148:
149: public static boolean editScripts(Dialog owner,
150: ConnectionProfile profile) {
151: EditConnectScriptsPanel p = new EditConnectScriptsPanel(profile);
152: ValidatingDialog d = new ValidatingDialog(owner, ResourceMgr
153: .getString("LblEditConnScripts"), p);
154: boolean hasSize = Settings.getInstance().restoreWindowSize(d,
155: "workbench.gui.connectscripts.window");
156: if (!hasSize) {
157: d.setSize(600, 550);
158: }
159: WbSwingUtilities.center(d, owner);
160: d.setVisible(true);
161: Settings.getInstance().storeWindowSize(d,
162: "workbench.gui.connectscripts.window");
163: if (d.isCancelled())
164: return false;
165: profile.setPreDisconnectScript(p.getPreDisconnectScript());
166: profile.setPostConnectScript(p.getPostConnectScript());
167: profile.setIdleScript(p.getKeepAliveScript());
168: profile.setIdleTime(p.getIdleTime());
169: return true;
170: }
171:
172: public boolean validateInput() {
173: return true;
174: }
175:
176: public void componentDisplayed() {
177: EventQueue.invokeLater(new Runnable() {
178: public void run() {
179: postConnectEditor.requestFocusInWindow();
180: }
181: });
182: }
183: }
|