View Javadoc

1   package com.explosion.datastream.exql.gui.scriptrunner;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.BorderLayout;
24  import java.awt.Color;
25  import java.awt.FlowLayout;
26  import java.awt.Font;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.io.File;
30  import java.sql.Connection;
31  
32  import javax.swing.BorderFactory;
33  import javax.swing.JButton;
34  import javax.swing.JPanel;
35  import javax.swing.JScrollPane;
36  import javax.swing.JSplitPane;
37  import javax.swing.JTextArea;
38  import javax.swing.event.DocumentEvent;
39  import javax.swing.event.DocumentListener;
40  
41  import com.explosion.datastream.exql.EXQLConstants;
42  import com.explosion.datastream.exql.EXQLModuleManager;
43  import com.explosion.datastream.exql.gui.EXQLBaseTool;
44  import com.explosion.datastream.exql.processes.SQLFileProcess;
45  import com.explosion.expfmodules.texteditor.TextEditor;
46  import com.explosion.utilities.exception.ExceptionManagerFactory;
47  import com.explosion.utilities.process.ProcessMonitoringStatusBar;
48  import com.explosion.utilities.process.threads.Finishable;
49  
50  /***
51   * This class provides a nice interface for runnign scripts in.
52   * 
53   * Each script runner appears as a tab in the database window
54   */
55  
56  public class ScriptRunnerBase extends JPanel implements Finishable, DocumentListener
57  {
58      private EXQLBaseTool tool;
59      private ProcessMonitoringStatusBar monitoringStatusBar;
60      
61      private JPanel commandPanel = new JPanel();
62      private TextEditor scriptTextArea;
63      private JTextArea logTextArea;
64      private JButton executeButton;
65      private JButton closeButton;
66      
67      private JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
68      private JPanel sqlPanel = new JPanel();
69      private Connection conn;
70      
71      private SQLFileProcess process;
72      
73      private File file;
74  
75      public ScriptRunnerBase(EXQLBaseTool tool, Connection conn, String connectionName, ProcessMonitoringStatusBar monitoringStatusBar, File file)
76      {
77          try
78          {
79              this.tool = tool;
80              this.conn = conn;
81              this.monitoringStatusBar = monitoringStatusBar;
82              this.file = file;
83              init();
84          }
85          catch (Exception e)
86          {
87              ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while constructing SQlQueryWriterBase");
88          }
89      }
90  
91      private void init() throws Exception
92      {
93          /* Initialise a connect area */
94          executeButton = new JButton("Execute");
95          closeButton = new JButton("Close");
96          JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
97          buttonPanel.add(executeButton);
98          buttonPanel.add(closeButton);
99          
100         executeButton.addActionListener(new ActionListener() {
101 
102             public void actionPerformed(ActionEvent e) {
103                 executeButton_Clicked();
104             }
105         });
106        
107         closeButton.addActionListener(new ActionListener() {
108 
109             public void actionPerformed(ActionEvent e) {
110                 closeButton_Clicked();
111             }
112         });
113         
114         scriptTextArea = new TextEditor(file, tool);
115         scriptTextArea.getTextComponent().getDocument().addDocumentListener(this);
116         
117         JScrollPane logScrollPane = new JScrollPane();
118         logTextArea = new JTextArea();
119         logTextArea.setEditable(false);
120         logScrollPane.getViewport().add(logTextArea);
121         
122         splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
123         splitPane.setLeftComponent(scriptTextArea);
124         splitPane.setRightComponent(logScrollPane);
125         splitPane.setResizeWeight(0.6);
126         
127         sqlPanel.setLayout(new BorderLayout());
128         sqlPanel.add(splitPane, BorderLayout.CENTER);
129         sqlPanel.setBorder(BorderFactory.createEmptyBorder());
130 
131         this.setLayout(new BorderLayout());
132         this.add(buttonPanel, BorderLayout.NORTH);
133         this.add(sqlPanel, BorderLayout.CENTER);
134         this.setBorder(BorderFactory.createEmptyBorder());
135     }
136     
137     /***
138      * displays noneditable results
139      */
140     private void closeButton_Clicked()
141     {
142         try{
143             if (scriptTextArea.close())
144             {
145                 tool.removeScriptRunner(this);
146             }
147         } catch (Exception e) {
148             ExceptionManagerFactory.getExceptionManager().manageException(e,"Caught exception while closing script runner");
149         }
150     }
151     
152    /***
153      * displays noneditable results
154      */
155     private void executeButton_Clicked()
156     {
157         if (conn == null)
158         {
159             ExceptionManagerFactory.getExceptionManager().manageException(new java.sql.SQLException("Not connected"), "Exception caught while running script: Not connected to a database.");
160             return;
161         }
162 
163         try
164         {
165             logTextArea.setText("");
166         	tool.disableComponent();
167 
168             /* Execute the query */
169             process = new SQLFileProcess( this, conn, scriptTextArea.getTextComponent().getText(), tool, logTextArea);
170             monitoringStatusBar.setProcessToMonitor(process);
171             monitoringStatusBar.setProgressBarEnabled(false);
172             try
173             {
174                 monitoringStatusBar.startProcess();
175             }
176             catch (Exception ex)
177             {
178                 ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while fetching tables.");
179                 tool.enableComponent();
180             }
181         }
182         catch (Exception ex)
183         {
184             ExceptionManagerFactory.getExceptionManager().manageException(ex, "Experienced exception while populating view of table data.");
185             tool.enableComponent();
186         }
187     }
188 
189     public void finish()
190     {
191         try
192         {
193             tool.enableComponent();
194         }
195         catch (Exception ex)
196         {
197             ExceptionManagerFactory.getExceptionManager().manageException(ex, "Experienced exception while running script.");
198         }
199     }
200     
201     /***
202      * Code to support document Listener
203      */
204     public void changedUpdate(DocumentEvent e)
205     {
206     	reTitle();
207     }
208     
209     /***
210      * Code to support document Listener
211      */
212     public void insertUpdate(DocumentEvent e)
213     {
214     	reTitle();
215     }
216 
217     /***
218      * Code to support document Listener
219      */
220     public void removeUpdate(DocumentEvent e)
221     {
222     	reTitle();
223     }
224 
225     private void reTitle()
226     {
227     	try {
228 			if (scriptTextArea.isDirty())
229 			    tool.renameScriptRunner(this,scriptTextArea.getDocument().getDocumentName() + "*");
230 			 else
231 			 	 tool.renameScriptRunner(this,scriptTextArea.getDocument().getDocumentName());
232 		} catch (Exception e) {
233 			ExceptionManagerFactory.getExceptionManager().manageException(e,"Exception caught while renaming script title");
234 		}
235     }
236     
237     /***
238      * Returns the connection from this toool
239      */
240     public Connection getConnection()
241     {
242         return this.conn;
243     }
244 
245     public void disableComponent()
246     {
247         scriptTextArea.setEnabled(false);
248         logTextArea.setEnabled(false);
249         executeButton.setEnabled(false);
250         closeButton.setEnabled(false);
251     }
252 
253     public void enableComponent()
254     {
255         scriptTextArea.setEnabled(true);
256         logTextArea.setEnabled(true);
257         executeButton.setEnabled(true);
258         closeButton.setEnabled(true);
259     }
260 
261     /***
262      * Apply preferences
263      */
264     public void applyPreferences()
265     {
266         /* This allows the user to change mode between command editing and command browsing */
267         //scriptTextArea.applyPreferences();
268         scriptTextArea.getTextComponent().setForeground((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_EDIT_MODE_FOREGROUND_COLOR).getValue());
269         scriptTextArea.getTextComponent().setBackground((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_EDIT_MODE_BACKGROUND_COLOR).getValue());
270         scriptTextArea.getTextComponent().setCaretColor((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_EDIT_MODE_FOREGROUND_COLOR).getValue());
271         scriptTextArea.getTextComponent().setSelectedTextColor((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_COLOR_SELECTEDFORGROUND).getValue());
272         scriptTextArea.getTextComponent().setSelectionColor((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_COLOR_SELECTEDBACKGROUND).getValue());
273         scriptTextArea.getTextComponent().setFont((Font) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_FONT).getValue());
274         
275         logTextArea.setForeground((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_EDIT_MODE_FOREGROUND_COLOR).getValue());
276         logTextArea.setBackground((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_EDIT_MODE_BACKGROUND_COLOR).getValue());
277         logTextArea.setCaretColor((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_EDIT_MODE_FOREGROUND_COLOR).getValue());
278         logTextArea.setSelectedTextColor((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_COLOR_SELECTEDFORGROUND).getValue());
279         logTextArea.setSelectionColor((Color) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_COLOR_SELECTEDBACKGROUND).getValue());
280         logTextArea.setFont((Font) EXQLModuleManager.instance().getPreference(EXQLConstants.COMMANDER_FONT).getValue());
281     }
282 }