/*******************************************************************************
 * Copyright (C) 2007, Google Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package com.google.eclipse.mechanic.plugin.ui;

import java.util.Collection;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.dialogs.ListDialog;

import com.google.eclipse.mechanic.Task;

/**
 * Allows the user to pick a single Task from a list of Tasks.
 * 
 * TODO(smckay): We should pretty this up to use a view similar to the
 * MechanicDialog.
 * 
 * @author smckay@google.com (Steve McKay)
 */
public final class TaskSelectionDialog extends ListDialog {

  public TaskSelectionDialog(Shell parent, String title,
      Collection<Task> items) {
    super(parent);
    setInput(items);
    setContentProvider(ArrayContentProvider.getInstance());
    setLabelProvider(new DefaultLabelProvider());
    setWidthInChars(80);
    setTitle(title);
  }

  @Override
  protected int getTableStyle() {
    return SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION;
  }


  private TableColumn newTableColumn(Table table, String name, int width) {
    TableColumn tc = new TableColumn(table, SWT.NONE, 0);
    tc.setResizable(true);
    tc.setText(name);
    tc.setWidth(width);
    return tc;
  }

  @Override
  protected Control createDialogArea(Composite container) {
    Control area = super.createDialogArea(container);
    TableViewer tableViewer = getTableViewer();
    Table table = tableViewer.getTable();
    table.setHeaderVisible(true);
    newTableColumn(table, "Description", 200);
    newTableColumn(table, "ID", 200);
    newTableColumn(table, "Name", 200);
    tableViewer.refresh();
    return area;
  }

  /**
   * Serializes Tasks for use in user facing dialogs.
   */
  private static class DefaultLabelProvider extends LabelProvider implements ITableLabelProvider {

    @Override
    public boolean isLabelProperty(Object element, String property) {
      return false;
    }

    public Image getColumnImage(Object element, int columnIndex) {
      return null;
    }

    public String getColumnText(Object element, int columnIndex) {
      Task item = ((Task) element);
      switch(columnIndex) {
      case 0:
        return item.getTitle();
      case 1:
        return item.getId();
      case 2:
        return item.getDescription();
      default:
        return "" + columnIndex;
      }
    }
  }
}

@@ -500,28 +500,40 @@
 ort 
-java.util.Collection
+com.google.eclipse.mechanic.Task
 ;%0A%0Ai
@@ -578,32 +578,145 @@
 ontentProvider;%0A
+import org.eclipse.jface.viewers.CellLabelProvider;%0Aimport org.eclipse.jface.viewers.ColumnViewerToolTipSupport;%0A
 import org.eclip
@@ -843,24 +843,110 @@
 ableViewer;%0A
+import org.eclipse.jface.viewers.ViewerCell;%0Aimport org.eclipse.jface.window.ToolTip;%0A
 import org.e
@@ -1256,40 +1256,28 @@
 ort 
-com.google.eclipse.mechanic.Task
+java.util.Collection
 ;%0A%0A/
@@ -1440,55 +1440,8 @@
 og.%0A
- * %0A * @author smckay@google.com (Steve McKay)%0A
  */%0A
@@ -2339,24 +2339,167 @@
 getTable();%0A
+    tableViewer.setLabelProvider(new DefaultCellLabelProvider());%0A    ColumnViewerToolTipSupport.enableFor(tableViewer, ToolTip.NO_RECREATE); %0A
     table.se
@@ -2668,24 +2668,24 @@
 .refresh();%0A
-
     return a
@@ -2694,16 +2694,800 @@
 a;%0A  %7D%0A%0A
+  private static class DefaultCellLabelProvider extends CellLabelProvider %7B%0A    @Override%0A    public void update(ViewerCell cell) %7B%0A      Object element = cell.getElement();%0A      int columnIndex = cell.getColumnIndex();%0A      cell.setText(getColumnText((Task) element, columnIndex));%0A    %7D%0A%0A    @Override%0A    public String getToolTipText(Object element) %7B%0A      Task item = (Task) element;%0A      return item.getTitle() + %22%5Cn%22 + item.getDescription() + %22%5Cn%5CnID:%22 + item.getId();%0A    %7D%0A%0A    public String getColumnText(Task item, int columnIndex) %7B%0A      switch(columnIndex) %7B%0A      case 0:%0A        return item.getTitle();%0A      case 1:%0A        return item.getId();%0A      case 2:%0A        return item.getDescription();%0A      default:%0A        return %22%22 + columnIndex;%0A      %7D%0A    %7D%0A  %7D%0A%0A
   /**%0A  

Add hover text to the task selection dialog.