Coverage Report - tempcontroller.gui.PainelPropriedadesAmostra
 
Classes in this File Line Coverage Branch Coverage Complexity
PainelPropriedadesAmostra
0% 
0% 
0
 
 1  
 // CHECKSTYLE:OFF 
 2  
 /*
 3  
  * PainelPropriedadesAmostra.java
 4  
  *
 5  
  * Created on June 6, 2007, 8:43 AM
 6  
  */
 7  
 
 8  
 package tempcontroller.gui;
 9  
 
 10  
 import java.awt.Component;
 11  
 import java.awt.Frame;
 12  
 import java.awt.event.WindowAdapter;
 13  
 import java.awt.event.WindowEvent;
 14  
 import java.text.DecimalFormat;
 15  
 import java.text.Format;
 16  
 import java.text.ParseException;
 17  
 import java.text.SimpleDateFormat;
 18  
 import java.util.Date;
 19  
 import java.util.HashSet;
 20  
 import java.util.Iterator;
 21  
 import java.util.Set;
 22  
 import javax.swing.JDialog;
 23  
 import javax.swing.JFormattedTextField;
 24  
 import javax.swing.JOptionPane;
 25  
 import javax.swing.text.MaskFormatter;
 26  
 import tempcontroller.base.IAmostragem;
 27  
 import tempcontroller.base.Utilities;
 28  
 
 29  
 /**
 30  
  *
 31  
  * @author  ipen
 32  
  */
 33  0
 public class PainelPropriedadesAmostra extends javax.swing.JPanel {
 34  
     
 35  
     /** Creates new form PainelPropriedadesAmostra */
 36  0
     public PainelPropriedadesAmostra() {
 37  
         try {
 38  0
             formatTempo = new MaskFormatter("**:**:**");
 39  0
             formatTempo.setPlaceholderCharacter('0');
 40  0
         } catch (ParseException ex) {
 41  0
             ex.printStackTrace();
 42  0
         }
 43  0
         initComponents();
 44  0
     }
 45  
     
 46  
     /** This method is called from within the constructor to
 47  
      * initialize the form.
 48  
      * WARNING: Do NOT modify this code. The content of this method is
 49  
      * always regenerated by the Form Editor.
 50  
      */
 51  
     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
 52  
     private void initComponents() {
 53  0
         buttonGroup1 = new javax.swing.ButtonGroup();
 54  0
         labelNome = new javax.swing.JLabel();
 55  0
         labelTempo = new javax.swing.JLabel();
 56  0
         labelTemperatura = new javax.swing.JLabel();
 57  0
         textfieldNome = new javax.swing.JTextField();
 58  0
         jToolBar1 = new javax.swing.JToolBar();
 59  0
         botaoOk = new javax.swing.JButton();
 60  0
         botalCancel = new javax.swing.JButton();
 61  0
         radioButtonCelsius = new javax.swing.JRadioButton();
 62  0
         radioButtonKelvin = new javax.swing.JRadioButton();
 63  0
         radioButtonAmbiente = new javax.swing.JRadioButton();
 64  0
         textfieldTempo = new JFormattedTextField(formatTempo);
 65  0
         textfieldTemperatura = new JFormattedTextField(formatTemperatura);
 66  
 
 67  0
         labelNome.setText("Nome:");
 68  
 
 69  0
         labelTempo.setText("Tempo:");
 70  
 
 71  0
         labelTemperatura.setText("Temperatura:");
 72  
 
 73  0
         jToolBar1.setFloatable(false);
 74  0
         botaoOk.setText("OK");
 75  0
         botaoOk.addActionListener(new java.awt.event.ActionListener() {
 76  
             public void actionPerformed(java.awt.event.ActionEvent evt) {
 77  0
                 botaoOkActionPerformed(evt);
 78  0
             }
 79  
         });
 80  
 
 81  0
         jToolBar1.add(botaoOk);
 82  
 
 83  0
         botalCancel.setText("Cancel");
 84  0
         botalCancel.addActionListener(new java.awt.event.ActionListener() {
 85  
             public void actionPerformed(java.awt.event.ActionEvent evt) {
 86  0
                 botalCancelActionPerformed(evt);
 87  0
             }
 88  
         });
 89  
 
 90  0
         jToolBar1.add(botalCancel);
 91  
 
 92  0
         buttonGroup1.add(radioButtonCelsius);
 93  0
         radioButtonCelsius.setText("Celsius");
 94  0
         radioButtonCelsius.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
 95  0
         radioButtonCelsius.setMargin(new java.awt.Insets(0, 0, 0, 0));
 96  0
         radioButtonCelsius.addActionListener(new java.awt.event.ActionListener() {
 97  
             public void actionPerformed(java.awt.event.ActionEvent evt) {
 98  0
                 radioButtonCelsiusActionPerformed(evt);
 99  0
             }
 100  
         });
 101  
 
 102  0
         buttonGroup1.add(radioButtonKelvin);
 103  0
         radioButtonKelvin.setText("Kelvin");
 104  0
         radioButtonKelvin.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
 105  0
         radioButtonKelvin.setMargin(new java.awt.Insets(0, 0, 0, 0));
 106  0
         radioButtonKelvin.addActionListener(new java.awt.event.ActionListener() {
 107  
             public void actionPerformed(java.awt.event.ActionEvent evt) {
 108  0
                 radioButtonKelvinActionPerformed(evt);
 109  0
             }
 110  
         });
 111  
 
 112  0
         buttonGroup1.add(radioButtonAmbiente);
 113  0
         radioButtonAmbiente.setText("Ambiente");
 114  0
         radioButtonAmbiente.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
 115  0
         radioButtonAmbiente.setMargin(new java.awt.Insets(0, 0, 0, 0));
 116  0
         radioButtonAmbiente.addActionListener(new java.awt.event.ActionListener() {
 117  
             public void actionPerformed(java.awt.event.ActionEvent evt) {
 118  0
                 radioButtonAmbienteActionPerformed(evt);
 119  0
             }
 120  
         });
 121  
 
 122  0
         textfieldTemperatura.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
 123  
 
 124  0
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
 125  0
         this.setLayout(layout);
 126  0
         layout.setHorizontalGroup(
 127  
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 128  
             .addGroup(layout.createSequentialGroup()
 129  
                 .addContainerGap()
 130  
                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 131  
                     .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
 132  
                     .addGroup(layout.createSequentialGroup()
 133  
                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 134  
                             .addComponent(labelTemperatura)
 135  
                             .addComponent(labelTempo)
 136  
                             .addComponent(labelNome))
 137  
                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 138  
                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 139  
                             .addComponent(textfieldTemperatura, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
 140  
                             .addComponent(textfieldNome, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
 141  
                             .addComponent(textfieldTempo, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)))
 142  
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
 143  
                         .addComponent(radioButtonCelsius)
 144  
                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 145  
                         .addComponent(radioButtonKelvin)
 146  
                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 147  
                         .addComponent(radioButtonAmbiente)))
 148  
                 .addContainerGap())
 149  
         );
 150  0
         layout.setVerticalGroup(
 151  
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 152  
             .addGroup(layout.createSequentialGroup()
 153  
                 .addContainerGap()
 154  
                 .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
 155  
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 156  
                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 157  
                     .addComponent(labelNome)
 158  
                     .addComponent(textfieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 159  
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 160  
                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 161  
                     .addComponent(labelTempo)
 162  
                     .addComponent(textfieldTempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 163  
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 164  
                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 165  
                     .addComponent(labelTemperatura)
 166  
                     .addComponent(textfieldTemperatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 167  
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 168  
                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 169  
                     .addComponent(radioButtonAmbiente)
 170  
                     .addComponent(radioButtonKelvin)
 171  
                     .addComponent(radioButtonCelsius))
 172  
                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 173  
         );
 174  0
     }// </editor-fold>//GEN-END:initComponents
 175  
 
 176  
     private void radioButtonAmbienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioButtonAmbienteActionPerformed
 177  0
         changeTextfieldTemperaturaStatus();
 178  0
     }//GEN-LAST:event_radioButtonAmbienteActionPerformed
 179  
 
 180  
     private void radioButtonKelvinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioButtonKelvinActionPerformed
 181  0
         changeTextfieldTemperaturaStatus();
 182  0
     }//GEN-LAST:event_radioButtonKelvinActionPerformed
 183  
 
 184  
     private void radioButtonCelsiusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioButtonCelsiusActionPerformed
 185  0
         changeTextfieldTemperaturaStatus();
 186  0
     }//GEN-LAST:event_radioButtonCelsiusActionPerformed
 187  
 
 188  0
     private enum TipoTemperatura { KELVIN, CELSIUS, AMBIENTE };
 189  
     private TipoTemperatura tipoTemperatura;
 190  
     
 191  
     private void changeTextfieldTemperaturaStatus() {
 192  0
         Utilities utilities = Utilities.getUtilities();
 193  
         
 194  0
         if(radioButtonAmbiente.isSelected()) {
 195  0
             textfieldTemperatura.setEnabled(false);
 196  0
             tipoTemperatura = TipoTemperatura.AMBIENTE;
 197  0
         } else if(radioButtonCelsius.isSelected()) {
 198  
             
 199  0
             if(tipoTemperatura.equals(TipoTemperatura.KELVIN)) {
 200  0
                 Double temperatura = ((Number) textfieldTemperatura
 201  
                         .getValue()).doubleValue();
 202  0
                 temperatura = Double.valueOf(
 203  
                         utilities.fromKelvinToCelsius(temperatura.doubleValue()));
 204  0
                 textfieldTemperatura.setValue(temperatura);
 205  
             }
 206  
             
 207  
             
 208  0
             textfieldTemperatura.setEnabled(true);
 209  0
             tipoTemperatura = tipoTemperatura.CELSIUS;
 210  0
         } else if(radioButtonKelvin.isSelected()) {
 211  
             
 212  0
             if(tipoTemperatura.equals(TipoTemperatura.CELSIUS)) {
 213  0
                 Double temperatura = ((Number) textfieldTemperatura
 214  
                         .getValue()).doubleValue();
 215  0
                 temperatura = Double.valueOf(
 216  
                         utilities.fromCelsiusToKelvin(temperatura.doubleValue()));
 217  0
                 textfieldTemperatura.setValue(temperatura);
 218  
             }
 219  
             
 220  0
             textfieldTemperatura.setEnabled(true);
 221  0
             tipoTemperatura = tipoTemperatura.KELVIN;
 222  
         }
 223  0
     }
 224  
     
 225  
     private void botalCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botalCancelActionPerformed
 226  0
         cancelAmostagem();
 227  0
     }//GEN-LAST:event_botalCancelActionPerformed
 228  
 
 229  
     private void botaoOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoOkActionPerformed
 230  0
         okAmostagem();
 231  0
     }//GEN-LAST:event_botaoOkActionPerformed
 232  
 
 233  
     private JDialog dialog;
 234  
     private Frame dialogFather;
 235  0
     private Set<IPainelAmostragemListener> listeners = 
 236  
             new HashSet<IPainelAmostragemListener>();
 237  
     private IAmostragem amostragem;
 238  
     
 239  
     public void showDialog(Component parent) {
 240  0
         if(dialogFather != JOptionPane.getFrameForComponent(parent)); {
 241  0
             dialogFather = JOptionPane.getFrameForComponent(parent);
 242  0
             dialog = new JDialog(dialogFather);
 243  
         }
 244  
         
 245  0
         dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
 246  0
         dialog.setContentPane(this);
 247  0
         dialog.setModal(true);
 248  0
         dialog.addWindowListener(new WindowAdapter() {
 249  
            public void windowClosing(WindowEvent evt) {
 250  0
                cancelAmostagem();
 251  0
            } 
 252  
         });
 253  0
         dialog.pack();
 254  0
         dialog.setLocationByPlatform(true);
 255  0
         dialog.setVisible(true);
 256  0
     }
 257  
     
 258  
     public void addPainelAmostragemListener(
 259  
             IPainelAmostragemListener listener) {
 260  0
         listeners.add(listener);
 261  0
     }
 262  
     
 263  
     public void removePainelAmostragemListener(
 264  
             IPainelAmostragemListener listener) {
 265  0
         listeners.remove(listener);
 266  0
     }
 267  
     
 268  
     private void fireEdicaoTerminada() {
 269  0
         Iterator<IPainelAmostragemListener> it = listeners.iterator();
 270  0
         while(it.hasNext()) {
 271  0
             it.next().edicaoTerminada(getAmostragem());
 272  
         }
 273  0
     }
 274  
     
 275  
     private IAmostragem backup;
 276  
     
 277  
     public void okAmostagem() {
 278  0
         getDisplayValues();
 279  0
         backup.copyFrom(amostragem);
 280  0
         fireEdicaoTerminada();
 281  0
         cancelAmostagem();
 282  0
     }
 283  
 
 284  
     public void cancelAmostagem() {
 285  0
         dialog.setVisible(false);
 286  0
         backup = null;
 287  0
         amostragem = null;
 288  0
     }
 289  
 
 290  
     public IAmostragem getAmostragem() {
 291  0
         return amostragem;
 292  
     }
 293  
 
 294  
     public void setAmostragem(IAmostragem amostragemSrc) {
 295  0
         amostragem = (IAmostragem) amostragemSrc.clone();
 296  0
         backup = amostragemSrc;
 297  0
         setDisplayValues();
 298  0
     }
 299  
     
 300  
     private void setDisplayValues() {
 301  0
         textfieldNome.setText(amostragem.getNome());
 302  0
         Utilities utilities = Utilities.getUtilities();
 303  0
         textfieldTempo.setValue(utilities.formatTempo(amostragem.getDuracao()));
 304  0
         if(!Double.isNaN(amostragem.getTemperatura())) {
 305  0
             double temperatura = amostragem.getTemperatura();
 306  0
             if(temperatura > Utilities.KELVIN_LIMIAR) {
 307  0
                 temperatura = temperatura - Utilities.KELVIN_DELTA;
 308  0
                 radioButtonCelsius.setSelected(true);
 309  0
                 tipoTemperatura = TipoTemperatura.CELSIUS;
 310  
                 
 311  
             } else {
 312  0
                 tipoTemperatura = TipoTemperatura.KELVIN;
 313  0
                 radioButtonKelvin.setSelected(true);
 314  
             }
 315  0
             textfieldTemperatura.setValue(Double.valueOf(temperatura));
 316  0
         } else {
 317  0
             radioButtonAmbiente.setSelected(true);
 318  0
             tipoTemperatura = TipoTemperatura.AMBIENTE;
 319  0
             textfieldTemperatura.setValue(Double.valueOf(0.0));
 320  
         }
 321  0
         changeTextfieldTemperaturaStatus();
 322  0
     }
 323  
     
 324  
     private void getDisplayValues() {
 325  0
         Utilities utilities = Utilities.getUtilities();
 326  0
         amostragem.setNome(textfieldNome.getText());
 327  0
         String tempoStr = (String) textfieldTempo.getValue();
 328  0
         amostragem.setDuracao(utilities.parseTempo(tempoStr));
 329  0
         double temperatura = ((Number) textfieldTemperatura.getValue())
 330  
                              .doubleValue();
 331  0
         if(radioButtonCelsius.isSelected()) {
 332  0
             temperatura = Utilities.KELVIN_DELTA + temperatura;
 333  0
         } else if(radioButtonAmbiente.isSelected()) {
 334  0
             temperatura = Double.NaN;
 335  
         }
 336  0
         amostragem.setTemperatura(temperatura);
 337  0
     }
 338  
     
 339  
     
 340  0
     private Format formatTemperatura = new DecimalFormat("#,##0.0");
 341  
     private MaskFormatter formatTempo;
 342  
     
 343  
     // Variables declaration - do not modify//GEN-BEGIN:variables
 344  
     private javax.swing.JButton botalCancel;
 345  
     private javax.swing.JButton botaoOk;
 346  
     private javax.swing.ButtonGroup buttonGroup1;
 347  
     private javax.swing.JToolBar jToolBar1;
 348  
     private javax.swing.JLabel labelNome;
 349  
     private javax.swing.JLabel labelTemperatura;
 350  
     private javax.swing.JLabel labelTempo;
 351  
     private javax.swing.JRadioButton radioButtonAmbiente;
 352  
     private javax.swing.JRadioButton radioButtonCelsius;
 353  
     private javax.swing.JRadioButton radioButtonKelvin;
 354  
     private javax.swing.JTextField textfieldNome;
 355  
     private javax.swing.JFormattedTextField textfieldTemperatura;
 356  
     private javax.swing.JFormattedTextField textfieldTempo;
 357  
     // End of variables declaration//GEN-END:variables
 358  
     
 359  
 }
 360  
 
 361  
 // CHECKSTYLE:ON