View Javadoc

1   package com.explosion.datastream.exql;
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.Frame;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.MouseEvent;
28  import java.io.InputStream;
29  import java.io.InputStreamReader;
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import javax.swing.JButton;
34  import javax.swing.JDialog;
35  import javax.swing.JMenuItem;
36  import javax.swing.JOptionPane;
37  
38  import org.apache.log4j.LogManager;
39  import org.apache.log4j.Logger;
40  
41  import com.explosion.datastream.exql.gui.ConnectPanel;
42  import com.explosion.expf.Application;
43  import com.explosion.expf.ExpActionListener;
44  import com.explosion.expf.ExpFrame;
45  import com.explosion.expf.ExpInternalFrame;
46  import com.explosion.expfmodules.rdbmsconn.RdbmsConnConstants;
47  import com.explosion.expfmodules.rdbmsconn.RdbmsConnModuleManager;
48  import com.explosion.expfmodules.rdbmsconn.connect.ConnectionManager;
49  import com.explosion.expfmodules.wizard.Wizard;
50  import com.explosion.expfmodules.wizard.standard.load.WizardDefinitionLoader;
51  import com.explosion.expfmodules.wizard.standard.view.WizardBasePanel;
52  import com.explosion.utilities.GeneralUtils;
53  import com.explosion.utilities.preferences.groups.PreferenceGroup;
54  import com.explosion.utilities.preferences.groups.PreferenceGroupManager;
55  import com.explosion.utilities.preferences.groups.dialogs.PreferenceGroupEditDialog;
56  import com.explosion.utilities.preferences.groups.dialogs.PreferenceGroupNameEditDialog;
57  import com.explosion.utilities.preferences.groups.dialogs.PreferenceGroupsEditorDialog;
58  
59  /***
60   * @author Stephen Cowx
61   *  
62   */
63  public class EXQLListener implements ExpActionListener
64  {
65  	private static Logger log = LogManager.getLogger(EXQLListener.class);
66      private HashMap map;
67      private PreferenceGroupsEditorDialog connectionsDialog;
68      private PreferenceGroupsEditorDialog driversDialog;
69  
70      /***
71       * Constructor for EXQLListener.
72       */
73      public EXQLListener()
74      {
75          map = new HashMap();
76          map.put(EXQLConstants.MENU_CONNECTIONS, EXQLConstants.MENU_CONNECTIONS);
77          map.put(EXQLConstants.MENU_DRIVERS, EXQLConstants.MENU_DRIVERS);
78          map.put(EXQLConstants.MENU_NEW_DRIVER, EXQLConstants.MENU_NEW_DRIVER);
79          map.put(EXQLConstants.MENU_NEW_CONNECTION, EXQLConstants.MENU_NEW_CONNECTION);
80          map.put(EXQLConstants.MENU_OPEN_CONNECTION, EXQLConstants.MENU_OPEN_CONNECTION);
81      }
82  
83      /***
84       * @see package
85       *      com.explosion.expf.Interfaces.ExpActionListener#getListensFor()
86       */
87      public Map getListensFor()
88      {
89          return map;
90      }
91  
92      /***
93       * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
94       */
95      public void actionPerformed(ActionEvent e)
96      {
97          try
98          {
99              log.debug("Handling event " + e.getActionCommand());
100         	if (e.getActionCommand().equals(EXQLConstants.MENU_CONNECTIONS))
101             {
102                 log.debug("Connections");
103         		connectionsDialog = new PreferenceGroupsEditorDialog(Application.getApplicationFrame(), "Connection definitions", true, RdbmsConnModuleManager.instance().getConnectionDescriptorManager());
104 
105                 JButton testButton = new JButton("Test");
106                 testButton.addActionListener(new java.awt.event.ActionListener()
107                 {
108 
109                     public void actionPerformed(ActionEvent e)
110                     {
111                         testConnection();
112                     }
113                 });
114                 connectionsDialog.addButton(testButton, true);
115 
116                 JButton connectButton = new JButton("Connect");
117                 connectButton.addActionListener(new java.awt.event.ActionListener()
118                 {
119 
120                     public void actionPerformed(ActionEvent e)
121                     {
122                         try
123                         {
124                             connect(connectionsDialog.getSelectedValue());
125                             connectionsDialog.setVisible(false);
126                         }
127                         catch (Exception ex)
128                         {
129                             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while connecting to database.");
130                         }
131                     }
132                 });
133                 connectionsDialog.addButton(connectButton, true);
134 
135                 connectionsDialog.getPersistentObjectList().addMouseListener(new java.awt.event.MouseAdapter()
136                 {
137 
138                     public void mouseClicked(MouseEvent e)
139                     {
140                         if (e.getClickCount() == 2)
141                         {
142                             try
143                             {
144                                 connect(connectionsDialog.getSelectedValue());
145                                 connectionsDialog.setVisible(false);
146                             }
147                             catch (Exception ex)
148                             {
149                                 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while connecting to database.");
150                             }
151                         }
152                     }
153                 });
154 
155                 connectionsDialog.setSize(RdbmsConnConstants.driversDialogDimension);
156                 GeneralUtils.centreWindowInParent(connectionsDialog);
157                 connectionsDialog.setVisible(true);
158                 EXQLModuleManager.instance().rebuildOpenMenu();
159             }
160             else if (e.getActionCommand().equals(EXQLConstants.MENU_DRIVERS))
161             {
162             	log.debug("Drivers");
163             	driversDialog = new PreferenceGroupsEditorDialog(Application.getApplicationFrame(), "JDBC Drivers", true, RdbmsConnModuleManager.instance().getDriverDescriptorManager());
164                 driversDialog.setSize(RdbmsConnConstants.driversDialogDimension);
165                 GeneralUtils.centreWindowInParent(driversDialog);
166                 driversDialog.setVisible(true);
167             }
168             else if (e.getActionCommand().equals(EXQLConstants.MENU_NEW_DRIVER))
169             {
170             	log.debug("New Driver");
171             	Frame frame = Application.getApplicationFrame();
172 
173                 WizardDefinitionLoader loader = new WizardDefinitionLoader();
174                 InputStream inputStream = loader.getClass().getClassLoader().getResourceAsStream("com/explosion/expfmodules/rdbmsconn/connectwizard/NewDriverWizard.xml");
175                 Wizard wizard = loader.getWizard(new InputStreamReader(inputStream));
176                 
177                 wizard.start();
178                 JDialog dialog = new JDialog(frame,true);
179                 WizardBasePanel view = new WizardBasePanel(wizard,dialog);
180                 dialog.getContentPane().setLayout(new BorderLayout());
181                 dialog.getContentPane().add(view, BorderLayout.CENTER);
182                 dialog.setSize(new Dimension(600,400));
183                 dialog.setTitle(wizard.getName());
184                 GeneralUtils.centreWindowInParent(dialog);
185                 dialog.setVisible(true);
186             }
187             else if (e.getActionCommand().equals(EXQLConstants.MENU_NEW_CONNECTION))
188             {
189             	log.debug("New connection");
190             	Frame frame = Application.getApplicationFrame();
191             	WizardDefinitionLoader loader = new WizardDefinitionLoader();
192                 InputStream inputStream = loader.getClass().getClassLoader().getResourceAsStream("com/explosion/expfmodules/rdbmsconn/connectwizard/DatabaseConnectionWizard.xml");
193                 Wizard wizard = loader.getWizard(new InputStreamReader(inputStream));
194                 wizard.start();
195                 JDialog dialog = new JDialog(frame,true);
196                 WizardBasePanel view = new WizardBasePanel(wizard,dialog);
197                 dialog.getContentPane().setLayout(new BorderLayout());
198                 dialog.getContentPane().add(view, BorderLayout.CENTER);
199                 dialog.setSize(new Dimension(600,400));
200                 dialog.setTitle(wizard.getName());
201                 GeneralUtils.centreWindowInParent(dialog);
202                 dialog.setVisible(true);
203                 EXQLModuleManager.instance().rebuildOpenMenu();
204             }
205             else if (e.getActionCommand().equals(EXQLConstants.MENU_OPEN_CONNECTION))
206             {
207             	log.debug("Open connection");
208             	try
209                 {
210                     String menuText = ((JMenuItem) e.getSource()).getText();
211                     int indexOfDot = menuText.indexOf(".");
212                     connect(menuText.substring(indexOfDot + 1).trim());
213                 }
214                 catch (Exception ex)
215                 {
216                     com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while connecting to database.");
217                 }
218             }
219 
220         }
221         catch (Throwable ex)
222         {
223         	log.debug("Exception");
224         	com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while handling event.");
225         }
226         log.debug("Done");
227     }
228     
229     private void newPersistentDescriptor(PreferenceGroupManager objectManager, String title) throws Exception
230     {
231         Frame frame = Application.getApplicationFrame();
232 
233         PreferenceGroupNameEditDialog nameDialog = new PreferenceGroupNameEditDialog(frame, title, null, objectManager);
234         nameDialog.setSize(PreferenceGroupNameEditDialog.nameEditorDimension);
235         GeneralUtils.centreWindowInParent(nameDialog);
236         nameDialog.setVisible(true);
237 
238         if (nameDialog.getObject() != null)
239         {
240             PreferenceGroupEditDialog editDialog = new PreferenceGroupEditDialog(frame, nameDialog.getObject().getIdentifier(), true);
241             editDialog.loadPreferences(nameDialog.getObject());
242             editDialog.setSize(PreferenceGroupEditDialog.editorDimension);
243             try
244             {
245                 GeneralUtils.centreWindowInParent(editDialog);
246             }
247             catch (Exception ex)
248             {
249             }
250 
251             editDialog.setVisible(true);
252         }
253     }
254 
255     private void newConnection(PreferenceGroupManager objectManager, String title) throws Exception
256     {
257         Frame frame = Application.getApplicationFrame();
258 
259         PreferenceGroupNameEditDialog nameDialog = new PreferenceGroupNameEditDialog(frame, title, null, objectManager);
260         nameDialog.setSize(PreferenceGroupNameEditDialog.nameEditorDimension);
261         GeneralUtils.centreWindowInParent(nameDialog);
262         nameDialog.setVisible(true);
263 
264         if (nameDialog.getObject() != null)
265         {
266             final PreferenceGroupEditDialog editDialog = new PreferenceGroupEditDialog(frame, nameDialog.getObject().getIdentifier(), true);
267 
268             editDialog.loadPreferences(nameDialog.getObject());
269             JButton connectButton = editDialog.getOKButton();
270             connectButton.setText("Connect");
271             connectButton.addActionListener(new java.awt.event.ActionListener()
272             {
273 
274                 public void actionPerformed(ActionEvent e)
275                 {
276                     try
277                     {
278                         connect(editDialog.getPersistentDescriptor().getIdentifier());
279                     }
280                     catch (Exception ex)
281                     {
282                         com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while connecting to database.");
283                     }
284                 }
285             });
286 
287             editDialog.setSize(PreferenceGroupEditDialog.editorDimension);
288             try
289             {
290                 GeneralUtils.centreWindowInParent(editDialog);
291             }
292             catch (Exception ex)
293             {
294             }
295 
296             editDialog.setVisible(true);
297         }
298     }
299 
300     void testConnection()
301     {
302         PreferenceGroup descriptor = RdbmsConnModuleManager.instance().getConnectionDescriptorManager().getGroup(connectionsDialog.getSelectedValue());
303 
304         try
305         {
306             if (descriptor == null)
307                 throw new Exception("Connection '" + connectionsDialog.getSelectedValue() + "' not defined.");
308 
309             int connectionKey = ConnectionManager.getInstance().connect(descriptor, RdbmsConnModuleManager.instance().getDriverDescriptorManager());
310             JOptionPane.showMessageDialog(Application.getApplicationFrame(), "Connected to '" + connectionsDialog.getSelectedValue() + "' successfully.");
311 
312             try
313             {
314             	ConnectionManager.getInstance().getConnection(connectionKey).close();
315             }
316             catch (Exception e)
317             {
318             }
319         }
320         catch (Exception e)
321         {
322             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Test for connection '" + connectionsDialog.getSelectedValue() + "' failed.");
323         }
324     }
325 
326     /***
327      * Connects to a database
328      * 
329      * @param connectionDescriptorName
330      * @throws Exception
331      */
332     void connect(String connectionDescriptorName) throws Exception
333     {
334         PreferenceGroup descriptor = RdbmsConnModuleManager.instance().getConnectionDescriptorManager().getGroup(connectionDescriptorName);
335 
336         if (descriptor == null)
337             throw new Exception("Connection '" + connectionDescriptorName + "' not defined.");
338 
339         int heightOfIt = 90;
340         int widthOfIt = 320;
341         
342         ConnectPanel connectPanel = new ConnectPanel();
343         ExpInternalFrame frame = ((ExpFrame) Application.getApplicationFrame()).createPaletteFrame(connectPanel, new Dimension(widthOfIt, heightOfIt), "Connecting to "
344                 + connectionDescriptorName,  false);
345         connectPanel.connect(descriptor);
346     }
347 }