InputDbDialog.java :  » XML-UI » xui32 » com » xoetrope » carousel » survey » Java Open Source

Java Open Source » XML UI » xui32 
xui32 » com » xoetrope » carousel » survey » InputDbDialog.java
package com.xoetrope.carousel.survey;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;

/**
 * A dialog for configuring input database connection.
 *
 * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
 * the GNU Public License (GPL), please see license.txt for more details. If
 * you make commercial use of this software you must purchase a commercial
 * license from Xoetrope.</p>
 * <p> $Revision: 1.5 $</p>
 */
//@todo show the available surveys, langauges, etc. in this dialog
public class InputDbDialog extends JDialog
{
  private static final String DEF_SURVEYNAME = "Survey1";
  private static final String DEF_LANGUAGE = "English";
  private static final String DEF_URL = "jdbc:hsqldb:hsql://localhost";
  private static final String DEF_DRIVER = "org.hsqldb.jdbcDriver";
  private static final String DEF_USERNAME = "sa";
  private static final String DEF_PASSWORD = "";
  
  public static final int LABEL_WIDTH = 80;
  public static final int EDIT_WIDTH = 160;
  public static final int ROW_HEIGHT = 24;
  public static final int WIDTH = LABEL_WIDTH + EDIT_WIDTH + 30;
  public static final int HEIGHT = 260;
  public static final int BUTTON_WIDTH = 80;
  public static final int BUTTON_HEIGHT = 24;
    
  protected JTextField surveyNameEdit;
  protected JTextField surveyLanguageEdit;
  protected JTextField dbUrlEdit;
  protected JTextField dbDriverEdit;
  protected JTextField dbUserEdit;  
  protected JPasswordField dbPasswordEdit;   
      
  protected boolean okPressed;  

  public InputDbDialog()
  {
    super();        
    setModal( true );
    okPressed = false;
    init();    
  }
    
  public boolean showDialog()
  {
    okPressed = false;
    setVisible( true );
    return okPressed;
  }
  
  private void init()
  {
    Dimension labelSize = new Dimension( LABEL_WIDTH, ROW_HEIGHT );
    Dimension editSize = new Dimension( EDIT_WIDTH, ROW_HEIGHT );
    Dimension buttonSize = new Dimension( BUTTON_WIDTH, BUTTON_HEIGHT );
    Dimension size = new Dimension( WIDTH, HEIGHT );
    setSize( size );
    setPreferredSize( size );

    TitledBorder titledBorder = BorderFactory.createTitledBorder( " Setup the database connection" );
        
    JPanel mainPanel = new JPanel();        
    mainPanel.setLayout( new BorderLayout() );
    JPanel editPanel = new JPanel();
    editPanel.setLayout( new GridBagLayout() );
    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout( new FlowLayout() );
    
    // survey name
    JLabel surveyNameLabel = new JLabel( "Survey name" );
    surveyNameLabel.setSize( labelSize );
    surveyNameLabel.setPreferredSize( labelSize );
    surveyNameLabel.setMinimumSize( labelSize );    
    surveyNameLabel.setHorizontalAlignment( SwingConstants.RIGHT );                    
    surveyNameEdit = new JTextField();
    surveyNameEdit.setSize( editSize );
    surveyNameEdit.setMinimumSize( editSize );
    surveyNameEdit.setPreferredSize( editSize );    
    
    // survey language
    JLabel surveyLanguageLabel = new JLabel( "Language" );        
    surveyLanguageLabel.setSize( labelSize );
    surveyLanguageLabel.setPreferredSize( labelSize );
    surveyLanguageLabel.setMinimumSize( labelSize );    
    surveyLanguageLabel.setHorizontalAlignment( SwingConstants.RIGHT );                
    surveyLanguageEdit = new JTextField();    
    surveyLanguageEdit.setSize( editSize );
    surveyLanguageEdit.setMinimumSize( editSize );
    surveyLanguageEdit.setPreferredSize( editSize );    
      
    // database url
    JLabel dbUrlLabel = new JLabel( "Database URL" );        
    dbUrlLabel.setSize( labelSize );
    dbUrlLabel.setPreferredSize( labelSize );
    dbUrlLabel.setMinimumSize( labelSize );    
    dbUrlLabel.setHorizontalAlignment( SwingConstants.RIGHT );                
    dbUrlEdit = new JTextField();    
    dbUrlEdit.setSize( editSize );
    dbUrlEdit.setMinimumSize( editSize );
    dbUrlEdit.setPreferredSize( editSize );    
    
    // database driver
    JLabel dbDriverLabel = new JLabel( "Database driver" );        
    dbDriverLabel.setSize( labelSize );
    dbDriverLabel.setPreferredSize( labelSize );
    dbDriverLabel.setMinimumSize( labelSize );    
    dbDriverLabel.setHorizontalAlignment( SwingConstants.RIGHT );                
    dbDriverEdit = new JTextField();    
    dbDriverEdit.setSize( editSize );
    dbDriverEdit.setMinimumSize( editSize );
    dbDriverEdit.setPreferredSize( editSize );    
    
    // database user
    JLabel dbUserLabel = new JLabel( "Username" );        
    dbUserLabel.setSize( labelSize );
    dbUserLabel.setPreferredSize( labelSize );
    dbUserLabel.setMinimumSize( labelSize );    
    dbUserLabel.setHorizontalAlignment( SwingConstants.RIGHT );                
    dbUserEdit = new JTextField();    
    dbUserEdit.setSize( editSize );
    dbUserEdit.setMinimumSize( editSize );
    dbUserEdit.setPreferredSize( editSize );    
    
    // database password
    JLabel dbPasswordLabel = new JLabel( "Password" );        
    dbPasswordLabel.setSize( labelSize );
    dbPasswordLabel.setPreferredSize( labelSize );
    dbPasswordLabel.setMinimumSize( labelSize );    
    dbPasswordLabel.setHorizontalAlignment( SwingConstants.RIGHT );                
    dbPasswordEdit = new JPasswordField();
    dbPasswordEdit.setSize( editSize );
    dbPasswordEdit.setMinimumSize( editSize );
    dbPasswordEdit.setPreferredSize( editSize );    
       
    // setup the default values
    surveyNameEdit.setText( DEF_SURVEYNAME );
    surveyLanguageEdit.setText( DEF_LANGUAGE );
    dbUrlEdit.setText( DEF_URL );
    dbDriverEdit.setText( DEF_DRIVER );
    dbUserEdit.setText( DEF_USERNAME );
    dbPasswordEdit.setText( DEF_PASSWORD );
        
    JButton buttonOk = new JButton( "Load" );
    buttonOk.setSize( buttonSize );
    buttonOk.setPreferredSize( buttonSize );
    buttonOk.setMinimumSize( buttonSize );
    buttonOk.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e ) {
        okPressed = true;
        InputDbDialog.this.setVisible( false );
      }
    });
        
    JButton buttonCancel = new JButton( "Cancel" );
    buttonCancel.setSize( buttonSize );
    buttonCancel.setPreferredSize( buttonSize );
    buttonCancel.setMinimumSize( buttonSize );      
    buttonCancel.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e ) {
        okPressed = false;
        InputDbDialog.this.setVisible( false );
      }
    });

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets( 1, 1, 1, 1 );
    
    // survey name
    c.gridx = 0; c.gridy = 0;
    editPanel.add( surveyNameLabel, c );        
    c.gridx = 1; c.gridy = 0;
    editPanel.add( surveyNameEdit, c );        
    
    // survey langauge
    c.gridx = 0; c.gridy = 1;
    editPanel.add( surveyLanguageLabel, c );    
    c.gridx = 1; c.gridy = 1;    
    editPanel.add( surveyLanguageEdit, c );
    
    // db url
    c.gridx = 0; c.gridy = 2;
    editPanel.add( dbUrlLabel, c );    
    c.gridx = 1; c.gridy = 2;    
    editPanel.add( dbUrlEdit, c );
        
    // db driver
    c.gridx = 0; c.gridy = 3;
    editPanel.add( dbDriverLabel, c );    
    c.gridx = 1; c.gridy = 3;    
    editPanel.add( dbDriverEdit, c );
    
    // db user
    c.gridx = 0; c.gridy = 4;
    editPanel.add( dbUserLabel, c );    
    c.gridx = 1; c.gridy = 4;    
    editPanel.add( dbUserEdit, c );
    
    // db password
    c.gridx = 0; c.gridy = 5;
    editPanel.add( dbPasswordLabel, c );    
    c.gridx = 1; c.gridy = 5;    
    editPanel.add( dbPasswordEdit, c );
            
    bottomPanel.add( buttonOk );
    bottomPanel.add( buttonCancel );
    mainPanel.add( editPanel, BorderLayout.CENTER );
    mainPanel.add( bottomPanel, BorderLayout.SOUTH );        
    editPanel.setBorder( titledBorder );
    add( mainPanel );
    pack();
    
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();        
    Dimension dialogSize = getSize();
    setLocation( ( screenSize.width - dialogSize.width ) / 2, ( screenSize.height - dialogSize.height ) / 2 );        
  }
  
  public String getSurveyName()
  {
    return surveyNameEdit.getText();
  }
  
  public String getSurveyLanguage()
  {
    return surveyLanguageEdit.getText();
  }
  
  public String getDbUrl()
  {
    return dbUrlEdit.getText();
  }
  
  public String getDbDriver()
  {
    return dbDriverEdit.getText();
  }
  
  public String getDbUser()
  {
    return dbUserEdit.getText();
  }
  
  public String getDbPassword()
  {
    return dbPasswordEdit.getText();
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.