/*
 * Created on 2004-10-08
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.widgets;

import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IMacroEntry;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
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.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianPrjNature;

/**
 * A dialog with a list of #define'd symbols
 * 
 * @author mikolaj
 */
public class DefinedSymbolsDialog extends Dialog {
    public DefinedSymbolsDialog(Shell parent, IProject prj)
    {
        super(parent);
        fProject=prj;
    }
    
	protected Control createDialogArea(Composite parent)
	{
	  	GridData data;	    
	  	Composite panel=new Composite(parent, SWT.NONE);
	  	
		GridLayout layout= new GridLayout();
		layout.numColumns=1;
		panel.setLayout(layout);
		
		Label l=new Label(panel, SWT.NONE);
		l.setText("Detected symbols:");

		fList=new List(panel, SWT.BORDER);
		data=new GridData();
		data.widthHint= convertWidthInCharsToPixels(30);
		data.heightHint= convertHeightInCharsToPixels(15);
		fList.setLayoutData(data);
		populateList();
	    
	    return panel;
	}
	
	protected void createButtonsForButtonBar(Composite parent)
	{
	    createButton(parent, IDialogConstants.CLIENT_ID+1, "Rescan", false);
	    createButton(parent, IDialogConstants.CLOSE_ID, "Close", true);
	}
	
	protected void buttonPressed(int buttonId)
	{
	    if (buttonId==IDialogConstants.CLOSE_ID)
	    {
	        close();
	        return;
	    }
	    
	    if (buttonId==IDialogConstants.CLIENT_ID+1)
	    {
	        SymbianPrjNature.applyPaths(fProject);
	        if (getShell()!=null)
	            populateList();
	    }
	}
	
	protected void populateList(IProject prj)
		throws CoreException
	{
	    fList.removeAll();
		ICProject cProject=CoreModel.getDefault().create(prj);
		IPathEntry[] entries=cProject.getRawPathEntries();
	    for (int i=0; i<entries.length; i++)
	    {
	        if (entries[i].getEntryKind()==IPathEntry.CDT_MACRO)
	        {
	            IMacroEntry macro=(IMacroEntry)entries[i];
	            fList.add(macro.getMacroName());
	        }
	    }
	}
	
	protected void populateList()
	{
		try
		{
		    populateList(fProject);
		}
		catch (CoreException e)
		{
		    ErrorDialog.openError(getShell(), SymbdevPlugin.MESSAGE_TITLE,
		            "Error while filling the list", e.getStatus());
		}	    
	}
	
	protected void configureShell(Shell shell) {
	  	super.configureShell(shell);
 		shell.setText(SymbdevPlugin.MESSAGE_TITLE);
	}  	
	
	List fList;
	IProject fProject;
	Composite fPanel;
}
