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

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

import com.xoetrope.survey.Question;
import com.xoetrope.survey.QuestionGroup;
import com.xoetrope.survey.Survey;
import com.xoetrope.survey.SurveyManager;
import java.awt.BorderLayout;
import java.util.Vector;
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.EtchedBorder;
import javax.swing.border.TitledBorder;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;

/**
 * A dialog for adding new groups
 *
 * <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>
 */
public class NewGroupDialog extends JDialog
{  
  public static final int LABEL_WIDTH = 70;
  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 = 200;
  public static final int BUTTON_WIDTH = 80;
  public static final int BUTTON_HEIGHT = 24;
  
  protected XSurvey survey;
  protected JTextField groupId;
  protected JTextField groupName;
  protected JComboBox nextGroup;
  protected boolean okPressed;
  
  /**
   * Creates a new instance of NewGroupDialog
   */
  public NewGroupDialog() {
    super();
    XProject project = XProjectManager.getCurrentProject();
    survey = ( XSurvey ) project.getObject( "Survey" );
    
    setModal( true );
    okPressed = false;
    init();
  }
  
  /**
   * Initialises the new group dialog
   */
  protected 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 );
    
    Border titledBorder = BorderFactory.createTitledBorder( " Add new group" );
    
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout( new BorderLayout() );
    JPanel editPanel = new JPanel();
    editPanel.setLayout( new GridBagLayout() );
    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout( new FlowLayout() );
    
    JLabel groupIdLabel = new JLabel( "Group id: " );      
    groupIdLabel.setSize( labelSize );
    groupIdLabel.setPreferredSize( labelSize );
    groupIdLabel.setMinimumSize( labelSize );
    groupIdLabel.setHorizontalAlignment( SwingConstants.RIGHT );    
        
    groupId = new JTextField();
    groupId.setSize( editSize );
    groupId.setMinimumSize( editSize );
    groupId.setPreferredSize( editSize );    
    
    JLabel groupNameLabel = new JLabel( "Group name: " );
    groupNameLabel.setSize( labelSize );
    groupNameLabel.setPreferredSize( labelSize );
    groupNameLabel.setMinimumSize( labelSize );    
    groupNameLabel.setHorizontalAlignment( SwingConstants.RIGHT );    
    
    groupName = new JTextField();
    groupName.setSize( editSize );
    groupName.setPreferredSize( editSize );
    groupName.setMinimumSize( editSize );    

    JLabel nextGroupLabel = new JLabel( "Next group: " );
    nextGroupLabel.setSize( labelSize );
    nextGroupLabel.setPreferredSize( labelSize );
    nextGroupLabel.setMinimumSize( labelSize );    
    nextGroupLabel.setHorizontalAlignment( SwingConstants.RIGHT );    
    
    nextGroup = new JComboBox( survey.getTargetGroups() );
    nextGroup.setSize( editSize );
    nextGroup.setMinimumSize( editSize );
    nextGroup.setPreferredSize( editSize );    
    
    JButton buttonOk = new JButton( "Ok" );
    buttonOk.setSize( buttonSize );
    buttonOk.setPreferredSize( buttonSize );
    buttonOk.setMinimumSize( buttonSize );
    buttonOk.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e ) {
        okPressed = true;
        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;
        setVisible( false );
      }
    });
    
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets( 1, 1, 1, 1 );
    
    c.gridx = 0; c.gridy = 0;
    editPanel.add( groupIdLabel, c );    
    c.gridx = 1; c.gridy = 0;
    editPanel.add( groupId, c );    
    c.gridx = 0; c.gridy = 1;     
    editPanel.add( groupNameLabel, c );            
    c.gridx = 1; c.gridy = 1;
    editPanel.add( groupName, c );    
    c.gridx = 0; c.gridy = 2;
    editPanel.add( nextGroupLabel, c );    
    c.gridx = 1; c.gridy = 2;
    editPanel.add( nextGroup, c );
    
    buttonOk.setMinimumSize( buttonSize );
    buttonOk.setPreferredSize( buttonSize );
    buttonOk.setSize( buttonSize );
    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 );    
  }
  
  /**
   * Shows this dialog
   */
  public boolean showDialog()
  {
    groupId.setText( String.valueOf( survey.getNextGroupId() ) );
    groupName.setText( "" );
    if ( survey.getGroups().size() == 0 ) {
      nextGroup.setEnabled( false );
    } else {
      nextGroup.setEnabled( true );
      nextGroup.setSelectedIndex( 0 );
    }
    groupName.requestFocus();
    okPressed = false;
    setVisible( true );
    return okPressed;
  }
  
  /**
   * Gets the id of this group
   */
  public int getGroupId()
  {
    return new Integer( groupId.getText() ).intValue();
  }
  
  public String getGroupName()
  {
    return groupName.getText();
  }
  
  public XTargetGroup getNextGroup()
  {
    return (XTargetGroup)nextGroup.getSelectedItem();
  }
  
}
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.