ChoosSemaphoreSelection.java :  » Report » jmagallanes-1.0 » com » calipso » reportgenerator » userinterface » Java Open Source

Java Open Source » Report » jmagallanes 1.0 
jmagallanes 1.0 » com » calipso » reportgenerator » userinterface » ChoosSemaphoreSelection.java
package com.calipso.reportgenerator.userinterface;

import com.calipso.reportgenerator.reportcalculator.expression.*;
import com.calipso.reportgenerator.common.InfoException;
import com.calipso.reportgenerator.common.ShowExceptionMessageDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.MessageFormat;
import java.util.Vector;

/**
 * Crea un Dialog de selleccion de Colores
 */

public class ChoosSemaphoreSelection extends javax.swing.JDialog {
  private Color bgColor;
  private ColorCondition resultColorCondition;
  private JPanel jPanel4;
  private JLabel colorjLabel;
  private JLabel valuejLabel;
  private JButton okjButton;
  private JTextField fromjTextField;
  private JRadioButton greaterOrEqualTojRadioButton;
  private JRadioButton greaterThanjRadioButton;
  private JPanel jPanel3;
  private Color storedColor;
  private JButton canceljButton;
  private JTextField colorjTextField;
  private JRadioButton lessThanjRadioButton;
  private JPanel jPanel2;
  private JPanel jPanel5;
  private ButtonGroup operatorbuttonGroup;
  private JTextField tojTextField;
  private JButton colorjButton;
  private JPanel jPanel7;
  private JLabel tojLabel;
  private JPanel jPanel1;
  private JPanel jPanel6;
  private JRadioButton lessOrEqualTojRadioButton;
  private JLabel fromjLabel;
  private JTextField valuejTextField;
  private JRadioButton equalTojRadioButton;
  private JTabbedPane tabPanel;
  private int seleccionRadioButton;
  private MetricState metricState;


  /**
   * Obtiene un ColorCondition
   * @param owner
   * @param metricState
   * @return
   */
  public static ColorCondition newColorCondition(Frame owner, MetricState metricState, ColorCondition condition) {
    ChoosSemaphoreSelection dialog = new ChoosSemaphoreSelection(owner, metricState, condition);
    ColorCondition colorCondition = dialog.execute();
    dialog.dispose();
    return colorCondition;
  }

  /**
   *  Muestra el Dialog y retorna un colorCondition nuevo
   * @return
   */
  public ColorCondition execute() {
    this.setVisible(true);
    return this.resultColorCondition;
  }

  /**
   * Crea un un Dialog modal  ChoosSemaphoreSelection
   * @param owner
   * @param metricState
   */
  public ChoosSemaphoreSelection(Frame owner, MetricState metricState, ColorCondition condition) {
    super(owner, true);
    this.metricState = metricState;
    resultColorCondition = null;
    initComponents();
    setValuesComponents(condition);
  }

  /**
   * Setea los valores de los componentes para un <code>ColorCondition</code> determinado
   * @param condition
   */
  private void setValuesComponents(ColorCondition condition) {
    if(condition != null) {
      storedColor = condition.getColor();
      bgColor = storedColor;
      Expression expression = condition.getExpression();
      colorjTextField.setBackground(storedColor);
      if(expression instanceof GreaterThan) {
        greaterThanjRadioButton.setSelected(true);
        greaterThanjRadioButtonMousePressed();
        Float aFloat = Float.valueOf(expression.getArguments() [1].toString());
        valuejTextField.setText(String.valueOf(aFloat.intValue()));
      } else if(expression instanceof GreaterOrEqualTo) {
        greaterOrEqualTojRadioButton.setSelected(true);
        greaterOrEqualTojRadioButtonMousePressed();
        Float aFloat = Float.valueOf(expression.getArguments() [1].toString());
        valuejTextField.setText(String.valueOf(aFloat.intValue()));
      } else if(expression instanceof LessThan) {
        lessThanjRadioButton.setSelected(true);
        lessThanjRadioButtonMousePressed();
        Float aFloat = Float.valueOf(expression.getArguments() [1].toString());
        valuejTextField.setText(String.valueOf(aFloat.intValue()));
      } else if(expression instanceof LessOrEqualTo) {
        lessOrEqualTojRadioButton.setSelected(true);
        lessOrEqualTojRadioButtonMousePressed();
        Float aFloat = Float.valueOf(expression.getArguments() [1].toString());
        valuejTextField.setText(String.valueOf(aFloat.intValue()));
      } else if(expression instanceof EqualTo) {
        equalTojRadioButton.setSelected(true);
        equalTojRadioButtonMousePressed();
        Float aFloat = Float.valueOf(expression.getArguments() [1].toString());
        valuejTextField.setText(String.valueOf(aFloat.intValue()));
      } else if(expression instanceof AndExp) {
        tabPanel.setSelectedIndex(1);
        Expression [] expressions = expression.getArguments();
        Expression greaterOrEqualTo = expressions [0];
        Expression lessOrEqualTo = expressions [1];
        Float aFloat = Float.valueOf(greaterOrEqualTo.getArguments() [1].toString());
        fromjTextField.setText(String.valueOf(aFloat.intValue()));
        aFloat = Float.valueOf(lessOrEqualTo.getArguments() [1].toString());
        tojTextField.setText(String.valueOf(aFloat.intValue()));
      }
    }
  }

  /**
   * Inicializa los componentes del Dialog
   */
  private void initComponents() {
    operatorbuttonGroup = new javax.swing.ButtonGroup();
    jPanel3 = new javax.swing.JPanel();
    jPanel4 = new javax.swing.JPanel();
    okjButton = new javax.swing.JButton();
    canceljButton = new javax.swing.JButton();
    jPanel6 = new javax.swing.JPanel();
    tabPanel = new javax.swing.JTabbedPane();
    jPanel2 = new javax.swing.JPanel();
    greaterThanjRadioButton = new javax.swing.JRadioButton();
    lessThanjRadioButton = new javax.swing.JRadioButton();
    greaterOrEqualTojRadioButton = new javax.swing.JRadioButton();
    lessOrEqualTojRadioButton = new javax.swing.JRadioButton();
    equalTojRadioButton = new javax.swing.JRadioButton();
    jPanel5 = new javax.swing.JPanel();
    valuejLabel = new javax.swing.JLabel();
    valuejTextField = new javax.swing.JTextField();
    jPanel1 = new javax.swing.JPanel();
    fromjLabel = new javax.swing.JLabel();
    tojLabel = new javax.swing.JLabel();
    fromjTextField = new javax.swing.JTextField();
    tojTextField = new javax.swing.JTextField();
    jPanel7 = new javax.swing.JPanel();
    colorjLabel = new javax.swing.JLabel();
    colorjTextField = new javax.swing.JTextField();
    colorjButton = new javax.swing.JButton();

    setResizable(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        setVisible(false);
      }
    });

    getContentPane().add(jPanel3, java.awt.BorderLayout.NORTH);

    jPanel4.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

    okjButton.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("112"));
    okjButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    okjButton.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
    jPanel4.add(okjButton);

    canceljButton.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("113"));
    canceljButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    canceljButton.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
    jPanel4.add(canceljButton);

    getContentPane().add(jPanel4, java.awt.BorderLayout.SOUTH);
    jPanel6.setLayout(new java.awt.BorderLayout());
    jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));

    greaterThanjRadioButton.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("117") + "(>)");
    operatorbuttonGroup.add(greaterThanjRadioButton);
    jPanel2.add(greaterThanjRadioButton);
    seleccionRadioButton = 0;
    greaterThanjRadioButton.setSelected(true);

    lessThanjRadioButton.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("118") + "(<)");
    operatorbuttonGroup.add(lessThanjRadioButton);
    jPanel2.add(lessThanjRadioButton);

    greaterOrEqualTojRadioButton.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("119") + "(>=)");
    operatorbuttonGroup.add(greaterOrEqualTojRadioButton);
    jPanel2.add(greaterOrEqualTojRadioButton);

    lessOrEqualTojRadioButton.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("120") + "(<=)");
    lessOrEqualTojRadioButton.setToolTipText("null");
    operatorbuttonGroup.add(lessOrEqualTojRadioButton);
    jPanel2.add(lessOrEqualTojRadioButton);

    equalTojRadioButton.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("121") + "(=)");
    operatorbuttonGroup.add(equalTojRadioButton);
    jPanel2.add(equalTojRadioButton);

    jPanel5.setLayout(null);

    valuejLabel.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("122") + ":");
    jPanel5.add(valuejLabel);
    valuejLabel.setBounds(10, 8, 31, 16);

    jPanel5.add(valuejTextField);
    valuejTextField.setBounds(76, 4, 150, 20);

    jPanel2.add(jPanel5);

    tabPanel.addTab(com.calipso.reportgenerator.common.LanguageTraslator.traslate("123"), jPanel2);

    jPanel1.setLayout(null);

    fromjLabel.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("124") + ":");
    jPanel1.add(fromjLabel);
    fromjLabel.setBounds(10, 20, 39, 16);

    tojLabel.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("125") + ":");
    jPanel1.add(tojLabel);
    tojLabel.setBounds(10, 50, 36, 16);

    jPanel1.add(fromjTextField);
    fromjTextField.setBounds(80, 20, 160, 20);

    jPanel1.add(tojTextField);
    tojTextField.setBounds(80, 50, 160, 20);

    tabPanel.addTab(com.calipso.reportgenerator.common.LanguageTraslator.traslate("126"), jPanel1);

    jPanel6.add(tabPanel, java.awt.BorderLayout.CENTER);

    jPanel7.setLayout(null);

    jPanel7.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), null));
    jPanel7.setPreferredSize(new java.awt.Dimension(10, 30));
    colorjLabel.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("127") + ":");
    jPanel7.add(colorjLabel);
    colorjLabel.setBounds(10, 6, 33, 16);

    colorjTextField.setBackground(java.awt.Color.RED);
    if(storedColor == null) {
      bgColor = java.awt.Color.RED;
    } else {
      bgColor = storedColor;
    }
    colorjTextField.setEditable(false);
    jPanel7.add(colorjTextField);
    colorjTextField.setBounds(80, 6, 140, 20);

    colorjButton.setText("...");
    colorjButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        colorjButtonActionPerformed();
      }
    });
    okjButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        try {
          doPaint();
        }
        catch (Exception e) {
          ShowExceptionMessageDialog.initExceptionDialogMessage(com.calipso.reportgenerator.common.LanguageTraslator.traslate("226"),e);
        }
      }
    });

    greaterThanjRadioButton.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        greaterThanjRadioButtonMousePressed();
      }
    });

    lessThanjRadioButton.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        lessThanjRadioButtonMousePressed();
      }
    });

    greaterOrEqualTojRadioButton.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        greaterOrEqualTojRadioButtonMousePressed();
      }
    });
    lessOrEqualTojRadioButton.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        lessOrEqualTojRadioButtonMousePressed();
      }
    });
    equalTojRadioButton.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        equalTojRadioButtonMousePressed();
      }
    });

    canceljButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        closeChoosSemaphoreSelection();

      }
    });


    jPanel7.add(colorjButton);
    colorjButton.setBounds(220, 6, 20, 20);

    jPanel6.add(jPanel7, java.awt.BorderLayout.SOUTH);

    getContentPane().add(jPanel6, java.awt.BorderLayout.CENTER);

    pack();
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setSize(new java.awt.Dimension(400, 300));
    setLocation((screenSize.width - 400) / 2, (screenSize.height - 300) / 2);
  }

  /**
   * Cierra el Dialog
   */
  private void closeChoosSemaphoreSelection() {
    this.setVisible(false);
  }

  /**
   * Retorna el indicador de para el JRadioButton equalTo
   */
  private void equalTojRadioButtonMousePressed() {
    seleccionRadioButton = 4;
  }

  /**
   * Retorna el indicador de para el JRadioButton lessOrEqualTo
   */
  private void lessOrEqualTojRadioButtonMousePressed() {
    seleccionRadioButton = 3;
  }

  /**
   * Retorna el indicador de para el JRadioButton greaterOrEqualTo
   */
  private void greaterOrEqualTojRadioButtonMousePressed() {
    seleccionRadioButton = 2;
  }

  /**
   * Retorna el indicador de para el JRadioButton lessThan
   */
  private void lessThanjRadioButtonMousePressed() {
    seleccionRadioButton = 1;
  }

  /**
   * Retorna el indicador de para el JRadioButton greaterThan
   */
  private void greaterThanjRadioButtonMousePressed() {
    seleccionRadioButton = 0;
  }

  /**
   * Actualiza el colorcondition y cierra el Dialog
   */
  private void doPaint() throws InfoException {
    switch (tabPanel.getSelectedIndex()) {
      case 0:
        {
          resultColorCondition = doPaintSimpleSelectionMode();
          break;
        }
      case 1:
        {
          resultColorCondition = doPaintRangoSeleccionMode();
          break;
        }
    }
    if (resultColorCondition != null) {
      setVisible(false);
    }
  }

  /**
   * retorna un color condition , seteando la expresion segun corresponda
   * @return
   */
  private ColorCondition doPaintSimpleSelectionMode() throws InfoException {
    switch (seleccionRadioButton) {
      case 0:
        {
          return greaterThan();
        }
      case 1:
        {
          return lessThan();
        }
      case 2:
        {
          return greaterOrEqualTo();
        }
      case 3:
        {
          return lessOrEqualTo();
        }
      case 4:
        {
          return equalTo();
        }
    }
    return null;
  }

  /**
   * Retorna el colorCondition cuando la expresion es igual a un numero determinaddo
   * @return
   */
  private ColorCondition equalTo() throws InfoException {
    Vector errors = new Vector();
    float value = 0;
    try {
      value = Float.parseFloat(this.valuejTextField.getText());
    }
    catch (NumberFormatException e) {
      errors.add(valuejTextField.getText());
      toString();
    }
    if (errors.size() == 0) {
      value = Float.parseFloat(this.valuejTextField.getText());
      Expression expression = (new VariableExp("VALUE")).newEqualTo(new ConstantExp(new Float(value)));
      return newColorCondition(expression);
    }
    else {
      if (errors.size() == 1) {
        String message = MessageFormat.format(com.calipso.reportgenerator.common.LanguageTraslator.traslate("128"), new Object[]{this.valuejTextField.getText()});
       throw new InfoException(message);
      }
      return null;
    }
  }

  /**
   * Retorna el colorCondition y actualiza su color
   * @return
   */
  private ColorCondition newColorCondition(Expression expression) {
    if (bgColor == null) {
      //bgColor = Color.RED;

      bgColor = colorjTextField.getBackground();
    }
    return new ColorCondition(expression, bgColor, metricState);
  }

  /**
   * Retorna el colorCondition cuando la expresion sea menor o igual  a un numero determinaddo
   * @return
   */
  private ColorCondition lessOrEqualTo() throws InfoException {
    Vector errors = new Vector();
    float value = 0;
    try {
      value = Float.parseFloat(this.valuejTextField.getText());
    }
    catch (NumberFormatException e) {
      errors.add(valuejTextField.getText());
    }
    if (errors.size() == 0) {
      value = Float.parseFloat(this.valuejTextField.getText());
      Expression expression = (new VariableExp("VALUE")).newLessOrEquealTo(new ConstantExp(new Float(value)));
      return newColorCondition(expression);
    }
    else {
      if (errors.size() == 1) {
        String message = MessageFormat.format(com.calipso.reportgenerator.common.LanguageTraslator.traslate("128"), new Object[]{this.valuejTextField.getText()});
        throw new InfoException(message);
      }
      return null;
    }
  }

  /**
   * Retorna el colorCondition cuando la expresion sea mayor o igual a un numero determinaddo
   * @return
   */
  private ColorCondition greaterOrEqualTo() throws InfoException {
    Vector errors = new Vector();
    float value = 0;
    try {
      value = Float.parseFloat(this.valuejTextField.getText());
    }
    catch (NumberFormatException e) {
      errors.add(valuejTextField.getText());
    }
    if (errors.size() == 0) {
      value = Float.parseFloat(this.valuejTextField.getText());
      Expression expression = (new VariableExp("VALUE")).newGreaterOrEqualTo(new ConstantExp(new Float(value)));
      return newColorCondition(expression);
    }
    else {
      if (errors.size() == 1) {
        String message = MessageFormat.format(com.calipso.reportgenerator.common.LanguageTraslator.traslate("128"), new Object[]{this.valuejTextField.getText()});
       throw  new InfoException(message);
      }
      return null;
    }
  }

  /**
   * Retorna el colorCondition cuando la expresion sea menor a  un numero determinaddo
   * @return
   */
  private ColorCondition lessThan() throws InfoException {
    Vector errors = new Vector();
    float value = 0;
    try {
      value = Float.parseFloat(this.valuejTextField.getText());
    }
    catch (NumberFormatException e) {
      errors.add(valuejTextField.getText());
    }
    if (errors.size() == 0) {
      value = Float.parseFloat(this.valuejTextField.getText());
      Expression expression = (new VariableExp("VALUE")).newLessThan(new ConstantExp(new Float(value)));
      return newColorCondition(expression);
    }
    else {
      if (errors.size() == 1) {
        String message = MessageFormat.format(com.calipso.reportgenerator.common.LanguageTraslator.traslate("128"), new Object[]{this.valuejTextField.getText()});
        throw new InfoException(message);
      }
      return null;
    }
  }

  /**
   * Retorna el colorCondition cuando la expresion sea mayor a un numero determinaddo
   * @return
   */

  private ColorCondition greaterThan() throws InfoException {
    Vector errors = new Vector();
    float value = 0;
    try {
      value = Float.parseFloat(this.valuejTextField.getText());
    }
    catch (NumberFormatException e) {
      errors.add(valuejTextField.getText());
    }
    if (errors.size() == 0) {
      value = Float.parseFloat(this.valuejTextField.getText());
      Expression expression = (new VariableExp("VALUE")).newGreaterThan(new ConstantExp(new Float(value)));
      return newColorCondition(expression);
    }
    else {
      if (errors.size() == 1) {
        String message = MessageFormat.format(com.calipso.reportgenerator.common.LanguageTraslator.traslate("128"), new Object[]{this.valuejTextField.getText()});
        throw new InfoException(message);
      }
      return null;
    }
  }

  /**
   * Evento del boton de seleccion  de colores
   */
  private void colorjButtonActionPerformed() {
    this.executeJColorChooser();
  }

  /**
   * Retona un colorCondition comprendido en un determinado rango de valores
   * @return
   */
  private ColorCondition doPaintRangoSeleccionMode() throws InfoException {
    float desdeValue = 0;
    float hastaValue = 0;
    Vector errors = new Vector();
    Expression exp = null;
    try {
      desdeValue = Float.parseFloat(this.fromjTextField.getText());
    }
    catch (NumberFormatException e) {
      errors.add(fromjTextField.getText());
    }
    try {
      hastaValue = Float.parseFloat(this.tojTextField.getText());
    }
    catch (NumberFormatException e) {
      errors.add(tojTextField.getText());
    }
    if (errors.size() == 0) {
      Expression subexp1 = (new VariableExp("VALUE")).newGreaterOrEqualTo(new ConstantExp(new Float(desdeValue)));
      Expression subexp2 = (new VariableExp("VALUE")).newLessOrEquealTo((new ConstantExp(new Float(hastaValue))));
      exp = subexp1.newAnd(subexp2);
      return newColorCondition(exp);
    }
    else {
      if (errors.size() == 1) {
        String message = MessageFormat.format(com.calipso.reportgenerator.common.LanguageTraslator.traslate("128"), new Object[]{errors.get(0)});
        throw new InfoException(message);
      }
      else {
        String message = MessageFormat.format(com.calipso.reportgenerator.common.LanguageTraslator.traslate("129"), new Object[]{errors.get(0), errors.get(1)});
        throw new InfoException(message);
      }
    }
  }

  /**
   * Pone no visible el JColorChooser y  setea el color que se quiere utilizar
   */
  private void executeJColorChooser() {
    if(storedColor != null) {
      bgColor = JColorChooser.showDialog(this, "", storedColor);
    } else {
      bgColor = JColorChooser.showDialog(this, "", getBackground());
    }
    if (bgColor != null) {
      colorjTextField.setBackground(bgColor);
    }
  }
}
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.