View Javadoc

1   package com.explosion.datastream.exql.gui;
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.Dimension;
25  import java.awt.GridBagConstraints;
26  import java.awt.GridBagLayout;
27  import java.awt.Insets;
28  import java.awt.event.ActionEvent;
29  
30  import javax.swing.BorderFactory;
31  import javax.swing.JButton;
32  import javax.swing.JLabel;
33  import javax.swing.JPanel;
34  import javax.swing.SwingUtilities;
35  
36  import org.apache.log4j.LogManager;
37  import org.apache.log4j.Logger;
38  
39  import com.explosion.datastream.exql.EXQLModuleManager;
40  import com.explosion.datastream.exql.processes.ConnectProcess;
41  import com.explosion.expf.Application;
42  import com.explosion.expf.ExpConstants;
43  import com.explosion.expf.ExpFrame;
44  import com.explosion.expf.ExpInternalFrame;
45  import com.explosion.expfmodules.rdbmsconn.connect.ConnectionManager;
46  import com.explosion.utilities.exception.ExceptionManagerFactory;
47  import com.explosion.utilities.preferences.groups.PreferenceGroup;
48  import com.explosion.utilities.process.threads.Finishable;
49  import com.explosion.utilities.process.threads.ProcessThread;
50  import com.explosion.utilities.process.threads.SimpleProcess;
51  import com.explosion.utilities.process.threads.SimpleProcessThread;
52  
53  /***
54   * This class is a dialog box that provides buttons for a user to cancel a
55   * connect. It also kicks off an EXQL Tool if the connection is succesful
56   * 
57   * @author Stephen Created on May 3, 2004
58   */
59  public class ConnectPanel extends JPanel implements Finishable
60  {
61  
62      private static Logger log = LogManager.getLogger(ConnectPanel.class);
63      private ConnectStatusBar statusBar;
64  
65      private ConnectProcess process;
66      private boolean cancelled = false;
67  
68      public ConnectPanel()
69      {
70          init();
71      }
72  
73      private void init()
74      {
75          statusBar = new ConnectStatusBar(this);
76  
77          this.setLayout(new BorderLayout());
78          this.add(statusBar, BorderLayout.CENTER);
79      }
80  
81      /***
82       * Starts the connection process
83       *  
84       */
85      public void connect(PreferenceGroup descriptor)
86      {
87          try
88          {
89              log.debug("connect");
90              process = new ConnectProcess(this, descriptor);
91              statusBar.setProcessToMonitor(process);
92              statusBar.startProcess();
93  
94          }
95          catch (Exception e)
96          {
97              ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while initialising SystemMonitoringProcess");
98          }
99      }
100 
101     /***
102      * Creates an EXQL Base tool and hands it the connection ey as well as the
103      * connection Manager
104      * 
105      * @see com.explosion.utilities.process.threads.Finishable#finish()
106      *  
107      */
108     public void finish()
109     {
110         log.debug("finish");
111         try
112         {
113             int connectionKey = process.getConnectionIdentifier();
114             if (connectionKey >= 0)
115             {
116                 /* Make it invisible */
117                 try
118                 {
119                     ((ExpFrame) Application.getApplicationFrame()).getFrameWithComponent(this, ExpFrame.PALETTE_LAYER.intValue()).setVisible(false);
120                 }
121                 catch (Exception e1)
122                 {
123                 }
124 
125                 /* Instantiate the tool */
126                 int lastHeight = ((Integer) EXQLModuleManager.instance().getPreference(ExpConstants.HEIGHT).getValue()).intValue();
127                 int lastWidth = ((Integer) EXQLModuleManager.instance().getPreference(ExpConstants.WIDTH).getValue()).intValue();
128 
129                 EXQLBaseTool tool = new EXQLBaseTool(connectionKey, ConnectionManager.getInstance());
130                 ExpInternalFrame frame = ((ExpFrame) Application.getApplicationFrame()).createDocumentFrame(tool, new Dimension(lastWidth, lastHeight), (connectionKey + 1) + " : "
131                         + ConnectionManager.getInstance().getConnectionDescriptor(connectionKey).getIdentifier(), true);
132                 frame.addSizePersistence(EXQLModuleManager.instance().getPreference(ExpConstants.HEIGHT), EXQLModuleManager.instance().getPreference(ExpConstants.WIDTH));
133                 frame.addToolbar(tool.getToolBar());
134                 tool.refreshTool();
135 
136             }
137         }
138         catch (Exception e)
139         {
140             /* Make it invisible */
141             try
142             {
143                 ((ExpFrame) Application.getApplicationFrame()).getFrameWithComponent(this, ExpFrame.PALETTE_LAYER.intValue()).setVisible(false);
144             }
145             catch (Exception e1)
146             {
147             }
148             /* Report the error */
149             ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while createing the EXQL tool.");
150 
151         }
152 
153         try
154         {
155 
156             ((ExpFrame) Application.getApplicationFrame()).closeFrameWithComponent(this, ExpFrame.PALETTE_LAYER);
157         }
158         catch (Exception e1)
159         {//ignore it
160         }
161     }
162 
163     /***
164      * @return Returns the cancelled.
165      */
166     public boolean isCancelled()
167     {
168         return cancelled;
169     }
170 
171     /***
172      * @param cancelled The cancelled to set.
173      */
174     public void cancel()
175     {
176         process.setPercentComplete(90);
177         process.setStatusText("Cancelling connect.");
178         this.setVisible(false);
179         this.cancelled = true;
180     }
181 }
182 
183 class ConnectStatusBar extends JPanel implements SimpleProcess
184 {
185 
186     private JButton cancelButton = new JButton("Cancel");
187 
188     private JPanel buttonPanel = new JPanel();
189 
190     private JPanel progressPanel = new JPanel();
191 
192     private JLabel statusLabel = new JLabel();
193 
194     private ProcessThread processThread;
195 
196     private ProcessThread workerThread;
197     private ConnectPanel connectPanel;
198     
199     private boolean isUserProcess = false;
200     
201     public ConnectStatusBar(ConnectPanel connectPanel)
202     {
203         try
204         {
205             this.connectPanel = connectPanel;
206             init();
207         }
208         catch (Exception e)
209         {
210             ExceptionManagerFactory.getExceptionManager().manageException(e, null);
211         }
212     }
213 
214     public void init() throws Exception
215     {
216         buttonPanel.setLayout(new GridBagLayout());
217         buttonPanel.add(cancelButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
218 
219         progressPanel.setLayout(new GridBagLayout());
220         progressPanel.add(statusLabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 3, 0, 0), 0, 0));
221 
222         cancelButton.addActionListener(new java.awt.event.ActionListener()
223         {
224 
225             public void actionPerformed(ActionEvent e)
226             {
227                 cancelButton_actionPerformed(e);
228             }
229         });
230 
231         this.setLayout(new GridBagLayout());
232         this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Status"));
233         this.add(progressPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
234         this.add(buttonPanel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
235     }
236 
237     public void setProcessToMonitor(com.explosion.utilities.process.threads.Process processToMonitor) throws Exception
238     {
239         if (this.workerThread != null && workerThread.getRunThread() != null && workerThread.getRunThread().isAlive())
240             throw new Exception("Worker thread is currently busy.  Cannot start another process yet.");
241 
242         this.workerThread = processToMonitor.getProcessControl();
243     }
244 
245     public void startProcess() throws Exception
246     {
247         if (this.workerThread == null)
248             throw new Exception("Worker thread is null.  Cannot initiaite processing.");
249 
250         processThread = new SimpleProcessThread();
251         processThread.setInterval(75);
252         processThread.setProcess(this);
253 
254         this.workerThread.start();
255         this.processThread.start();
256     }
257 
258     /***
259      * Stops the process but asks the user to confirm, first
260      *  
261      */
262     public void stopProcess()
263     {
264         cancelButton_actionPerformed(null);
265     }
266 
267     /***
268      * Stops the process without asking for confirmation
269      *  
270      */
271     public void stopNoConfirm() throws InterruptedException
272     {
273         workerThread.stop();
274     }
275 
276     void cancelButton_actionPerformed(ActionEvent ed)
277     {
278         try
279         {
280             connectPanel.cancel();
281             workerThread.stop();
282         }
283         catch (Exception ex)
284         {
285             ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
286         }
287     }
288 
289     /***
290      * @see com.explosion.utilities.process.threads.SimpleProcess#process()
291      */
292     public void process() throws Exception
293     {
294         Runnable updateProgress = new Runnable()
295         {
296 
297             public void run()
298             {
299                 statusLabel.setText(workerThread.getProcess().getStatusText());
300             }
301         };
302 
303         SwingUtilities.invokeLater(updateProgress);
304 
305     }
306 
307 
308     /***
309      * @see com.explosion.utilities.process.threads.SimpleProcess#finalise()
310      * @throws Exception
311      */
312     public void finalise() throws Exception
313     {}
314     /***
315      * @see com.explosion.utilities.process.threads.SimpleProcess#finalise(java.lang.Exception)
316      * @param e
317      */
318     public void finalise(Exception e)
319     {}
320     /***
321      * @see com.explosion.utilities.process.threads.SimpleProcess#initialise()
322      * @throws Exception
323      */
324     public void initialise() throws Exception
325     {}
326     /***
327      * @see com.explosion.utilities.process.threads.Process#getPercentComplete()
328      * @return
329      */
330     public int getPercentComplete()
331     {
332         return 0;
333     }
334     /***
335      * @see com.explosion.utilities.process.threads.Process#getProcessControl()
336      * @return
337      */
338     public ProcessThread getProcessControl()
339     {
340         return null;
341     }
342     /***
343      * @see com.explosion.utilities.process.threads.Process#getStatusText()
344      * @return
345      */
346     public String getStatusText()
347     {
348         return null;
349     }
350     /***
351      * @see com.explosion.utilities.process.threads.Process#isStopped()
352      * @return
353      */
354     public boolean isStopped()
355     {
356         return false;
357     }
358     /***
359      * @see com.explosion.utilities.process.threads.Process#log(java.lang.Exception, java.lang.String)
360      * @param exception
361      * @param message
362      */
363     public void log(Exception exception, String message)
364     {}
365     /***
366      * @see com.explosion.utilities.process.threads.Process#log(java.lang.String)
367      * @param string
368      */
369     public void log(String string)
370     {}
371     /***
372      * @see com.explosion.utilities.process.threads.Process#setPercentComplete(int)
373      * @param percentComplete
374      */
375     public void setPercentComplete(int percentComplete)
376     {}
377     /***
378      * @see com.explosion.utilities.process.threads.Process#setProcessControl(com.explosion.Utils.Processing.Threading.ProcessThread)
379      * @param processThread
380      */
381     public void setProcessControl(ProcessThread processThread)
382     {}
383     /***
384      * @see com.explosion.utilities.process.threads.Process#setStatusText(java.lang.String)
385      * @param statusText
386      */
387     public void setStatusText(String statusText)
388     {}
389     
390     /***
391      * Returns a boolean value indicating whther this is a user process or not
392      * @return
393      */
394     public boolean isUserProcess()
395     {
396         return this.isUserProcess;
397     }
398     
399     /***
400      * Sets whtether or not this is a user process.  True means that it is
401      * @param truth
402      */
403     public void setIsUserProcess(boolean truth)
404     {
405         this.isUserProcess = true;
406     }    
407 }
408