package com.redhat.ceylon.eclipse.code.refactor;


import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.redhat.ceylon.compiler.typechecker.model.Declaration;
import com.redhat.ceylon.eclipse.code.outline.CeylonLabelProvider;

public class DeleteInputPage extends UserInputWizardPage {
    public DeleteInputPage(String name) {
        super(name);
    }

    public void createControl(Composite parent) {
        int count = getDeleteRefactoring().getCount();
        
        Composite result = new Composite(parent, SWT.NONE);
        setControl(result);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        result.setLayout(layout);
        
        Label title = new Label(result, SWT.LEFT);  
        Declaration dec = getDeleteRefactoring().getDeclaration();
        title.setText("Delete '" + dec.getName() + 
                "' which is referenced in " + count + 
                " places.");
        GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
        gd2.horizontalSpan=2;
        new Label(result, SWT.SEPARATOR|SWT.HORIZONTAL).setLayoutData(gd2);
        
        Composite composite = new Composite(result, SWT.NONE);
        GridData cgd = new GridData(GridData.FILL_HORIZONTAL|GridData.FILL_VERTICAL);
        cgd.grabExcessHorizontalSpace = true;
        cgd.grabExcessVerticalSpace = true;
        composite.setLayoutData(cgd);
        GridLayout tableLayout = new GridLayout(3, true);
        tableLayout.marginWidth=0;
        composite.setLayout(tableLayout);
        
        TableViewer table = new TableViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
        table.setLabelProvider(new CeylonLabelProvider(true));
        table.setContentProvider(ArrayContentProvider.getInstance());
        table.setInput(getDeleteRefactoring().getReferences());
//        Table table = new Table(composite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
//        for (CeylonElement e: getDeleteRefactoring().getReferences()) {
//            TableItem item = new TableItem(table, SWT.NONE);
//            item.setText(e.getLabel().getString() + 
//                    " - " + e.getPackageLabel() + 
//                    " - " + e.getFile().getFullPath() + 
//                    ":" + e.getLocation());
//            item.setImage(CeylonPlugin.getInstance().getImageRegistry().get(e.getImageKey()));
//        }
        GridData tgd = new GridData(GridData.FILL_HORIZONTAL|GridData.FILL_VERTICAL);
        tgd.horizontalSpan=3;
        tgd.verticalSpan=4;
        tgd.grabExcessHorizontalSpace = true;
        tgd.heightHint = 100;
        tgd.widthHint = 250;
        table.getTable().setLayoutData(tgd);
        
        Declaration declaration = getDeleteRefactoring().getDeclaration();
        Declaration refinedDeclaration = getDeleteRefactoring().getRefinedDeclaration();
        if (declaration.isActual() && refinedDeclaration!=null) {
            setMessage("'" + declaration.getName() + "' refines '" + 
                    refinedDeclaration.getName() + "' declared by '" +
                    ((Declaration) refinedDeclaration.getContainer()).getName() + "'", 
                    WARNING);
        }
        else if (count>0) {
            setMessage("There are " + count + " references to '" + 
                    declaration.getName() + "'", WARNING); 
        }
    }
    
    private DeleteRefactoring getDeleteRefactoring() {
        return (DeleteRefactoring) getRefactoring();
    }

}

@@ -933,17 +933,17 @@
 lumns = 
-2
+1
 ;%0A      
@@ -3002,32 +3002,111 @@
 (tgd);%0A        %0A
+//        new Label(result, SWT.SEPARATOR%7CSWT.HORIZONTAL).setLayoutData(gd2);%0A%0A
         Declarat
@@ -3245,32 +3245,191 @@
 dDeclaration();%0A
+        if (count%3E0) %7B%0A            addWarning(result, %22There are %22 + count + %22 references to '%22 + %0A                    declaration.getName() + %22'%22);%0A        %7D%0A
         if (decl
@@ -3494,51 +3494,44 @@
     
-setMessage(%22'%22 + declaration.getName() + %22'
+addWarning(result, %22This declaration
  ref
@@ -3701,38 +3701,8 @@
  %22'%22
-, %0A                    WARNING
 );%0A 
@@ -3718,169 +3718,401 @@
     
+%7D%0A%0A
     
-else if (count%3E0) %7B%0A            setMessage(%22There are %22 + c
+private void addWarning(Composite result, String text) %7B%0A        Composite warn = new Composite(result, SWT.NONE);%0A        GridLay
 ou
-n
 t 
-+ %22 references to '%22 + %0A                    declaration.getName() + %22'%22, WARNING); %0A        %7D
+l = new GridLayout();%0A        l.numColumns = 2;%0A        warn.setLayout(l);%0A        Label icon = new Label(warn, SWT.LEFT);%0A        icon.setImage(CeylonLabelProvider.WARNING);%0A        Label label = new Label(warn, SWT.LEFT);%0A        label.setText(text);
 %0A   

improve warnings