org.geneweaver.query.ui.PathChoice Maven / Gradle / Ivy
package org.geneweaver.query.ui;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
/**
* A composite containing a label and a button for choosing a file.
* @author gerrim
*
* Add bit SWT.
*
*/
public class PathChoice extends Composite {
public static enum Type {
File, Directory;
}
private Consumer consumer;
private Label flabel;
private Type type;
private String fileLabelText="Input file";
public PathChoice(Composite parent, int style, Consumer consumer, Type type) {
super(parent, style);
setLayout(new GridLayout(3, false));
setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
this.consumer = consumer;
this.type = type;
}
/**
* Call to create the widgets.
*/
public void createControls() {
Label label = new Label(this, SWT.LEFT);
label.setText(fileLabelText);
label.setToolTipText("Choose the input file which has columns such as rsId, SNP etc. and MUST be in CSV/TSV style format.");
final Button ci = new Button(this, SWT.PUSH);
ci.setImage(ImageDescriptor.createFromURL(getClass().getClassLoader().getResource("folder.png")).createImage());
this.flabel = new Label(this, SWT.LEFT);
flabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
ci.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String path = null;
if (type == Type.File) {
FileDialog fdialog = new FileDialog(getShell(), SWT.APPLICATION_MODAL|SWT.OPEN);
path = fdialog.open();
} else if (type == Type.Directory) {
DirectoryDialog ddialog = new DirectoryDialog(getShell(), SWT.APPLICATION_MODAL|SWT.OPEN);
path = ddialog.open();
}
Path pnew = Paths.get(path);
if (type == Type.File) {
setFileText(pnew.getFileName().toString());
} else {
setFileText(pnew.toString());
}
consumer.accept(pnew);
}
});
}
public void setFileText(String text) {
flabel.setText(text);
layout(new Control[] {flabel});
getParent().layout(new Control[] {this});
}
/**
* @return the fileLabelText
*/
public String getFileLabelText() {
return fileLabelText;
}
/**
* @param fileLabelText the fileLabelText to set
*/
public void setFileLabelText(String fileLabelText) {
this.fileLabelText = fileLabelText;
}
}