Java tutorial
/* * Copyright 2016 Dmitry Avtonomov. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package umich.ms.batmass.gui.viewers.chromatogram.actions; import javax.swing.JTextField; import umich.ms.batmass.gui.core.components.util.textinput.DocumentFilters; import umich.ms.batmass.gui.viewers.chromatogram.components.ChromatogramComponent; /** * * @author Dmitry Avtonomov */ public class XICDialog extends javax.swing.JPanel { /** * Creates new form XICDialog */ public XICDialog() { initComponents(); } public JTextField getTxtMzLo() { return txtMzLo; } public JTextField getTxtDeltaMz() { return txtDeltaMz; } public JTextField getTxtMz() { return txtMz; } public JTextField getTxtMzHi() { return txtMzHi; } public JTextField getTxtPpm() { return txtPpm; } public JTextField getTxtRtHi() { return txtRtHi; } public JTextField getTxtRtLo() { return txtRtLo; } public Double getMzLo() { return getValidatedDouble(txtMzLo); } public Double getMzHi() { return getValidatedDouble(txtMzHi); } public Double getMz() { return getValidatedDouble(txtMz); } public Double getPpm() { return getValidatedDouble(txtPpm); } public Double getDeltaMz() { return getValidatedDouble(txtDeltaMz); } public Double getRtLo() { return getValidatedDouble(txtRtLo); } public Double getRtHi() { return getValidatedDouble(txtRtHi); } private Double getValidatedDouble(JTextField field) { String text = field.getText(); return org.apache.commons.validator.routines.DoubleValidator.getInstance().validate(text); } public void initFields(ChromatogramComponent.Viewport view) { if (view.getMzLo() != null) { txtMzLo.setText(formatFloatToDecimalPoints(view.getMzLo(), 5)); } if (view.getMzHi() != null) { txtMzHi.setText(formatFloatToDecimalPoints(view.getMzHi(), 5)); } if (view.getRtLo() != null) { txtRtLo.setText(formatFloatToDecimalPoints(view.getRtLo(), 2)); } if (view.getRtHi() != null) { txtRtHi.setText(formatFloatToDecimalPoints(view.getRtHi(), 2)); } } private String formatFloatToDecimalPoints(Double mz, int decimalDigits) { return String.format("%." + decimalDigits + "f", mz); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); txtMzLo = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); txtMzHi = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); txtMz = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); txtPpm = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); txtDeltaMz = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); txtRtLo = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); txtRtHi = new javax.swing.JTextField(); org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.jLabel1.text")); // NOI18N txtMzLo.setDocument(DocumentFilters.getDigitsAndDotFilter()); txtMzLo.setText(org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.txtMzLo.text")); // NOI18N txtMzLo.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { txtMzLoFocusGained(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.jLabel2.text")); // NOI18N txtMzHi.setDocument(DocumentFilters.getDigitsAndDotFilter()); txtMzHi.setText(org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.txtMzHi.text")); // NOI18N txtMzHi.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { txtMzHiFocusGained(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.jLabel3.text")); // NOI18N txtMz.setDocument(DocumentFilters.getDigitsAndDotFilter()); txtMz.setText(org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.txtMz.text")); // NOI18N txtMz.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { txtMzFocusGained(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.jLabel4.text")); // NOI18N txtPpm.setDocument(DocumentFilters.getDigitsAndDotFilter()); txtPpm.setText(org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.txtPpm.text")); // NOI18N txtPpm.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { txtPpmFocusGained(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.jLabel5.text")); // NOI18N txtDeltaMz.setDocument(DocumentFilters.getDigitsAndDotFilter()); txtDeltaMz.setText(org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.txtDeltaMz.text")); // NOI18N txtDeltaMz.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { txtDeltaMzFocusGained(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.jLabel6.text")); // NOI18N txtRtLo.setDocument(DocumentFilters.getDigitsAndDotFilter()); txtRtLo.setText(org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.txtRtLo.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.jLabel7.text")); // NOI18N txtRtHi.setDocument(DocumentFilters.getDigitsAndDotFilter()); txtRtHi.setText(org.openide.util.NbBundle.getMessage(XICDialog.class, "XICDialog.txtRtHi.text")); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtMz, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE) .addComponent(txtMzLo))) .addGroup(layout.createSequentialGroup().addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txtRtLo, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtDeltaMz, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2).addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtMzHi).addComponent(txtPpm))) .addGroup(layout.createSequentialGroup().addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtRtHi))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1) .addComponent(txtMzLo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2).addComponent(txtMzHi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(txtMz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4).addComponent(txtPpm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(txtDeltaMz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(txtRtLo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7).addComponent(txtRtHi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); }// </editor-fold>//GEN-END:initComponents private void txtMzLoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMzLoFocusGained txtMzLo.selectAll(); }//GEN-LAST:event_txtMzLoFocusGained private void txtMzHiFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMzHiFocusGained txtMzHi.selectAll(); }//GEN-LAST:event_txtMzHiFocusGained private void txtPpmFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPpmFocusGained txtPpm.selectAll(); }//GEN-LAST:event_txtPpmFocusGained private void txtMzFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMzFocusGained txtMz.selectAll(); }//GEN-LAST:event_txtMzFocusGained private void txtDeltaMzFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDeltaMzFocusGained txtDeltaMz.selectAll(); }//GEN-LAST:event_txtDeltaMzFocusGained // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JTextField txtDeltaMz; private javax.swing.JTextField txtMz; private javax.swing.JTextField txtMzHi; private javax.swing.JTextField txtMzLo; private javax.swing.JTextField txtPpm; private javax.swing.JTextField txtRtHi; private javax.swing.JTextField txtRtLo; // End of variables declaration//GEN-END:variables }