package org.csstudio.platform.libs.yamcs.ui.prefs;

import org.csstudio.platform.libs.yamcs.YamcsPlugin;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */
public class YamcsPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

    @SuppressWarnings("unused")
    private static final long serialVersionUID = 1L;

    private StringFieldEditor yamcsHost;
    private IntegerFieldEditor yamcsPort;
    private StringFieldEditor yamcsInstance;
    private StringFieldEditor mdbNamespace;

    public YamcsPreferencePage() {
        super(FieldEditorPreferencePage.GRID);
        setPreferenceStore(YamcsPlugin.getDefault().getPreferenceStore());
        setDescription("Set connection properties to a Yamcs server (as used for yamcs:// datasources)");
    }

    /**
     * Creates the field editors. Field editors are abstractions of the common
     * GUI blocks needed to manipulate various types of preferences. Each field
     * editor knows how to save and restore itself.
     */
    @Override
    public void createFieldEditors() {
        yamcsHost = new StringFieldEditor("yamcs_host", "Host", getFieldEditorParent());
        addField(yamcsHost);
        yamcsPort = new IntegerFieldEditor("yamcs_port", "Port", getFieldEditorParent());
        addField(yamcsPort);
        yamcsInstance = new StringFieldEditor("yamcs_instance", "Instance", getFieldEditorParent());
        addField(yamcsInstance);

        addField(new SpacerFieldEditor(getFieldEditorParent()));
        mdbNamespace = new StringFieldEditor("mdb_namespace", "MDB Namespace", getFieldEditorParent());
        addField(mdbNamespace);
    }

    @Override
    public void init(IWorkbench workbench) {
    }

    @Override
    protected void checkState() {
        super.checkState();
        if (!isValid()) {
            return;
        }
        String yamcsHostText = yamcsHost.getStringValue();
        String yamcsInstanceText = yamcsInstance.getStringValue();
        if (!(yamcsHostText.trim().matches("[a-zA-Z0-9_]+")) || !(yamcsInstanceText.trim().matches("[a-zA-Z0-9_]+"))) {
            setErrorMessage("Host and instance must contain alphanumerics and _ with no white space");
            setValid(false);
        } else {
            setErrorMessage(null);
            setValid(true);
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent event) {
        super.propertyChange(event);
        if (event.getProperty().equals(FieldEditor.VALUE)) {
            if (event.getSource() == yamcsHost
                    || event.getSource() == yamcsPort
                    || event.getSource() == yamcsInstance
                    || event.getSource() == mdbNamespace) {
                checkState();
            }
        }
    }

    @Override
    public boolean performOk() {
        boolean ret = super.performOk();
        // TODO replace yservice or do something smarter than that
        return ret;
    }
}

@@ -528,19 +528,16 @@
 d to the
-%0A *
  Prefere
@@ -551,16 +551,19 @@
 alog. By
+%0A *
  subclas
@@ -609,19 +609,16 @@
 amp%3E, we
-%0A *
  can use
@@ -652,16 +652,19 @@
 to JFace
+%0A *
  that al
@@ -687,19 +687,16 @@
 e a page
-%0A *
  that is
@@ -829,19 +829,16 @@
 d in the
-%0A *
  prefere
@@ -851,16 +851,19 @@
 ore that
+%0A *
  belongs
@@ -899,19 +899,16 @@
 hat way,
-%0A *
  prefere
@@ -940,24 +940,27 @@
 ctly via the
+%0A *
  preference 
@@ -1691,23 +1691,16 @@
 e common
-%0A     *
  GUI blo
@@ -1712,16 +1712,23 @@
 eeded to
+%0A     *
  manipul
@@ -1771,23 +1771,16 @@
 ch field
-%0A     *
  editor 
@@ -1808,16 +1808,23 @@
  restore
+%0A     *
  itself.
@@ -2832,32 +2832,35 @@
 matches(%22%5Ba-zA-Z
+%5C%5C-
 0-9_%5D+%22)) %7C%7C !(y
@@ -2945,77 +2945,28 @@
 ge(%22
-Host and instance must contain alphanumerics and _ with no white spac
+Not a valid host nam
 e%22);

Fix issue where ip's were not accepted