# JndiDsPanel.java Sample

This topic inludes the source code for the JndiDsPanel.java Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/ExtensionDevKit/ControlDevKit/DBScripter/dbScript/designUI/

## Sample Source Code

`001 package dbScript.designUI; 002 003 import com.bea.ide.control.EditorSupport.*; 004 import java.awt.Component; 005 import javax.swing.JPanel; 006 import javax.swing.JTextField; 007 import javax.swing.JComponent; 008 import javax.swing.JLabel; 009 import java.util.Collection; 010 import javax.swing.JComboBox; 011 import javax.swing.DefaultComboBoxModel; 012 013 import java.awt.GridBagLayout; 014 import java.awt.GridBagConstraints; 015 import javax.swing.JSeparator; 016 import java.awt.Font; 017 import java.awt.Insets; 018 import java.awt.event.KeyAdapter; 019 import java.awt.event.KeyEvent; 020 import javax.swing.JButton; 021 import dbScript.util.DsUtils; 022 import javax.swing.JOptionPane; 023 import javax.swing.ButtonGroup; 024 import javax.swing.JRadioButton; 025 import java.awt.event.ActionListener; 026 import java.awt.event.ActionEvent; 027 import javax.swing.JCheckBox; 028 import java.text.MessageFormat; 029 import java.util.Properties; 030 031 import dbScript.util.Constants; 032 import javax.swing.SwingUtilities; 033 import com.bea.ide.control.EditorContext; 034 import weblogic.management.MBeanHome; 035 import java.io.File; 036 import java.net.URL; 037 import weblogic.management.configuration.ConfigurationMBean; 038 039 public class JndiDsPanel extends javax.swing.JPanel 040 { 041 private String Title = "Method Configuration "; 042 043 private JLabel dstLabel; 044 private JLabel dsLabel; 045 private JComboBox cmbExistingDS; 046 private JComboBox cmbNewDSTypes; 047 private JRadioButton rdoNewDS; 048 private JRadioButton rdoExistingDS; 049 protected JLabel lblMessage; 050 051 private Collection dataSources; 052 private String _selectedDSType=Constants.DS_DEFAULT_TYPE; 053 private dbScript.util.DsUtils _dsu; 054 055 private JCheckBox chkInstNwind; 056 private JCheckBox chkInstAny; 057 private JCheckBox chkPromptServer; 058 private JCheckBox chkPromptUsername; 059 private JLabel scriptFilePath; 060 private static InsertWizCreateJCX _wiz; 061 062 public JndiDsPanel(InsertWizCreateJCX wiz) 063 { 064 _wiz=wiz; 065 } 066 067 public void setInitialEnabledState() 068 { 069 InsertWizCreateJCX._fPanelIsInited=true; 070 SwingUtilities.invokeLater ( 071 new Runnable () 072 { 073 public void run () 074 { 075 try 076 { 077 rdoExistingDS.setSelected(false); 078 cmbExistingDS.setEnabled(false); 079 rdoNewDS.setSelected(true); 080 enableNewDS(true); 081 setMessage(" "); 082 } 083 catch ( Exception e ) 084 { 085 return; 086 } 087 } 088 } ); 089 } 090 091 public void retrieveDataSourceNames () 092 { 093 EditorContext ctx = _wiz.getEditorContext(); 094 if ((null==ctx) || !ctx.ensureServerRunning()) 095 { 096 setMessage("Unable to retrieve Data Source names"); 097 return; 098 } 099 final MBeanHome home = (MBeanHome)ctx.getMBeanHome(); 100 101 SwingUtilities.invokeLater ( 102 new Runnable () 103 { 104 public void run () 105 { 106 try 107 { 108 setMessage("Fetching DataSource list from server..."); 109 } 110 catch ( Exception e ) 111 { 112 return; 113 } 114 } 115 } ); 116 117 SwingUtilities.invokeLater ( 118 new Runnable () 119 { 120 public void run () 121 { 122 try 123 { 124 dataSources = _dsu.getDatasourceJNDINames(home); 125 cmbExistingDS.setModel ( new DefaultComboBoxModel ( dataSources.toArray () ) ); 126 setMessage(" "); 127 } 128 catch ( Exception e ) 129 { 130 setMessage("Error fetching DataSource names: " + e.getMessage()); 131 return; 132 } 133 134 } 135 } ); 136 } 137 138 139 public void retrieveNewDSTypes () 140 { 141 try 142 { 143 _dsu = new DsUtils(); 144 cmbNewDSTypes.setModel ( new DefaultComboBoxModel ( _dsu.getDriverList()) ); 145 } 146 catch (Exception e) 147 { 148 e.printStackTrace(); 149 } 150 } 151 152 public void buildUI () 153 { 154 if ( getComponents () != null && getComponents ().length > 0 ) removeAll (); 155 156 GridBagConstraints constraints = new GridBagConstraints (); 157 GridBagLayout layout = new GridBagLayout (); 158 159 setLayout ( layout ); 160 constraints.anchor = GridBagConstraints.WEST; 161 constraints.fill = GridBagConstraints.HORIZONTAL; 162 constraints.gridy= 0; 163 constraints.gridx = 0; 164 constraints.gridwidth=2; 165 constraints.ipadx=0; 166 constraints.ipady=0; 167 JLabel titleLabel = new JLabel ( "Data Source options", JLabel.LEFT ); 168 titleLabel.setFont(getHeaderFont()); 169 add( titleLabel, constraints); 170 171 constraints.gridy= 0; 172 constraints.gridx = 2; 173 constraints.gridwidth=1; 174 constraints.ipadx=0; 175 constraints.ipady=0; 176 constraints.anchor = GridBagConstraints.EAST; 177 constraints.fill = GridBagConstraints.NONE; 178 JButton btnHelp= new JButton( "Help"); 179 btnHelp.setFont(getHeaderFont()); 180 btnHelp.setMnemonic('H'); 181 btnHelp.addActionListener ( new ActionListener () 182 { 183 public void actionPerformed ( ActionEvent e ) 184 { 185 try 186 { 187 EditorContext ctx=_wiz.getContext(); 188 if (null==ctx) 189 { 190 setMessage("Can't access editor context."); 191 return; 192 } 193 File dir = ctx.getComponentsDir(); 194 195 File helpfile = new File(dir.getAbsolutePath() 196 +"/" + Constants.VENDORNAME 197 + "/help/en/index.html"); 198 ctx.invokeBrowser(helpfile.toURL()); 199 } 200 catch (Exception ex) 201 { 202 System.out.println(ex.getMessage()); 203 } 204 } 205 } ); 206 207 add( btnHelp, constraints); 208 209 // datasource 210 ButtonGroup group = new ButtonGroup (); 211 constraints.gridx = 0; 212 constraints.gridy++; 213 constraints.anchor = GridBagConstraints.WEST; 214 constraints.fill = GridBagConstraints.HORIZONTAL; 215 constraints.insets = new Insets(10,0,0,0); 216 rdoExistingDS = new JRadioButton ( "Use existing DataSource", true ); 217 rdoExistingDS.setMnemonic('U'); 218 rdoExistingDS.setEnabled ( true ); 219 rdoExistingDS.addActionListener ( new ActionListener () 220 { 221 public void actionPerformed ( ActionEvent e ) 222 { 223 enableNewDS(false); 224 } 225 } ); 226 group.add ( rdoExistingDS ); 227 add ( rdoExistingDS, constraints ); 228 229 constraints.insets = new Insets(0,15,0,0); 230 constraints.gridy++; 231 constraints.gridwidth=1; 232 add ( dsLabel = new JLabel ( "DataSource:", JLabel.LEFT ), constraints ); 233 234 constraints.gridx=1; 235 constraints.gridwidth=2; 236 constraints.ipadx=85; 237 add ( cmbExistingDS=new JComboBox(), constraints ); 238 cmbExistingDS.setEditable ( false ); 239 cmbExistingDS.addActionListener ( new ActionListener () 240 { 241 public void actionPerformed ( ActionEvent e ) 242 { 243 setDsType(); 244 } 245 } ); 246 dsLabel.setDisplayedMnemonic('D'); 247 dsLabel.setLabelFor(cmbExistingDS); 248 249 constraints.insets = new Insets(0,0,0,0); 250 constraints.gridx = 0; 251 constraints.gridy++; 252 constraints.gridwidth=3; 253 constraints.ipadx=0; 254 rdoNewDS = new JRadioButton ( "Create new DataSource (using name of new JCX file)", false); 255 rdoNewDS.setMnemonic('C'); 256 257 rdoNewDS.addActionListener ( new ActionListener () 258 { 259 public void actionPerformed ( ActionEvent e ) 260 { 261 enableNewDS(true); 262 } 263 } ); 264 rdoNewDS.setEnabled ( true); 265 266 group.add ( rdoNewDS ); 267 add ( rdoNewDS, constraints ); 268 269 270 constraints.insets = new Insets(0,15,0,0); 271 constraints.gridx = 0; 272 constraints.gridy++; 273 constraints.gridwidth=1; 274 add ( dstLabel = new JLabel ( "DB Type", JLabel.LEFT ), constraints ); 275 276 277 constraints.gridx =1; 278 constraints.gridwidth=2; 279 constraints.ipadx=85; 280 add( cmbNewDSTypes=new JComboBox(), constraints ); 281 cmbNewDSTypes.setEditable ( false ); 282 retrieveNewDSTypes(); 283 cmbNewDSTypes.setSelectedItem(Constants.DS_DEFAULT_TYPE); 284 285 cmbNewDSTypes.addActionListener ( new ActionListener () 286 { 287 public void actionPerformed ( ActionEvent e ) 288 { 289 setDsType(); 290 } 291 } ); 292 dstLabel.setDisplayedMnemonic('T'); 293 dstLabel.setLabelFor(cmbNewDSTypes); 294 295 constraints.gridx = 0; 296 constraints.gridy++; 297 constraints.gridwidth=3; 298 constraints.ipadx=0; 299 chkPromptServer = new JCheckBox ( "Prompt for DataSource URL", false); 300 chkPromptServer.setMnemonic('P'); 301 add ( chkPromptServer, constraints ); 302 303 constraints.gridy++; 304 chkPromptUsername = new JCheckBox ( "Prompt for Database User and Password", true); 305 add ( chkPromptUsername, constraints ); 306 chkPromptUsername.setMnemonic('r'); 307 308 JLabel scriptLabel = new JLabel("Script methods to create:"); 309 constraints.insets=new Insets(10,0,0,0); 310 constraints.gridx = 0; 311 constraints.gridy++; 312 constraints.gridwidth=3; 313 add (scriptLabel, constraints); 314 315 constraints.insets=new Insets(0,15,0,0); 316 constraints.gridx = 0; 317 constraints.gridy++; 318 constraints.gridwidth=1; 319 chkInstNwind = new JCheckBox ( "Built-in script: ", true); 320 chkInstNwind.setMnemonic('B'); 321 chkInstNwind.setEnabled ( true ); 322 add ( chkInstNwind, constraints ); 323 324 constraints.gridx=1; 325 constraints.gridwidth=2; 326 add ( scriptFilePath=new JLabel ("..." , JLabel.LEFT ), constraints ); 327 328 constraints.gridx=0; 329 constraints.gridy++; 330 constraints.gridwidth=3; 331 chkInstAny = new JCheckBox ( "Run any script file", true); 332 chkInstAny.setMnemonic('a'); 333 chkInstAny.setEnabled ( true ); 334 add ( chkInstAny, constraints ); 335 336 constraints.insets = new Insets(10,0,0,0); 337 constraints.gridx=0; 338 constraints.gridy++; 339 constraints.gridwidth=3; 340 add ( lblMessage = new JLabel ( "", JLabel.LEFT ) , constraints); 341 InsertWizCreateJCX._fPanelIsInited=false; 342 343 344 } 345 public void enableNewDS(boolean fNewDSSelected) 346 { 347 cmbExistingDS.setEnabled (!fNewDSSelected); 348 dsLabel.setEnabled(!fNewDSSelected); 349 cmbNewDSTypes.setEnabled (fNewDSSelected ); 350 dstLabel.setEnabled(fNewDSSelected); 351 chkPromptServer.setEnabled (fNewDSSelected); 352 chkPromptUsername.setEnabled (fNewDSSelected); 353 setDsType(); 354 } 355 356 public boolean isValidState () 357 { 358 if (rdoExistingDS.isSelected() ) 359 { 360 String dsn = ( String ) cmbExistingDS.getSelectedItem (); 361 if ( dsn == null || dsn.length () < 1 ) 362 { 363 setMessage ("Choose a datasource"); 364 return false; 365 } 366 } 367 else 368 { 369 String dst = ( String ) cmbNewDSTypes.getSelectedItem (); 370 if ( dst == null || dst.length () < 1 ) 371 { 372 setMessage ("Choose a driver type"); 373 return false; 374 } 375 } 376 return true; 377 378 } 379 380 381 protected String getHeaderFontName () 382 { // todo should be a property 383 return "SansSerif"; 384 } 385 386 protected Font getHeaderFont () 387 { 388 return new Font ( getHeaderFontName (), Font.BOLD, 12 ) ; 389 } 390 391 392 protected void setMessage ( String message) 393 { 394 if ( lblMessage == null ) return; 395 if (!lblMessage.isEnabled()) 396 lblMessage.setEnabled(true); 397 lblMessage.setText ( message ); 398 lblMessage.repaint (); 399 } 400 401 private String getPathForScript( String scriptName) 402 { 403 return ("/dbScript/scripts/" + _selectedDSType + "/" + scriptName + ".sql"); 404 } 405 private void setDsType() 406 { 407 String sel=null; 408 try 409 { 410 if (rdoNewDS.isSelected()) 411 { 412 sel = (String)cmbNewDSTypes.getSelectedItem(); 413 } 414 else 415 { 416 sel = (String)(cmbExistingDS.getSelectedItem()); 417 if (sel != null) 418 sel = _dsu.getTypeForDSName(sel); 419 } 420 } 421 catch (Exception e) 422 { 423 System.out.println("failed in setDsType"); 424 e.printStackTrace(); 425 } 426 427 if (sel==null) 428 _selectedDSType = Constants.DS_DEFAULT_TYPE; 429 else 430 _selectedDSType = sel; 431 432 if (scriptFilePath!=null) 433 scriptFilePath.setText(getPathForScript("instnwind")); 434 435 436 } 437 public String getJcxFileContent(String packageName, String jcxName) 438 { 439 String dsName; 440 441 // if creating a new datasource, use jcxName as base; 442 if (rdoExistingDS.isSelected()) 443 dsName=(String)cmbExistingDS.getSelectedItem(); 444 else 445 dsName=jcxName; 446 447 StringBuffer sb = new StringBuffer(); 448 sb.append(MessageFormat.format( 449 getHeaderTemplate(), 450 new Object [] { packageName, 451 _dsu.getDsName(dsName,true), //use the NoTx Name for running scripts 452 jcxName })); 453 454 if (rdoNewDS.isSelected()) 455 { 456 String sProps=_dsu.getDriverProperties(_selectedDSType); 457 String urlDefaults=_dsu.getDriverURLDefaults(_selectedDSType); 458 String [] aPrompts=_dsu.getDriverURLPrompts(_selectedDSType); 459 StringBuffer sbParams=new StringBuffer(); 460 String methodName = Constants.METH_CREATEDS + dsName; 461 462 if (chkPromptServer.isSelected()) 463 { 464 String sc=", "; 465 for (int i=0;i<aPrompts.length;i++) 466 { 467 if ((i==(aPrompts.length -1)) && (!chkPromptUsername.isSelected())) 468 sc=" "; 469 sbParams.append("String ").append(aPrompts[i]).append(sc); 470 } 471 } 472 if (chkPromptUsername.isSelected()) 473 sbParams.append("String user, String password"); 474 475 sb.append(MessageFormat.format( 476 getCreateDSMethodTemplate(), 477 new Object [] { cmbNewDSTypes.getSelectedItem(), 478 urlDefaults, sProps, 479 Boolean.toString(chkPromptServer.isSelected()), 480 Boolean.toString(chkPromptUsername.isSelected()), 481 Constants.WLSADMIN, Constants.WLSADMINPWD, 482 dsName, methodName, sbParams.toString()})); 483 } 484 if (chkInstNwind.isSelected()) 485 { 486 String scriptPath = getPathForScript("instnwind"); 487 sb.append(MessageFormat.format( 488 getScriptMethodTemplate(), 489 new Object [] { scriptPath, "Instnwind" })); 490 } 491 if (chkInstAny.isSelected()) 492 sb.append(getFileMethodTemplate()); 493 494 if ((chkInstNwind.isSelected()) || (chkInstAny.isSelected())) 495 sb.append(getCallbackTemplate()); 496 497 sb.append("} \n"); 498 return sb.toString(); 499 } 500 501 private String getHeaderTemplate() 502 { 503 String sTemplate = 504 "package {0}; \n\n " + 505 "import com.bea.control.*; \n " + 506 507 " /** \n" + 508 " * @" + Constants.TAG_CONN + " " + Constants.ATTR_DS + "=\"{1}\" \n" + 509 " */ \n" + 510 "public interface {2} extends dbScript.DBScriptRunner, com.bea.control.ControlExtension \n " + 511 "'{' \n \n"; 512 return sTemplate; 513 } 514 515 private String getScriptMethodTemplate() 516 { 517 String sTemplate = 518 " /** \n" + 519 " * @" + Constants.TAG_DBSCR + " " + Constants.ATTR_SF + "=\"{0}\" \n" + 520 " * \t\t @common:message-buffer enable=\"true\" \n" + 521 " */ \n" + 522 " public void " + Constants.METH_RUNSCRIPT + "{1}(); \n \n" ; 523 return sTemplate; 524 } 525 private String getFileMethodTemplate() 526 { 527 String sTemplate = 528 " /** \n" + 529 " * \t\t @common:message-buffer enable=\"true\" \n" + 530 " */ \n" + 531 532 " public void " + Constants.METH_RUNFILE + "(String filePath); \n \n" ; 533 return sTemplate; 534 } 535 private String getCallbackTemplate() 536 { 537 String sTemplate = 538 "\n\t\t interface Callback extends dbScript.DBScriptRunner.Callback \n" + 539 " \t\t { \n" + 540 " \t\t\t void onScriptDone(String msg); \n" + 541 " \t\t } \n\n" ; 542 return sTemplate; 543 } 544 545 private String getCreateDSMethodTemplate() 546 { 547 String sTemplate = 548 " /** \n" + 549 " * @" + Constants.TAG_DBSCR + " " + Constants.ATTR_NEWDSTYPE + "=\"{0}\" \n" + 550 " * \t\t " + Constants.ATTR_URL + "=\"{1}\" \n" + 551 " * \t\t " + Constants.ATTR_PROPS + "=\"{2}\" \n" + 552 " * \t\t " + Constants.ATTR_PROMPTURL + "={3} \n" + 553 " * \t\t " + Constants.ATTR_PROMPTUSER + "={4} \n" + 554 " * \t\t " + Constants.ATTR_ADMINUSER + "=\"{5}\" \n" + 555 " * \t\t " + Constants.ATTR_ADMINPWD + "=\"{6}\" \n" + 556 " * \t\t " + Constants.ATTR_NEWDSNAME + "=\"{7}\" \n" + 557 " * \t\t " + Constants.ATTR_CREATENOTX + "=true \n" + 558 " */ \n" + 559 " public String {8}( {9} ); \n \n" ; 560 return sTemplate; 561 } 562 }`