<commit_before>package com.doos.update_module.nongui;

import com.doos.settings_manager.ApplicationConstants;
import com.doos.settings_manager.registry.RegistryCanNotReadInfoException;
import com.doos.settings_manager.registry.RegistryCanNotWriteInfoException;
import com.doos.settings_manager.registry.RegistryManager;
import com.doos.update_module.core.Main;
import com.doos.update_module.gui.UpdateDialog;
import com.doos.update_module.update.AppVersion;
import com.doos.update_module.update.Updater;
import com.doos.update_module.utils.Internal;

import java.awt.*;
import java.awt.event.*;

/**
 * Created by <NAME> on 04.11.2016.
 */


public class NonGuiUpdater {
    public static final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit()
                                                                 .getImage(
                                                                         NonGuiUpdater.class.getResource("/icon.png")));
    public static final SystemTray tray = SystemTray.getSystemTray();
    private AppVersion serverAppVersion = null;


    public NonGuiUpdater() {
        Updater updater = new Updater();
        serverAppVersion = updater.getAppVersion();
        compareVersions();
    }

    private void compareVersions() {
        String str = ApplicationConstants.APP_VERSION;
        if (Internal.versionCompare(str, serverAppVersion.getVersion()) < 0) {
            //create trayicon and show pop-up
            createTrayIcon();
            trayIcon.displayMessage(ApplicationConstants.APP_NAME + " - Updater",
                                    "There is a new version of application:" + serverAppVersion.getVersion(),
                                    TrayIcon.MessageType.INFO);
        } /*else if (Internal.versionCompare(str, serverAppVersion.getVersion()) == 0) {
            //System.exit(0);

        }*/
    }

    private void createTrayIcon() {

        PopupMenu trayMenu = new PopupMenu();

        final CheckboxMenuItem autoUpdateCheckBox = new CheckboxMenuItem("Auto-update");
        try {
            System.out.println(
                    RegistryManager.KEY_AUTO_UPDATE + ": " + RegistryManager.isAutoUpdateActive());
        } catch (RegistryCanNotReadInfoException ignore) {/*NOP*/}

        try {
            autoUpdateCheckBox.setState(RegistryManager.isAutoUpdateActive());
        } catch (RegistryCanNotReadInfoException e) {
            RegistryManager.setDefaultSettings();
            autoUpdateCheckBox.setState(ApplicationConstants.IS_APP_AUTO_UPDATE_DEFAULT_VALUE);
        }
        autoUpdateCheckBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                System.out.println(RegistryManager.KEY_AUTO_UPDATE + ": " + autoUpdateCheckBox.getState());
                try {
                    RegistryManager.setAutoUpdateActive(autoUpdateCheckBox.getState());
                } catch (RegistryCanNotWriteInfoException e1) {
                    RegistryManager.setDefaultSettings();
                }
            }
        });
        trayMenu.add(autoUpdateCheckBox);
        trayMenu.addSeparator();

        MenuItem exit = new MenuItem("Exit");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tray.remove(trayIcon);
                //System.exit(0); //FIXME
            }
        });
        trayMenu.add(exit);

        trayIcon.setImageAutoSize(true);
        trayIcon.setPopupMenu(trayMenu);

        trayIcon.addMouseListener(new MouseAdapter() {
            UpdateDialog updateDialog = null;

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1) {
                    trayIcon.removeMouseListener(this);
                    Main.initUpdateJar();
                    tray.remove(trayIcon);
                    /*if (updateDialog != null) {
                        updateDialog.addWindowListener(new WindowAdapter() {
                            @Override
                            public void windowClosed(WindowEvent e) {
                                try {
                                    Main.loadProperties();
                                } catch (RegistryException e1) {
                                    e1.printStackTrace();
                                }
                                String str = properties.getProperty(RegistryManager.KEY_CURRENT_VERSION);

                                if (Internal.versionCompare(str, serverAppVersion.getVersion()) == 0) {
                                    tray.remove(trayIcon);
                                    System.exit(0);
                                }
                                super.windowClosing(e);

                            }

                        });
                    }*/

                    /*updateDialog.setVisible(true);
                    updateDialog.checkForUpdates();*/


                }
                super.mouseClicked(e);
            }
        });
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {<commit_msg>Code improvements<commit_after>package com.doos.update_module.nongui;

import com.doos.settings_manager.ApplicationConstants;
import com.doos.settings_manager.registry.RegistryCanNotReadInfoException;
import com.doos.settings_manager.registry.RegistryCanNotWriteInfoException;
import com.doos.settings_manager.registry.RegistryManager;
import com.doos.update_module.core.Main;
import com.doos.update_module.gui.UpdateDialog;
import com.doos.update_module.update.AppVersion;
import com.doos.update_module.update.Updater;
import com.doos.update_module.utils.Internal;

import java.awt.*;
import java.awt.event.*;

/**
 * Created by <NAME> on 04.11.2016.
 */


public class NonGuiUpdater {
    public static final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit()
                                                                 .getImage(
                                                                         NonGuiUpdater.class.getResource("/icon.png")));
    public static final SystemTray tray = SystemTray.getSystemTray();
    private AppVersion serverAppVersion = null;


    public NonGuiUpdater() {
        Updater updater = new Updater();
        serverAppVersion = updater.getAppVersion();
        compareVersions();
    }

    private void compareVersions() {
        String str = ApplicationConstants.APP_VERSION;
        if (Internal.versionCompare(str, serverAppVersion.getVersion()) < 0) {
            //create trayicon and show pop-up
            createTrayIcon();
            trayIcon.displayMessage(ApplicationConstants.APP_NAME + " - Updater",
                                    "There is a new version of application:" + serverAppVersion.getVersion(),
                                    TrayIcon.MessageType.INFO);
        } /*else if (Internal.versionCompare(str, serverAppVersion.getVersion()) == 0) {
            //System.exit(0);

        }*/
    }

    private void createTrayIcon() {

        PopupMenu trayMenu = new PopupMenu();

        final CheckboxMenuItem autoUpdateCheckBox = new CheckboxMenuItem("Auto-update");
        try {
            System.out.println(
                    RegistryManager.KEY_AUTO_UPDATE + ": " + RegistryManager.isAutoUpdateActive());
        } catch (RegistryCanNotReadInfoException ignore) {/*NOP*/}

        try {
            autoUpdateCheckBox.setState(RegistryManager.isAutoUpdateActive());
        } catch (RegistryCanNotReadInfoException e) {
            RegistryManager.setDefaultSettings();
            autoUpdateCheckBox.setState(ApplicationConstants.IS_APP_AUTO_UPDATE_DEFAULT_VALUE);
        }
        autoUpdateCheckBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                System.out.println(RegistryManager.KEY_AUTO_UPDATE + ": " + autoUpdateCheckBox.getState());
                try {
                    RegistryManager.setAutoUpdateActive(autoUpdateCheckBox.getState());
                } catch (RegistryCanNotWriteInfoException e1) {
                    RegistryManager.setDefaultSettings();
                }
            }
        });
        trayMenu.add(autoUpdateCheckBox);
        trayMenu.addSeparator();

        MenuItem exit = new MenuItem("Exit");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tray.remove(trayIcon);
                //System.exit(0); //FIXME
            }
        });
        trayMenu.add(exit);

        trayIcon.setImageAutoSize(true);
        trayIcon.setPopupMenu(trayMenu);

        trayIcon.addMouseListener(new MouseAdapter() {
            UpdateDialog updateDialog = null;

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1) {
                    trayIcon.removeMouseListener(this);
                    Main.initUpdateJar();
                    tray.remove(trayIcon);
                }
                super.mouseClicked(e);
            }
        });
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
{'fraction_non_alphanumeric': 0.09148638467333979, 'fraction_numerical': 0.0034441933053492627, 'mean_word_length': 1.8080991236022967, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6614343', 'n_tokens_mistral': 2337, 'n_tokens_neox': 2279, 'n_words': 381}