Advertisement
Guest User

Untitled

a guest
Dec 6th, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.70 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package lab7;
  6.  
  7. import java.util.Locale;
  8. import java.util.ResourceBundle;
  9. import java.lang.management.ManagementFactory;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.management.ObjectName;
  13. import javax.management.JMException;
  14. import javax.management.Notification;
  15. import javax.management.NotificationListener;
  16. import lab7.ApplicationMBeanImpl;
  17.  
  18. /**
  19.  *
  20.  * @author buharin
  21.  */
  22. public class JGui extends javax.swing.JFrame implements NotificationListener{
  23.     private Locale locale = Locale.getDefault();
  24.     private ResourceBundle rb;
  25.     private ApplicationMBeanImpl mbean;
  26.     /**
  27.      * Creates new form JGui
  28.      */
  29.     public JGui() {
  30.         initComponents();
  31.         internacjonalizacjaInterfejsu(locale);
  32.         try {
  33.             initManagement();
  34.         } catch (Exception ex) {
  35.             System.out.println("Źle się załadowało.");
  36.         }
  37.         initMBeanValues();
  38.     }
  39.    
  40.     private void internacjonalizacjaInterfejsu(Locale locale)
  41.     {
  42.         rb = ResourceBundle.getBundle("resources.Tlumaczenia", locale);
  43.         name.setText(rb.getString("name"));
  44.         sex.setText(rb.getString("sex"));
  45.         jRadioButtonKobieta.setText(rb.getString("kobieta"));
  46.         if(!jTextField1.getText().isEmpty()) {
  47.             String str = "";
  48.             if(jRadioButtonKobieta.isSelected())
  49.                 str = rb.getString("miss");
  50.             if(jRadioButtonMale.isSelected())
  51.                 str = rb.getString("mister");
  52.         jHello.setText(rb.getString("hello")+" "+str+" "+jTextField1.getText());
  53.         jMessage.setText(rb.getString("greeting"));
  54.         }
  55.     }
  56.     /**
  57.      * This method is called from within the constructor to initialize the form.
  58.      * WARNING: Do NOT modify this code. The content of this method is always
  59.      * regenerated by the Form Editor.
  60.      */
  61.     @SuppressWarnings("unchecked")
  62.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  63.     private void initComponents() {
  64.  
  65.         buttonGroup2 = new javax.swing.ButtonGroup();
  66.         jComboBox1 = new javax.swing.JComboBox();
  67.         name = new javax.swing.JLabel();
  68.         sex = new javax.swing.JLabel();
  69.         jTextField1 = new javax.swing.JTextField();
  70.         jRadioButtonKobieta = new javax.swing.JRadioButton();
  71.         jRadioButtonMale = new javax.swing.JRadioButton();
  72.         jHello = new javax.swing.JLabel();
  73.         jMessage = new javax.swing.JLabel();
  74.  
  75.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  76.         setTitle("Lab7");
  77.  
  78.         jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PL", "EN" }));
  79.         jComboBox1.addActionListener(new java.awt.event.ActionListener() {
  80.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  81.                 jComboBox1ActionPerformed(evt);
  82.             }
  83.         });
  84.  
  85.         name.setText("jLabel1");
  86.  
  87.         sex.setText("jLabel1");
  88.  
  89.         buttonGroup2.add(jRadioButtonKobieta);
  90.         jRadioButtonKobieta.setText("K");
  91.         jRadioButtonKobieta.addActionListener(new java.awt.event.ActionListener() {
  92.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  93.                 jRadioButtonKobietaActionPerformed(evt);
  94.             }
  95.         });
  96.  
  97.         buttonGroup2.add(jRadioButtonMale);
  98.         jRadioButtonMale.setText("M");
  99.         jRadioButtonMale.addActionListener(new java.awt.event.ActionListener() {
  100.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  101.                 jRadioButtonMaleActionPerformed(evt);
  102.             }
  103.         });
  104.  
  105.         jHello.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  106.  
  107.         jMessage.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  108.  
  109.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  110.         getContentPane().setLayout(layout);
  111.         layout.setHorizontalGroup(
  112.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  113.             .addGroup(layout.createSequentialGroup()
  114.                 .addContainerGap()
  115.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  116.                     .addComponent(jHello, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  117.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  118.                         .addGap(0, 0, Short.MAX_VALUE)
  119.                         .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  120.                     .addGroup(layout.createSequentialGroup()
  121.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  122.                             .addComponent(name)
  123.                             .addComponent(sex))
  124.                         .addGap(44, 44, 44)
  125.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  126.                             .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
  127.                             .addGroup(layout.createSequentialGroup()
  128.                                 .addComponent(jRadioButtonKobieta)
  129.                                 .addGap(18, 18, 18)
  130.                                 .addComponent(jRadioButtonMale)))
  131.                         .addGap(0, 73, Short.MAX_VALUE))
  132.                     .addComponent(jMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  133.                 .addContainerGap())
  134.         );
  135.         layout.setVerticalGroup(
  136.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  137.             .addGroup(layout.createSequentialGroup()
  138.                 .addContainerGap()
  139.                 .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  140.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  141.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  142.                     .addComponent(name)
  143.                     .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  144.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  145.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  146.                     .addComponent(sex)
  147.                     .addComponent(jRadioButtonKobieta)
  148.                     .addComponent(jRadioButtonMale))
  149.                 .addGap(56, 56, 56)
  150.                 .addComponent(jHello, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  151.                 .addGap(26, 26, 26)
  152.                 .addComponent(jMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  153.                 .addContainerGap(27, Short.MAX_VALUE))
  154.         );
  155.  
  156.         pack();
  157.     }// </editor-fold>                        
  158.  
  159.     private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  160.        if((jComboBox1.getSelectedItem()).toString().equalsIgnoreCase("EN"))
  161.        {
  162.            locale = new Locale("en", "EN");
  163.            internacjonalizacjaInterfejsu(locale);
  164.        } else {
  165.            locale = new Locale("pl", "PL");
  166.            internacjonalizacjaInterfejsu(locale);
  167.        }
  168.     }                                          
  169.  
  170.     private void jRadioButtonKobietaActionPerformed(java.awt.event.ActionEvent evt) {                                                    
  171.         if(jRadioButtonKobieta.isSelected()) {
  172.             internacjonalizacjaInterfejsu(locale);
  173.         }
  174.     }                                                  
  175.  
  176.     private void jRadioButtonMaleActionPerformed(java.awt.event.ActionEvent evt) {                                                
  177.         if(jRadioButtonMale.isSelected()) {
  178.             internacjonalizacjaInterfejsu(locale);
  179.         }
  180.     }                                                
  181.  
  182.     private void initManagement() throws Exception {
  183.          try { // Register MBean in Platform MBeanServer
  184.             mbean = new ApplicationMBeanImpl();
  185.             ManagementFactory.getPlatformMBeanServer().
  186.                     registerMBean(mbean,
  187.                     new ObjectName("lab7:type=ApplicationMBeanImpl"));
  188.         } catch (JMException ex) {
  189.             ex.printStackTrace();
  190.         }
  191.     }
  192.    
  193.     private void initMBeanValues() {
  194.         jTextField1.setText(mbean.getName());
  195.         if(mbean.getKraj().equalsIgnoreCase("pl") || mbean.getKraj().equalsIgnoreCase("en")){
  196.             locale = new Locale(mbean.getKraj().toLowerCase());
  197.             internacjonalizacjaInterfejsu(locale);
  198.         }
  199.         if(mbean.getSex().equalsIgnoreCase("m"))
  200.             jRadioButtonMale.setSelected(true);
  201.         if(mbean.getSex().equalsIgnoreCase("k"))
  202.             jRadioButtonKobieta.setSelected(true);
  203.     }
  204.     /**
  205.      * @param args the command line arguments
  206.      */
  207.     public static void main(String args[]) {
  208.         /* Set the Nimbus look and feel */
  209.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  210.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  211.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  212.          */
  213.         try {
  214.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  215.                 if ("Nimbus".equals(info.getName())) {
  216.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  217.                     break;
  218.                 }
  219.             }
  220.         } catch (ClassNotFoundException ex) {
  221.             java.util.logging.Logger.getLogger(JGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  222.         } catch (InstantiationException ex) {
  223.             java.util.logging.Logger.getLogger(JGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  224.         } catch (IllegalAccessException ex) {
  225.             java.util.logging.Logger.getLogger(JGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  226.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  227.             java.util.logging.Logger.getLogger(JGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  228.         }
  229.         //</editor-fold>
  230.  
  231.         /* Create and display the form */
  232.         java.awt.EventQueue.invokeLater(new Runnable() {
  233.             public void run() {
  234.                 new JGui().setVisible(true);
  235.             }
  236.         });
  237.     }
  238.     // Variables declaration - do not modify                    
  239.     private javax.swing.ButtonGroup buttonGroup2;
  240.     private javax.swing.JComboBox jComboBox1;
  241.     private javax.swing.JLabel jHello;
  242.     private javax.swing.JLabel jMessage;
  243.     private javax.swing.JRadioButton jRadioButtonKobieta;
  244.     private javax.swing.JRadioButton jRadioButtonMale;
  245.     private javax.swing.JTextField jTextField1;
  246.     private javax.swing.JLabel name;
  247.     private javax.swing.JLabel sex;
  248.     // End of variables declaration                  
  249.  
  250.     @Override
  251.     public void handleNotification(Notification ntfctn, Object o) {
  252.         System.out.println(ntfctn.toString());
  253.         System.out.println(o.getClass().toString());
  254.     }
  255.    
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement