PDA

View Full Version : Task Tree Item



yopiyop
12 Sep 2008, 5:15 AM
In reply to
http://extjs.com/forum/showthread.php?t=45304 (cannot reply, forum locked !!!)
http://extjs.com/forum/showthread.php?t=42599

this is not a GXT extension but simple GWT subclass:




import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

public class TaskTreeItem extends TreeItem {

public static enum TaskState {
UNCHECKED,
PARTIAL,
CHECKED
}

public final static String IMG_CHECKED = "img/check2.gif";
public final static String IMG_PARTIAL = "img/check1.gif";
public final static String IMG_UNCHECKED = "img/check0.gif";

TaskState state = TaskState.UNCHECKED;
final Image img = new Image(IMG_UNCHECKED);
boolean root = false;

public TaskTreeItem(String html,TaskState state) {
if(state!=null) this.state = state;
init(new HTML(html, false));
}

protected void init(Widget widget) {
setWidget(new TaskComposite(widget,img));
img.addClickListener(new ClickListener(){
public void onClick(Widget sender) {
if (state.equals(TaskState.CHECKED))
uncheck();
else
check();
}
});
}

public void check() {
state = TaskState.CHECKED;
img.setUrl(IMG_CHECKED);
if (getChildCount() != 0) {
final int count = getChildCount();
for (int i=0; i<count; i++)
((TaskTreeItem) getChild(i)).check();
}

TaskTreeItem parent = (TaskTreeItem) getParentItem();
if (parent != null)
parent.update();
}

public void uncheck() {
state = TaskState.UNCHECKED;
img.setUrl(IMG_UNCHECKED);
if (getChildCount() != 0) {
final int count = getChildCount();
for (int i=0; i<count; i++)
((TaskTreeItem) getChild(i)).uncheck();
}

TaskTreeItem parent = (TaskTreeItem) getParentItem();
if (parent != null)
parent.update();
}

public void update() {
boolean hasChecked = false;
boolean hasPartial = false;
boolean hasUnchecked = false;
final int count = getChildCount();
for (int i=0; i<count; i++) {
if (hasPartial) break;//pas la peine de regarder les autres
if (hasChecked && hasUnchecked) break;//pas la peine de regarder les autres
TaskTreeItem item = (TaskTreeItem) getChild(i);
if (item.state.equals(TaskState.CHECKED))
hasChecked = true;
if (item.state.equals(TaskState.PARTIAL))
hasPartial = true;
if (item.state.equals(TaskState.UNCHECKED))
hasUnchecked = true;
}

final TaskState old = state;
if (hasPartial) {
state = TaskState.PARTIAL;
img.setUrl(IMG_PARTIAL);
} else if (hasChecked)
if (hasUnchecked) {
state = TaskState.PARTIAL;
img.setUrl(IMG_PARTIAL);
} else {
state = TaskState.CHECKED;
img.setUrl(IMG_CHECKED);
}
else {
state = TaskState.UNCHECKED;
img.setUrl(IMG_UNCHECKED);
}

if (! old.equals(state)) {
TaskTreeItem parent = (TaskTreeItem) getParentItem();
if (parent != null)
parent.update();
}
}

static class TaskComposite extends Composite {
protected Widget widget = null;
protected Image image = null;


public TaskComposite(Widget widget, Image image) {
super();
this.widget = widget;
this.image = image;

final HorizontalPanel hp = new HorizontalPanel();
hp.add(image);
hp.add(widget);
initWidget(hp);
}


}


}
test case:


Viewport v = new Viewport();
ContentPanel p = new ContentPanel(new FitLayout());
p.setScrollMode(Scroll.AUTO);
// -----------------------------------------
Tree tree = new Tree();

for (int i = 0; i < 4; i++) {
TaskTreeItem t1 = new TaskTreeItem("noeud numero " + i, null);
tree.addItem(t1);
for (int j = 0; j < 2; j++) {
TaskTreeItem t2 = new TaskTreeItem("noeud numero " + j, null);
t1.addItem(t2);
for (int k = 0; k < 3; k++) {
TaskTreeItem t3 = new TaskTreeItem("noeud numero " + k, TaskState.UNCHECKED );
t2.addItem(t3);
}
}
}

p.add(tree);
// --------------
v.add(p);
RootPanel.get().add(v);



9383

9384

9385

9386



@+David