001: package net.sourceforge.squirrel_sql.plugins.mssql.gui;
002:
003: /*
004: * Copyright (C) 2004 Ryan Walberg <generalpf@yahoo.com>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.awt.GridBagConstraints;
022: import java.awt.GridBagLayout;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.sql.CallableStatement;
026: import java.sql.Connection;
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.util.Date;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.JButton;
033: import javax.swing.JPanel;
034: import javax.swing.JScrollPane;
035: import javax.swing.JSlider;
036: import javax.swing.Timer;
037: import javax.swing.event.ChangeEvent;
038: import javax.swing.event.ChangeListener;
039:
040: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
041: import net.sourceforge.squirrel_sql.fw.datasetviewer.BaseDataSetViewerDestination;
042: import net.sourceforge.squirrel_sql.fw.datasetviewer.DataSetException;
043: import net.sourceforge.squirrel_sql.fw.datasetviewer.IDataSetViewer;
044: import net.sourceforge.squirrel_sql.fw.datasetviewer.ResultSetDataSet;
045: import net.sourceforge.squirrel_sql.fw.util.StringManager;
046: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
047: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
048: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
049:
050: public class MonitorPanel
051: extends
052: net.sourceforge.squirrel_sql.client.session.mainpanel.BaseMainPanelTab {
053:
054: private static final StringManager s_stringMgr = StringManagerFactory
055: .getStringManager(MonitorPanel.class);
056:
057: /** Logger for this class. */
058: private final static ILogger s_log = LoggerController
059: .createLogger(MonitorPanel.class);
060:
061: private Connection _conn = null;
062:
063: private Date _refreshDate;
064: private JPanel _mainPanel;
065:
066: private JSlider _frequency = null;
067:
068: private IDataSetViewer _whoViewer;
069: private CallableStatement _whoStmt = null;
070: private ResultSetDataSet _whoDataSet = null;
071:
072: private IDataSetViewer _perfViewer;
073: private PreparedStatement _perfStmt = null;
074: private ResultSetDataSet _perfDataSet = null;
075:
076: private CallableStatement _monitorStmt = null;
077:
078: private Timer _refreshTimer = null;
079:
080: private boolean _haveSession = false;
081:
082: private boolean _inRefresh = false;
083:
084: /** Creates a new instance of MonitorPanel */
085: public MonitorPanel() {
086: super ();
087: }
088:
089: public java.awt.Component getComponent() {
090: if (_mainPanel == null) {
091: _mainPanel = buildMainPanel();
092:
093: // create the timer, but DO NOT start it.
094: _refreshTimer = new Timer(_frequency.getValue(),
095: new ActionListener() {
096: public void actionPerformed(ActionEvent e) {
097: if (!_haveSession)
098: return;
099: if (_inRefresh)
100: return;
101: _inRefresh = true;
102: refreshData();
103: _inRefresh = false;
104: }
105: });
106: }
107: return _mainPanel;
108: }
109:
110: public String getHint() {
111: // i18n[mssql.activity=Displays the current activity on the SQL Server.]
112: return s_stringMgr.getString("mssql.activity");
113: }
114:
115: public String getTitle() {
116: if (_refreshDate == null)
117: // i18n[mssql.monitor=Monitor]
118: return s_stringMgr.getString("mssql.monitor");
119: else {
120: // i18n[mssql.monitorAsOf=Monitor (as of {0,date,full})]
121: return s_stringMgr.getString("mssql.monitorAsOf",
122: _refreshDate);
123: }
124: }
125:
126: protected void refreshComponent() {
127: if (!_haveSession) {
128: try {
129: _conn = this .getSession().getSQLConnection()
130: .getConnection();
131:
132: _whoStmt = _conn.prepareCall("{ call sp_who }");
133: _whoDataSet = new ResultSetDataSet();
134:
135: _perfStmt = _conn
136: .prepareStatement("SELECT * FROM master.dbo.sysperfinfo");
137: _perfDataSet = new ResultSetDataSet();
138:
139: _monitorStmt = _conn.prepareCall("{ call sp_monitor }");
140:
141: _haveSession = true;
142: } catch (java.sql.SQLException ex) {
143: s_log.error("Unexpected exception: " + ex.getMessage(),
144: ex);
145: }
146: }
147: refreshData();
148: }
149:
150: private void refreshData() {
151: ResultSet rs;
152:
153: try {
154: _refreshDate = new Date();
155:
156: rs = _whoStmt.executeQuery();
157: _whoDataSet.setResultSet(rs);
158: _whoViewer.show(_whoDataSet);
159:
160: rs = _perfStmt.executeQuery();
161: _perfDataSet.setResultSet(rs);
162: _perfViewer.show(_perfDataSet);
163: } catch (java.sql.SQLException ex) {
164: s_log.error("Unexpected exception: " + ex.getMessage(), ex);
165: } catch (DataSetException dse) {
166: s_log.error("Unexpected exception: " + dse.getMessage(),
167: dse);
168: }
169: }
170:
171: private JPanel buildMainPanel() {
172: SessionProperties props = this .getSession().getProperties();
173:
174: JPanel panel = new JPanel();
175: GridBagLayout gridBag = new GridBagLayout();
176: panel.setLayout(gridBag);
177:
178: _frequency = new JSlider();
179: _frequency.setMinimum(0);
180: _frequency.setMaximum(20);
181: _frequency.setValue(0); // by default, it is not running.
182: _frequency.setMajorTickSpacing(2);
183: _frequency.setMinorTickSpacing(1);
184: _frequency.setPaintLabels(true);
185: _frequency.setPaintTicks(true);
186: _frequency.setSnapToTicks(true);
187: _frequency.addChangeListener(new ChangeListener() {
188: public void stateChanged(ChangeEvent e) {
189: JSlider slider = (JSlider) e.getSource();
190: if (slider.getValue() == 0) {
191: // don't bother changing the delay, but stop it if it's running.
192: if (_refreshTimer.isRunning())
193: _refreshTimer.stop();
194: } else {
195: // change the delay, and start it if it was stopped.
196: _refreshTimer.setDelay(slider.getValue() * 1000);
197: if (!_refreshTimer.isRunning())
198: _refreshTimer.start();
199: }
200: // i18n[mssql.delay={0}s delay]
201: slider.setToolTipText(s_stringMgr.getString(
202: "mssql.delay", Integer.valueOf(slider
203: .getValue())));
204: }
205: });
206: addComponentToGridBag(0, 0, 1, 1, 0.0, 0.0,
207: GridBagConstraints.BOTH, gridBag, _frequency, panel);
208:
209: // i18n[mssql.refreshNow=Refresh Now]
210: JButton refreshButton = new JButton(s_stringMgr
211: .getString("mssql.refreshNow"));
212: refreshButton.addActionListener(new ActionListener() {
213: public void actionPerformed(ActionEvent e) {
214: if (!_haveSession)
215: return;
216: if (_inRefresh)
217: return;
218: _inRefresh = true;
219: refreshData();
220: _inRefresh = false;
221: }
222: });
223: addComponentToGridBag(GridBagConstraints.RELATIVE, 0, 1, 1,
224: 0.0, 0.0, GridBagConstraints.NONE, gridBag,
225: refreshButton, panel);
226:
227: _whoViewer = BaseDataSetViewerDestination.getInstance(props
228: .getReadOnlySQLResultsOutputClassName(), null);
229: JScrollPane whoScroll = new JScrollPane(_whoViewer
230: .getComponent());
231: // i18n[mssql.currentActivity=Current Activity]
232: whoScroll.setBorder(BorderFactory
233: .createTitledBorder(s_stringMgr
234: .getString("mssql.currentActivity")));
235: addComponentToGridBag(0, 1, 1, 1, 1.0, 1.0,
236: GridBagConstraints.BOTH, gridBag, whoScroll, panel);
237:
238: _perfViewer = BaseDataSetViewerDestination.getInstance(props
239: .getReadOnlySQLResultsOutputClassName(), null);
240: JScrollPane perfScroll = new JScrollPane(_perfViewer
241: .getComponent());
242: // i18n[mssql.performace=Performance Counters]
243: perfScroll.setBorder(BorderFactory
244: .createTitledBorder(s_stringMgr
245: .getString("mssql.performace")));
246: addComponentToGridBag(GridBagConstraints.RELATIVE, 1, 1, 1,
247: 1.0, 1.0, GridBagConstraints.BOTH, gridBag, perfScroll,
248: panel);
249:
250: return panel;
251: }
252:
253: private void addComponentToGridBag(int gridx, int gridy,
254: int gridwidth, int gridheight, double weightx,
255: double weighty, int fill, GridBagLayout gridBag,
256: java.awt.Component component, java.awt.Container container) {
257: GridBagConstraints c = new GridBagConstraints();
258: c.gridx = gridx;
259: c.gridy = gridy;
260: c.gridwidth = gridwidth;
261: c.gridheight = gridheight;
262: c.fill = fill;
263: c.weightx = weightx;
264: c.weighty = weighty;
265: gridBag.setConstraints(component, c);
266: container.add(component);
267: }
268: }
|