package scrum.client.tasks;

import ilarkesto.gwt.client.AIntegerViewEditWidget;
import ilarkesto.gwt.client.ARichtextViewEditWidget;
import ilarkesto.gwt.client.ATextViewEditWidget;
import scrum.client.ScrumGwtApplication;
import scrum.client.common.ABlockWidget;
import scrum.client.common.AExtensibleBlockWidget;
import scrum.client.common.BlockWidgetFactory;
import scrum.client.common.FieldsWidget;
import scrum.client.dnd.ClipboardSupport;
import scrum.client.dnd.TrashSupport;
import scrum.client.img.Img;
import scrum.client.sprint.SprintBacklogWidget;
import scrum.client.sprint.Task;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class TaskBlock extends AExtensibleBlockWidget<Task> implements TrashSupport, ClipboardSupport {

	private Task task;

	private Label owner;
	private FieldsWidget fields;

	@Override
	protected Task getObject() {
		return task;
	}

	@Override
	protected void setObject(Task object) {
		this.task = object;
	}

	@Override
	protected void onCollapsedInitialization() {}

	@Override
	protected void onCollapsedUpdate() {
		setBlockTitle(task.getLabel());
		setIcon(task.isDone() ? Img.bundle.done16() : Img.bundle.task16());
		createToolbar();
	}

	@Override
	protected void onExtendedInitialization() {
		fields = new FieldsWidget();
		fields.setAutoUpdateWidget(SprintBacklogWidget.get());

		fields.add("Label", new ATextViewEditWidget() {

			@Override
			protected void onViewerUpdate() {
				setViewerText(task.getLabel());
			}

			@Override
			protected void onEditorUpdate() {
				setViewerText(task.getLabel());
			}

			@Override
			protected void onEditorSubmit() {
				task.setLabel(getEditorText());
			}

		});

		fields.add("Burned Work", new AIntegerViewEditWidget() {

			@Override
			protected void onIntegerViewerUpdate() {
				setViewerValue(task.getBurnedWork(), "hours");
			}

			@Override
			protected void onEditorUpdate() {
				setEditorValue(task.getBurnedWork());
			}

			@Override
			protected void onEditorSubmit() {
				Integer value = getEditorValue(0);
				if (value == null) value = 0;
				int previous = task.getBurnedWork();
				int diff = value - previous;
				task.setBurnedWork(value);
				task.adjustRemainingWork(diff);
			}

			@Override
			protected void onMinusClicked() {
				task.decrementBurnedWork();
				task.adjustRemainingWork(-1);
			}

			@Override
			protected void onPlusClicked() {
				task.incrementBurnedWork();
				task.adjustRemainingWork(1);
			}
		});

		fields.add("Remaining Work", new AIntegerViewEditWidget() {

			@Override
			protected void onIntegerViewerUpdate() {
				setViewerValue(task.getRemainingWork(), "hours");
			}

			@Override
			protected void onEditorUpdate() {
				setEditorValue(task.getRemainingWork());
			}

			@Override
			protected void onEditorSubmit() {
				task.setRemainingWork(getEditorValue(1));
			}

			@Override
			protected void onMinusClicked() {
				task.decrementRemainingWork();
			}

			@Override
			protected void onPlusClicked() {
				task.incrementRemainingWork();
			}

		});

		fields.add("Note", new ARichtextViewEditWidget() {

			@Override
			protected void onViewerUpdate() {
				setViewerText(task.getNotice());
			}

			@Override
			protected void onEditorUpdate() {
				setEditorText(task.getNotice());
			}

			@Override
			protected void onEditorSubmit() {
				task.setNotice(getEditorText());
			}

		});

		owner = new Label();
		fields.add("Owner", owner);

	}

	@Override
	protected void onExtendedUpdate() {
		setBlockTitle(task.getLabel());
		setIcon(task.isDone() ? Img.bundle.done16() : Img.bundle.task16());
		fields.update();
		owner.setText(task.getOwner() == null ? "No owner specified." : task.getOwner().getName());
		setContent(fields);
		createToolbar();
	}

	public Task getTask() {
		return task;
	}

	protected void createToolbar() {
		if (!task.isDone()) {
			addToolbarButton("Own").addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					task.setOwner(ScrumGwtApplication.get().getUser());
					TaskOverviewWidget.get().update();
				}

			});
		}

		if (isTrashable()) {
			addToolbarButton("Delete").addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					trash();
					TaskOverviewWidget.get().update();
				}

			});
		}

		if (!task.isDone()) {
			addToolbarButton("Done").addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					task.setDone();
					TaskOverviewWidget.get().update();
				}

			});
		}
	}

	public Image getClipboardIcon() {
		return Img.bundle.task16().createImage();
	}

	public String getClipboardLabel() {
		return task.getLabel();
	}

	public ABlockWidget getClipboardPayload() {
		return this;
	}

	public boolean isTrashable() {
		return true;
	}

	public void trash() {
		task.getRequirement().deleteTask(task);
		TaskOverviewWidget.get().update();
	}

	public static BlockWidgetFactory<Task> FACTORY = new BlockWidgetFactory<Task>() {

		public TaskBlock createBlock() {
			return new TaskBlock();
		}
	};
}

@@ -688,52 +688,8 @@
 ge;%0A
-import com.google.gwt.user.client.ui.Label;%0A
 impo
@@ -860,30 +860,8 @@
 k;%0A%0A
-%09private Label owner;%0A
 %09pri
@@ -3444,65 +3444,11 @@
 );%0A%0A
-%09%09owner = new Label();%0A%09%09fields.add(%22Owner%22, owner);%0A%0A
 %09%7D%0A
+
 %0A%09@O
@@ -3619,102 +3619,8 @@
 ();%0A
-%09%09owner.setText(task.getOwner() == null ? %22No owner specified.%22 : task.getOwner().getName());%0A
 %09%09se
@@ -3738,16 +3738,16 @@
 bar() %7B%0A
-
 %09%09if (!t
@@ -3750,32 +3750,86 @@
  (!task.isDone()
+ && !task.isOwner(ScrumGwtApplication.get().getUser())
 ) %7B%0A%09%09%09addToolba

View erstellen