package waelti.statistics.actions;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.Window;
import waelti.statistics.queries.AbstractQuery;
import waelti.statistics.views.OutputView;
import waelti.statistics.views.QueryInputDialog;
import ch.elexis.actions.BackgroundJob;
import ch.elexis.actions.BackgroundJob.BackgroundJobListener;
/**
* This action is responsible for the whole procedure of creating a new query:
* getting all information needed of the user, starting the query in the
* background and updating the view in the end.
*
* @author michael waelti
*/
public class NewQueryAction extends Action implements BackgroundJobListener {
private OutputView view;
private AbstractQuery configuredQuery;
/** constructor */
public NewQueryAction() {
super();
this.setText("new query");
this.setToolTipText("starts a new query");
}
/** Standard constructor which should be used normally. */
public NewQueryAction(OutputView view) {
this();
this.view = view;
}
@Override
public void run() {
this.setEnabled(false); // as long as the job is in progress, no new
// query can be started.
this.getInput();
if (this.configuredQuery == null) { // user aborted
this.setEnabled(true);
} else { // user did not cancel
this.configuredQuery.addListener(this);
this.view.createOrUpdateResultTable(this.configuredQuery);
this.view.setHeader(this.configuredQuery.getHeader());
}
}
/**
* Opens a dialog which asks the user to define a new query which then is
* set by the opened dialog to this.configuredQuery. If null, the user
* aborted.
*/
private void getInput() {
QueryInputDialog dialog = new QueryInputDialog(this.view.getSite()
.getShell(), this);
if (dialog.open() != Window.OK) {
this.configuredQuery = null; // user aborted
}
}
/** This action is enabled as soon as the last job finished. */
public void jobFinished(BackgroundJob j) {
this.setEnabled(true);
}
public void setConfiguredQuery(AbstractQuery configuredQuery) {
this.configuredQuery = configuredQuery;
}
}