/*
 * Autopsy Forensic Browser
 *
 * Copyright 2011-2017 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.corecomponents;

import java.awt.Insets;
import java.io.File;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import javax.swing.BorderFactory;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import org.netbeans.spi.sendopts.OptionProcessor;
import org.netbeans.swing.tabcontrol.plaf.DefaultTabbedContainerUI;
import org.openide.modules.ModuleInstall;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.windows.WindowManager;
import org.sleuthkit.autopsy.actions.IngestRunningCheck;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.casemodule.CaseActionException;
import org.sleuthkit.autopsy.casemodule.CaseMetadata;
import org.sleuthkit.autopsy.casemodule.OpenFromArguments;
import org.sleuthkit.autopsy.casemodule.StartupWindowProvider;
import org.sleuthkit.autopsy.coreutils.Logger;

/**
 * Manages this module's life cycle. Opens the startup dialog during startup.
 */
public class Installer extends ModuleInstall {

    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(Installer.class.getName());
    private static Installer instance;

    public synchronized static Installer getDefault() {
        if (null == instance) {
            instance = new Installer();
        }
        return instance;
    }

    private Installer() {
        super();
    }

    @Override
    public void restored() {
        super.restored();

        setLookAndFeel();
        UIManager.put("ViewTabDisplayerUI", "org.sleuthkit.autopsy.corecomponents.NoTabsTabDisplayerUI");
        UIManager.put(DefaultTabbedContainerUI.KEY_VIEW_CONTENT_BORDER, BorderFactory.createEmptyBorder());
        UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));

        /*
         * Open the case if a case metadata file was double-clicked. This only
         * works if the user has associated files with ".aut" extensions with
         * Autopsy.
         */
        WindowManager.getDefault().invokeWhenUIReady(() -> {
            Collection<? extends OptionProcessor> processors = Lookup.getDefault().lookupAll(OptionProcessor.class);
            for (OptionProcessor processor : processors) {
                if (processor instanceof OpenFromArguments) {
                    OpenFromArguments argsProcessor = (OpenFromArguments) processor;
                    final String caseFile = argsProcessor.getDefaultArg();
                    if (caseFile != null && !caseFile.isEmpty() && caseFile.endsWith(CaseMetadata.getFileExtension()) && new File(caseFile).exists()) { //NON-NLS
                        try {
                            Case.openAsCurrentCase(caseFile);
                        } catch (CaseActionException ex) {
                            logger.log(Level.SEVERE, String.format("Error opening case with metadata file path %s", caseFile), ex); //NON-NLS
                        }
                        return;
                    }
                }
            }
            StartupWindowProvider.getInstance().open();
        });
    }

    @Override
    public void uninstalled() {
        super.uninstalled();
    }

    private void setLookAndFeel() {
        if (System.getProperty("os.name").toLowerCase().contains("mac")) { //NON-NLS
            setOSXLookAndFeel();
        }
    }

    /**
     * Set the look and feel to be the Cross Platform 'Metal', but keep Aqua
     * dependent elements that set the Menu Bar to be in the correct place on
     * Mac OS X.
     */
    private void setOSXLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            logger.log(Level.WARNING, "Error setting OS-X look-and-feel", ex); //NON-NLS
        }

        // Store the keys that deal with menu items
        final String[] UI_MENU_ITEM_KEYS = new String[]{"MenuBarUI",}; //NON-NLS    
        Map<Object, Object> uiEntries = new TreeMap<>();
        for (String key : UI_MENU_ITEM_KEYS) {
            uiEntries.put(key, UIManager.get(key));
        }

        // Use Metal if available.
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) { //NON-NLS
                try {
                    UIManager.setLookAndFeel(info.getClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    logger.log(Level.WARNING, "Error setting OS-X look-and-feel", ex); //NON-NLS
                }
                break;
            }
        }

        // Overwrite the Metal menu item keys to use the Aqua versions.
        uiEntries.entrySet().stream().forEach((entry) -> {
            UIManager.put(entry.getKey(), entry.getValue());
        });
    }
}

@@ -755,58 +755,8 @@
 ts;%0A
-import java.io.File;%0Aimport java.util.Collection;%0A
 impo
@@ -769,24 +769,24 @@
 a.util.Map;%0A
+
 import java.
@@ -869,43 +869,8 @@
 ry;%0A
-import javax.swing.SwingUtilities;%0A
 impo
@@ -997,58 +997,8 @@
 on;%0A
-import org.netbeans.spi.sendopts.OptionProcessor;%0A
 impo
@@ -1107,456 +1107,8 @@
 ll;%0A
-import org.openide.util.Lookup;%0Aimport org.openide.util.NbBundle;%0Aimport org.openide.windows.WindowManager;%0Aimport org.sleuthkit.autopsy.actions.IngestRunningCheck;%0Aimport org.sleuthkit.autopsy.casemodule.Case;%0Aimport org.sleuthkit.autopsy.casemodule.CaseActionException;%0Aimport org.sleuthkit.autopsy.casemodule.CaseMetadata;%0Aimport org.sleuthkit.autopsy.casemodule.OpenFromArguments;%0Aimport org.sleuthkit.autopsy.casemodule.StartupWindowProvider;%0A
 impo
@@ -1753,17 +1753,16 @@
 ored();%0A
-%0A
         
@@ -1989,24 +1989,24 @@
 yBorder());%0A
+
         UIMa
@@ -2077,1303 +2077,8 @@
 0));
-%0A%0A        /*%0A         * Open the case if a case metadata file was double-clicked. This only%0A         * works if the user has associated files with %22.aut%22 extensions with%0A         * Autopsy.%0A         */%0A        WindowManager.getDefault().invokeWhenUIReady(() -%3E %7B%0A            Collection%3C? extends OptionProcessor%3E processors = Lookup.getDefault().lookupAll(OptionProcessor.class);%0A            for (OptionProcessor processor : processors) %7B%0A                if (processor instanceof OpenFromArguments) %7B%0A                    OpenFromArguments argsProcessor = (OpenFromArguments) processor;%0A                    final String caseFile = argsProcessor.getDefaultArg();%0A                    if (caseFile != null && !caseFile.isEmpty() && caseFile.endsWith(CaseMetadata.getFileExtension()) && new File(caseFile).exists()) %7B //NON-NLS%0A                        try %7B%0A                            Case.openAsCurrentCase(caseFile);%0A                        %7D catch (CaseActionException ex) %7B%0A                            logger.log(Level.SEVERE, String.format(%22Error opening case with metadata file path %25s%22, caseFile), ex); //NON-NLS%0A                        %7D%0A                        return;%0A                    %7D%0A                %7D%0A            %7D%0A            StartupWindowProvider.getInstance().open();%0A        %7D);
 %0A   

Remove double-click case metadata file to open feature