package com.darkyen;

import com.intellij.openapi.components.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.WindowManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 *
 */
@State(
        name="DarkyenusTimeTracker",
        storages = {@Storage(value = StoragePathMacros.WORKSPACE_FILE, roamingType = RoamingType.DEFAULT)}
        )
public final class TimeTrackerComponent implements ProjectComponent, PersistentStateComponent<TimeTrackerState> {

    private final Project project;
    private TimeTrackerWidget widget;

    private TimeTrackerState lastStateCache = null;

    public TimeTrackerComponent(Project project) {
        this.project = project;
    }

    @Override
    public void initComponent() {
    }

    @Override
    public void disposeComponent() {
    }

    @Override
    @NotNull
    public String getComponentName() {
        return "TimeTrackerComponent";
    }

    @Override
    public void projectOpened() {
        if (widget == null) {
            widget = new TimeTrackerWidget(project);
            if (lastStateCache != null) {
                widget.setState(lastStateCache);
                lastStateCache = null;
            }
            WindowManager.getInstance().getStatusBar(project).addWidget(widget);
        }
    }

    @Override
    public void projectClosed() {
        if (widget != null) {
            WindowManager.getInstance().getStatusBar(project).removeWidget(widget.ID());
            lastStateCache = widget.getState();
        }
    }

    @Nullable
    @Override
    public TimeTrackerState getState() {
        if (widget != null) {
            return widget.getState();
        } else if(lastStateCache != null) {
            return lastStateCache;
        } else {
            return new TimeTrackerState();
        }
    }

    @Override
    public void loadState(TimeTrackerState state) {
        if (widget != null) {
            widget.setState(state);
        } else {
            lastStateCache = state;
        }
    }
}

@@ -102,16 +102,58 @@
 roject;%0A
+import com.intellij.openapi.wm.StatusBar;%0A
 import c
@@ -276,16 +276,82 @@
 lable;%0A%0A
+import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0A
 /**%0A *%0A 
@@ -630,16 +630,111 @@
 ate%3E %7B%0A%0A
+    private static final Logger LOG = Logger.getLogger(TimeTrackerComponent.class.getName());%0A%0A
     priv
@@ -1159,32 +1159,258 @@
 ponent%22;%0A    %7D%0A%0A
+    private StatusBar getProjectStatusBar() %7B%0A        final WindowManager windowManager = WindowManager.getInstance();%0A        if (windowManager == null) return null;%0A        return windowManager.getStatusBar(project);%0A    %7D%0A%0A
     @Override%0A  
@@ -1684,76 +1684,291 @@
     
-WindowManager.getInstance().getStatusBar(project).addWidget(widget);
+final StatusBar statusBar = getProjectStatusBar();%0A            if (statusBar != null) %7B%0A                statusBar.addWidget(widget);%0A            %7D else %7B%0A                LOG.log(Level.SEVERE, %22Can't initialize time tracking widget, status bar returned by IDE is null%22);%0A            %7D
 %0A   
@@ -2075,84 +2075,296 @@
     
-WindowManager.getInstance().getStatusBar(project).removeWidget(widget.ID());
+final StatusBar statusBar = getProjectStatusBar();%0A            if (statusBar != null) %7B%0A                statusBar.removeWidget(widget.ID());%0A            %7D else %7B%0A                LOG.log(Level.WARNING, %22Can't remove time tracking widget, status bar returned by IDE is null%22);%0A            %7D
 %0A   

Fix error on IDE closing