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

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import pl.zalewski.mikolaj.symbdev.core.mmpmodel.ILanguage;
import pl.zalewski.mikolaj.symbdev.ui.SymbdevImages;

/**
 * The main pane of the "Choose Language(s)" dialog
 * 
 * @author mikolaj
 */
public class LanguagesDlgMainPane extends Composite {

	public LanguagesDlgMainPane(Composite parent, boolean allowMany) {
		super(parent, SWT.NONE);
		initialize();
		GridLayout layout=new GridLayout(2, false);
		setLayout(layout);
		Label l=new Label(this, SWT.NONE);
		l.setText("Languages codes:");
		
		fCodes=new Text(this, SWT.BORDER|(allowMany?0:SWT.READ_ONLY));
		fCodes.addModifyListener(new ModifyListener(){
            public void modifyText(ModifyEvent e) {
                textModified();
            }
		    });
		GridData gd=new GridData(GridData.FILL_HORIZONTAL);
		fCodes.setLayoutData(gd);
		
		fTable=new Table(this, SWT.BORDER|(allowMany?SWT.MULTI:0));
		for (int i=0; i<ILanguage.stdLanguages.length; i++)
		{
		    TableItem it=new TableItem(fTable, SWT.NONE);
		    it.setText(0, ILanguage.stdLanguages[i].name+" ("+
		            ILanguage.stdLanguages[i].getStringId()+")");
		    it.setImage(0, SymbdevImages.getLanguageIcon(""+ILanguage.stdLanguages[i].getStringId()));
		}
		gd=new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan=2;
		gd.heightHint=200;
		gd.widthHint=400;
		fTable.setLayoutData(gd);
		fTable.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent e) { event(); }
            public void widgetSelected(SelectionEvent e) { event(); }
            void event() { tableSelect(); }
        });
	}
	private void initialize() {
		setSize(new org.eclipse.swt.graphics.Point(400,300));
	}
	
	private void textModified()
	{
	    String text=fCodes.getText();
	    String[] codes=text.split("\\s+");
	    int[] tmp=new int[codes.length];
	    int numItems=0;
	    
	    for (int i=0; i<codes.length; i++)
	    {
	        int k=ILanguage.StdLanguage.getStdLanguagePos(codes[i]);
	        if (k==-1)
	            continue;
	        tmp[numItems++]=k;
	    }
	    
	    int[] items=new int[numItems];
	    for (int i=0; i<numItems; i++)
	        items[i]=tmp[i];
	    fTable.setSelection(items);
	}
	
	private static int getToken(String text, int fromIndex)
	{
		int pos1=text.indexOf(' ', fromIndex), pos2=text.indexOf('\t', fromIndex);
		if (pos1==-1)
		    pos1=text.length();
		if (pos2==-1)
		    pos2=text.length();
		int pos=Math.min(pos1, pos2);
		while (pos<text.length())
		{
		    int ch=text.charAt(pos);
		    if (ch==' ' || ch=='\t') pos++;
		    	else break;
		}
		return pos;
	}
	
	private void tableSelect()
	{
	    boolean[] intext=new boolean[fTable.getItemCount()];
	    String text=fCodes.getText();
	    StringBuffer newText=new StringBuffer();
	    int lastPos=0;
	    while (lastPos<text.length())
	    {
	        int pos=getToken(text, lastPos);
	        String pretoken=text.substring(lastPos, pos);
	        String token=pretoken.trim();
	        lastPos=pos;
	        int k=ILanguage.StdLanguage.getStdLanguagePos(token);
	        if (k==-1 || fTable.isSelected(k))
	        {
	            newText.append(pretoken);
	            if (fTable.isSelected(k))
	                intext[k]=true;
	            continue;
	        }
	    }
	    
	    for (int i=0; i<intext.length; i++)
	    {
	        if (!intext[i] && fTable.isSelected(i))
	        {
	            if (newText.length()>0)
	                newText.append(' ');
	            newText.append(ILanguage.stdLanguages[i].getStringId());
	        }
	    }
	    fCodes.setText(newText.toString());
	}
	
	public Table fTable;
	public Text fCodes;
}
