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

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.ILanguage;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.IMMPModel;
import pl.zalewski.mikolaj.symbdev.ui.SymbdevImages;
import pl.zalewski.mikolaj.symbdev.utils.SystemBrowserHyperlinkListener;
import pl.zalewski.mikolaj.symbdev.widgets.ChooseLanguageDialog;

/**
 * Creates a section for languages configuration
 * 
 * @author mikolaj
 */
public class LanguagesSection extends MMPSectionPart {

    /**
     * Creates a section for languages configuartion
     * @param parent The parent control
     * @param toolkit The form toolkit
     */
    public LanguagesSection(Composite parent, FormToolkit toolkit, IMMPModel model) {
        super(parent, toolkit, "Languages", "Select languages:", 2);
        fModel=model;
        fList=new Table(fContainer, SWT.NONE);
        toolkit.adapt(fList, true, true);
        TableWrapData td=new TableWrapData();
        td.heightHint=150;
        td.align=TableWrapData.FILL;
        td.grabHorizontal=true;
        fList.setLayoutData(td);
        updateList();
        fListener=new Listener() {
            public void handleEvent(Event e) {
                updateList();
            }			
        };
        fModel.addChangeListener(IMMPModel.CHANGE_LANG, fListener);
        fList.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent e) { event(); }
            public void widgetSelected(SelectionEvent e) { event(); }
            void event() { enableRemove(); }
        });
        
        fPanel=toolkit.createComposite(fContainer, SWT.NONE);
        GridLayout gl=new GridLayout();
        gl.numColumns=1;
        fPanel.setLayout(gl);
        
        fAdd=toolkit.createButton(fPanel, "Add...", SWT.PUSH);
        fAdd.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        fAdd.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                addLang();
            }			
        });
        fRemove=toolkit.createButton(fPanel, "Remove", SWT.PUSH);
        fRemove.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        fRemove.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                removeCurrentLang();
            }			
        });
        enableRemove();

        FormText ft=toolkit.createFormText(fContainer, true);
        ft.setText("<form><p>The build system will run the resource compilation for each " +
        		"selected language with a LANGUAGE_xx symbol defined, where xx is the language " +
        		"code you enetered.</p><li><a href=\"http://www.newlc.com/article.php3?id_article=443\">" +
        		"Read a tutorial</a></li></form>", true, false);
        ft.addHyperlinkListener(new SystemBrowserHyperlinkListener());
        td=new TableWrapData();
        td.align=TableWrapData.FILL;
        td.colspan=2;
        ft.setLayoutData(td);
        contentCreated();
    }
    
    public void dispose()
    {
        fModel.removeChangeListener(IMMPModel.CHANGE_LANG, fListener);
        super.dispose();
    }
    
    void updateList()
    {
        if (fList.isDisposed())
            return;
        
        fList.setRedraw(false);
        int pos=fList.getSelectionIndex();
        int top=fList.getTopIndex();
        fList.removeAll();
        ILanguage[] langs=fModel.getLanguages();
        for (int i=0; i<langs.length; i++)
        {
            TableItem ti=new TableItem(fList, SWT.NONE);
            String code=langs[i].getCode();
            String str=ILanguage.StdLanguage.getStdLanguageName(langs[i].getCode());
            if (str!=null)
              ti.setText(str+" ("+code+")");
              else ti.setText(code);
            ti.setImage(SymbdevImages.getLanguageIcon(code));
        }
        if (pos>=fList.getItemCount())
            pos=fList.getItemCount()-1;
        fList.setSelection(pos);
        fList.setTopIndex(top);
        fList.setRedraw(true);
    }
    
    void enableRemove()
    {
        fRemove.setEnabled(fList.getSelectionIndex()>=0);
    }
    
    void removeCurrentLang()
    {
        try {
            fModel.removeLanguage(fList.getSelectionIndex());
        } catch (CoreException e) {
            SymbdevPlugin.showError(e, null, getSection().getShell());
        }
        enableRemove();
    }
    
    void addLang()
    {
        ChooseLanguageDialog dlg=new ChooseLanguageDialog(getSection().getShell(), true);
        if (dlg.open()==Window.OK)
        {
            String[] result=dlg.getResult();
            try {
                fModel.addLanguages(result);
            } catch (CoreException e) {
                SymbdevPlugin.showError(e, "Couldn't add languages", getSection().getShell());
            }
        }
    }
    
    Table fList;
    Composite fPanel;
    Button fAdd, fRemove;
    Listener fListener;
    IMMPModel fModel;
}
