package ru.runa.specific.alf;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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.Text;
import ru.runa.specific.Activator;
import tk.eclipse.plugin.ftl.FormatMapping;
import tk.eclipse.plugin.ftl.FreemarkerUtil.TagParser;
public final class AlfrescoConfigurationWizardPage extends WizardPage {
private Composite clientArea;
private Map<String, String> properties;
protected Combo loginModeCombo;
private final Map<String, String> variableNames;
protected final List<ParamDef> paramDefs;
protected AlfrescoConfigurationWizardPage(Map<String, String> variableNames, List<ParamDef> paramDefs, String headerText) {
super("config", headerText, Activator.getImageDescriptor("/icons/logo.gif"));
this.variableNames = variableNames;
this.paramDefs = paramDefs;
}
public void init(Map<String, String> properties) {
this.properties = properties;
}
protected Text addTextField(final ParamDef paramDef) {
Label label = new Label(clientArea, SWT.NONE);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.minimumWidth = 200;
label.setLayoutData(gridData);
label.setText(getLabelText(paramDef));
Text textInput = new Text(clientArea, SWT.BORDER);
textInput.setData(paramDef.name);
textInput.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
setErrorMessage(null);
setMessage(paramDef.help != null ? paramDef.help : "");
}
});
GridData typeComboData = new GridData(GridData.FILL_HORIZONTAL);
typeComboData.minimumWidth = 200;
textInput.setLayoutData(typeComboData);
String savedValue = properties.get(paramDef.name);
textInput.setText(savedValue != null ? savedValue : "");
return textInput;
}
protected void addSeparator(String header) {
Label label = new Label(clientArea, SWT.SEPARATOR | SWT.HORIZONTAL);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 2;
label.setLayoutData(gridData);
label.setText(header);
}
private Map<String, List<String>> comboItems = new HashMap<String, List<String>>();
private List<String> getVariableNames(String formatFilter) {
if (formatFilter == null) {
return new ArrayList<String>(variableNames.keySet());
}
List<String> result = new ArrayList<String>();
for (String varName : variableNames.keySet()) {
String varType = variableNames.get(varName);
FormatMapping mapping = TagParser.getFormatMapping(varType);
if (formatFilter.equals(mapping.getName())) {
result.add(varName);
}
}
return result;
}
protected Combo addComboField(final ParamDef paramDef) {
List<String> variableNames = new ArrayList<String>();
/* TODO
for (String string : aParam.additionalItems) {
variableNames.add(string);
}
*/
variableNames.addAll(getVariableNames(paramDef.formatFilter));
Collections.sort(variableNames);
if (paramDef.optional) {
variableNames.add(0, "");
}
comboItems.put(paramDef.name, variableNames);
Label label = new Label(clientArea, SWT.NONE);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.minimumWidth = 200;
label.setLayoutData(gridData);
label.setText(getLabelText(paramDef));
Combo combo = new Combo(clientArea, SWT.BORDER | SWT.READ_ONLY);
combo.setData(paramDef.name);
combo.setVisibleItemCount(10);
for (String item : variableNames) {
combo.add(item);
}
combo.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
setErrorMessage(null);
setMessage(paramDef.help != null ? paramDef.help : "");
}
});
GridData typeComboData = new GridData(GridData.FILL_HORIZONTAL);
typeComboData.minimumWidth = 200;
combo.setLayoutData(typeComboData);
String selectedValue = properties.get(paramDef.name);
if (selectedValue != null) {
combo.setText(selectedValue);
}
return combo;
}
private String getLabelText(ParamDef aParam) {
String labelText = aParam.label;
if (!aParam.optional) {
labelText += " *";
}
return labelText;
}
protected void changeStateForInputControls(List<String> propertyNames, boolean enabled) {
for (Control control : clientArea.getChildren()) {
if (control.getData() != null) {
String propertyName = (String) control.getData();
if (propertyNames.contains(propertyName)) {
control.setEnabled(enabled);
}
}
}
}
protected Map<String, String> readUserInput() {
Map<String, String> properties = new HashMap<String, String>();
Control[] controls = clientArea.getChildren();
for (Control control : controls) {
if (control.getData() != null) {
String propertyName = (String) control.getData();
String propertyValue;
if (control instanceof Text) {
propertyValue = ((Text) control).getText();
} else { // Combo
propertyValue = ((Combo) control).getText();
}
properties.put(propertyName, propertyValue);
}
}
return properties;
}
public void createControl(Composite parent) {
clientArea = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
clientArea.setLayout(layout);
setControl(clientArea);
if (properties == null) {
setErrorMessage("�� ������� ��������� ������� ������������ ��-�� ������");
properties = new HashMap<String, String>();
}
boolean outputStarted = false;
for (ParamDef aParam : paramDefs) {
if (!aParam.isInput && !outputStarted) {
addSeparator("���������");
outputStarted = true;
}
if (aParam.useVariable) {
addComboField(aParam);
} else {
addTextField(aParam);
}
}
}
public String getConfiguration() {
Map<String, String> properties = readUserInput();
StringBuffer buffer = new StringBuffer();
buffer.append("<config>\n");
buffer.append(" <input>\n");
boolean outputStarted = false;
for (ParamDef aParam : paramDefs) {
if (!aParam.isInput && !outputStarted) {
buffer.append(" </input>\n");
buffer.append(" <output>\n");
outputStarted = true;
}
buffer.append(" <param name=\"").append(aParam.name).append("\"");
String value = properties.get(aParam.name);
if (aParam.useVariable) {
buffer.append(" variable=\"").append(value).append("\"");
} else {
buffer.append(" value=\"").append(value).append("\"");
}
if (aParam.optional) {
buffer.append(" optional=\"true\"");
}
buffer.append(" />\n");
}
if (outputStarted) {
buffer.append(" </output>\n");
} else {
buffer.append(" </input>\n");
}
buffer.append("</config>");
return buffer.toString();
}
}