All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geneweaver.query.ui.InputMapPage Maven / Gradle / Ivy

package org.geneweaver.query.ui;

import java.nio.file.Path;
import java.util.Collection;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
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.Composite;
import org.geneweaver.query.dao.GraphAttribute;
import org.geneweaver.query.dao.GraphNode;
import org.geneweaver.query.dao.MappedParameter;
import org.geneweaver.query.dao.QueryBuildRequest;
import org.geneweaver.query.service.InputParser;
import org.geneweaver.query.service.QueryBuildException;
import org.geneweaver.query.service.QueryBuildValidator;

class InputMapPage extends WizardPage {
	
	private QueryBuildRequest request;
	private TableViewer viewer;

	protected InputMapPage(QueryBuildRequest request, String pageName) {
		super(pageName);
		this.request = request;
		
		setPageComplete(true);
		setTitle("Map to Graph");
		setImageDescriptor(ImageDescriptor.createFromURL(getClass().getClassLoader().getResource("GW2-logo-blue.png")));
		setMessage("Map the referenced columns in the input file to properties in the graph.");
	}
	
	@Override
	public void createControl(Composite parent) {
		
		Composite main = new Composite(parent, SWT.NONE);
	    main.setLayout(new GridLayout(1, false));
	    
	    this.viewer = new TableViewer(main, SWT.H_SCROLL|SWT.V_SCROLL|SWT.FULL_SELECTION | SWT.BORDER);
	    viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
	    viewer.getTable().setHeaderVisible(true);
	    viewer.getTable().setLinesVisible(true);
	    viewer.setColumnProperties(new String[] {"inputName", "node", "attribute"});
	    createColums(viewer);
	    
	    viewer.setContentProvider(new IStructuredContentProvider() {
			@Override
			public Object[] getElements(Object inputElement) {
				Collection params = (Collection)inputElement;
				return params.toArray(new MappedParameter[params.size()]);
			}
		});
	    
	    setControl(main);
	    
	}

	private void createColums(TableViewer viewer) {
	    TableViewerColumn pname = new TableViewerColumn(viewer, SWT.NONE, 0);
	    pname.getColumn().setWidth(150);
	    pname.getColumn().setText("Input Name");
	    pname.setLabelProvider(new ColumnLabelProvider() {
	        @Override
	        public String getText(Object element) {
	          return ((MappedParameter)element).getInputName();
	        }
	    });
	    
	    TableViewerColumn gnode = new TableViewerColumn(viewer, SWT.NONE, 1);
	    gnode.getColumn().setText("Graph Node");
	    gnode.getColumn().setWidth(150);
	    gnode.setLabelProvider(new ColumnLabelProvider() {
	        @Override
	        public String getText(Object element) {
	            GraphNode go = ((MappedParameter)element).getNode();
	            if (go==null) return "...";
	            return go.name();
	        }
	    });
	    gnode.setEditingSupport(new GraphNodeEditingSupport(this, viewer));

	    TableViewerColumn gparam = new TableViewerColumn(viewer, SWT.NONE, 2);
	    gparam.getColumn().setText("Graph Attribute");
	    gparam.getColumn().setWidth(150);
	    gparam.setLabelProvider(new ColumnLabelProvider() {
	        @Override
	        public String getText(Object element) {
	            GraphAttribute gp = ((MappedParameter)element).getAttribute();
	            if (gp==null) return "...";
	            return gp.getAttributeName();
	        }
	    });
	    gparam.setEditingSupport(new GraphAttributeEditingSupport(this, viewer));
		
	}
	
	@Override
	public boolean canFlipToNextPage() {
		// If new data set it in table
		if (viewer.getInput()==null || isNewFile()) {
			InputParser parser = new InputParser(request);
			parser.updateMappedParameters();
		    viewer.setInput(request.getMappedParameters());
		}
		
		Collection ps  = (Collection)viewer.getInput();
		Collection nps = request.getMappedParameters();
		if (!ps.equals(nps)) {
			viewer.setInput(nps);
		}
		boolean valid = validate(false);
		viewer.refresh();
		if (!valid) return false;
		return super.canFlipToNextPage();
	}
	
	private Path previousFile;

	private boolean isNewFile() {
		try {
			if (previousFile!=request.getInputFile()) return true;
			if (!previousFile.equals(request.getInputFile())) return true;
			return false;
		} finally {
			previousFile = request.getInputFile();
		}
	}
	

	boolean validate(boolean updateComplete) {
		try {
			QueryBuildValidator validator = new QueryBuildValidator();
			validator.validate(request);
			setErrorMessage(null);
			if (updateComplete) setPageComplete(true);
			return true;
		} catch (QueryBuildException ne) {
			setErrorMessage(ne.getMessage());
			if (updateComplete) setPageComplete(false);
			return false;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy