1 package com.explosion.expfmodules.rdbmsconn.connectwizard.screens;
2
3 import java.awt.BorderLayout;
4 import java.awt.GridBagConstraints;
5 import java.awt.GridBagLayout;
6 import java.awt.Insets;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9
10 import javax.swing.JButton;
11 import javax.swing.JComboBox;
12 import javax.swing.JEditorPane;
13 import javax.swing.JOptionPane;
14 import javax.swing.JPanel;
15 import javax.swing.text.html.HTMLEditorKit;
16
17 import com.explosion.expf.Application;
18 import com.explosion.expfmodules.rdbmsconn.RdbmsConnConstants;
19 import com.explosion.expfmodules.rdbmsconn.RdbmsConnModuleManager;
20 import com.explosion.expfmodules.rdbmsconn.connect.ConnectionManager;
21 import com.explosion.expfmodules.wizard.StepDefinition;
22 import com.explosion.expfmodules.wizard.Wizard;
23 import com.explosion.expfmodules.wizard.standard.view.StandardStepView;
24 import com.explosion.expfmodules.wizard.standard.view.WizardBasePanel;
25 import com.explosion.utilities.GeneralConstants;
26 import com.explosion.utilities.preferences.Preference;
27 import com.explosion.utilities.preferences.groups.PreferenceGroup;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 /***
50 * @author Stephen Cowx
51 * Created on 21-Feb-2005
52 */
53 public class ConnectionTestScreen extends StandardStepView
54 {
55
56 private static org.apache.log4j.Logger log = org.apache.log4j.LogManager.getLogger(ConnectionTestScreen.class);
57 private JComboBox comboBox = null;
58 private String[] driverList = null;
59 private StepDefinition stepDefinition = null;
60
61 /***
62 * @see com.explosion.expfmodules.wizard.StepView#init(com.explosion.expfmodules.wizard.StepDefinition, java.awt.Component)
63 * @param stepDefinition
64 * @param parent
65 */
66 public void init(final StepDefinition stepDefinition)
67 {
68 this.removeAll();
69 JPanel helpAndDisplayPanel = new JPanel();
70
71 JEditorPane name = new JEditorPane();
72 name.setEditorKit(new HTMLEditorKit());
73 name.setText(WizardBasePanel.FONT + stepDefinition.getName());
74 name.setFont(GeneralConstants.DEFAULT_WINDOW_FONT);
75 name.setOpaque(false);
76 name.setEditable(false);
77
78 JEditorPane notes = new JEditorPane();
79 notes.setEditorKit(new HTMLEditorKit());
80 notes.setText(WizardBasePanel.FONT + stepDefinition.getNotes());
81 notes.setFont(GeneralConstants.DEFAULT_WINDOW_FONT);
82 notes.setOpaque(false);
83 notes.setEditable(false);
84
85 helpAndDisplayPanel.removeAll();
86 helpAndDisplayPanel.setLayout(new BorderLayout());
87 helpAndDisplayPanel.add(name, BorderLayout.NORTH);
88 helpAndDisplayPanel.add(notes, BorderLayout.CENTER);
89
90 this.setLayout(new BorderLayout());
91 this.add(helpAndDisplayPanel,BorderLayout.NORTH);
92
93 PreferenceGroup connectionDescriptor = (PreferenceGroup) stepDefinition.getWizard().getWizardDataValues().get("connection_descriptor");
94 Preference driver = connectionDescriptor.getPreference(RdbmsConnConstants.CD_DRIVER);
95 Preference user = connectionDescriptor.getPreference(RdbmsConnConstants.CD_USER);
96 Preference pwd = connectionDescriptor.getPreference(RdbmsConnConstants.CD_PASS);
97 Preference url = connectionDescriptor.getPreference(RdbmsConnConstants.CD_URL);
98
99 JEditorPane details = new JEditorPane();
100 details.setEditorKit(new HTMLEditorKit());
101 String text = WizardBasePanel.FONT;
102 text += "<P><B>Driver:</B> " + driver;
103 text += "<P><B>User:</B> " + user;
104 text += "<P><B>URL:</B> " + url;
105 text += "<P><B>Using password:</B> " + new Boolean(((String)pwd.getValue()).length() > 0);
106 text += "";
107
108 JButton testButton = new JButton("Test");
109 testButton.addActionListener(new ActionListener(){
110 public void actionPerformed(ActionEvent e)
111 {
112 testConnection(stepDefinition.getWizard());
113 }
114 });
115
116 details.setText(text);
117 details.setFont(GeneralConstants.DEFAULT_WINDOW_FONT);
118 details.setOpaque(false);
119 details.setEditable(false);
120
121 JPanel inputPanel = new JPanel();
122 inputPanel.setLayout(new GridBagLayout());
123 inputPanel.add(details, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
124 inputPanel.add(testButton, new GridBagConstraints(0, 1, 1, 1, 0.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
125
126
127 this.add(inputPanel,BorderLayout.CENTER);
128
129 }
130
131 /***
132 * This method performsa test to try and connect to the database
133 * @param wizard
134 */
135 public void testConnection(Wizard wizard)
136 {
137 try
138 {
139 PreferenceGroup descriptor = (PreferenceGroup) wizard.getWizardDataValues().get("connection_descriptor");
140
141 int connectionKey = ConnectionManager.getInstance().connect(descriptor, RdbmsConnModuleManager.instance().getDriverDescriptorManager());
142 JOptionPane.showMessageDialog(Application.getApplicationFrame(), "Connected successfully.");
143
144 try
145 {
146 ConnectionManager.getInstance().getConnection(connectionKey).close();
147 }
148 catch (Exception e)
149 {
150 }
151 }
152 catch (Exception e)
153 {
154 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Connection failed.");
155 }
156 }
157
158 }