KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > administrator > helpers > DefineRepositoryDialog


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.administrator.helpers;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Toolkit JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.FocusEvent JavaDoc;
37 import java.sql.Connection JavaDoc;
38
39 import javax.swing.DefaultComboBoxModel JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JCheckBox JavaDoc;
42 import javax.swing.JComboBox JavaDoc;
43 import javax.swing.JDialog JavaDoc;
44 import javax.swing.JLabel JavaDoc;
45 import javax.swing.JOptionPane JavaDoc;
46 import javax.swing.JPanel JavaDoc;
47 import javax.swing.JTextField JavaDoc;
48
49 import com.genimen.djeneric.language.Messages;
50 import com.genimen.djeneric.repository.DjPersistenceManager;
51 import com.genimen.djeneric.repository.DjPersistenceManagerFactory;
52 import com.genimen.djeneric.repository.DjRepositoryDescriptor;
53 import com.genimen.djeneric.tools.administrator.Administrator;
54 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
55 import com.genimen.djeneric.ui.Util;
56 import com.genimen.djeneric.util.DjLogger;
57
58 /**
59  * Description of the Class
60  *
61  * @author Wido Riezebos @created 27 mei 2002
62  */

63 public class DefineRepositoryDialog extends JDialog JavaDoc
64 {
65   private static final long serialVersionUID = 1L;
66   public static final String JavaDoc POINTBASE_DRIVER = "com.pointbase.jdbc.jdbcUniversalDriver";
67   public static final String JavaDoc UNA_DRIVER = "com.inet.tds.TdsDriver";
68   public static final String JavaDoc JTDS_DRIVER = "net.sourceforge.jtds.jdbc.Driver";
69   public static final String JavaDoc HYPERSONIC_DRIVER = "org.hsqldb.jdbcDriver";
70   public static final String JavaDoc DB2_DRIVER = "COM.ibm.db2.jdbc.app.DB2Driver";
71   public static final String JavaDoc DERBY_DRIVER = "com.ibm.db2.jcc.DB2Driver";
72   public static final String JavaDoc ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
73   public static final String JavaDoc DATASOURCE = DjRepositoryDescriptor.JNDI_DATASOURCE_KEY;
74
75   JPanel JavaDoc mainPanel = new JPanel JavaDoc();
76   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
77   JPanel JavaDoc labelPanel = new JPanel JavaDoc();
78   JPanel JavaDoc fieldsPanel = new JPanel JavaDoc();
79   JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
80   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
81   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
82   JButton JavaDoc _butOk = new JButton JavaDoc();
83   JButton JavaDoc _butCancel = new JButton JavaDoc();
84   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
85   JLabel JavaDoc _lblDriver = new JLabel JavaDoc();
86   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
87   JLabel JavaDoc jLabel3 = new JLabel JavaDoc();
88   JTextField JavaDoc _edtName = new JTextField JavaDoc();
89   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
90   JComboBox JavaDoc _cbbDriver = new JComboBox JavaDoc();
91   JTextField JavaDoc _edtURL = new JTextField JavaDoc();
92   JPanel JavaDoc jPanel5 = new JPanel JavaDoc();
93   JButton JavaDoc _butTest = new JButton JavaDoc();
94   boolean _wasCancelled = true;
95   DjRepositoryDescriptor _repositoryDescriptor;
96   JLabel JavaDoc jLabel4 = new JLabel JavaDoc();
97   JComboBox JavaDoc _cbbManagerClass = new JComboBox JavaDoc();
98   Administrator _administrator;
99   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
100   JCheckBox JavaDoc _chkSharedConnection = new JCheckBox JavaDoc();
101   JLabel JavaDoc jLabel5 = new JLabel JavaDoc();
102   JLabel JavaDoc jLabel6 = new JLabel JavaDoc();
103   JTextField JavaDoc _edtUser = new JTextField JavaDoc();
104   JTextField JavaDoc _edtPassword = new JTextField JavaDoc();
105   JLabel JavaDoc jLabel7 = new JLabel JavaDoc();
106   JComboBox JavaDoc _cbbTraceLevel = new JComboBox JavaDoc();
107
108   /**
109    * Constructor for the DefineRepositoryDialog object
110    *
111    * @param administrator
112    * Description of the Parameter
113    * @param title
114    * Description of the Parameter
115    * @param descr
116    * Description of the Parameter
117    * @exception Exception
118    * Description of the Exception
119    */

120   public DefineRepositoryDialog(Administrator administrator, String JavaDoc title, DjRepositoryDescriptor descr)
121       throws Exception JavaDoc
122   {
123     super(administrator, title, true);
124     _administrator = administrator;
125     _repositoryDescriptor = descr;
126     jbInit();
127
128     setSize(new Dimension JavaDoc(480, 280));
129
130     DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc(new String JavaDoc[]{DjPersistenceManagerFactory.TYPE_POLYMORPH,
131         DjPersistenceManagerFactory.TYPE_DIRECT});
132     _cbbManagerClass.setModel(model);
133     _cbbManagerClass.setEditable(true);
134
135     _cbbDriver.setModel(new DefaultComboBoxModel JavaDoc(new String JavaDoc[]{ORACLE_DRIVER, DERBY_DRIVER, DB2_DRIVER, HYPERSONIC_DRIVER,
136         JTDS_DRIVER, UNA_DRIVER, POINTBASE_DRIVER, DATASOURCE}));
137     _edtName.setText(descr.getName());
138
139     boolean isLocationSet = false;
140     for (int i = 0; i < model.getSize(); i++)
141     {
142       if (model.getElementAt(i).toString().equalsIgnoreCase(descr.getRepositoryLocation()))
143       {
144         _cbbManagerClass.setSelectedIndex(i);
145         isLocationSet = true;
146       }
147     }
148
149     if (!isLocationSet) _cbbManagerClass.getEditor().setItem(descr.getRepositoryLocation());
150
151     _cbbDriver.setSelectedIndex(-1);
152     _cbbDriver.getEditor().setItem(descr.getDriver());
153     _edtURL.setText(descr.getUrl());
154     _chkSharedConnection.setSelected(descr.isSharedConnection());
155     _edtUser.setText(descr.getSharedUser());
156     _edtPassword.setText(descr.getSharedPassword());
157
158     Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
159     Dimension JavaDoc frameSize = getSize();
160     setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
161
162     _cbbManagerClass_actionPerformed(null);
163
164     DefaultComboBoxModel JavaDoc traceModel = new DefaultComboBoxModel JavaDoc();
165     traceModel.addElement(new TraceLevel(DjPersistenceManager.TRACE_NOTHING, "Nothing"));
166     traceModel.addElement(new TraceLevel(DjPersistenceManager.TRACE_FLOW, "Flow"));
167     traceModel.addElement(new TraceLevel(DjPersistenceManager.TRACE_INTERNAL, "Internal"));
168     _cbbTraceLevel.setModel(traceModel);
169
170     int level = descr.getTraceLevel();
171     int idx = 0;
172     if (level == DjPersistenceManager.TRACE_FLOW) idx = 1;
173     if (level == DjPersistenceManager.TRACE_INTERNAL) idx = 2;
174     _cbbTraceLevel.setSelectedIndex(idx);
175
176     setVisible(true);
177   }
178
179   /**
180    * Description of the Method
181    *
182    * @return Description of the Return Value
183    */

184   public boolean wasCancelled()
185   {
186     return _wasCancelled;
187   }
188
189   void jbInit() throws Exception JavaDoc
190   {
191     mainPanel.setLayout(borderLayout1);
192     buttonPanel.setLayout(borderLayout2);
193     _butOk.setText(Messages.getString("global.Ok"));
194     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
195     {
196       public void actionPerformed(ActionEvent JavaDoc e)
197       {
198         _butOk_actionPerformed(e);
199       }
200     });
201     _butCancel.setText(Messages.getString("global.Cancel"));
202     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
203     {
204       public void actionPerformed(ActionEvent JavaDoc e)
205       {
206         _butCancel_actionPerformed(e);
207       }
208     });
209     jLabel1.setText(Messages.getString("global.Name"));
210     _lblDriver.setText(Messages.getString("DefineRepositoryDialog.Driver"));
211     labelPanel.setLayout(verticalFlowLayout1);
212     jLabel3.setText(Messages.getString("DefineRepositoryDialog.URL"));
213     fieldsPanel.setLayout(verticalFlowLayout2);
214     _cbbDriver.setPreferredSize(new Dimension JavaDoc(240, 21));
215     _cbbDriver.setEditable(true);
216     _butTest.setText(Messages.getString("DefineRepositoryDialog.Test"));
217     _butTest.addActionListener(new java.awt.event.ActionListener JavaDoc()
218     {
219       public void actionPerformed(ActionEvent JavaDoc e)
220       {
221         _butTest_actionPerformed(e);
222       }
223     });
224     jLabel4.setText(Messages.getString("DefineRepositoryDialog.Location"));
225     _cbbManagerClass.setPreferredSize(new Dimension JavaDoc(130, 21));
226     _cbbManagerClass.addActionListener(new java.awt.event.ActionListener JavaDoc()
227     {
228       public void actionPerformed(ActionEvent JavaDoc e)
229       {
230         _cbbManagerClass_actionPerformed(e);
231       }
232     });
233     _edtURL.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
234     {
235       public void focusGained(FocusEvent JavaDoc e)
236       {
237         _edtURL_focusGained(e);
238       }
239     });
240     jLabel2.setText(Messages.getString("DefineRepositoryDialog.SharedConnection"));
241     jLabel5.setText(Messages.getString("DefineRepositoryDialog.SharedUser"));
242     jLabel6.setText(Messages.getString("DefineRepositoryDialog.SharedPassword"));
243     jLabel7.setText(Messages.getString("DefineRepositoryDialog.TraceLevel"));
244     _edtUser.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
245     {
246       public void focusGained(FocusEvent JavaDoc e)
247       {
248         _edtUser_focusGained(e);
249       }
250     });
251     getContentPane().add(mainPanel);
252     mainPanel.add(labelPanel, BorderLayout.WEST);
253     labelPanel.add(jLabel1, null);
254     labelPanel.add(jLabel4, null);
255     labelPanel.add(_lblDriver, null);
256     labelPanel.add(jLabel3, null);
257     labelPanel.add(jLabel2, null);
258     mainPanel.add(fieldsPanel, BorderLayout.CENTER);
259     fieldsPanel.add(_edtName, null);
260     fieldsPanel.add(_cbbManagerClass, null);
261     fieldsPanel.add(_cbbDriver, null);
262     fieldsPanel.add(_edtURL, null);
263     fieldsPanel.add(_chkSharedConnection, null);
264     mainPanel.add(buttonPanel, BorderLayout.SOUTH);
265     buttonPanel.add(jPanel4, BorderLayout.EAST);
266     jPanel4.add(_butCancel, null);
267     jPanel4.add(_butOk, null);
268     buttonPanel.add(jPanel5, BorderLayout.WEST);
269     jPanel5.add(_butTest, null);
270     labelPanel.add(jLabel5, null);
271     labelPanel.add(jLabel6, null);
272     labelPanel.add(jLabel7, null);
273     fieldsPanel.add(_edtUser, null);
274     fieldsPanel.add(_edtPassword, null);
275     fieldsPanel.add(_cbbTraceLevel, null);
276     Util.sizeButtons(buttonPanel);
277     Util.sizeLabels(labelPanel);
278   }
279
280   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
281   {
282     setVisible(false);
283   }
284
285   void _butOk_actionPerformed(ActionEvent JavaDoc e)
286   {
287     if (_edtName.getText().trim().length() == 0)
288     {
289       JOptionPane.showMessageDialog(this, Messages.getString("DefineRepositoryDialog.NameRequired"));
290       return;
291     }
292
293     if (_chkSharedConnection.isSelected()
294         && (_edtUser.getText().trim().length() == 0 && _edtPassword.getText().trim().length() == 0))
295     {
296       JOptionPane.showMessageDialog(this, Messages.getString("DefineRepositoryDialog.UserPasswordReqd"));
297       return;
298     }
299     _wasCancelled = false;
300     _repositoryDescriptor.setName(_edtName.getText());
301     _repositoryDescriptor.setDriver(_cbbDriver.getEditor().getItem().toString());
302     _repositoryDescriptor.setRepositoryLocation(getRepositoryLocation());
303     _repositoryDescriptor.setUrl(_edtURL.getText());
304     _repositoryDescriptor.setSharedConnection(_chkSharedConnection.isSelected());
305     _repositoryDescriptor.setSharedUser(_edtUser.getText());
306     _repositoryDescriptor.setSharedPassword(_edtPassword.getText());
307     _repositoryDescriptor.setUrl(_edtURL.getText());
308     _repositoryDescriptor.setTraceLevel(((TraceLevel) _cbbTraceLevel.getSelectedItem()).getLevel());
309
310     setVisible(false);
311   }
312
313   /**
314    * Returns the repositoryLocation of the DefineRepositoryDialog object
315    *
316    * @return The repositoryLocation value
317    */

318   public String JavaDoc getRepositoryLocation()
319   {
320     return _cbbManagerClass.getEditor().getItem().toString();
321   }
322
323   /**
324    * Returns the driverLocation of the DefineRepositoryDialog object
325    *
326    * @return The driverLocation value
327    */

328   public String JavaDoc getDriverLocation()
329   {
330     if (_cbbDriver.getSelectedItem() == null) return "";
331     return _cbbDriver.getSelectedItem().toString();
332   }
333
334   void _butTest_actionPerformed(ActionEvent JavaDoc e)
335   {
336     try
337     {
338       DjRepositoryDescriptor rd = new DjRepositoryDescriptor(_edtName.getText(), _cbbManagerClass.getSelectedItem()
339           .toString(), _cbbDriver.getEditor().getItem().toString(), _edtURL.getText());
340
341       rd.setSharedConnection(_chkSharedConnection.isSelected());
342       rd.setSharedUser(_edtUser.getText());
343       rd.setSharedPassword(_edtPassword.getText());
344
345       Connection JavaDoc conn = _administrator.getConnection(rd);
346       conn.close();
347       JOptionPane.showMessageDialog(this, Messages.getString("DefineRepositoryDialog.ConnectionTestedOK"));
348     }
349     catch (Exception JavaDoc x)
350     {
351       DjLogger.log(x);
352       JOptionPane.showMessageDialog(this, x.getClass().getName() + ":\n" + x.getMessage(), Messages
353           .getString("DefineRepositoryDialog.ErrorDuringTest"), JOptionPane.ERROR_MESSAGE);
354       JOptionPane.showMessageDialog(this, Messages.getString("DefineRepositoryDialog.tips"), Messages
355           .getString("DefineRepositoryDialog.tipsTitle"), JOptionPane.INFORMATION_MESSAGE);
356     }
357   }
358
359   void _cbbManagerClass_actionPerformed(ActionEvent JavaDoc e)
360   {
361     // boolean b =
362
// getRepositoryLocation().equals(DjPersistenceManagerFactory.TYPE_POLYMORPH);
363
// _lblDriver.setVisible(b);
364
// _cbbDriver.setVisible(b);
365
}
366
367   void _edtURL_focusGained(FocusEvent JavaDoc e)
368   {
369     if (_edtURL.getText().trim().length() != 0) return;
370
371     String JavaDoc driver = getDriverLocation();
372
373     if (driver.indexOf("OracleDriver") != -1)
374     {
375       _edtURL.setText("jdbc:oracle:thin:@<SERVER>:1521:<SID>");
376     }
377     else if (driver.indexOf("DB2Driver") != -1)
378     {
379       _edtURL.setText("jdbc:db2:<DBNAME>");
380     }
381     else if (driver.indexOf("hsqldb") != -1)
382     {
383       _edtURL.setText("jdbc:hsqldb:hsql://localhost");
384       _edtUser.setText("sa");
385       _edtPassword.setText("");
386     }
387     else if (driver.indexOf("inet") != -1)
388     {
389       _edtURL.setText("jdbc:inetdae:<SERVER>?sql7=true&charset=Cp1250");
390     }
391     else if (driver.indexOf("jtds") != -1)
392     {
393       _edtURL.setText("jdbc:jtds:sqlserver://<SERVER>/<DBNAME>;charset=Cp1250");
394     }
395     else if (driver.indexOf("pointbase") != -1)
396     {
397       _edtURL.setText("jdbc:pointbase:server://localhost/<DBNAME>");
398       _edtUser.setText("PBPUBLIC");
399       _edtPassword.setText("");
400     }
401     else if (driver.indexOf("JNDI") != -1)
402     {
403       _edtURL.setText("jdbc/<DBNAME>");
404       _edtUser.setText("username");
405       _edtPassword.setText("");
406     }
407   }
408
409   void _edtUser_focusGained(FocusEvent JavaDoc e)
410   {
411     if (_edtUser.getText().trim().length() != 0) return;
412     String JavaDoc driver = getDriverLocation();
413
414     if (driver.indexOf("hsqldb") != -1)
415     {
416       _edtUser.setText("sa");
417       _edtPassword.setText("");
418     }
419     else if (driver.indexOf("pointbase") != -1)
420     {
421       _edtUser.setText("PBPUBLIC");
422       _edtPassword.setText("");
423     }
424
425   }
426 }
427
428 class TraceLevel
429 {
430
431   int _level;
432   String JavaDoc _title;
433
434   /**
435    * Constructor for the TraceLevel object
436    *
437    * @param level
438    * Description of the Parameter
439    * @param title
440    * Description of the Parameter
441    */

442   public TraceLevel(int level, String JavaDoc title)
443   {
444     _level = level;
445     _title = title;
446   }
447
448   /**
449    * Description of the Method
450    *
451    * @return Description of the Return Value
452    */

453   public String JavaDoc toString()
454   {
455     return _title;
456   }
457
458   /**
459    * Returns the level of the TraceLevel object
460    *
461    * @return The level value
462    */

463   public int getLevel()
464   {
465     return _level;
466   }
467 }
Popular Tags