server.view.ServerGui.java Source code

Java tutorial

Introduction

Here is the source code for server.view.ServerGui.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server.view;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import server.ChatServerImpl;
import server.Server;

/**
 *
 * @author dono
 */
public class ServerGui extends javax.swing.JFrame {

    /**
     * Creates new form ServerGui
     */

    Thread th;
    Server srv;
    boolean flag = true;
    static ServerGui srvGui;
    PieChartDemo1 demo;

    public ServerGui() {
        initComponents();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        srv = new Server();
        th = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    onlineLabel.setText("" + srv.numberOfOnlineUser());
                    numberOfUserLabel.setText("" + srv.numberOfAllUser());
                    offlineLabel.setText("" + srv.numberOfOfflineUser());

                    // System.out.println(srv.numberOfOnlineUser());
                }
            }

        });
        th.start();

    }

    public Server getMyServer() {
        return srv;
    }

    /**
     * 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() {

        graphPanel = new javax.swing.JPanel();
        offlineLabel = new javax.swing.JLabel();
        onlineLabel = new javax.swing.JLabel();
        numberOfUserLabel = new javax.swing.JLabel();
        startButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        showStatisticalButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Server");
        setBackground(new java.awt.Color(0, 102, 153));
        setBounds(new java.awt.Rectangle(500, 200, 0, 0));
        setResizable(false);

        graphPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        offlineLabel.setBackground(new java.awt.Color(153, 153, 153));
        offlineLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        offlineLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon_user_offline.gif"))); // NOI18N

        onlineLabel.setBackground(new java.awt.Color(204, 204, 204));
        onlineLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        onlineLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon_user_online.gif"))); // NOI18N

        numberOfUserLabel.setBackground(new java.awt.Color(153, 153, 153));
        numberOfUserLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        numberOfUserLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/users1.png"))); // NOI18N

        startButton.setBackground(new java.awt.Color(153, 153, 153));
        startButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        startButton.setText("start server");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        stopButton.setBackground(new java.awt.Color(153, 153, 153));
        stopButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        stopButton.setText("stop server");
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        });

        showStatisticalButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        showStatisticalButton.setText("show statistic");
        showStatisticalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showStatisticalButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout graphPanelLayout = new javax.swing.GroupLayout(graphPanel);
        graphPanel.setLayout(graphPanelLayout);
        graphPanelLayout
                .setHorizontalGroup(
                        graphPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(graphPanelLayout.createSequentialGroup().addGroup(graphPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(graphPanelLayout.createSequentialGroup().addGap(18, 18, 18)
                                                .addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(stopButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        151, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(graphPanelLayout.createSequentialGroup().addGap(55, 55, 55)
                                                .addComponent(onlineLabel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        145, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(offlineLabel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        145, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(numberOfUserLabel,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 145,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(0, 0, Short.MAX_VALUE)))
                                        .addGap(33, 33, 33))
                                .addGroup(graphPanelLayout.createSequentialGroup().addGap(178, 178, 178)
                                        .addComponent(showStatisticalButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        graphPanelLayout.setVerticalGroup(graphPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, graphPanelLayout.createSequentialGroup()
                        .addContainerGap(42, Short.MAX_VALUE)
                        .addGroup(graphPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(stopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(graphPanelLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(offlineLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(onlineLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(numberOfUserLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(108, 108, 108).addComponent(showStatisticalButton,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(122, 122, 122)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(graphPanel,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(graphPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed

        srv.stopServer();
        // flag=false;
        th.suspend();
    }//GEN-LAST:event_stopButtonActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed

        srv.startServer();
        //  flag=true;
        th.resume();

        // onlineLabel.setText(""+srv.numberOfOnlineUser());

    }//GEN-LAST:event_startButtonActionPerformed

    private void showStatisticalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showStatisticalButtonActionPerformed
        demo = new PieChartDemo1("Pie Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }//GEN-LAST:event_showStatisticalButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ServerGui.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ServerGui.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ServerGui.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ServerGui.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                srvGui = new ServerGui();
                srvGui.setVisible(true);

            }
        });
    }

    class PieChartDemo1 extends ApplicationFrame {
        DefaultPieDataset dataset;

        public PieChartDemo1(String title) {
            super(title);
            JFreeChart chart = createChart(createDataset());

            setContentPane(new ChartPanel(chart));

            //setDefaultCloseOperation(HIDE_ON_CLOSE);
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosed(WindowEvent e) {

                    // PieChartDemo1.this.setVisible(false);
                    demo.setVisible(false);

                }

            });

        }

        private PieDataset createDataset() {

            dataset = new DefaultPieDataset();
            dataset.setValue("online", new Double(srv.numberOfOnlineUser()));
            dataset.setValue("all User", new Double(srv.numberOfAllUser()));
            return dataset;
        }

        private JFreeChart createChart(PieDataset dataset) {

            JFreeChart chart = ChartFactory.createPieChart("Statistic", // chart title
                    dataset, // data
                    true, // include legend
                    true, false);

            PiePlot plot = (PiePlot) chart.getPlot();
            plot.setSectionOutlinesVisible(false);
            plot.setNoDataMessage("No data available");

            return chart;

        }

        /**
         * Creates a panel for the demo (used by SuperDemo.java).
         *
         * @return A panel.
         */

        /**
         * Starting point for the demonstration application.
         *
         * @param args  ignored.
         */
        /* public static void main(String[] args) {
            
             // ******************************************************************
             //  More than 150 demo applications are included with the JFreeChart
             //  Developer Guide...for more information, see:
             //
             //  >   http://www.object-refinery.com/jfreechart/guide.html
             //
             // ******************************************************************
            
                
         }*/

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel graphPanel;
    private javax.swing.JLabel numberOfUserLabel;
    private javax.swing.JLabel offlineLabel;
    private javax.swing.JLabel onlineLabel;
    private javax.swing.JButton showStatisticalButton;
    private javax.swing.JButton startButton;
    private javax.swing.JButton stopButton;
    // End of variables declaration//GEN-END:variables

}