KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > db > CConnectionDialog


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.db;
15
16 import javax.swing.*;
17 import java.awt.*;
18 import java.awt.event.*;
19
20 import org.compiere.util.Ini;
21 import org.compiere.plaf.*;
22 import org.compiere.swing.*;
23 import java.util.ResourceBundle JavaDoc;
24
25 /**
26  * Connection Dialog.
27  *
28  * @author Jorg Janke
29  * @author Marek Mosiewicz<marek.mosiewicz@jotel.com.pl> - support for RMI over HTTP
30  * @version $Id: CConnectionDialog.java,v 1.18 2003/09/27 01:22:18 jjanke Exp $
31  */

32 public class CConnectionDialog extends JDialog implements ActionListener
33 {
34     /**
35      * Connection Dialog using current Connection
36      */

37     public CConnectionDialog()
38     {
39         this (null);
40     } // CConnectionDialog
41

42     /**
43      * Connection Dialog
44      * @param cc Compiere Connection
45      */

46     public CConnectionDialog(CConnection cc)
47     {
48         super((Frame)null, true);
49         try
50         {
51             jbInit();
52             setConnection (cc);
53         }
54         catch(Exception JavaDoc e)
55         {
56             System.err.println("CConnectionDialog");
57             e.printStackTrace();
58         }
59         CompierePLAF.showCenterScreen(this);
60     } // CConnection
61

62     private static ResourceBundle JavaDoc res = ResourceBundle.getBundle("org.compiere.db.DBRes");
63
64     public static final String JavaDoc APPS_PORT_HTTP = "80";
65     public static final String JavaDoc APPS_PORT_JNP = "1099";
66
67     private CConnection m_cc = null;
68     private CConnection m_ccResult = null;
69     private boolean m_updating = false;
70     private boolean m_saved = false;
71
72
73     private CPanel mainPanel = new CPanel();
74     private BorderLayout mainLayout = new BorderLayout();
75     private CPanel centerPanel = new CPanel();
76     private CPanel southPanel = new CPanel();
77     private CButton bOK = CompierePLAF.getOKButton();
78     private CButton bCancel = CompierePLAF.getCancelButton();
79     private FlowLayout southLayout = new FlowLayout();
80     private GridBagLayout centerLayout = new GridBagLayout();
81     private CLabel nameLabel = new CLabel();
82     private CTextField nameField = new CTextField();
83     private CLabel hostLabel = new CLabel();
84     private CTextField hostField = new CTextField();
85     private CLabel portLabel = new CLabel();
86     private CTextField portField = new CTextField();
87     private CLabel sidLabel = new CLabel();
88     private CTextField sidField = new CTextField();
89     private CCheckBox cbFirewall = new CCheckBox();
90     private CLabel fwHostLabel = new CLabel();
91     private CTextField fwHostField = new CTextField();
92     private CLabel fwPortLabel = new CLabel();
93     private CTextField fwPortField = new CTextField();
94     private CButton bTestDB = new CButton();
95     private CLabel typeLabel = new CLabel();
96     private CComboBox typeField = new CComboBox(Database.DB_NAMES);
97     private CCheckBox cbBequeath = new CCheckBox();
98     private CLabel appsHostLabel = new CLabel();
99     private CTextField appsHostField = new CTextField();
100     private CLabel appsPortLabel = new CLabel();
101     private CTextField appsPortField = new CTextField();
102     private CButton bTestApps = new CButton();
103     private CCheckBox cbOverwrite = new CCheckBox();
104     private CLabel dbUidLabel = new CLabel();
105     private CTextField dbUidField = new CTextField();
106     private JPasswordField dbPwdField = new JPasswordField();
107     private CCheckBox cbRMIoverHTTP = new CCheckBox();
108
109
110     /**
111      * Static Layout
112      * @throws Exception
113      */

114     private void jbInit() throws Exception JavaDoc
115     {
116         this.setTitle(res.getString("CConnectionDialog"));
117         CompiereColor.setBackground(this);
118         mainPanel.setLayout(mainLayout);
119         southPanel.setLayout(southLayout);
120         southLayout.setAlignment(FlowLayout.RIGHT);
121         centerPanel.setLayout(centerLayout);
122         nameLabel.setText(res.getString("Name"));
123         nameField.setColumns(30);
124         hostLabel.setText(res.getString("DBHost"));
125         hostField.setColumns(30);
126         portLabel.setText(res.getString("DBPort"));
127         portField.setColumns(10);
128         sidLabel.setText(res.getString("DBName"));
129         cbFirewall.setToolTipText("");
130         cbFirewall.setText(res.getString("ViaFirewall"));
131         fwHostLabel.setText(res.getString("FWHost"));
132         fwHostField.setColumns(30);
133         fwPortLabel.setText(res.getString("FWPort"));
134         bTestDB.setText(res.getString("TestConnection"));
135         bTestDB.setHorizontalAlignment(JLabel.LEFT);
136         typeLabel.setText(res.getString("Type"));
137         sidField.setColumns(30);
138         fwPortField.setColumns(10);
139         cbBequeath.setText(res.getString("BequeathConnection"));
140         appsHostLabel.setText(res.getString("AppsHost"));
141         appsHostField.setColumns(30);
142         appsPortLabel.setText(res.getString("AppsPort"));
143         appsPortField.setColumns(10);
144         bTestApps.setText(res.getString("TestApps"));
145         bTestApps.setHorizontalAlignment(JLabel.LEFT);
146         cbOverwrite.setText(res.getString("Overwrite"));
147         dbUidLabel.setText(res.getString("DBUidPwd"));
148         dbUidField.setColumns(10);
149         cbRMIoverHTTP.addActionListener(this);
150         cbRMIoverHTTP.setText(res.getString("RMIoverHTTP"));
151         this.getContentPane().add(mainPanel, BorderLayout.CENTER);
152         mainPanel.add(centerPanel, BorderLayout.CENTER);
153         mainPanel.add(southPanel, BorderLayout.SOUTH);
154         southPanel.add(bCancel, null);
155         southPanel.add(bOK, null);
156         centerPanel.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
157             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(12, 12, 5, 5), 0, 0));
158         centerPanel.add(nameField, new GridBagConstraints(1, 0, 2, 1, 0.0, 0.0
159             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(12, 0, 5, 12), 0, 0));
160         centerPanel.add(hostLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0
161             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 12, 5, 5), 0, 0));
162         centerPanel.add(hostField, new GridBagConstraints(1, 6, 2, 1, 0.0, 0.0
163             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 12), 0, 0));
164         centerPanel.add(portLabel, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0
165             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
166         centerPanel.add(portField, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0
167             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
168         centerPanel.add(sidLabel, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0
169             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
170         centerPanel.add(sidField, new GridBagConstraints(1, 8, 2, 1, 0.0, 0.0
171             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 12), 0, 0));
172         centerPanel.add(cbFirewall, new GridBagConstraints(1, 10, 2, 1, 0.0, 0.0
173             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 12), 0, 0));
174         centerPanel.add(fwHostLabel, new GridBagConstraints(0, 11, 1, 1, 0.0, 0.0
175             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
176         centerPanel.add(fwHostField, new GridBagConstraints(1, 11, 2, 1, 0.0, 0.0
177             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 12), 0, 0));
178         centerPanel.add(fwPortLabel, new GridBagConstraints(0, 12, 1, 1, 0.0, 0.0
179             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
180         centerPanel.add(fwPortField, new GridBagConstraints(1, 12, 1, 1, 0.0, 0.0
181             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
182         centerPanel.add(bTestDB, new GridBagConstraints(1, 13, 1, 1, 0.0, 0.0
183             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 12, 0), 0, 0));
184         centerPanel.add(typeLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0
185             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 12, 5, 5), 0, 0));
186         centerPanel.add(typeField, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0
187             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
188         centerPanel.add(appsHostLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
189             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 12, 5, 5), 0, 0));
190         centerPanel.add(appsHostField, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0
191             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 12), 0, 0));
192         centerPanel.add(appsPortLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
193             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
194         centerPanel.add(appsPortField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
195             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
196         centerPanel.add(cbBequeath, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0
197             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 12), 0, 0));
198         centerPanel.add(cbOverwrite, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0
199             ,GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(0, 5, 0, 12), 0, 0));
200         centerPanel.add(dbUidLabel, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0
201             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
202         centerPanel.add(dbUidField, new GridBagConstraints(1, 9, 1, 1, 0.0, 0.0
203             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
204         centerPanel.add(dbPwdField, new GridBagConstraints(2, 9, 1, 1, 0.0, 0.0
205             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 12), 0, 0));
206         centerPanel.add(bTestApps, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0
207             ,GridBagConstraints.SOUTHWEST, GridBagConstraints.HORIZONTAL, new Insets(2, -1, 2, 1), 0, 0));
208         centerPanel.add(cbRMIoverHTTP, new GridBagConstraints(2, 2, 1, 2, 0.0, 0.0
209             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 12), 0, 0));
210         //
211
nameField.addActionListener(this);
212         appsHostField.addActionListener(this);
213         appsPortField.addActionListener(this);
214         cbOverwrite.addActionListener(this);
215         bTestApps.addActionListener(this);
216         //
217
typeField.addActionListener(this);
218         hostField.addActionListener(this);
219         portField.addActionListener(this);
220         sidField.addActionListener(this);
221         cbBequeath.addActionListener(this);
222         cbFirewall.addActionListener(this);
223         fwHostField.addActionListener(this);
224         fwPortField.addActionListener(this);
225         bTestDB.addActionListener(this);
226         bOK.addActionListener(this);
227         bCancel.addActionListener(this);
228
229         // Server
230
if (!Ini.isClient())
231         {
232             appsHostLabel.setVisible(false);
233             appsHostField.setVisible(false);
234             appsPortLabel.setVisible(false);
235             appsPortField.setVisible(false);
236             bTestApps.setVisible(false);
237             cbRMIoverHTTP.setVisible(false);
238         }
239         else // Client
240
cbBequeath.setVisible(false);
241     } // jbInit
242

243     /**
244      * Set Busy - lock UI
245      * @param busy busy
246      */

247     private void setBusy (boolean busy)
248     {
249         if (busy)
250             this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
251         else
252             this.setCursor(Cursor.getDefaultCursor());
253         m_updating = busy;
254     } // setBusy
255

256     /**
257      * Set Connection
258      * @param cc - if null use current connection
259      */

260     public void setConnection (CConnection cc)
261     {
262         m_cc = cc;
263         if (m_cc == null)
264         {
265             m_cc = CConnection.get();
266             m_cc.setName();
267         }
268         /** @todo copy to preserve old values */
269         m_ccResult = m_cc;
270         //
271
m_cc.setType(m_cc.getType()); // sets defaults
272
updateInfo();
273     } // setConnection
274

275     /**
276      * Get Connection
277      * @return CConnection
278      */

279     public CConnection getConnection()
280     {
281         return m_ccResult;
282     } // getConnection;
283

284     /**
285      * ActionListener
286      * @param e event
287      */

288     public void actionPerformed(ActionEvent e)
289     {
290         if (m_updating)
291             return;
292         Object JavaDoc src = e.getSource();
293
294         if (src == bOK)
295         {
296             m_cc.setName();
297             m_ccResult = m_cc;
298             dispose();
299             return;
300         }
301         else if (src == bCancel)
302         {
303             m_cc.setName();
304             dispose();
305             return;
306         }
307         else if (src == cbRMIoverHTTP)
308         {
309             if (cbRMIoverHTTP.isSelected())
310                 appsPortField.setText(APPS_PORT_HTTP);
311             else
312                 appsPortField.setText(APPS_PORT_JNP);
313             return;
314         }
315
316
317         if (Ini.isClient())
318         {
319             m_cc.setAppsHost(appsHostField.getText());
320             m_cc.setAppsPort(appsPortField.getText());
321         }
322         else
323             m_cc.setAppsHost("localhost");
324         //
325
m_cc.setRMIoverHTTP(cbRMIoverHTTP.isSelected());
326         //
327
m_cc.setDbHost(hostField.getText());
328         m_cc.setDbPort(portField.getText());
329         m_cc.setDbName(sidField.getText());
330         m_cc.setDbUid(dbUidField.getText());
331         m_cc.setDbPwd(String.valueOf(dbPwdField.getPassword()));
332         m_cc.setBequeath(cbBequeath.isSelected());
333         m_cc.setViaFirewall(cbFirewall.isSelected());
334         m_cc.setFwHost(fwHostField.getText());
335         m_cc.setFwPort(fwPortField.getText());
336         //
337
if (src == bTestApps)
338             cmd_testApps();
339
340         // Database Selection Changed
341
else if (src == typeField)
342             m_cc.setType(typeField.getSelectedItem().toString());
343         //
344
else if (src == bTestDB)
345             cmd_testDB();
346
347         // Name
348
if (src == nameField)
349             m_cc.setName(nameField.getText());
350
351         updateInfo();
352     } // actionPerformed
353

354     /**
355      * Update Fields from Connection
356      */

357     private void updateInfo()
358     {
359         m_updating = true;
360         nameField.setText(m_cc.getName());
361         appsHostField.setText(m_cc.getAppsHost());
362         appsPortField.setText(String.valueOf(m_cc.getAppsPort()));
363         cbRMIoverHTTP.setSelected(m_cc.isRMIoverHTTP());
364         bTestApps.setIcon(getStatusIcon(m_cc.isAppsServerOK(false)));
365     // bTestApps.setToolTipText(m_cc.getRmiUri());
366

367         cbOverwrite.setVisible(m_cc.isAppsServerOK(false));
368         boolean rw = cbOverwrite.isSelected() || !m_cc.isAppsServerOK(false);
369         //
370
typeLabel.setReadWrite(rw);
371         typeField.setReadWrite(rw);
372         typeField.setSelectedItem(m_cc.getType());
373         //
374
hostLabel.setReadWrite(rw);
375         hostField.setReadWrite(rw);
376         hostField.setText(m_cc.getDbHost());
377         portLabel.setReadWrite(rw);
378         portField.setReadWrite(rw);
379         portField.setText(String.valueOf(m_cc.getDbPort()));
380         sidLabel.setReadWrite(rw);
381         sidField.setReadWrite(rw);
382         sidField.setText(m_cc.getDbName());
383         //
384
dbUidField.setText(m_cc.getDbUid());
385         dbPwdField.setText(m_cc.getDbPwd());
386         //
387
cbBequeath.setReadWrite(rw);
388         cbBequeath.setEnabled(m_cc.isOracle());
389         cbBequeath.setSelected(m_cc.isBequeath());
390         //
391
boolean fwEnabled = m_cc.isViaFirewall() && m_cc.isOracle();
392         cbFirewall.setReadWrite(rw);
393         cbFirewall.setSelected(fwEnabled);
394         cbFirewall.setEnabled(m_cc.isOracle());
395         fwHostLabel.setReadWrite(fwEnabled && rw);
396         fwHostField.setReadWrite(fwEnabled && rw);
397         fwHostField.setText(m_cc.getFwHost());
398         fwPortLabel.setReadWrite(fwEnabled && rw);
399         fwPortField.setReadWrite(fwEnabled && rw);
400         fwPortField.setText(String.valueOf(m_cc.getFwPort()));
401         //
402
bTestDB.setToolTipText(m_cc.getConnectionURL());
403         bTestDB.setIcon(getStatusIcon(m_cc.isDatabaseOK()));
404         m_updating = false;
405     } // updateInfo
406

407     /**
408      * Get Status Icon - ok or not
409      * @param ok ok
410      * @return Icon
411      */

412     private Icon getStatusIcon (boolean ok)
413     {
414         if (ok)
415             return bOK.getIcon();
416         else
417             return bCancel.getIcon();
418     } // getStatusIcon
419

420     /**
421      * Test Database connection
422      */

423     private void cmd_testDB()
424     {
425         setBusy (true);
426         Exception JavaDoc e = m_cc.testDatabase();
427         if (e != null)
428         {
429             JOptionPane.showMessageDialog(this,
430                 e, // message
431
res.getString("ConnectionError") + " - " + m_cc.getConnectionURL(),
432                 JOptionPane.ERROR_MESSAGE);
433         }
434         setBusy (false);
435     } // cmd_testDB
436

437     /**
438      * Test Application connection
439      */

440     private void cmd_testApps()
441     {
442         setBusy (true);
443         Exception JavaDoc e = m_cc.testAppsServer();
444         if (e != null)
445         {
446             JOptionPane.showMessageDialog(this,
447                 e.getLocalizedMessage(),
448                 res.getString("ServerNotActive") + " - " + m_cc.getAppsHost(),
449                 JOptionPane.ERROR_MESSAGE);
450         }
451         setBusy (false);
452     } // cmd_testApps
453

454 } // CConnectionDialog
455
Popular Tags