/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2012 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.plugin.deskmanager.client.gwt.manager.common.layertree;

import org.geomajas.plugin.deskmanager.client.gwt.geodesk.widget.infowindow.NotificationWindow;
import org.geomajas.plugin.deskmanager.client.gwt.manager.common.LayerGroupConfigurationWindow;
import org.geomajas.widget.layer.configuration.client.ClientBranchNodeInfo;

import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.util.ValueCallback;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.MenuItemIfFunction;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

/**
 * Used by LayerSelectPanel, this is one Layertree.
 * 
 * @author Kristof Heirwegh
 */
public class LayerTreeGrid extends TreeGrid {

	private HLayout rollOverCanvas;

	private LayerGroupConfigurationWindow layerGroupConfWi;

	private final Menu menu = new Menu();

	private final MenuItem menuMapAdd = new MenuItem("Nieuwe map maken");

	private final MenuItem menuMapDelete = new MenuItem("Map verwijderen");

	private TreeGrid sourceTreeGrid;

	private LayerTreeNode rollOverNode;

	private Integer rowNumber;

	public LayerTreeGrid(String title, boolean editable) {
		super();
		setWidth100();
		setHeight100();
		setShowRoot(false);
		if (editable) {
			setShowRollOverCanvas(true);
			setCanSort(false); // this messes things up
		} else {
			setShowRollOverCanvas(false);
		}
		setCanReorderRecords(true);
		setDragDataAction(DragDataAction.MOVE);
		setCanAcceptDroppedRecords(true);
		setCanDragRecordsOut(true);
		setSelectionType(SelectionStyle.MULTIPLE);
		setShowAllRecords(true);

		TreeGridField nameFld = new TreeGridField(LayerTreeNode.FLD_NAME);
		nameFld.setWidth("*");
		nameFld.setTitle(title);

		TreeGridField publicFld = new TreeGridField(LayerTreeNode.FLD_PUBLIC, "Publiek");
		publicFld.setType(ListGridFieldType.BOOLEAN);
		publicFld.setWidth(90);
		publicFld.setPrompt("Aan: laag kan geraadpleegd worden in een publiek loket.");

		setFields(nameFld, publicFld);

		// -- Context menu --------------------------------------------------------

		menuMapAdd.addClickHandler(new ClickHandler() {

			public void onClick(MenuItemClickEvent event) {
				mapAdd();
			}
		});
		menuMapDelete.addClickHandler(new ClickHandler() {

			public void onClick(MenuItemClickEvent event) {
				mapDelete();
			}
		});
		menuMapDelete.setEnableIfCondition(new MenuItemIfFunction() {

			public boolean execute(Canvas target, Menu menu, MenuItem item) {
				if (item.equals(menuMapDelete)) {
					LayerTreeNode node = (LayerTreeNode) getSelectedRecord();
					if (node == null) {
						return false;
					}
					return !getTree().isLeaf(node);
				} else {
					return true;
				}
			}
		});

		menu.addItem(menuMapAdd);
		menu.addItem(menuMapDelete);
		setContextMenu(menu);
	}

	public void setSourceTreeGrid(TreeGrid treeGrid) {
		this.sourceTreeGrid = treeGrid;
	}

	private void mapDelete() {
		menu.hideContextMenu();
		LayerTreeNode node = (LayerTreeNode) getSelectedRecord();
		if (node != null && !getTree().isLeaf(node)) {
			TreeNode[] children = getTree().getChildren(node);
			if (children != null && children.length > 0) {
				if (sourceTreeGrid != null) {
					sourceTreeGrid.getTree().addList(children, sourceTreeGrid.getTree().getRoot());
				} else {
					getTree().addList(children, getTree().getRoot());
				}
			}
			getTree().remove(node);
		} else {
			NotificationWindow.showErrorMessage("Gelieve een map te selecteren vooraleer"
					+ " de actie \"Map Verwijderen\" uit te voeren.");
		}
	}

	private void mapAdd() {
		menu.hideContextMenu();
		SC.askforValue("Gelieve de naam voor de nieuwe map in te geven.", new ValueCallback() {

			public void execute(String value) {
				if ((value != null) && (!"".equals(value.trim()))) {
					ClientBranchNodeInfo newNode = new ClientBranchNodeInfo();
					newNode.setLabel(value);
					LayerTreeNode mapNode = new LayerTreeNode(newNode);
					mapNode.setAttribute(LayerTreeNode.FLD_PUBLIC, true);
					getTree().add(mapNode, getClosestFolder((LayerTreeNode) getSelectedRecord()));
				}
			}
		});
	}

	private LayerTreeNode getClosestFolder(LayerTreeNode node) {
		LayerTreeNode parent = node;
		while (parent != null) {
			if (getTree().isLeaf(parent)) {
				parent = (LayerTreeNode) getTree().getParent(parent);
			} else {
				return parent;
			}
		}
		return (LayerTreeNode) getTree().getRoot();
	}
}

@@ -590,104 +590,8 @@
 ow;%0A
-import org.geomajas.plugin.deskmanager.client.gwt.manager.common.LayerGroupConfigurationWindow;%0A
 impo
@@ -662,16 +662,16 @@
 deInfo;%0A
+
 %0Aimport 
@@ -943,59 +943,8 @@
 as;%0A
-import com.smartgwt.client.widgets.layout.HLayout;%0A
 impo
@@ -1522,100 +1522,8 @@
  %7B%0A%0A
-%09private HLayout rollOverCanvas;%0A%0A%09private LayerGroupConfigurationWindow layerGroupConfWi;%0A%0A
 %09pri
@@ -1700,24 +1700,24 @@
 ijderen%22);%0A%0A
+
 %09private Tre
@@ -1743,75 +1743,8 @@
 d;%0A%0A
-%09private LayerTreeNode rollOverNode;%0A%0A%09private Integer rowNumber;%0A%0A
 %09pub

Add GeodeskSelectionHandler to geodesk configuration panels GDM-2: Layertree configuration from widgetinfo