/*******************************************************************************
 * Copyright (c) 2012-2015 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.ext.java.client.project.node;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.api.project.shared.dto.ItemReference;
import org.eclipse.che.api.project.shared.dto.ProjectDescriptor;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.ide.api.project.node.HasStorablePath;
import org.eclipse.che.ide.api.project.node.Node;
import org.eclipse.che.ide.api.project.node.resource.RenameProcessor;
import org.eclipse.che.ide.ext.java.client.project.settings.JavaNodeSettings;
import org.eclipse.che.ide.project.node.FolderReferenceNode;
import org.eclipse.che.ide.project.node.resource.ItemReferenceProcessor;
import org.eclipse.che.ide.ui.smartTree.presentation.NodePresentation;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Vlad Zhukovskiy
 */
public class PackageNode extends FolderReferenceNode {

    private final JavaNodeManager nodeManager;

    @Inject
    public PackageNode(@Assisted ItemReference itemReference,
                       @Assisted ProjectDescriptor projectDescriptor,
                       @Assisted JavaNodeSettings nodeSettings,
                       @NotNull EventBus eventBus,
                       @NotNull JavaNodeManager nodeManager,
                       @NotNull ItemReferenceProcessor resourceProcessor) {
        super(itemReference, projectDescriptor, nodeSettings, eventBus, nodeManager, resourceProcessor);
        this.nodeManager = nodeManager;
    }

    @NotNull
    @Override
    protected Promise<List<Node>> getChildrenImpl() {
        return nodeManager.getChildren(getStorablePath(),
                                       getProjectDescriptor(),
                                       getSettings());
    }

    @Override
    public void updatePresentation(@NotNull NodePresentation presentation) {
        presentation.setPresentableText(getDisplayPackage());
        presentation.setPresentableIcon(nodeManager.getJavaNodesResources().packageIcon());
    }

    @Override
    public String getName() {
        return getPackage();
    }

    public String getPackage() {
        final SourceFolderNode sourceFolder = getSourceFolder();
        final String sourcePath = sourceFolder.getStorablePath();
        String path = "";

        if (getData().getPath().startsWith(sourcePath + "/")) {
            path = getData().getPath().substring(sourcePath.length() + 1);
        }

        return path.replace('/', '.');
    }

    public String getDisplayPackage() {
        if (getParent() == null || !(getParent() instanceof PackageNode)) {
            return getPackage();
        }

        PackageNode parent = (PackageNode)getParent();

        return getPackage().startsWith(parent.getPackage()) ? getPackage().substring(parent.getPackage().length() + 1) : getPackage();
    }

    @Override
    public RenameProcessor<ItemReference> getRenameProcessor() {
        return null;
    }

    @Override
    public String getStorablePath() {
        if (getParent() == null || !(getParent() instanceof HasStorablePath)) {
            return getData().getPath();
        }

        return ((HasStorablePath)getParent()).getStorablePath() + "/" + getDisplayPackage().replace(".", "/");
    }

    public SourceFolderNode getSourceFolder() {
        Node parent = getParent();

        while (parent != null) {
            if (parent instanceof SourceFolderNode) {
                return (SourceFolderNode)parent;
            }

            parent = parent.getParent();
        }

        throw new IllegalStateException("Source directory wasn't bind to package.");
    }
}

@@ -2106,24 +2106,49 @@
 ger;%0A    %7D%0A%0A
+    /** %7B@inheritDoc%7D */%0A
     @NotNull
@@ -2148,16 +2148,16 @@
 NotNull%0A
-
     @Ove
@@ -2391,32 +2391,57 @@
 ings());%0A    %7D%0A%0A
+    /** %7B@inheritDoc%7D */%0A
     @Override%0A  
@@ -2668,32 +2668,57 @@
 Icon());%0A    %7D%0A%0A
+    /** %7B@inheritDoc%7D */%0A
     @Override%0A  
@@ -2773,32 +2773,153 @@
 ckage();%0A    %7D%0A%0A
+    /**%0A     * Return full FQN name for the package. E.g. a.b.c.d%0A     *%0A     * @return FQN name for the package%0A     */%0A
     public Strin
@@ -3281,32 +3281,217 @@
 ', '.');%0A    %7D%0A%0A
+    /**%0A     * Return display FQN for the package according to the parent. E.g. from package a.b.c.d will displayed only c.d%0A     *%0A     * @return partially displayed FQN name.%0A     */%0A
     public Strin
@@ -3824,32 +3824,57 @@
 ckage();%0A    %7D%0A%0A
+    /** %7B@inheritDoc%7D */%0A
     @Override%0A  
@@ -3960,24 +3960,49 @@
 ull;%0A    %7D%0A%0A
+    /** %7B@inheritDoc%7D */%0A
     @Overrid
@@ -4286,24 +4286,151 @@
 /%22);%0A    %7D%0A%0A
+    /**%0A     * Return source folder node in which package node exists.%0A     *%0A     * @return parent source folder node%0A     */%0A
     public S

Add javadoc