View Javadoc
1 package net.plugin.sql.gui;
2
3 /*
4 * Created by IntelliJ IDEA.
5 * User: birchfield
6 * Date: Aug 18, 2002
7 * Time: 2:12:56 PM
8 * To change template for new class use
9 * Code Style | Class Templates options (Tools | IDE Options).
10 */
11
12 import net.plugin.sql.beans.DataSource;
13 import net.plugin.sql.listeners.QueryListener;
14 import net.plugin.sql.util.*;
15
16 import javax.swing.*;
17 import javax.swing.event.InternalFrameListener;
18 import javax.swing.event.InternalFrameEvent;
19 import java.awt.BorderLayout;
20 import java.awt.event.*;
21 import java.sql.*;
22
23 import sun.jdbc.rowset.CachedRowSet;
24
25 public class SQLQueryFrame extends JInternalFrame implements KeyListener, ActionListener, QueryListener, InternalFrameListener {
26
27 private JSplitPane splitPane = null;
28
29 private SQLDataTablePanel tablePanel = null;
30 private QueryHistoryFrame historyFrame = null;
31
32 private DataSource dataSource = null;
33
34 private JPanel inputPanel = null;
35 private JPanel masterPanel = null;
36 private JLabel sqlLabel = null;
37 private JTextArea text = null;
38 private JButton button = null;
39
40 private JScrollPane textPane = null;
41 private JScrollPane tablePane = null;
42
43 private JMenuBar menuBar = null;
44 private JMenu menu = null;
45 private JMenuItem queryHistoryItem = null;
46
47
48 public SQLQueryFrame(DataSource dataSource) {
49 super(dataSource.getName(), true, true, true, true);
50 getContentPane().setLayout(new BorderLayout());
51 this.dataSource = dataSource;
52
53 menuBar = new JMenuBar();
54 menu = new JMenu("SQL");
55 queryHistoryItem = new JMenuItem("Query History");
56 queryHistoryItem.addActionListener(this);
57 menuBar.add(menu);
58 menu.add(queryHistoryItem);
59 getContentPane().add(menuBar, BorderLayout.NORTH);
60
61 inputPanel = new JPanel();
62 masterPanel = new JPanel(new BorderLayout());
63 sqlLabel = new JLabel("Enter SQL Command:");
64 text = new JTextArea();
65 button = new JButton("Run");
66 button.addActionListener(this);
67 inputPanel.add(sqlLabel);
68 inputPanel.add(button);
69 textPane = new JScrollPane(text);
70 masterPanel.add(inputPanel, BorderLayout.NORTH);
71 masterPanel.add(textPane, BorderLayout.CENTER);
72 tablePanel = new SQLDataTablePanel(dataSource);
73
74 tablePane = new JScrollPane(tablePanel);
75 text.addKeyListener(this);
76
77 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, masterPanel, tablePane);
78 splitPane.setDividerLocation(150);
79 getContentPane().add(splitPane);
80 addInternalFrameListener(this);
81 }
82
83 public void queryPerformed(String query) {
84 text.setText(query);
85 }
86
87
88 public void actionPerformed(ActionEvent e) {
89 if (e.getSource().equals(button)) {
90 highlightText();
91 performQuery();
92 } else {
93 if (historyFrame == null) {
94 historyFrame = new QueryHistoryFrame(dataSource.getName());
95 historyFrame.setListener(this);
96 getParent().add(historyFrame);
97 historyFrame.setSize(300, 300);
98 historyFrame.setVisible(true);
99 historyFrame.addInternalFrameListener(this);
100 }
101 }
102
103 }
104
105 private void performQuery() {
106 String sql = text.getText();
107 if (sql == null || sql.length() < 1 || sql.equals("")) {
108 JOptionPane.showMessageDialog(null, "You must type something in the query window.", "Error", JOptionPane.ERROR_MESSAGE);
109 return;
110 }
111 Connection con = null;
112 Statement stmt = null;
113 ResultSet rs = null;
114 CachedRowSet crs = null;
115
116 try {
117 JDBCConnectionManager manager = new JDBCConnectionManager(dataSource);
118 con = manager.getConnection();
119 stmt = con.createStatement();
120 stmt.execute(text.getText());
121 if (stmt.getResultSet() != null) {
122 crs = new CachedRowSet();
123 crs.populate(stmt.getResultSet());
124 tablePanel.setRowSet(crs);
125 } else {
126 int uc = stmt.getUpdateCount();
127 JOptionPane.showMessageDialog(null, uc + " rows were updated.", "Results", JOptionPane.INFORMATION_MESSAGE);
128 }
129 QueryManager.getInstance().addQuery(text.getText());
130 } catch (DataSourceException e1) {
131 JOptionPane.showMessageDialog(null, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
132 } catch (SQLException e1) {
133 JOptionPane.showMessageDialog(null, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
134 } finally {
135 try {
136 if (rs != null) {
137 rs.close();
138 }
139 if (stmt != null) {
140 stmt.close();
141 }
142 if (con != null) {
143 con.close();
144 }
145 } catch (SQLException e1) {
146 e1.printStackTrace();
147 }
148 }
149 }
150
151 private void highlightText() {
152 System.out.println("SQLQueryFrame.highlightText");
153 text.requestFocus();
154 text.setCaretPosition(0);
155 text.moveCaretPosition(text.getText().length());
156 }
157
158 public void keyTyped(KeyEvent e) {
159
160 }
161
162 public void keyPressed(KeyEvent e) {
163 if (e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() == KeyEvent.CTRL_MASK) {
164 highlightText();
165 performQuery();
166 }
167 }
168
169 public void keyReleased(KeyEvent e) {
170 }
171
172 public void internalFrameOpened(InternalFrameEvent e) {
173 }
174
175 public void internalFrameClosing(InternalFrameEvent e) {
176 if (historyFrame != null) {
177 historyFrame.setVisible(false);
178 historyFrame.dispose();
179 historyFrame = null;
180 }
181 }
182
183 public void internalFrameClosed(InternalFrameEvent e) {
184 }
185
186 public void internalFrameIconified(InternalFrameEvent e) {
187 }
188
189 public void internalFrameDeiconified(InternalFrameEvent e) {
190 }
191
192 public void internalFrameActivated(InternalFrameEvent e) {
193 }
194
195 public void internalFrameDeactivated(InternalFrameEvent e) {
196 }
197
198
199 public static void main(String[] args) {
200 JFrame frame = new JFrame("Test");
201 JDesktopPane pane = new JDesktopPane();
202 QueryManager.getInstance().addQuery("select * from plant");
203 QueryManager.getInstance().addQuery("select * from plant");
204 QueryManager.getInstance().addQuery("select * from plant");
205 QueryManager.getInstance().addQuery("select * from plant");
206 QueryManager.getInstance().addQuery("select * from plant1");
207 QueryManager.getInstance().addQuery("select * from plant12");
208 QueryManager.getInstance().addQuery("select * from\n plant123");
209 QueryManager.getInstance().addQuery("select * from plant1234");
210 DataSource ds = new DataSource();
211 ds.setName("Test");
212 ds.setDriver("oracle.jdbc.driver.OracleDriver");
213 ds.setUrl("jdbc:oracle:thin:@hades.genscape.com:1581:test");
214 ds.setUser("genscape");
215 ds.setPassword("gentest");
216 SQLQueryFrame frme = new SQLQueryFrame(ds);
217 frme.setSize(500, 300);
218 frme.setVisible(true);
219 pane.add(frme);
220 frame.getContentPane().add(pane);
221 frame.setSize(600, 500);
222 frame.show();
223 }
224
225
226 }
This page was automatically generated by Maven