// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.debug.ui;

import java.util.List;

import org.chromium.debug.core.model.Messages;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * A dialog where users select which Chromium tab to attach to.
 */
class ChromiumTabSelectionDialog extends Dialog {

  private final List<String> urls;

  private Table table;

  private int selectedLine = -1;

  ChromiumTabSelectionDialog(Shell shell, List<String> urls) {
    super(shell);
    this.urls = urls;
  }

  @Override
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setText(Messages.ChromiumTabSelectionDialog_DialogTitle);
  }

  @Override
  public int open() {
    return super.open();
  }

  @Override
  protected Control createDialogArea(Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    Label label = new Label(composite, SWT.NONE);
    label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    label.setText(Messages.ChromiumTabSelectionDialog_TableTitle);

    table = new Table(composite, SWT.VIRTUAL | SWT.BORDER | SWT.SINGLE);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setItemCount(10);
    final TableColumn urlColumn = new TableColumn(table, SWT.NONE);
    urlColumn.setText(Messages.ChromiumTabSelectionDialog_UrlColumnName);
    urlColumn.setWidth(400);

    synchronized (this) {
      table.addListener(SWT.SetData, new Listener() {
        public void handleEvent(Event event) {
          if (table.isDisposed()) {
            return;
          }
          processData(event);
        }

        private void processData(Event event) {
          TableItem item = (TableItem) event.item;
          int index = table.indexOf(item);
          if (index < urls.size()) {
            item.setText(urls.get(index));
            GridData data = new GridData();
            data.grabExcessHorizontalSpace = true;
            item.setData(data);
            if (index == 0) {
              table.select(0);
            }
          }
        }
      });
      table.setItemCount(urls.size());
      table.clearAll();
    }
    return composite;
  }

  @Override
  protected void okPressed() {
    selectedLine = table.getSelectionIndex();
    super.okPressed();
  }

  int getSelectedLine() {
    return selectedLine;
  }
}

@@ -335,16 +335,111 @@
 wt.SWT;%0A
+import org.eclipse.swt.events.SelectionEvent;%0Aimport org.eclipse.swt.events.SelectionListener;%0A
 import o
@@ -2067,36 +2067,8 @@
 );%0A%0A
-    synchronized (this) %7B%0A  
     
@@ -2121,18 +2121,16 @@
 %7B%0A      
-  
 public v
@@ -2164,26 +2164,24 @@
 ) %7B%0A        
- 
- 
 if (table.is
@@ -2204,18 +2204,16 @@
         
-  
 return;%0A
@@ -2212,38 +2212,34 @@
 return;%0A        
- 
- 
 %7D%0A
-  
         processD
@@ -2250,18 +2250,16 @@
 event);%0A
-  
       %7D%0A
@@ -2265,18 +2265,16 @@
 %0A%0A      
-  
 private 
@@ -2301,26 +2301,24 @@
 nt event) %7B%0A
-  
         Tabl
@@ -2362,18 +2362,16 @@
         
-  
 int inde
@@ -2395,34 +2395,32 @@
 (item);%0A        
-  
 if (index %3C urls
@@ -2432,34 +2432,32 @@
 ()) %7B%0A          
- 
- 
 item.setText(url
@@ -2467,26 +2467,24 @@
 et(index));%0A
-  
           Gr
@@ -2523,18 +2523,16 @@
         
- 
- 
 data.gra
@@ -2564,34 +2564,32 @@
 true;%0A          
-  
 item.setData(dat
@@ -2592,18 +2592,16 @@
 (data);%0A
-  
         
@@ -2628,26 +2628,24 @@
             
-  
 table.select
@@ -2649,24 +2649,32 @@
 ect(0);%0A    
+      %7D%0A
         %7D%0A  
@@ -2677,34 +2677,233 @@
 %7D%0A      
+%7D%0A
     %7D
+);
 %0A    
+table.addSelectionListener(new SelectionListener() %7B%0A      public void widgetDefaultSelected(SelectionEvent e) %7B%0A        okPressed();%0A      %7D%0A      public void widgetSelected(SelectionEvent e) %7B%0A  
     %7D%0A
-  
     %7D);%0A
@@ -2902,24 +2902,27 @@
     %7D);%0A    
+%0A  
   table.setI
@@ -2944,26 +2944,24 @@
 ize());%0A    
-  
 table.clearA
@@ -2966,21 +2966,16 @@
 rAll();%0A
-    %7D
 %0A    ret

Add double-click to tab selector