package ee.ut.cs.rum.workspaces.internal.ui.workspace;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

import ee.ut.cs.rum.database.domain.Workspace;
import ee.ut.cs.rum.workspaces.internal.ui.task.TasksTableViewer;
import ee.ut.cs.rum.workspaces.internal.ui.task.newtask.NewTaskDetails;

public class WorkspaceDetailsTabContents extends Composite {
	private static final long serialVersionUID = 1649148279320216160L;
	
	private Workspace workspace;
	private TasksTableViewer tasksTableViewer;

	WorkspaceDetailsTabContents(WorkspaceTabFolder workspaceTabFolder, Composite workspaceContainer, Workspace workspace) {
		super(workspaceTabFolder, SWT.BORDER);
		
		this.workspace=workspace;

		this.setLayout(new GridLayout());
		this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Label l = new Label(this, SWT.BORDER);
		if (workspace.getName()!=null) {
			l.setText(workspace.getName());
		}
		l = new Label(this, SWT.BORDER);
		if (workspace.getDescription()!=null) {
			l.setText(workspace.getDescription());
		}
		
		tasksTableViewer = new TasksTableViewer(this);
		
		Button addTaskDialogueButton = new Button(this, SWT.PUSH);
		addTaskDialogueButton.setText("Add task");
		addTaskDialogueButton.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true, true));
		
		addTaskDialogueButton.addListener(SWT.Selection, new Listener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void handleEvent(Event arg0) {
				CTabItem cTabItem = null;
				
				cTabItem = new CTabItem (workspaceTabFolder, SWT.CLOSE);
				cTabItem.setText ("New task");
				cTabItem.setControl(new NewTaskDetails(workspaceTabFolder));
				workspaceTabFolder.setSelection(cTabItem);
				
				//NewTaskDialog newTaskDialog = new NewTaskDialog(Display.getCurrent().getActiveShell(), WorkspaceDetailsTabContents.this);
				//newTaskDialog.open();
			}
		});
	}
	
	public Workspace getWorkspace() {
		return workspace;
	}
	
	public TasksTableViewer getTasksTableViewer() {
		return tasksTableViewer;
	}

}

@@ -731,19 +731,18 @@
 6160L;%0D%0A
-%09
 %0D%0A
+
 %09private
@@ -975,20 +975,18 @@
 RDER);%0D%0A
-%09%09
 %0D%0A
+
 %09%09this.w
@@ -1362,20 +1362,18 @@
 ;%0D%0A%09%09%7D%0D%0A
-%09%09
 %0D%0A
+
 %09%09tasksT
@@ -1414,20 +1414,18 @@
 this);%0D%0A
-%09%09
 %0D%0A
+
 %09%09Button
@@ -1612,20 +1612,18 @@
 rue));%0D%0A
-%09%09
 %0D%0A
+
 %09%09addTas
@@ -1825,22 +1825,248 @@
  null;%0D%0A
+%0D%0A
 %09%09%09%09
-%0D%0A
+for (CTabItem c : workspaceTabFolder.getItems()) %7B%0D%0A%09%09%09%09%09if (c.getControl().getClass() == NewTaskDetails.class) %7B%0D%0A%09%09%09%09%09%09cTabItem = c;%0D%0A%09%09%09%09%09%09workspaceTabFolder.setSelection(c);%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%7D%0D%0A%0D%0A%09%09%09%09if (cTabItem == null) %7B%0D%0A%09
 %09%09%09%09cTab
@@ -2115,32 +2115,33 @@
 WT.CLOSE);%0D%0A%09%09%09%09
+%09
 cTabItem.setText
@@ -2152,24 +2152,25 @@
 ew task%22);%0D%0A
+%09
 %09%09%09%09cTabItem
@@ -2223,24 +2223,25 @@
 der));%0D%0A%09%09%09%09
+%09
 workspaceTab
@@ -2280,166 +2280,9 @@
 %09%09%09%09
-%0D%0A%09%09%09%09//NewTaskDialog newTaskDialog = new NewTaskDialog(Display.getCurrent().getActiveShell(), WorkspaceDetailsTabContents.this);%0D%0A%09%09%09%09//newTaskDialog.open();
+%7D
 %0D%0A%09%09
@@ -2292,27 +2292,26 @@
 %0A%09%09%7D);%0D%0A%09%7D%0D%0A
-%09
 %0D%0A
+
 %09public Work
@@ -2359,17 +2359,16 @@
 e;%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09publi

Check if new task tab is open before adding a new tab