1 package com.explosion.datastream.exql.gui.querywriter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.BorderLayout;
24 import java.io.File;
25 import java.sql.Connection;
26
27 import javax.swing.BorderFactory;
28 import javax.swing.JPanel;
29 import javax.swing.JSplitPane;
30 import javax.swing.JTabbedPane;
31
32 import com.explosion.datastream.exql.gui.EXQLBaseTool;
33 import com.explosion.datastream.exql.gui.dbbrowser.TabularDataView;
34 import com.explosion.datastream.exql.processes.QueryProcess;
35 import com.explosion.datastream.exql.processes.SQLFileProcess;
36 import com.explosion.expfmodules.rdbmsconn.dbom.DataSet;
37 import com.explosion.expfmodules.rdbmsconn.dbom.EntityMetaData;
38 import com.explosion.expfmodules.texteditor.ExpfTextArea;
39 import com.explosion.utilities.GeneralConstants;
40 import com.explosion.utilities.exception.ExceptionManagerFactory;
41 import com.explosion.utilities.process.ProcessMonitoringStatusBar;
42 import com.explosion.utilities.process.threads.Finishable;
43 import com.explosion.utilities.process.threads.SimpleProcess;
44
45 /***
46 * This class provides an extremely simple database interface.
47 */
48
49 public class SQlQueryWriterBase extends JPanel implements Finishable
50 {
51
52 private JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
53 private JPanel sqlPanel = new JPanel();
54
55 private SQLCommandController commandPanel;
56 private Connection conn;
57
58 private TabularDataView viewer;
59 private ExpfTextArea errors;
60 private ProcessMonitoringStatusBar monitoringStatusBar;
61 private SimpleProcess process;
62 private EXQLBaseTool tool;
63 private JTabbedPane resultsAndErrosPane;
64 private String connectionName;
65
66 public SQlQueryWriterBase(EXQLBaseTool tool, Connection conn, String connectionName, ProcessMonitoringStatusBar monitoringStatusBar)
67 {
68 try
69 {
70 this.tool = tool;
71 this.conn = conn;
72 this.monitoringStatusBar = monitoringStatusBar;
73 this.connectionName = connectionName;
74 this.viewer = new TabularDataView(tool, false);
75 init();
76 }
77 catch (Exception e)
78 {
79 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while constructing SQlQueryWriterBase");
80 }
81 }
82
83 private void init() throws Exception
84 {
85
86 commandPanel = new SQLCommandController(conn, connectionName, tool);
87
88 resultsAndErrosPane = new JTabbedPane();
89 errors = new ExpfTextArea(this);
90 resultsAndErrosPane.addTab("Results", viewer);
91 resultsAndErrosPane.addTab("Messages", errors);
92
93 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
94 splitPane.setLeftComponent(commandPanel);
95 splitPane.setRightComponent(resultsAndErrosPane);
96 splitPane.setResizeWeight(0.6);
97
98 sqlPanel.setLayout(new BorderLayout());
99 sqlPanel.add(splitPane, BorderLayout.CENTER);
100 sqlPanel.setBorder(BorderFactory.createEmptyBorder());
101
102 this.setLayout(new BorderLayout());
103 this.add(sqlPanel, BorderLayout.CENTER);
104 this.setBorder(BorderFactory.createEmptyBorder());
105 }
106
107 /***
108 * displays noneditable results
109 */
110 public void executeCommand(String command)
111 {
112 if (conn == null)
113 {
114 ExceptionManagerFactory.getExceptionManager().manageException(new java.sql.SQLException("Not connected"), "Exception caught while executing query: Not connected to a database.");
115 return;
116 }
117
118 try
119 {
120 tool.disableComponent();
121
122
123 process = new QueryProcess(this, conn, command, false, tool);
124 monitoringStatusBar.setProcessToMonitor(process);
125 monitoringStatusBar.setProgressBarEnabled(false);
126 try
127 {
128 monitoringStatusBar.startProcess();
129 }
130 catch (Exception ex)
131 {
132 ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while initialising command execution.");
133 tool.enableComponent();
134 }
135 }
136 catch (Exception ex)
137 {
138 ExceptionManagerFactory.getExceptionManager().manageException(ex, "Experienced exception while populating view of table data.");
139 viewer.show_NoInfo_Message();
140 tool.enableComponent();
141 }
142 }
143
144 /***
145 * displays noneditable results
146 */
147 public void executeScript(File file)
148 {
149 if (conn == null)
150 {
151 ExceptionManagerFactory.getExceptionManager().manageException(new java.sql.SQLException("Not connected"), "Exception caught while running script: Not connected to a database.");
152 return;
153 }
154
155 try
156 {
157 tool.disableComponent();
158
159
160 process = new SQLFileProcess(this, conn, file, tool);
161 monitoringStatusBar.setProcessToMonitor(process);
162 monitoringStatusBar.setProgressBarEnabled(false);
163 try
164 {
165 monitoringStatusBar.startProcess();
166 }
167 catch (Exception ex)
168 {
169 ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while fetching tables.");
170 tool.enableComponent();
171 }
172 }
173 catch (Exception ex)
174 {
175 ExceptionManagerFactory.getExceptionManager().manageException(ex, "Experienced exception while populating view of table data.");
176 viewer.show_NoInfo_Message();
177 tool.enableComponent();
178 }
179 }
180
181 public void finish()
182 {
183 try
184 {
185 if (process instanceof QueryProcess)
186 {
187 DataSet set = ((QueryProcess) process).getSet();
188
189 if (set == null)
190 {
191 viewer.clear();
192 errors.clear();
193 tool.log("No results. Unexpected error occurred");
194 resultsAndErrosPane.setSelectedIndex(1);
195 }
196 else if (set.getTableModel().getRowCount() < 1)
197 {
198 viewer.clear();
199 errors.clear();
200 errors.append(set.getMessage() + GeneralConstants.LS);
201 tool.log(set.getMessage());
202
203 resultsAndErrosPane.setSelectedIndex(1);
204 }
205 else
206 {
207 EntityMetaData metaData = new EntityMetaData();
208 metaData.setDataSet(set);
209 tool.setCurrentEntityMetaData(metaData);
210
211 viewer.update(tool);
212 errors.clear();
213 errors.append(set.getMessage() + GeneralConstants.LS);
214 tool.log(set.getMessage());
215
216 resultsAndErrosPane.setSelectedIndex(0);
217 }
218 }
219
220 monitoringStatusBar.updateStatus();
221
222 tool.enableComponent();
223 commandPanel.getCommandTextComponent().requestFocus();
224
225 }
226 catch (Exception ex)
227 {
228 ExceptionManagerFactory.getExceptionManager().manageException(ex, "Experienced exception while populating view of table data.");
229 tool.enableComponent();
230 }
231 }
232
233 /***
234 * Returns the connection fromthistoool
235 */
236 public Connection getConnection()
237 {
238 return this.conn;
239 }
240
241 /***
242 * Clear up all of the windows etc.
243 */
244 public void clear() throws Exception
245 {
246 viewer.clear();
247 commandPanel.clear();
248 }
249
250 public void disableComponent()
251 {
252 viewer.getDataTable().setEnabled(false);
253 commandPanel.disableComponent();
254 }
255
256 public void enableComponent()
257 {
258 viewer.getDataTable().setEnabled(true);
259 commandPanel.enableComponent();
260 }
261
262 /***
263 * Apply preferences
264 */
265 public void applyPreferences()
266 {
267 viewer.applyPreferences();
268 commandPanel.applyPreferences();
269 }
270
271 public TabularDataView getView()
272 {
273 return this.viewer;
274 }
275
276 /***
277 *
278 */
279 public void close()
280 {
281 this.commandPanel.persistCommandHistory();
282 }
283
284 }