package org.sf.mustru.ui;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
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.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
/**
* Create the third page of the wizard
*/
public class ThirdPage extends WizardPage
{
Table table;
ArrayList<String> dirs = new ArrayList<String>();
/**
* Constructor for the ThirdPage
*/
public ThirdPage()
{ super("Third Page");
setTitle("Crawl Configuration Part 3/4");
setDescription("Add a list of directories to EXCLUDE from the index.");
setPageComplete(true);
}
/**
* Create the third page with the list of directories to exclude
*/
public void createControl(Composite parent)
{
final Composite composite = new Composite(parent, SWT.NULL);
GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 2;
composite.setLayout(gridLayout);
//*-- build the table consisting of the list of directories to exclude from the scan
table = new Table ( composite, SWT.BORDER | SWT.SINGLE );
table.setLinesVisible(true); table.setHeaderVisible(true);
GridData tableGd = new GridData(); tableGd.horizontalSpan= 2; tableGd.grabExcessHorizontalSpace = true;
tableGd.horizontalAlignment = GridData.FILL;
table.setLayoutData(tableGd);
TableColumn tcol1 = new TableColumn( table, SWT.LEFT, 0);
tcol1.setText("Directories:"); tcol1.setWidth(500);
//*-- populate the table with the entries from the crawl configuration
String excDirList = ( (CrawlConfigWizard) getWizard()).crawlConfig.getExcDirs().trim();
String[] excDirs = excDirList.split(";");
for (int i = 0; i < excDirs.length; i++)
if (!excDirs[i].equals("")) dirs.add(excDirs[i]);
fillUpTable();
table.setItemCount(5);
//*-- add the two buttons to add and delete directory names
final Button addButton = new Button(composite, SWT.PUSH); addButton.setText("Add");
GridData addButtonGd = new GridData(); addButtonGd.horizontalAlignment = GridData.BEGINNING;
addButtonGd.grabExcessHorizontalSpace = false;
addButton.setLayoutData(addButtonGd);
addButton.addListener( SWT.Selection, new Listener()
{
public void handleEvent(Event e)
{
DirectoryDialog dd = new DirectoryDialog(composite.getShell());
dd.setMessage("Please select a directory to exclude from the scan");
String dir = dd.open();
if (dir != null) { dirs.add(dir); fillUpTable(); }
}
});
final Button deleteButton = new Button(composite, SWT.PUSH); deleteButton.setText("Delete");
GridData delButtonGd = new GridData(); delButtonGd.horizontalAlignment = GridData.BEGINNING;
delButtonGd.grabExcessHorizontalSpace = false;
deleteButton.setLayoutData(delButtonGd);
deleteButton.addListener( SWT.Selection, new Listener()
{
public void handleEvent(Event e)
{
int i = table.getSelectionIndex();
if (i != -1) { dirs.remove(i); fillUpTable(); }
}
});
setControl(composite);
}
//*-- populate the table with entries from the dirs list
private void fillUpTable()
{
//*-- create a hash with the directories to remove dups from the dirs list
HashSet<String> h = new HashSet<String>();
for (int i = 0; i < dirs.size(); i++) h.add((String) dirs.get(i));
dirs = new ArrayList<String>();
Iterator it = h.iterator();
while (it != null && it.hasNext()) dirs.add( (String) it.next() );
//*-- re-create the table and set the excDirs string
StringBuffer excDirs = new StringBuffer();
table.removeAll();
for (int i = 0; i < dirs.size(); i++)
{ TableItem ti = new TableItem(table, SWT.LEFT, i);
String dir = (String) dirs.get(i);
ti.setText(dir); excDirs.append(dir); excDirs.append( ";");
}
( (CrawlConfigWizard) getWizard()).crawlConfig.setExcDirs(excDirs.toString());
}
}