Package periman

Source Code of periman.QuestionnaireRowSpinner$SpinnerProperties

package periman;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.w3c.dom.Document;
import org.w3c.dom.Element;



public class QuestionnaireRowSpinner implements QuestionnaireRow
{
  JDialog parentDialog;

  String labelCaption;
  int minValue;
  int maxValue;
  int stepSize;
  int defaultValue;

  SpinnerProperties propertiesDialog = null;

  public QuestionnaireRowSpinner(JDialog mw)
  {
    parentDialog = mw;
  }

  @Override
  public boolean getPropertiesRowFromUser() {
   
    if(propertiesDialog == null
      propertiesDialog = new SpinnerProperties( parentDialog );
    else
      propertiesDialog.showForModify();

    if(propertiesDialog.isAccepted() == false)
      return false;

    minValue = propertiesDialog.getMinValue();
    maxValue = propertiesDialog.getMaxValue();
    stepSize = propertiesDialog.getStepSize();
    defaultValue = propertiesDialog.getDefaultValue();
    labelCaption = propertiesDialog.getCaption();

    return true;
  }

  @Override
  public Element toDom(Document dom) {
    Element element = dom.createElement( "spinner" );
    element.setAttribute( "label", labelCaption);
    element.setAttribute( "default", ""+defaultValue );
    element.setAttribute( "min", ""+minValue);
    element.setAttribute( "max", ""+maxValue);
    element.setAttribute( "step", ""+stepSize);

    return element;
  }

  @Override
  public String toString()
  {
    return "Spinner: " + labelCaption + ", min value: " + minValue + ", max value: " + maxValue + ", step size:" + stepSize + ", default value: " + defaultValue;
  }



  @SuppressWarnings("serial")
  class SpinnerProperties extends JDialog implements ActionListener, ChangeListener
  {
    // hold true if user click ok to close the dialog or hold false if user click cancel to close the dialog.
    private boolean accepted=false;
    boolean isAccepted() {return accepted;}

    private SpinnerNumberModel minModel;
    public int getMinValue() { return minModel.getNumber().intValue(); }
    private SpinnerNumberModel maxModel;
    public int getMaxValue() { return maxModel.getNumber().intValue(); }
    private SpinnerNumberModel stepSizeModel;
    public int getStepSize() { return stepSizeModel.getNumber().intValue(); }
    private SpinnerNumberModel defaultModel;
    public int getDefaultValue() {return defaultModel.getNumber().intValue(); }
    private JTextField capField;
    public String getCaption() { return capField.getText(); }


    boolean alreadyInHere = false;

    SpinnerProperties(JDialog mw)
    {
      super(mw, true);
      createGui();
      setTitle( "Spinner Row Properties");
      pack();
      setLocation( 400, 400 );
      setVisible(true);
    }

    void createGui()
    {
      // set the layout of the dialog to be grid bag
      GridBagLayout dialogLayout = new GridBagLayout();
      setLayout( dialogLayout );
     
      // create the constraints for the main panel in the dialog- to cover the entire area.
      GridBagConstraints panelConstraint = new GridBagConstraints();
      panelConstraint.gridx = panelConstraint.gridy = 0;
      panelConstraint.anchor = GridBagConstraints.FIRST_LINE_START;
      panelConstraint.weightx = panelConstraint.weighty = 1;
      panelConstraint.fill = GridBagConstraints.BOTH;     
     
      JPanel panel = new JPanel( new GridBagLayout() );
      add(panel, panelConstraint);

      //
      // ***** first row
      GridBagConstraints lblCapConstraint = new GridBagConstraints();
      lblCapConstraint.anchor = GridBagConstraints.FIRST_LINE_START;
      lblCapConstraint.gridx = 0;
      lblCapConstraint.gridy = 0;
     
      JLabel lblCapLbl = new JLabel("Label Caption");
     
      panel.add(lblCapLbl, lblCapConstraint);
     
     
      GridBagConstraints capFieldConstraint = new GridBagConstraints();
      capFieldConstraint.gridx = 1;
      capFieldConstraint.gridy = 0;
      capFieldConstraint.fill = GridBagConstraints.HORIZONTAL;
      capFieldConstraint.weightx = 1;
     
      capField = new JTextField();
      panel.add(capField, capFieldConstraint);

      // ***** second row
      GridBagConstraints defaultLblConstraint = new GridBagConstraints();
      defaultLblConstraint.gridx = 0;
      defaultLblConstraint.gridy = 1;
      defaultLblConstraint.anchor = GridBagConstraints.FIRST_LINE_START;
     
      JLabel defaultLbl = new JLabel("Default value");
      panel.add(defaultLbl, defaultLblConstraint);
     
     
      GridBagConstraints defaultSpinnerConstraint = new GridBagConstraints();
      defaultSpinnerConstraint.gridx = 1;
      defaultSpinnerConstraint.gridy = 1;
      defaultSpinnerConstraint.fill = GridBagConstraints.HORIZONTAL;
      defaultModel= new SpinnerNumberModel();
      JSpinner defaultSpinner = new JSpinner( defaultModel );
      defaultSpinner.addChangeListener( this );
      panel.add(defaultSpinner, defaultSpinnerConstraint);

      // ***** 3rt row
      GridBagConstraints minLblConstraint = new GridBagConstraints();
      minLblConstraint.gridx = 0;
      minLblConstraint.gridy = 2;
      minLblConstraint.anchor = GridBagConstraints.FIRST_LINE_START;
     
      JLabel minLbl = new JLabel("Min value");
      panel.add(minLbl, minLblConstraint);

      GridBagConstraints minSpinnerConstraint = new GridBagConstraints();
      minSpinnerConstraint.gridx = 1;
      minSpinnerConstraint.gridy = 2;
      minSpinnerConstraint.fill = GridBagConstraints.HORIZONTAL;

     
      minModel = new SpinnerNumberModel();
      JSpinner minSpinner = new JSpinner( minModel );
      minSpinner.addChangeListener( this );
      panel.add(minSpinner , minSpinnerConstraint);
     
      // ***** 4th row
      GridBagConstraints maxLblConstraint = new GridBagConstraints();
      maxLblConstraint.gridx = 0;
      maxLblConstraint.gridy = 3;
      maxLblConstraint.anchor = GridBagConstraints.FIRST_LINE_START;
     
      JLabel maxLbl = new JLabel("Max value");
      panel.add(maxLbl, maxLblConstraint);

      GridBagConstraints maxSpinnerConstraint = new GridBagConstraints();
      maxSpinnerConstraint.gridx = 1;
      maxSpinnerConstraint.gridy = 3;
      maxSpinnerConstraint.fill = GridBagConstraints.HORIZONTAL;
   
     
      maxModel = new SpinnerNumberModel();
      maxModel.setValue( 100 );
      JSpinner maxSpinner = new JSpinner( maxModel );
      maxSpinner.addChangeListener( this );
      panel.add(maxSpinner , maxSpinnerConstraint);

      // ***** 5th row
      GridBagConstraints stepSizeLblConstraint = new GridBagConstraints();
      stepSizeLblConstraint.gridx = 0;
      stepSizeLblConstraint.gridy = 4;
      JLabel stepSizeLbl = new JLabel("Step size value");
      panel.add(stepSizeLbl, stepSizeLblConstraint);

      GridBagConstraints stepSizeSpinnerConstraint = new GridBagConstraints();
      stepSizeSpinnerConstraint.gridx = 1;
      stepSizeSpinnerConstraint.gridy = 4;
      stepSizeSpinnerConstraint.fill = GridBagConstraints.HORIZONTAL;
      stepSizeSpinnerConstraint.anchor = GridBagConstraints.FIRST_LINE_START;
     
      stepSizeModel = new SpinnerNumberModel();
      stepSizeModel.setValue(1);
      stepSizeModel.setMinimum(1);
      JSpinner stepSizeSpinner = new JSpinner( stepSizeModel );
      stepSizeSpinner.addChangeListener( this );
      panel.add(stepSizeSpinner , stepSizeSpinnerConstraint);

      // ****** 6 row

      GridBagConstraints okBtnConstraint = new GridBagConstraints();
      okBtnConstraint.gridx = 0;
      okBtnConstraint.gridy = 5;
     
      JButton okBtn = new JButton("OK");
      okBtn.addActionListener(this);
      okBtn.setActionCommand("OK");
      okBtn.setMnemonic( KeyEvent.VK_O );
      panel.add(okBtn, okBtnConstraint);
     
      GridBagConstraints cancelBtnConstraint = new GridBagConstraints();
      cancelBtnConstraint.gridx = 1;
      cancelBtnConstraint.gridy = 5;
     
      JButton cancelBtn = new JButton("Cancel");
      cancelBtn.setActionCommand("Cancel");
      cancelBtn.addActionListener(this);
      cancelBtn.setMnemonic( KeyEvent.VK_C );
      panel.add(cancelBtn, cancelBtnConstraint);

    }

    @Override
    public void actionPerformed(ActionEvent fp)
    {
      if(fp.getActionCommand().equals("OK"))
      {
        accepted = true;
        setVisible(false);
      }
     
      if(fp.getActionCommand().equals("Cancel") )
      {
        accepted = false;
        setVisible(false);
      }
     
    }

    @Override
    public void stateChanged(ChangeEvent e) {
      if(alreadyInHere == true)
        return;

      alreadyInHere = true;
      if(defaultModel.getNumber().intValue() < minModel.getNumber().intValue())
        defaultModel.setValue( minModel.getNumber() );
     
      if(defaultModel.getNumber().intValue() > maxModel.getNumber().intValue())
        defaultModel.setValue( maxModel.getNumber() );

      defaultModel.setMaximum( (Comparable<?>) maxModel.getNumber() );
      defaultModel.setMinimum( (Comparable<?>) minModel.getNumber() );
      defaultModel.setStepSize( stepSizeModel.getNumber() );


      alreadyInHere = false;
     
    }

    public void showForModify()
    {
      accepted = false;
      setVisible( true );
    }

  } // SpinnerProperties

}
TOP

Related Classes of periman.QuestionnaireRowSpinner$SpinnerProperties

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.