Java tutorial
/*- * Copyright 2016 Diamond Light Source Ltd. * * This file is part of GDA. * * GDA is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License version 3 as published by the Free * Software Foundation. * * GDA is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along * with GDA. If not, see <http://www.gnu.org/licenses/>. */ package uk.ac.gda.dls.client.views; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.springframework.util.StringUtils; import gda.jython.ICommandRunner; public class RunCommandComposite extends Composite { public RunCommandComposite(Composite parent, int style, final ICommandRunner commandRunner, String label, final String command, final String commandObserver, final String jobTitle, String tooltip) { super(parent, style); final Display display = parent.getDisplay(); GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this); GridDataFactory.fillDefaults().applyTo(this); final Button btn = new Button(this, SWT.PUSH); btn.setText(label); btn.setToolTipText(tooltip); btn.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { super.widgetSelected(e); btn.setEnabled(false); Job job = new Job(jobTitle) { @Override protected IStatus run(IProgressMonitor monitor) { if (StringUtils.hasLength(commandObserver)) { commandRunner.runCommand(command, commandObserver); } else { commandRunner.runCommand(command); } display.asyncExec(new Runnable() { @Override public void run() { btn.setEnabled(true); } }); return Status.OK_STATUS; } }; job.setUser(true); job.schedule(); } }); } }