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

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

package org.geneweaver.query.ui;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.geneweaver.query.dao.GraphTraverse;
import org.geneweaver.query.dao.QueryBuildRequest;
import org.geneweaver.query.dao.Route;

class RouteChoicePage extends WizardPage {
	
	private QueryBuildRequest request;
	private Map buttonMap;
	private Map labelMap;

	protected RouteChoicePage(QueryBuildRequest request, String pageName) {
		super(pageName);
		this.request = request;
		
		setPageComplete(false);
		setTitle("Links Between Nodes");
		setImageDescriptor(ImageDescriptor.createFromURL(getClass().getClassLoader().getResource("GW2-logo-blue.png")));
		setMessage("Choose the routes over the graph. Tip: You may need to look at the diagram on the first page again.");
		buttonMap = new HashMap<>();
		labelMap = new HashMap<>();
	}

	@Override
	public void createControl(Composite parent) {
		
		Composite main = new Composite(parent, SWT.NONE);
	    main.setLayout(new GridLayout(2, false));
	    
	    SelectionAdapter listener = new SelectionAdapter() {
	    	public void widgetSelected(SelectionEvent e) {
	    		Button button = (Button)e.getSource();
	    		Route route = (Route)button.getData();
	    		boolean selected = button.getSelection();
	    		if (selected) {
	    			request.add(route);
	    		} else {
	    			request.remove(route);
	    		}
	    		labelMap.get(route).setEnabled(selected);
	    		labelMap.get(route).setVisible(selected);
	    		setPageComplete(checkComplete());
	    	}
	    };
	    
	    Label label = new Label(main, SWT.NONE);
	    label.setText("Graph Traverse");
	    
	    Combo searchType = new Combo(main, SWT.READ_ONLY);
	    searchType.setItems(GraphTraverse.getLabels());
	    searchType.select(2);
	    searchType.addSelectionListener(new SelectionAdapter() {
	    	public void widgetSelected(SelectionEvent e) {
	    		GraphTraverse traverse = GraphTraverse.values()[searchType.getSelectionIndex()];
	    		boolean vfrom = true;
	    		boolean vto = true;
	    		if (traverse==GraphTraverse.GENE_TO_GENE) {
	    			vfrom = false;
	    			vto = false;
	    		} else if (traverse==GraphTraverse.VARIANT_TO_GENE) {
	    			vto = false;
	    		} else if (traverse==GraphTraverse.GENE_TO_VARIANT) {
	    			vfrom = false;
	    		}
	    		
	    		request.setTraverse(traverse);
	    		buttonMap.get(Route.VEG).setVisible(vfrom);
	    		buttonMap.get(Route.VTG).setVisible(vfrom);
	    		labelMap.get(Route.VEG).setVisible(vfrom && buttonMap.get(Route.VEG).getSelection());
	    		labelMap.get(Route.VTG).setVisible(vfrom && buttonMap.get(Route.VTG).getSelection());
	    		
	    		buttonMap.get(Route.GEV).setVisible(vto);
	    		buttonMap.get(Route.GTV).setVisible(vto);
	    		labelMap.get(Route.GEV).setVisible(vto && buttonMap.get(Route.GEV).getSelection());
	    		labelMap.get(Route.GTV).setVisible(vto && buttonMap.get(Route.GTV).getSelection());
	    		
	    		setPageComplete(checkComplete());
	    	}
		});
	    
	    Label hsep = new Label(main, SWT.HORIZONTAL);
	    GridData gdata = new GridData(SWT.FILL, SWT.NONE, true, false);
	    gdata.horizontalSpan = 2;
	    hsep.setLayoutData(gdata);
	    
	    createButtonAndLabel(main, "Variant/SNP to Gene using eQTL",			Route.VEG,	listener);
	    createButtonAndLabel(main, "Variant/SNP to Gene using Transcipt",		Route.VTG,	listener);
	    
	    hsep = new Label(main, SWT.HORIZONTAL);
	    hsep.setLayoutData(gdata);
	    createButtonAndLabel(main, "Gene to end with Variant/SNP using eQTL",		Route.GEV,	listener);
	    createButtonAndLabel(main, "Gene to end with variant/SNP using Transcipt",	Route.GTV,	listener);
	    
	    setControl(main);
		
	}

	protected boolean checkComplete() {
		if (request.getRoutes()==null || request.getRoutes().isEmpty()) return false;
		GraphTraverse traverse = request.getTraverse();
		Set routes = request.getRoutes();
		if (traverse==GraphTraverse.VARIANT_TO_VARIANT) {
			// At least one route all the way must be there.
			if (!routes.contains(Route.VEG) && !routes.contains(Route.VTG)) return false;
			if (!routes.contains(Route.GEV) && !routes.contains(Route.GTV)) return false;
		}
		
		return true;
	}

	private void createButtonAndLabel(Composite main, String text, Route route, SelectionListener listener) {
	    Button button = new Button(main, SWT.CHECK);
	    button.setText(text);
	    button.setData(route);
	    button.addSelectionListener(listener);
	    button.setSelection(false);
	    buttonMap.put(route, button);
	    
	    Label label = new Label(main, SWT.NONE);
	    label.setImage(getImageDescriptor(route.getImageName()).createImage());
	    label.setLayoutData(new GridData(SWT.FILL, SWT.RIGHT, true, false));
	    label.setEnabled(false);
	    label.setVisible(false);
	    labelMap.put(route, label);
	}

	private ImageDescriptor getImageDescriptor(String imageName) {
		return ImageDescriptor.createFromURL(getClass().getClassLoader().getResource(imageName));
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy