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

Java Open Source » XML UI » xui32 
xui32 » com » xoetrope » carousel » survey » XQuestionsPanel.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.awt.Container;
import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;

/**
 * A panel containing question table
 *
 * <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 XQuestionsPanel extends JPanel
{

  protected JButton addQuestionB, deleteQuestionB, moveQuestionUpB, moveQuestionDownB;
  protected XSurveyEditorFrame editorFrame;
  protected XSurvey survey;
  protected XQuestionsTableModel questionsTableModel;
  protected XGroupComboBox groupComboBox;
  protected JLabel comboBoxLabel;
  
  public XQuestionsPanel()
  {
    super();    
    XProject project = XProjectManager.getCurrentProject();
    editorFrame = ( XSurveyEditorFrame ) project.getObject( "EditorFrame" );
    survey = ( XSurvey ) project.getObject( "Survey" );    
    init();
    createObservers();
  }

  protected void init()
  {
    setLayout( new BorderLayout() );
    
    XTable questionsTable = new XTable();
    questionsTable.setBorder( new EmptyBorder( 0, 0, 0, 0 ) );
    questionsTableModel = new XQuestionsTableModel( questionsTable );
    JScrollPane scrollPaneQuestions = new JScrollPane( questionsTable );
    TitledBorder questionBorder = BorderFactory.createTitledBorder( "Questions" );
    scrollPaneQuestions.setBorder( questionBorder );
  
    JToolBar questionsToolBar = new JToolBar();        
    addQuestionB = editorFrame.addButton( "addQuestion.png", "add question", questionsToolBar );    
    deleteQuestionB = editorFrame.addButton( "deleteQuestion.png", "delete question", questionsToolBar );       
    moveQuestionUpB = editorFrame.addButton( "moveQuestionUp.png", "move up", questionsToolBar );    
    moveQuestionDownB = editorFrame.addButton( "moveQuestionDown.png", "move down", questionsToolBar );
    
    groupComboBox = new XGroupComboBox();
        
    comboBoxLabel = new JLabel( "group  " );    
    questionsToolBar.add( comboBoxLabel );
    questionsToolBar.add( groupComboBox );
   
    deleteQuestionB.setEnabled( false );
    moveQuestionUpB.setEnabled( false );
    moveQuestionDownB.setEnabled( false );
    
    add( questionsToolBar, BorderLayout.NORTH );
    add( scrollPaneQuestions, BorderLayout.CENTER );
    
    questionsTableModel.getNotifier().addObserver( new Observer() {
      public void update( Observable o, Object arg ) {
        boolean state = ( arg != null );
        deleteQuestionB.setEnabled( state );
        moveQuestionUpB.setEnabled( state );
        moveQuestionDownB.setEnabled( state );        
      }
    });
    groupComboBox.getNotifier().addObserver( questionsTableModel );
    
    addQuestionB.addActionListener( questionsTableModel.getAddQuestion() );
    deleteQuestionB.addActionListener( questionsTableModel.getDeleteQuestion() );
    moveQuestionUpB.addActionListener( questionsTableModel.getMoveQuestionUp() );
    moveQuestionDownB.addActionListener( questionsTableModel.getMoveQuestionDown() );
  }
  
  protected void createObservers()
  {
    Observer comboObserver = ( new Observer() {
      public void update( Observable o, Object arg ) {refresh();}
    });
    survey.getAddGroupNotifier().addObserver( comboObserver );
    survey.getDeleteGroupNotifier().addObserver( comboObserver );
  }
  
  public void refresh()
  {
    groupComboBox.updateModel();
    boolean state = ( survey.getGroups().size() > 0 );
    groupComboBox.setEnabled( state );
    addQuestionB.setEnabled( state );
    if ( !state ) 
      questionsTableModel.update( null, null );
  }
  
  public void showQuestion( Question question )
  {
    int groupIdx = survey.getGroupIdxById( question.getGroup().getId() );
    groupComboBox.setSelectedIndex( groupIdx );
    questionsTableModel.selectQuestion( question );
  }
  
  public XQuestionsTableModel getQuestionsTableModel()
  {
    return questionsTableModel;
  }

  public JButton getAddQuestionB()
  {
    return addQuestionB;
  }
  
  public XGroupComboBox getGroupComboBox()
  {
    return groupComboBox;
  }
  
  protected JLabel getComboBoxLabel()
  {
    return comboBoxLabel;
  }
  
}
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.