package waelti.statistics.views;
import java.util.Hashtable;
import java.util.Set;
import org.eclipse.jface.dialogs.Dialog;
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.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import waelti.statistics.actions.NewQueryAction;
import waelti.statistics.queries.AbstractQuery;
import waelti.statistics.queries.Consultations;
import ch.elexis.util.SWTHelper;
public class QueryInputDialog extends Dialog {
/** List of all available queries. TODO: reflection of query package */
private Hashtable<String, AbstractQuery> queryTable;
private OptionPanel options;
private Combo combo;
private NewQueryAction action;
public QueryInputDialog(Shell parentShell) {
super(parentShell);
queryTable = new Hashtable<String, AbstractQuery>();
// TODO fill with reflection
Consultations cons = new Consultations();
queryTable.put(cons.getTitle(), cons);
}
public QueryInputDialog(Shell shell, NewQueryAction newQueryAction) {
this(shell);
this.action = newQueryAction;
}
@Override
public Control createDialogArea(Composite parent) {
GridData gridData = new GridData(GridData.FILL_VERTICAL
| GridData.FILL_HORIZONTAL);
gridData.grabExcessHorizontalSpace = true;
Label header = new Label(parent, SWT.WRAP);
header.setText("Wählen Sie die Query aus: ");
header.setLayoutData(gridData);
initCombo(parent);
initOptions(parent);
Control control = super.createDialogArea(parent);
return control;
}
private void initOptions(Composite parent) {
this.options = new OptionPanel(parent, this.action);
this.options.setLayoutData(SWTHelper.getFillGridData(1, true, 1, true));
}
private void initCombo(Composite parent) {
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.grabExcessHorizontalSpace = true;
combo = new Combo(parent, SWT.READ_ONLY);
combo.setLayoutData(gridData);
// populate
Set<String> titleSet = queryTable.keySet();
for (String title : titleSet) {
combo.add(title);
}
combo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String text = combo.getText();
AbstractQuery selectedQuery = queryTable.get(text);
options.updateContent(selectedQuery);
}
});
}
}