package de.ugoe.cs.swe.memos.gui.mainview;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.widgets.TreeItem;

import de.ugoe.cs.swe.memos.datamodel.Category;

public class CatExpandListener implements TreeListener {

	@Override
	public void treeCollapsed(TreeEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void treeExpanded(TreeEvent e) {
		TreeItem i = (TreeItem) e.item;
		expandHandler(i);
	}

	public static void expandHandler(TreeItem item) {

		item.removeAll();
		// DatabaseConnector dB=DatabaseConnector.getInstance();
		if (item.getData() != null) {
			Category c = (Category) item.getData();
			c.loadChildrenFromDB();

			for (Category ci : c.getChildCategories()) {
				TreeItem newti = new TreeItem(item, SWT.NONE);
				newti.setText(ci.getName());
				newti.setData(ci);
				TreeItem newSubTI = new TreeItem(newti, SWT.NONE);
				newSubTI.setText("...");
			}
		}
	}
}
