/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing.framemanager.tree;

import com.dmdirc.addons.ui_swing.SwingWindowFactory;
import com.dmdirc.addons.ui_swing.components.TreeScroller;
import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
import com.dmdirc.logger.ErrorLevel;
import com.dmdirc.logger.Logger;

import javax.swing.tree.TreePath;

/**
 * Extention of TreeScroller to scroll the tree view.
 */
public class TreeTreeScroller extends TreeScroller {

    /** Active frame manager. */
    private final ActiveFrameManager activeFrameManager;
    /** Factory to use to retrieve swing windows. */
    private final SwingWindowFactory windowFactory;

    /**
     * Creates a new Tree scroller for the tree view.
     *
     * @param activeFrameManager The active window manager
     * @param windowFactory     Factory to use to retrieve swing windows.
     * @param tree              Tree view tree
     */
    public TreeTreeScroller(
            final ActiveFrameManager activeFrameManager,
            final SwingWindowFactory windowFactory,
            final Tree tree) {
        super(tree);

        this.activeFrameManager = activeFrameManager;
        this.windowFactory = windowFactory;
    }

    @Override
    protected void setPath(final TreePath path) {
        if (path == null) {
            Logger.appError(ErrorLevel.HIGH, "Unable to change focus",
                    new IllegalArgumentException("path == null"));
            return;
        }
        if (path.getLastPathComponent() == null) {
            Logger.appError(ErrorLevel.HIGH, "Unable to change focus",
                    new IllegalArgumentException("Last component == null"));
            return;
        }
        if (((TreeViewNode) path.getLastPathComponent()).getWindow() == null) {
            Logger.appError(ErrorLevel.HIGH, "Unable to change focus",
                    new IllegalArgumentException("Frame is null"));
            return;
        }
        super.setPath(path);

        activeFrameManager.setActiveFrame(windowFactory.getSwingWindow(
                ((TreeViewNode) path.getLastPathComponent()).getWindow()));
    }

}

@@ -1358,111 +1358,107 @@
 er;%0A
+%0A
 import 
-com.dmdirc.logger.ErrorLevel;%0Aimport com.dmdirc.logger.Logger;%0A%0Aimport javax.swing.tree.TreePath
+javax.swing.tree.TreePath;%0A%0Aimport static com.google.common.base.Preconditions.checkNotNull
 ;%0A%0A/
@@ -1928,16 +1928,17 @@
 ory     
+ 
 Factory 
@@ -2003,16 +2003,17 @@
         
+ 
 Tree vie
@@ -2399,673 +2399,158 @@
     
-if (path == null) %7B%0A            Logger.appError(ErrorLevel.HIGH, %22Unable to change focus%22,%0A                    new IllegalArgumentException(%22path == null%22));%0A            return;%0A        %7D%0A        if (path.getLastPathComponent() == null) %7B%0A            Logger.appError(ErrorLevel.HIGH, %22Unable to change focus%22,%0A                    new IllegalArgumentException(%22Last component == null%22));%0A            return;%0A        %7D%0A        if (((TreeViewNode) path.getLastPathComponent()).getWindow() == null) %7B%0A            Logger.appError(ErrorLevel.HIGH, %22Unable to change focus%22,%0A                    new IllegalArgumentException(%22Frame is null%22));%0A            return;%0A        %7D
+checkNotNull(path);%0A        checkNotNull(path.getLastPathComponent());%0A        checkNotNull(((TreeViewNode) path.getLastPathComponent()).getWindow());
 %0A   
@@ -2575,17 +2575,16 @@
 (path);%0A
-%0A
         

Tidy up TreeTreeScroller.