package factOrFiction.preferences;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
public class DownloadPreferencePage
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
private Group proxyParent;
private BooleanFieldEditor enableEditor;
private StringFieldEditor hostEditor;
private StringFieldEditor portEditor;
public DownloadPreferencePage() {
super(GRID);
setPreferenceStore(factOrFiction.Activator.getDefault().getPreferenceStore());
setDescription("Card images will be downloaded from the WWW.");
}
@Override
public void createFieldEditors() {
addField(
new StringFieldEditor(PreferenceConstants.P_IMAGE_REPOSITORY_URL,
"URL image repository at Magiccards.info:", getFieldEditorParent()));
createHttpProxy(getFieldEditorParent(), 3);
}
protected void createHttpProxy(final Composite composite, int columnSpan) {
proxyParent = new Group(composite, SWT.NONE);
proxyParent.setText("Proxy settings:");
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = columnSpan;
proxyParent.setLayoutData(gd);
GridLayout layout = new GridLayout();
layout.marginLeft = 5;
layout.marginRight = 5;
layout.marginTop = 5;
layout.marginBottom = 5;
layout.numColumns = 2;
proxyParent.setLayout(layout);
enableEditor = new BooleanFieldEditor(PreferenceConstants.P_HTTP_PROXY_ENABLE, "Enable HTTP proxy connection", proxyParent);
addField( enableEditor );
hostEditor = new StringFieldEditor(PreferenceConstants.P_HTTP_PROXY_HOST, "HTTP proxy host address", proxyParent);
addField( hostEditor );
portEditor = new StringFieldEditor(PreferenceConstants.P_HTTP_PROXY_PORT, "HTTP proxy host port", proxyParent) {
@Override
protected boolean doCheckState() {
try {
String portValue = getStringValue();
int num = 80;
if (portValue != null && portValue.trim().length() > 0)
num = Integer.valueOf(portValue).intValue();
if (0 <= num && num <= 0xFFFF)
// port is valid
return true;
// port is invalid
} catch (NumberFormatException nfe) {
}
return false;
}
};
portEditor.setErrorMessage("Port number must be in the range of 0-65535.");
addField( portEditor );
}
protected Label createLabel(Composite parent, String text) {
Font font= parent.getFont();
Label label = new Label(parent, SWT.LEFT | SWT.WRAP);
label.setText(text);
GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
data.horizontalSpan = 3;
label.setLayoutData(data);
label.setFont(font);
return label;
}
protected Control createLinkControl(Composite composite, String text, String toolTip, SelectionAdapter selectionAdapter) {
Link link= new Link(composite, SWT.WRAP);
link.setText(text);
link.addSelectionListener(selectionAdapter);
link.setToolTipText(toolTip);
GridData gridData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
gridData.horizontalSpan = 3;
link.setLayoutData(gridData);
return link;
}
// This might be a hack to get at the field editor control
// after the page property change listener has been set.
// This way we overide it here (It's boolean and always valid, so who cares)
@Override
protected void initialize() {
super.initialize();
IPreferenceStore prefs = getPreferenceStore();
boolean proxyEnable = prefs.getBoolean(PreferenceConstants.P_HTTP_PROXY_ENABLE);
hostEditor.setEnabled(proxyEnable, proxyParent );
portEditor.setEnabled(proxyEnable, proxyParent );
enableEditor.setPropertyChangeListener( new IPropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
if( event.getProperty().equals(FieldEditor.VALUE) ) {
boolean enable = ((Boolean)event.getNewValue()).booleanValue();
hostEditor.setEnabled(enable, proxyParent );
portEditor.setEnabled(enable, proxyParent );
}
}
});
}
/**
/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
*/
public void init(IWorkbench workbench) {
}
}