View Javadoc

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  public class PainelPropriedadesAmostra extends javax.swing.JPanel {
34      
35      /** Creates new form PainelPropriedadesAmostra */
36      public PainelPropriedadesAmostra() {
37          try {
38              formatTempo = new MaskFormatter("**:**:**");
39              formatTempo.setPlaceholderCharacter('0');
40          } catch (ParseException ex) {
41              ex.printStackTrace();
42          }
43          initComponents();
44      }
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          buttonGroup1 = new javax.swing.ButtonGroup();
54          labelNome = new javax.swing.JLabel();
55          labelTempo = new javax.swing.JLabel();
56          labelTemperatura = new javax.swing.JLabel();
57          textfieldNome = new javax.swing.JTextField();
58          jToolBar1 = new javax.swing.JToolBar();
59          botaoOk = new javax.swing.JButton();
60          botalCancel = new javax.swing.JButton();
61          radioButtonCelsius = new javax.swing.JRadioButton();
62          radioButtonKelvin = new javax.swing.JRadioButton();
63          radioButtonAmbiente = new javax.swing.JRadioButton();
64          textfieldTempo = new JFormattedTextField(formatTempo);
65          textfieldTemperatura = new JFormattedTextField(formatTemperatura);
66  
67          labelNome.setText("Nome:");
68  
69          labelTempo.setText("Tempo:");
70  
71          labelTemperatura.setText("Temperatura:");
72  
73          jToolBar1.setFloatable(false);
74          botaoOk.setText("OK");
75          botaoOk.addActionListener(new java.awt.event.ActionListener() {
76              public void actionPerformed(java.awt.event.ActionEvent evt) {
77                  botaoOkActionPerformed(evt);
78              }
79          });
80  
81          jToolBar1.add(botaoOk);
82  
83          botalCancel.setText("Cancel");
84          botalCancel.addActionListener(new java.awt.event.ActionListener() {
85              public void actionPerformed(java.awt.event.ActionEvent evt) {
86                  botalCancelActionPerformed(evt);
87              }
88          });
89  
90          jToolBar1.add(botalCancel);
91  
92          buttonGroup1.add(radioButtonCelsius);
93          radioButtonCelsius.setText("Celsius");
94          radioButtonCelsius.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
95          radioButtonCelsius.setMargin(new java.awt.Insets(0, 0, 0, 0));
96          radioButtonCelsius.addActionListener(new java.awt.event.ActionListener() {
97              public void actionPerformed(java.awt.event.ActionEvent evt) {
98                  radioButtonCelsiusActionPerformed(evt);
99              }
100         });
101 
102         buttonGroup1.add(radioButtonKelvin);
103         radioButtonKelvin.setText("Kelvin");
104         radioButtonKelvin.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
105         radioButtonKelvin.setMargin(new java.awt.Insets(0, 0, 0, 0));
106         radioButtonKelvin.addActionListener(new java.awt.event.ActionListener() {
107             public void actionPerformed(java.awt.event.ActionEvent evt) {
108                 radioButtonKelvinActionPerformed(evt);
109             }
110         });
111 
112         buttonGroup1.add(radioButtonAmbiente);
113         radioButtonAmbiente.setText("Ambiente");
114         radioButtonAmbiente.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
115         radioButtonAmbiente.setMargin(new java.awt.Insets(0, 0, 0, 0));
116         radioButtonAmbiente.addActionListener(new java.awt.event.ActionListener() {
117             public void actionPerformed(java.awt.event.ActionEvent evt) {
118                 radioButtonAmbienteActionPerformed(evt);
119             }
120         });
121 
122         textfieldTemperatura.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
123 
124         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
125         this.setLayout(layout);
126         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         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     }// </editor-fold>//GEN-END:initComponents
175 
176     private void radioButtonAmbienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioButtonAmbienteActionPerformed
177         changeTextfieldTemperaturaStatus();
178     }//GEN-LAST:event_radioButtonAmbienteActionPerformed
179 
180     private void radioButtonKelvinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioButtonKelvinActionPerformed
181         changeTextfieldTemperaturaStatus();
182     }//GEN-LAST:event_radioButtonKelvinActionPerformed
183 
184     private void radioButtonCelsiusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioButtonCelsiusActionPerformed
185         changeTextfieldTemperaturaStatus();
186     }//GEN-LAST:event_radioButtonCelsiusActionPerformed
187 
188     private enum TipoTemperatura { KELVIN, CELSIUS, AMBIENTE };
189     private TipoTemperatura tipoTemperatura;
190     
191     private void changeTextfieldTemperaturaStatus() {
192         Utilities utilities = Utilities.getUtilities();
193         
194         if(radioButtonAmbiente.isSelected()) {
195             textfieldTemperatura.setEnabled(false);
196             tipoTemperatura = TipoTemperatura.AMBIENTE;
197         } else if(radioButtonCelsius.isSelected()) {
198             
199             if(tipoTemperatura.equals(TipoTemperatura.KELVIN)) {
200                 Double temperatura = ((Number) textfieldTemperatura
201                         .getValue()).doubleValue();
202                 temperatura = Double.valueOf(
203                         utilities.fromKelvinToCelsius(temperatura.doubleValue()));
204                 textfieldTemperatura.setValue(temperatura);
205             }
206             
207             
208             textfieldTemperatura.setEnabled(true);
209             tipoTemperatura = tipoTemperatura.CELSIUS;
210         } else if(radioButtonKelvin.isSelected()) {
211             
212             if(tipoTemperatura.equals(TipoTemperatura.CELSIUS)) {
213                 Double temperatura = ((Number) textfieldTemperatura
214                         .getValue()).doubleValue();
215                 temperatura = Double.valueOf(
216                         utilities.fromCelsiusToKelvin(temperatura.doubleValue()));
217                 textfieldTemperatura.setValue(temperatura);
218             }
219             
220             textfieldTemperatura.setEnabled(true);
221             tipoTemperatura = tipoTemperatura.KELVIN;
222         }
223     }
224     
225     private void botalCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botalCancelActionPerformed
226         cancelAmostagem();
227     }//GEN-LAST:event_botalCancelActionPerformed
228 
229     private void botaoOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoOkActionPerformed
230         okAmostagem();
231     }//GEN-LAST:event_botaoOkActionPerformed
232 
233     private JDialog dialog;
234     private Frame dialogFather;
235     private Set<IPainelAmostragemListener> listeners = 
236             new HashSet<IPainelAmostragemListener>();
237     private IAmostragem amostragem;
238     
239     public void showDialog(Component parent) {
240         if(dialogFather != JOptionPane.getFrameForComponent(parent)); {
241             dialogFather = JOptionPane.getFrameForComponent(parent);
242             dialog = new JDialog(dialogFather);
243         }
244         
245         dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
246         dialog.setContentPane(this);
247         dialog.setModal(true);
248         dialog.addWindowListener(new WindowAdapter() {
249            public void windowClosing(WindowEvent evt) {
250                cancelAmostagem();
251            } 
252         });
253         dialog.pack();
254         dialog.setLocationByPlatform(true);
255         dialog.setVisible(true);
256     }
257     
258     public void addPainelAmostragemListener(
259             IPainelAmostragemListener listener) {
260         listeners.add(listener);
261     }
262     
263     public void removePainelAmostragemListener(
264             IPainelAmostragemListener listener) {
265         listeners.remove(listener);
266     }
267     
268     private void fireEdicaoTerminada() {
269         Iterator<IPainelAmostragemListener> it = listeners.iterator();
270         while(it.hasNext()) {
271             it.next().edicaoTerminada(getAmostragem());
272         }
273     }
274     
275     private IAmostragem backup;
276     
277     public void okAmostagem() {
278         getDisplayValues();
279         backup.copyFrom(amostragem);
280         fireEdicaoTerminada();
281         cancelAmostagem();
282     }
283 
284     public void cancelAmostagem() {
285         dialog.setVisible(false);
286         backup = null;
287         amostragem = null;
288     }
289 
290     public IAmostragem getAmostragem() {
291         return amostragem;
292     }
293 
294     public void setAmostragem(IAmostragem amostragemSrc) {
295         amostragem = (IAmostragem) amostragemSrc.clone();
296         backup = amostragemSrc;
297         setDisplayValues();
298     }
299     
300     private void setDisplayValues() {
301         textfieldNome.setText(amostragem.getNome());
302         Utilities utilities = Utilities.getUtilities();
303         textfieldTempo.setValue(utilities.formatTempo(amostragem.getDuracao()));
304         if(!Double.isNaN(amostragem.getTemperatura())) {
305             double temperatura = amostragem.getTemperatura();
306             if(temperatura > Utilities.KELVIN_LIMIAR) {
307                 temperatura = temperatura - Utilities.KELVIN_DELTA;
308                 radioButtonCelsius.setSelected(true);
309                 tipoTemperatura = TipoTemperatura.CELSIUS;
310                 
311             } else {
312                 tipoTemperatura = TipoTemperatura.KELVIN;
313                 radioButtonKelvin.setSelected(true);
314             }
315             textfieldTemperatura.setValue(Double.valueOf(temperatura));
316         } else {
317             radioButtonAmbiente.setSelected(true);
318             tipoTemperatura = TipoTemperatura.AMBIENTE;
319             textfieldTemperatura.setValue(Double.valueOf(0.0));
320         }
321         changeTextfieldTemperaturaStatus();
322     }
323     
324     private void getDisplayValues() {
325         Utilities utilities = Utilities.getUtilities();
326         amostragem.setNome(textfieldNome.getText());
327         String tempoStr = (String) textfieldTempo.getValue();
328         amostragem.setDuracao(utilities.parseTempo(tempoStr));
329         double temperatura = ((Number) textfieldTemperatura.getValue())
330                              .doubleValue();
331         if(radioButtonCelsius.isSelected()) {
332             temperatura = Utilities.KELVIN_DELTA + temperatura;
333         } else if(radioButtonAmbiente.isSelected()) {
334             temperatura = Double.NaN;
335         }
336         amostragem.setTemperatura(temperatura);
337     }
338     
339     
340     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