package tool.content.actions;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
import tool.ToolNavigatorActivator;
import tool.content.SupplierPlanFolder;
import tool.model.SupplierPlan;
import tool.model.ToolPlan;
public class SupplierPlanDialog extends FilteredItemsSelectionDialog {
private List<String> availablePlans;
private List<String> excludedPlans;
private SupplierPlanFolder existingPlans;
/**
* Create the dialog.
* @param parentShell
*/
public SupplierPlanDialog(Shell parentShell, boolean multi) {
super(parentShell, multi);
setShellStyle(SWT.RESIZE);
setImage(ToolNavigatorActivator.getImage("icons/notuds.gif"));
setListLabelProvider(new SupplierPlanLabelProvider());
setDetailsLabelProvider(new SupplierPlanLabelProvider());
setInitialPattern("?");
}
/**
/**
* Create contents of the button bar.
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
true);
createButton(parent, IDialogConstants.CANCEL_ID,
IDialogConstants.CANCEL_LABEL, false);
}
@Override
protected Control createExtendedContentArea(Composite parent) {
return null;
}
@Override
protected ItemsFilter createFilter() {
return new ItemsFilter(){
@Override
public boolean isConsistentItem(Object item) {
return true;
}
@Override
public boolean matchItem(Object item) {
return matches(item.toString());
}
};
}
@Override
protected void fillContentProvider(AbstractContentProvider provider,
ItemsFilter filter, IProgressMonitor monitor) throws CoreException {
availablePlans = new ArrayList<String>(ToolPlan.forteLibrariesSet);
try {
ResourcesPlugin.getWorkspace().getRoot().accept(new IResourceProxyVisitor() {
public boolean visit(IResourceProxy proxy) throws CoreException {
if (proxy.getType() == IResource.FILE) {
IFile file = (IFile) proxy.requestResource();
if (file.getLocation().getFileExtension() != null && file.getLocation().getFileExtension().matches("pex")) {
String planName = file.getName().substring(0, file.getName().lastIndexOf('.'));
if (!existingPlans.contains(planName))
availablePlans.add(planName);
}
}
return true;
}
}, IResource.DEPTH_INFINITE);
} catch (CoreException e) {
ErrorDialog.openError(getShell(),
"Error finding available plans", e.getMessage(), Status.OK_STATUS );
}
monitor.beginTask("Searching", availablePlans.size()); //$NON-NLS-1$
for (String planName : availablePlans) {
provider.add(planName, filter);
monitor.worked(1);
}
monitor.done();
}
@Override
public String getElementName(Object item) {
return item.toString();
}
@Override
protected Comparator getItemsComparator() {
return new Comparator() {
public int compare(Object arg0, Object arg1) {
return arg0.toString().compareTo(arg1.toString());
}
};
}
@Override
protected IStatus validateItem(Object item) {
return Status.OK_STATUS;
}
private static final String DIALOG_SETTINGS = "SupplierPlanDialogSettings";
@Override
protected IDialogSettings getDialogSettings() {
IDialogSettings settings = ToolNavigatorActivator.getDefault().getDialogSettings()
.getSection(DIALOG_SETTINGS);
if (settings == null) {
settings = ToolNavigatorActivator.getDefault().getDialogSettings()
.addNewSection(DIALOG_SETTINGS);
}
return settings;
}
public void setExistingPlans(SupplierPlanFolder folder) {
this.existingPlans = folder;
}
}