001: package org.jsqltool.gui;
002:
003: import javax.swing.*;
004: import javax.swing.table.*;
005: import javax.swing.event.*;
006: import java.awt.*;
007: import java.util.*;
008: import java.sql.*;
009: import java.awt.event.*;
010: import org.jsqltool.conn.DbConnectionUtil;
011: import org.jsqltool.conn.DbConnection;
012: import org.jsqltool.gui.panel.*;
013: import org.jsqltool.model.*;
014: import org.jsqltool.MainApp;
015: import org.jsqltool.utils.Options;
016: import org.jsqltool.utils.ImageLoader;
017:
018: /**
019: * <p>Title: JSqlTool Project</p>
020: * <p>Description: Window used to view current opened sessions.
021: * Currently it supports only Oracle databases.
022: * </p>
023: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
024: *
025: * <p> This file is part of JSqlTool project.
026: * This library is free software; you can redistribute it and/or
027: * modify it under the terms of the (LGPL) Lesser General Public
028: * License as published by the Free Software Foundation;
029: *
030: * GNU LESSER GENERAL PUBLIC LICENSE
031: * Version 2.1, February 1999
032: *
033: * This library is distributed in the hope that it will be useful,
034: * but WITHOUT ANY WARRANTY; without even the implied warranty of
035: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036: * Library General Public License for more details.
037: *
038: * You should have received a copy of the GNU Library General Public
039: * License along with this library; if not, write to the Free
040: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
041: *
042: * The author may be contacted at:
043: * maurocarniel@tin.it</p>
044: *
045: * @author Mauro Carniel
046: * @version 1.0
047: */
048: public class TraceSessionFrame extends JInternalFrame implements
049: DbConnWindow, TableModelListener {
050:
051: private DbConnectionUtil dbConnUtil = null;
052: private DataPanel dataPanel = null;
053: private JFrame parent = null;
054: JPanel topPanel = new JPanel();
055: JPanel filterPanel = new JPanel();
056: JPanel buttonsPanel = new JPanel();
057: ImageIcon refreshImage = null;
058: GridLayout gridLayout1 = new GridLayout();
059: JButton refreshButton = new JButton();
060: JLabel filterLabel = new JLabel();
061: JLabel likeLabel = new JLabel();
062: JTextField likeTF = new JTextField();
063: JLabel refreshLabel = new JLabel();
064: JTextField refreshTF = new JTextField();
065: JButton sxButton = new JButton();
066: JButton dxButton = new JButton();
067: JCheckBox autoCheckBox = new JCheckBox();
068: JComboBox filterComboBox = new JComboBox();
069: FlowLayout flowLayout1 = new FlowLayout();
070: FlowLayout flowLayout2 = new FlowLayout();
071: double refreshTime = 20;
072: RefreshThread refreshThread = null;
073: private String select = null;
074: private String from = null;
075: private String where = null;
076: private String ordergroup = null;
077: private String detailQuery = null;
078: private int detailModelIndex = -1;
079: private final String NO_FILTER = "No Filter";
080: JPanel resultPanel = new JPanel();
081: BorderLayout borderLayout1 = new BorderLayout();
082: JScrollPane sp = new JScrollPane();
083: JTextArea resultTA = new JTextArea();
084: JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
085:
086: public TraceSessionFrame(JFrame parent, DbConnectionUtil dbConnUtil) {
087: super (Options.getInstance().getResource("trace sessions")
088: + " - " + dbConnUtil.getDbConnection().getName(), true,
089: true, true, true);
090: this .dbConnUtil = dbConnUtil;
091: this .dataPanel = new DataPanel(dbConnUtil, this );
092: this .parent = parent;
093: init();
094: try {
095: jbInit();
096: refreshThread = new RefreshThread();
097: dataPanel.getTable().setCellSelectionEnabled(false);
098: } catch (Exception e) {
099: e.printStackTrace();
100: }
101: setSize(500, 400);
102: setVisible(true);
103: }
104:
105: public TraceSessionFrame() {
106: this (null, null);
107: try {
108: jbInit();
109: } catch (Exception e) {
110: e.printStackTrace();
111: }
112: }
113:
114: private void init() {
115: if (dbConnUtil.getDbConnection().getDbType() == DbConnection.ORACLE_TYPE) {
116: filterComboBox.addItem(NO_FILTER);
117: filterComboBox.addItem("Oracle User");
118: filterComboBox.addItem("OS User");
119: filterComboBox.addItem("Program");
120: filterComboBox.addItem("Status");
121: // filterComboBox.addItem("Client User");
122: filterComboBox.addItem("Machine");
123: filterComboBox.addItem("Terminal");
124: select = "SELECT s.status \"Status\", s.serial# \"Serial#\", s.TYPE \"Type\","
125: + "s.username \"DB User\", s.osuser \"Client User\", s.server \"Server\","
126: + "s.machine \"Machine\", s.module \"Module\", s.client_info \"Client Info\","
127: + "s.terminal \"Terminal\", s.program \"Program\", p.program \"O.S. Program\","
128: + "s.logon_time \"Connect Time\", lockwait \"Lock Wait\","
129: + "si.physical_reads \"Physical Reads\", si.block_gets \"Block Gets\","
130: + "si.consistent_gets \"Consistent Gets\","
131: + "si.block_changes \"Block Changes\","
132: + "si.consistent_changes \"Consistent Changes\", s.process \"Process\","
133: + "p.spid, p.pid, si.SID, s.audsid, s.sql_address \"Address\","
134: + "s.sql_hash_value \"Sql Hash\", s.action,"
135: + "SYSDATE - (s.last_call_et / 86400) \"Last Call\" ";
136: from = "FROM v$session s, v$process p, SYS.v_$sess_io si ";
137: where = "WHERE s.paddr = p.addr(+) AND si.SID(+) = s.SID ";
138: ordergroup = "ORDER BY 5 DESC";
139:
140: detailQuery = "SELECT sql_text FROM v$sqltext_with_newlines WHERE hash_value = TO_NUMBER (?) ORDER BY piece";
141: detailModelIndex = 25;
142: }
143:
144: dataPanel.getTable().setListSelectionListener(
145: new ListSelectionListener() {
146: public void valueChanged(ListSelectionEvent e) {
147: if (dataPanel.getTable().getSelectedRow() != -1)
148: dataPanel.getTable()
149: .setRowSelectionInterval(
150: dataPanel.getTable()
151: .getSelectedRow(),
152: dataPanel.getTable()
153: .getSelectedRow());
154: if (detailQuery != null
155: && detailModelIndex != -1
156: && dataPanel.getTable()
157: .getSelectedRow() != -1) {
158: String res = "";
159: try {
160: PreparedStatement pstmt = dbConnUtil
161: .getConn().prepareStatement(
162: detailQuery);
163: pstmt
164: .setObject(
165: 1,
166: dataPanel
167: .getTableModel()
168: .getValueAt(
169: dataPanel
170: .getTable()
171: .getSelectedRow(),
172: detailModelIndex));
173: ResultSet rset = pstmt.executeQuery();
174: if (rset.next())
175: res = rset.getString(1);
176: rset.close();
177: pstmt.close();
178: } catch (SQLException ex) {
179: ex.printStackTrace();
180: }
181: resultTA.setText(res);
182: }
183: }
184: });
185:
186: }
187:
188: public DbConnectionUtil getDbConnectionUtil() {
189: return dbConnUtil;
190: }
191:
192: /**
193: * This fine grain notification tells listeners the exact range
194: * of cells, rows, or columns that changed.
195: */
196: public void tableChanged(TableModelEvent e) {
197:
198: }
199:
200: private void jbInit() throws Exception {
201: topPanel.setLayout(gridLayout1);
202: gridLayout1.setColumns(1);
203: gridLayout1.setRows(2);
204: filterLabel
205: .setText(Options.getInstance().getResource("filter"));
206: likeLabel.setText(Options.getInstance().getResource("like"));
207: refreshLabel.setText(Options.getInstance().getResource(
208: "refresh (secs)"));
209: refreshTF.setText(String.valueOf(refreshTime));
210: refreshTF.setColumns(4);
211: refreshTF
212: .addFocusListener(new TraceSessionFrame_refreshTF_focusAdapter(
213: this ));
214: sxButton.setText("<");
215: sxButton
216: .addActionListener(new TraceSessionFrame_sxButton_actionAdapter(
217: this ));
218: // dxButton.setPreferredSize(new Dimension(41, 25));
219: dxButton.setText(">");
220: dxButton
221: .addActionListener(new TraceSessionFrame_dxButton_actionAdapter(
222: this ));
223: autoCheckBox.setSelected(true);
224: autoCheckBox.setText(Options.getInstance().getResource(
225: "auto refresh data"));
226: autoCheckBox
227: .addItemListener(new TraceSessionFrame_autoCheckBox_itemAdapter(
228: this ));
229: refreshButton.setText("");
230: refreshButton
231: .addActionListener(new TraceSessionFrame_refreshButton_actionAdapter(
232: this ));
233: likeTF.setText("");
234: likeTF.setColumns(20);
235: likeTF
236: .addActionListener(new TraceSessionFrame_likeTF_actionAdapter(
237: this ));
238: buttonsPanel.setLayout(flowLayout1);
239: flowLayout1.setAlignment(FlowLayout.LEFT);
240: filterPanel.setLayout(flowLayout2);
241: flowLayout2.setAlignment(FlowLayout.LEFT);
242: resultPanel.setLayout(borderLayout1);
243: resultTA.setEditable(false);
244: resultTA.setText("");
245: resultTA.setColumns(200);
246: resultTA.setRows(80);
247: split.setOrientation(JSplitPane.VERTICAL_SPLIT);
248: split.setBottomComponent(resultPanel);
249: split.setDividerSize(5);
250: split.setTopComponent(dataPanel);
251: this .getContentPane().add(split, BorderLayout.CENTER);
252: this .getContentPane().add(topPanel, BorderLayout.NORTH);
253: topPanel.add(buttonsPanel, null);
254: topPanel.add(filterPanel, null);
255: buttonsPanel.add(refreshButton, null);
256: buttonsPanel.add(refreshLabel, null);
257: buttonsPanel.add(refreshTF, null);
258: buttonsPanel.add(sxButton, null);
259: buttonsPanel.add(dxButton, null);
260: buttonsPanel.add(autoCheckBox, null);
261: filterPanel.add(filterLabel, null);
262: filterPanel.add(filterComboBox, null);
263: filterPanel.add(likeLabel, null);
264: filterPanel.add(likeTF, null);
265: resultPanel.add(sp, BorderLayout.WEST);
266: sp.getViewport().add(resultTA, null);
267: refreshImage = ImageLoader.getInstance()
268: .getIcon("refresh2.gif");
269: refreshButton.setBorder(null);
270: refreshButton.setMaximumSize(new Dimension(24, 24));
271: refreshButton.setPreferredSize(new Dimension(24, 24));
272: refreshButton.setIcon(refreshImage);
273: refreshButton.setToolTipText(Options.getInstance().getResource(
274: "refresh"));
275: split.setDividerLocation(200);
276:
277: }
278:
279: void refreshButton_actionPerformed(ActionEvent e) {
280: if (select != null && from != null && where != null
281: && ordergroup != null) {
282: String filter = "";
283: if (!filterComboBox.getSelectedItem().equals(NO_FILTER)
284: && likeTF.getText().length() > 0) {
285: String fieldName = filterComboBox.getSelectedItem()
286: .toString();
287: if (fieldName.equals("Oracle User"))
288: fieldName = "s.username";
289: else if (fieldName.equals("OS User"))
290: fieldName = "s.osuser";
291: else if (fieldName.equals("Program"))
292: fieldName = "s.program";
293: else if (fieldName.equals("Status"))
294: fieldName = "s.status";
295: else if (fieldName.equals("Machine"))
296: fieldName = "s.machine";
297: else if (fieldName.equals("Terminal"))
298: fieldName = "s.terminal";
299: filter = " AND '" + fieldName + "' like '"
300: + likeTF.getText() + "%' ";
301: }
302: dataPanel.setQuery(select + from + where + filter
303: + ordergroup, new Vector());
304: ((CustomTableModel) dataPanel.getTableModel())
305: .setEditMode(CustomTableModel.DETAIL_REC);
306: }
307: }
308:
309: void sxButton_actionPerformed(ActionEvent e) {
310: try {
311: refreshTime--;
312: if (refreshTime <= 0)
313: refreshTime = 1;
314: refreshTF.setText(String.valueOf(refreshTime));
315: } catch (NumberFormatException ex) {
316: refreshTime = 20;
317: refreshTF.setText(String.valueOf(refreshTime));
318: }
319: }
320:
321: void dxButton_actionPerformed(ActionEvent e) {
322: try {
323: refreshTime++;
324: refreshTF.setText(String.valueOf(refreshTime));
325: } catch (NumberFormatException ex) {
326: refreshTime = 20;
327: refreshTF.setText(String.valueOf(refreshTime));
328: }
329: }
330:
331: void autoCheckBox_itemStateChanged(ItemEvent e) {
332: if (autoCheckBox.isSelected())
333: refreshThread = new RefreshThread();
334: }
335:
336: void likeTF_actionPerformed(ActionEvent e) {
337: refreshButton_actionPerformed(null);
338: }
339:
340: void refreshTF_focusLost(FocusEvent e) {
341: try {
342: refreshTime = Double.valueOf(refreshTF.getText())
343: .doubleValue();
344: if (refreshTime <= 0) {
345: refreshTime = 1;
346: refreshTF.setText(String.valueOf(refreshTime));
347: }
348: } catch (NumberFormatException ex) {
349: refreshTime = 20;
350: refreshTF.setText(String.valueOf(refreshTime));
351: }
352: }
353:
354: /**
355: * esegue un thread per l'aggiornamento automatico del contenuto della griglia,
356: * ogni "refreshTime" secondi.
357: */
358: class RefreshThread extends Thread {
359:
360: public RefreshThread() {
361: if (autoCheckBox.isSelected())
362: start();
363: }
364:
365: public void run() {
366: while (isVisible() && autoCheckBox.isSelected()) {
367: refreshButton_actionPerformed(null);
368: try {
369: this .sleep((int) (refreshTime * 1000));
370: } catch (Exception ex) {
371: }
372: }
373: }
374:
375: }
376:
377: }
378:
379: class TraceSessionFrame_refreshButton_actionAdapter implements
380: java.awt.event.ActionListener {
381: TraceSessionFrame adaptee;
382:
383: TraceSessionFrame_refreshButton_actionAdapter(
384: TraceSessionFrame adaptee) {
385: this .adaptee = adaptee;
386: }
387:
388: public void actionPerformed(ActionEvent e) {
389: adaptee.refreshButton_actionPerformed(e);
390: }
391: }
392:
393: class TraceSessionFrame_sxButton_actionAdapter implements
394: java.awt.event.ActionListener {
395: TraceSessionFrame adaptee;
396:
397: TraceSessionFrame_sxButton_actionAdapter(TraceSessionFrame adaptee) {
398: this .adaptee = adaptee;
399: }
400:
401: public void actionPerformed(ActionEvent e) {
402: adaptee.sxButton_actionPerformed(e);
403: }
404: }
405:
406: class TraceSessionFrame_dxButton_actionAdapter implements
407: java.awt.event.ActionListener {
408: TraceSessionFrame adaptee;
409:
410: TraceSessionFrame_dxButton_actionAdapter(TraceSessionFrame adaptee) {
411: this .adaptee = adaptee;
412: }
413:
414: public void actionPerformed(ActionEvent e) {
415: adaptee.dxButton_actionPerformed(e);
416: }
417: }
418:
419: class TraceSessionFrame_autoCheckBox_itemAdapter implements
420: java.awt.event.ItemListener {
421: TraceSessionFrame adaptee;
422:
423: TraceSessionFrame_autoCheckBox_itemAdapter(TraceSessionFrame adaptee) {
424: this .adaptee = adaptee;
425: }
426:
427: public void itemStateChanged(ItemEvent e) {
428: adaptee.autoCheckBox_itemStateChanged(e);
429: }
430: }
431:
432: class TraceSessionFrame_likeTF_actionAdapter implements
433: java.awt.event.ActionListener {
434: TraceSessionFrame adaptee;
435:
436: TraceSessionFrame_likeTF_actionAdapter(TraceSessionFrame adaptee) {
437: this .adaptee = adaptee;
438: }
439:
440: public void actionPerformed(ActionEvent e) {
441: adaptee.likeTF_actionPerformed(e);
442: }
443: }
444:
445: class TraceSessionFrame_refreshTF_focusAdapter extends
446: java.awt.event.FocusAdapter {
447: TraceSessionFrame adaptee;
448:
449: TraceSessionFrame_refreshTF_focusAdapter(TraceSessionFrame adaptee) {
450: this .adaptee = adaptee;
451: }
452:
453: public void focusLost(FocusEvent e) {
454: adaptee.refreshTF_focusLost(e);
455: }
456: }
|