<commit_before>package org.safehaus.kiskis.mgmt.server.ui;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.HttpServletRequestListener;
import com.vaadin.ui.*;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.themes.Runo;
import org.safehaus.kiskis.mgmt.api.agentmanager.AgentManager;
import org.safehaus.kiskis.mgmt.api.commandrunner.CommandRunner;
import org.safehaus.kiskis.mgmt.api.tracker.Tracker;
import org.safehaus.kiskis.mgmt.server.ui.services.MainUISelectedTabChangeListener;
import org.safehaus.kiskis.mgmt.server.ui.services.Module;
import org.safehaus.kiskis.mgmt.server.ui.services.ModuleNotifier;
import org.safehaus.kiskis.mgmt.server.ui.services.ModuleServiceListener;
import org.safehaus.kiskis.mgmt.shared.protocol.Agent;
import org.safehaus.kiskis.mgmt.shared.protocol.Disposable;
import org.safehaus.kiskis.mgmt.shared.protocol.settings.Common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

@SuppressWarnings("serial")
public class MgmtApplication extends Application implements ModuleServiceListener, HttpServletRequestListener {

    private static final Logger LOG = Logger.getLogger(MgmtApplication.class.getName());
    private static final ThreadLocal<MgmtApplication> threadLocal = new ThreadLocal<MgmtApplication>();
    private final ModuleNotifier moduleNotifier;
    private final AgentManager agentManager;
    private final CommandRunner commandRunner;
    private final Tracker tracker;
    private final String title;
    private TabSheet tabs;

    public MgmtApplication(String title, AgentManager agentManager, CommandRunner commandRunner, Tracker tracker, ModuleNotifier moduleNotifier) {
        this.agentManager = agentManager;
        this.commandRunner = commandRunner;
        this.tracker = tracker;
        this.moduleNotifier = moduleNotifier;
        this.title = title;
    }


    private static MgmtApplication getInstance() {
        return threadLocal.get();
    }

    private static void setInstance(MgmtApplication application) {
        threadLocal.set(application);
    }

    public static void showConfirmationDialog(final String caption, final String question,
                                              final String okLabel, final String cancelLabel, final ConfirmationDialogCallback callback) {
        try {
            if (getInstance() != null) {
                final ConfirmationDialog cd = new ConfirmationDialog(
                        caption,
                        question,
                        okLabel, cancelLabel, callback);
                getInstance().getMainWindow().addWindow(cd);
                cd.bringToFront();
            }

        } catch (IllegalArgumentException e) {
            LOG.log(Level.SEVERE, "Error in showConfirmationDialog", e);
        } catch (NullPointerException e) {
            LOG.log(Level.SEVERE, "Error in showConfirmationDialog", e);
        }
    }

    public static void addCustomWindow(Window window) {
        try {
            if (getInstance() != null && window != null) {
                getInstance().getMainWindow().addWindow(window);
                window.bringToFront();
            }
        } catch (IllegalArgumentException e) {
            LOG.log(Level.SEVERE, "Error in addCustomWindow", e);
        } catch (NullPointerException e) {
            LOG.log(Level.SEVERE, "Error in addCustomWindow", e);
        }
    }

    public static void removeCustomWindow(Window window) {
        try {
            if (getInstance() != null && window != null) {
                getInstance().getMainWindow().removeWindow(window);
            }
        } catch (Exception e) {
            LOG.log(Level.SEVERE, "Error in removeCustomWindow", e);
        }
    }

    public static MgmtAgentManager createAgentTree() {
        if (getInstance() != null) {
            return new MgmtAgentManager(getInstance().agentManager);
        }
        return null;
    }

    public static Window createTerminalWindow(final Set<Agent> agents) {
        if (getInstance() != null) {
            return new TerminalWindow(agents, getInstance().commandRunner, getInstance().agentManager);
        }
        return null;
    }

    public static Window createProgressWindow(String source, UUID trackID) {
        if (getInstance() != null) {
            return new ProgressWindow(getInstance().tracker, trackID, source);
        }
        return null;
    }

    public static void showProgressWindow(String source, UUID trackID, final Window.CloseListener closeCallback) {
        Window progressWindow = MgmtApplication.createProgressWindow(source, trackID);
        MgmtApplication.addCustomWindow(progressWindow);
        if (closeCallback != null) {
            progressWindow.addListener(new Window.CloseListener() {

                @Override
                public void windowClose(Window.CloseEvent e) {
                    closeCallback.windowClose(e);
                }
            });
        }
    }

    @Override
    public void init() {
        setInstance(this);
        try {
            setTheme(Runo.themeName());

            Window window = new Window(title);
            setMainWindow(window);

            VerticalLayout layout = new VerticalLayout();
            layout.setSizeFull();
            tabs = new TabSheet();
            tabs.setSizeFull();
            tabs.setImmediate(true);
            for (Module module : moduleNotifier.getModules()) {
                Component component = module.createComponent();
                tabs.addTab(component, module.getName(), null);
            }
            layout.addComponent(tabs);
            layout.setExpandRatio(tabs, 1f);

            getMainWindow().setContent(layout);
            //add listener
            moduleNotifier.addListener(this);
            getMainWindow().addListener(new Window.CloseListener() {
                @Override
                public void windowClose(Window.CloseEvent e) {
                    close();
                }
            });
            //
            final ProgressIndicator indicator
                    = new ProgressIndicator(new Float(0.0));
            indicator.setPollingInterval(Common.REFRESH_UI_SEC * 1000);
            indicator.setWidth("1px");
            indicator.setHeight("1px");
            getMainWindow().addComponent(indicator);
            //

            tabs.addListener(new TabSheet.SelectedTabChangeListener() {

                public void selectedTabChange(TabSheet.SelectedTabChangeEvent event) {
                    TabSheet tabsheet = event.getTabSheet();
                    Tab selectedTab = tabsheet.getTab(event.getTabSheet().getSelectedTab());
                    notifyTabListeners(selectedTab);
                }
            });

            if (tabs.getSelectedTab() != null) {
                notifyTabListeners(tabs.getTab(tabs.getSelectedTab()));
            }
        } catch (Exception ex) {
        }
    }

    private void notifyTabListeners(Tab selectedTab) {
        Iterator<Component> it = tabs.getComponentIterator();
        while (it.hasNext()) {
            Component component = it.next();
            if (component instanceof MainUISelectedTabChangeListener) {
                try {
                    ((MainUISelectedTabChangeListener) component).selectedTabChanged(selectedTab);
                } catch (Exception e) {
                }
            }
        }
    }

    @Override
    public void close() {
        try {
            super.close();
            Iterator<Component> it = tabs.getComponentIterator();
            while (it.hasNext()) {
                Component component = it.next();
                if (component instanceof Disposable) {
                    try {
                        ((Disposable) component).dispose();
                    } catch (Exception e) {
                    }
                }
            }
            moduleNotifier.removeListener(this);
            LOG.log(Level.INFO, "Kiskis Management Vaadin UI: Application closing, removing module service listener");
        } catch (Exception e) {
            LOG.log(Level.SEVERE, "Kiskis Management Vaadin UI: Error closing", e);
        }
    }

    @Override
    public void moduleRegistered(Module module) {
        try {
            LOG.log(Level.INFO, "Kiskis Management Vaadin UI: Module registered, adding tab");
            Component component = module.createComponent();
            Tab tab = tabs.addTab(component, module.getName(), null);
            List<Module> modules = new ArrayList<Module>(moduleNotifier.getModules());
            LOG.info(moduleNotifier.getModules().toString());
            tabs.setTabPosition(tab, modules.indexOf(module));
        } catch (Exception e) {
            LOG.log(Level.SEVERE, "Kiskis Management Vaadin UI: Error registering module{0}", e);
        }<commit_msg>fixed agent tree bug not showing when redeploying component<commit_after>package org.safehaus.kiskis.mgmt.server.ui;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.HttpServletRequestListener;
import com.vaadin.ui.*;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.themes.Runo;
import org.safehaus.kiskis.mgmt.api.agentmanager.AgentManager;
import org.safehaus.kiskis.mgmt.api.commandrunner.CommandRunner;
import org.safehaus.kiskis.mgmt.api.tracker.Tracker;
import org.safehaus.kiskis.mgmt.server.ui.services.MainUISelectedTabChangeListener;
import org.safehaus.kiskis.mgmt.server.ui.services.Module;
import org.safehaus.kiskis.mgmt.server.ui.services.ModuleNotifier;
import org.safehaus.kiskis.mgmt.server.ui.services.ModuleServiceListener;
import org.safehaus.kiskis.mgmt.shared.protocol.Agent;
import org.safehaus.kiskis.mgmt.shared.protocol.Disposable;
import org.safehaus.kiskis.mgmt.shared.protocol.settings.Common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

@SuppressWarnings("serial")
public class MgmtApplication extends Application implements ModuleServiceListener, HttpServletRequestListener {

    private static final Logger LOG = Logger.getLogger(MgmtApplication.class.getName());
    private static final ThreadLocal<MgmtApplication> threadLocal = new ThreadLocal<MgmtApplication>();
    private static AgentManager agentManager;
    private final ModuleNotifier moduleNotifier;
    private final CommandRunner commandRunner;
    private final Tracker tracker;
    private final String title;
    private TabSheet tabs;

    public MgmtApplication(String title, AgentManager agentManager, CommandRunner commandRunner, Tracker tracker, ModuleNotifier moduleNotifier) {
        MgmtApplication.agentManager = agentManager;
        this.commandRunner = commandRunner;
        this.tracker = tracker;
        this.moduleNotifier = moduleNotifier;
        this.title = title;
    }


    private static MgmtApplication getInstance() {
        return threadLocal.get();
    }

    private static void setInstance(MgmtApplication application) {
        threadLocal.set(application);
    }

    public static void showConfirmationDialog(final String caption, final String question,
                                              final String okLabel, final String cancelLabel, final ConfirmationDialogCallback callback) {
        try {
            if (getInstance() != null) {
                final ConfirmationDialog cd = new ConfirmationDialog(
                        caption,
                        question,
                        okLabel, cancelLabel, callback);
                getInstance().getMainWindow().addWindow(cd);
                cd.bringToFront();
            }

        } catch (IllegalArgumentException e) {
            LOG.log(Level.SEVERE, "Error in showConfirmationDialog", e);
        } catch (NullPointerException e) {
            LOG.log(Level.SEVERE, "Error in showConfirmationDialog", e);
        }
    }

    public static void addCustomWindow(Window window) {
        try {
            if (getInstance() != null && window != null) {
                getInstance().getMainWindow().addWindow(window);
                window.bringToFront();
            }
        } catch (IllegalArgumentException e) {
            LOG.log(Level.SEVERE, "Error in addCustomWindow", e);
        } catch (NullPointerException e) {
            LOG.log(Level.SEVERE, "Error in addCustomWindow", e);
        }
    }

    public static void removeCustomWindow(Window window) {
        try {
            if (getInstance() != null && window != null) {
                getInstance().getMainWindow().removeWindow(window);
            }
        } catch (Exception e) {
            LOG.log(Level.SEVERE, "Error in removeCustomWindow", e);
        }
    }

    public static MgmtAgentManager createAgentTree() {
        return new MgmtAgentManager(agentManager);
    }

    public static Window createTerminalWindow(final Set<Agent> agents) {
        if (getInstance() != null) {
            getInstance();
            return new TerminalWindow(agents, getInstance().commandRunner, agentManager);
        }
        return null;
    }

    public static Window createProgressWindow(String source, UUID trackID) {
        if (getInstance() != null) {
            return new ProgressWindow(getInstance().tracker, trackID, source);
        }
        return null;
    }

    public static void showProgressWindow(String source, UUID trackID, final Window.CloseListener closeCallback) {
        Window progressWindow = MgmtApplication.createProgressWindow(source, trackID);
        MgmtApplication.addCustomWindow(progressWindow);
        if (closeCallback != null) {
            progressWindow.addListener(new Window.CloseListener() {

                @Override
                public void windowClose(Window.CloseEvent e) {
                    closeCallback.windowClose(e);
                }
            });
        }
    }

    @Override
    public void init() {
        setInstance(this);
        try {
            setTheme(Runo.themeName());

            Window window = new Window(title);
            setMainWindow(window);

            VerticalLayout layout = new VerticalLayout();
            layout.setSizeFull();
            tabs = new TabSheet();
            tabs.setSizeFull();
            tabs.setImmediate(true);
            for (Module module : moduleNotifier.getModules()) {
                Component component = module.createComponent();
                tabs.addTab(component, module.getName(), null);
            }
            layout.addComponent(tabs);
            layout.setExpandRatio(tabs, 1f);

            getMainWindow().setContent(layout);
            //add listener
            moduleNotifier.addListener(this);
            getMainWindow().addListener(new Window.CloseListener() {
                @Override
                public void windowClose(Window.CloseEvent e) {
                    close();
                }
            });
            //
            final ProgressIndicator indicator
                    = new ProgressIndicator(new Float(0.0));
            indicator.setPollingInterval(Common.REFRESH_UI_SEC * 1000);
            indicator.setWidth("1px");
            indicator.setHeight("1px");
            getMainWindow().addComponent(indicator);
            //

            tabs.addListener(new TabSheet.SelectedTabChangeListener() {

                public void selectedTabChange(TabSheet.SelectedTabChangeEvent event) {
                    TabSheet tabsheet = event.getTabSheet();
                    Tab selectedTab = tabsheet.getTab(event.getTabSheet().getSelectedTab());
                    notifyTabListeners(selectedTab);
                }
            });

            if (tabs.getSelectedTab() != null) {
                notifyTabListeners(tabs.getTab(tabs.getSelectedTab()));
            }
        } catch (Exception ex) {
        }
    }

    private void notifyTabListeners(Tab selectedTab) {
        Iterator<Component> it = tabs.getComponentIterator();
        while (it.hasNext()) {
            Component component = it.next();
            if (component instanceof MainUISelectedTabChangeListener) {
                try {
                    ((MainUISelectedTabChangeListener) component).selectedTabChanged(selectedTab);
                } catch (Exception e) {
                }
            }
        }
    }

    @Override
    public void close() {
        MgmtApplication.agentManager = null;
        try {
            super.close();
            Iterator<Component> it = tabs.getComponentIterator();
            while (it.hasNext()) {
                Component component = it.next();
                if (component instanceof Disposable) {
                    try {
                        ((Disposable) component).dispose();
                    } catch (Exception e) {
                    }
                }
            }
            moduleNotifier.removeListener(this);
            LOG.log(Level.INFO, "Kiskis Management Vaadin UI: Application closing, removing module service listener");
        } catch (Exception e) {
            LOG.log(Level.SEVERE, "Kiskis Management Vaadin UI: Error closing", e);
        }
    }

    @Override
    public void moduleRegistered(Module module) {
        try {
            LOG.log(Level.INFO, "Kiskis Management Vaadin UI: Module registered, adding tab");
            Component component = module.createComponent();
            Tab tab = tabs.addTab(component, module.getName(), null);
            List<Module> modules = new ArrayList<Module>(moduleNotifier.getModules());
            LOG.info(moduleNotifier.getModules().toString());
            tabs.setTabPosition(tab, modules.indexOf(module));
        } catch (Exception e) {
            LOG.log(Level.SEVERE, "Kiskis Management Vaadin UI: Error registering module{0}", e);
        }
{'fraction_non_alphanumeric': 0.09490148057441834, 'fraction_numerical': 0.0011132138483802739, 'mean_word_length': 2.2141323792486585, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5533356', 'n_tokens_mistral': 4632, 'n_tokens_neox': 4405, 'n_words': 964}