package org.jbpm.ui.sync;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
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 org.jbpm.ui.SharedImages;
import org.jbpm.ui.orgfunctions.ExecutorsImporter;
import org.jbpm.ui.resource.Messages;
public abstract class SyncSettingsWizardPage extends WizardPage {
private Composite clientArea;
private final ExecutorsImporter importer;
protected Combo loginModeCombo;
protected SyncSettingsWizardPage(ExecutorsImporter importer, String headerText) {
super("config", headerText, SharedImages.getImageDescriptor("/icons/connection_wizard.png"));
this.importer = importer;
}
protected void updateLoginModeTextInputs() {
changeStateForInputControls(SyncResources.LOGIN_PROPERTY_NAMES, loginModeCombo.getSelectionIndex() == 0);
}
protected Text addTextField(String propertyName, String labelText, final String hintText) {
return addTextField(propertyName, labelText, "", hintText);
}
protected Text addTextField(String propertyName, String labelText, String defaultValue, final String hintText) {
Label label = new Label(clientArea, SWT.NONE);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.minimumWidth = 200;
label.setLayoutData(gridData);
label.setText(labelText);
int textStyle = SWT.BORDER;
if (SyncResources.PASSWORD.equals(propertyName)) {
textStyle |= SWT.PASSWORD;
}
Text textInput = new Text(clientArea, textStyle);
textInput.setData(propertyName);
textInput.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
setErrorMessage(null);
setMessage(hintText);
}
});
GridData typeComboData = new GridData(GridData.FILL_HORIZONTAL);
typeComboData.minimumWidth = 200;
textInput.setLayoutData(typeComboData);
String savedValue = importer.getResources().getProperty(propertyName, null);
textInput.setText(savedValue != null ? savedValue : defaultValue);
return textInput;
}
private Map<String, String[]> comboItems = new HashMap<String, String[]>();
protected Combo addComboField(String propertyName, String labelText, String[] items) {
comboItems.put(propertyName, items);
Label label = new Label(clientArea, SWT.NONE);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.minimumWidth = 200;
label.setLayoutData(gridData);
label.setText(labelText);
Combo combo = new Combo(clientArea, SWT.BORDER | SWT.READ_ONLY);
combo.setData(propertyName);
for (String item : items) {
combo.add(Messages.getString(item));
}
combo.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
setErrorMessage(null);
setMessage("");
}
});
GridData typeComboData = new GridData(GridData.FILL_HORIZONTAL);
typeComboData.minimumWidth = 200;
combo.setLayoutData(typeComboData);
String propertyKey = importer.getResources().getProperty(propertyName, items[0]);
combo.setText(Messages.getString(propertyKey));
return combo;
}
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 Properties readUserInput() {
Properties properties = new Properties();
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 = comboItems.get(propertyName)[((Combo) control).getSelectionIndex()];
}
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);
}
protected boolean testConnection() {
try {
saveSettinds();
importer.synchronize();
return true;
} catch (Exception e) {
setErrorMessage(Messages.getString("error.Synchronize") + ": " + e.getMessage());
return false;
}
}
protected boolean synchronize() {
try {
importer.getResources().reload(readUserInput());
importer.connect();
return true;
} catch (Exception e) {
setErrorMessage(Messages.getString("error.ConnectionFailed") + ": " + e.getMessage());
return false;
}
}
public void saveSettinds() throws IOException {
importer.getResources().reload(readUserInput());
importer.saveProperties();
}
}