Package dk.contix.eclipse.hudson.preference

Source Code of dk.contix.eclipse.hudson.preference.HudsonPreferencesPage$HudsonUrlField

package dk.contix.eclipse.hudson.preference;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.StringButtonFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import dk.contix.eclipse.hudson.Activator;
import dk.contix.eclipse.hudson.HudsonClient;
import dk.contix.eclipse.hudson.digger.DiggerDialog;

public class HudsonPreferencesPage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

  private IntegerFieldEditor interval;

  private StringFieldEditor password;

  private StringFieldEditor username;

  private BooleanFieldEditor authEnabled;

  public HudsonPreferencesPage() {
    super(FieldEditorPreferencePage.GRID);
  }

  protected void createFieldEditors() {
    addField(new HudsonUrlField(getFieldEditorParent()));

    addPasswordFields();
   
    final BooleanFieldEditor enabled = new BooleanFieldEditor(Activator.PREF_AUTO_UPDATE, "Update periodically?", getFieldEditorParent()) {
      protected Button getChangeControl(Composite parent) {
        final Button c = super.getChangeControl(parent);

        c.addSelectionListener(new SelectionListener() {
          public void widgetDefaultSelected(SelectionEvent arg0) {
          }

          public void widgetSelected(SelectionEvent e) {
            interval.setEnabled(c.getSelection(), getFieldEditorParent());
          }

        });
        return c;
      }
    };
    interval = new IntegerFieldEditor(Activator.PREF_UPDATE_INTERVAL, "Update interval (seconds)", getFieldEditorParent());

    addField(enabled);
    addField(interval);

    addField(new BooleanFieldEditor(Activator.PREF_POPUP_ON_ERROR, "Popup window when state changes to error?", getFieldEditorParent()));
    addField(new BooleanFieldEditor(Activator.PREF_POPUP_ON_CONNECTION_ERROR, "Popup error when connection to Hudson fails?", getFieldEditorParent()));

    interval.setEnabled(getPreferenceStore().getBoolean(Activator.PREF_AUTO_UPDATE), getFieldEditorParent());
  }

  private void addPasswordFields() {
   
    //adds fields for authentication
    authEnabled = new BooleanFieldEditor(Activator.PREF_USE_AUTH, "Use authentication", getFieldEditorParent()) {
      protected Button getChangeControl(Composite parent) {
        final Button c = super.getChangeControl(parent);

        c.addSelectionListener(new SelectionListener() {
          public void widgetDefaultSelected(SelectionEvent arg0) {
          }

          public void widgetSelected(SelectionEvent e) {
            username.setEnabled(c.getSelection(), getFieldEditorParent());
            password.setEnabled(c.getSelection(), getFieldEditorParent());
          }

        });
        return c;
      }
    };
    addField(authEnabled);
    username = new StringFieldEditor(Activator.PREF_LOGIN, "Login", getFieldEditorParent());
    password = new StringFieldEditor(Activator.PREF_PASSWORD, "Password", getFieldEditorParent()) {
      @Override
      protected void doFillIntoGrid(Composite parent, int numColumns) {
        super.doFillIntoGrid(parent, numColumns);
       
        getTextControl().setEchoChar('*');
      }
    };
    addField(username);
    addField(password);
    username.setEnabled(getPreferenceStore().getBoolean(Activator.PREF_USE_AUTH), getFieldEditorParent());
    password.setEnabled(getPreferenceStore().getBoolean(Activator.PREF_USE_AUTH), getFieldEditorParent());
  }

  public void init(IWorkbench workbench) {
  }

  protected IPreferenceStore doGetPreferenceStore() {
    return Activator.getDefault().getPreferenceStore();
  }

  public void dispose() {
    super.dispose();
  }

  private class HudsonUrlField extends StringButtonFieldEditor {
    public HudsonUrlField(Composite parent) {
      init(Activator.PREF_BASE_URL, "Hudson base url");

      setChangeButtonText("Check url");
      setValidateStrategy(StringButtonFieldEditor.VALIDATE_ON_FOCUS_LOST);
      setEmptyStringAllowed(true);
      setErrorMessage("Invalid url");

      createControl(parent);

      Button button = getChangeControl(parent);
      button.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
        }

        public void focusLost(FocusEvent e) {
          if (isValid()) {
            getPage().setMessage(null);
          }
        }

      });
    }

    protected boolean checkState() {
      try {
        check();
        clearErrorMessage();
        return true;
      } catch (Exception e) {
        showErrorMessage();
        return false;
      }
    }

    private void check() throws Exception {
      if (getStringValue() != null && !"".equals(getStringValue().trim())) {
        new HudsonClient().checkValidUrl(getStringValue(), authEnabled.getBooleanValue(), username.getStringValue(), password.getStringValue());
      }
    }

    protected String changePressed() {
      try {
        check();
        getPage().setMessage("Valid url", FieldEditorPreferencePage.INFORMATION);
        setValid(true);
      } catch (Exception e) {
        showErrorMessage(e.getMessage());
        setValid(false);
      }
      return null;
    }
   
    @Override
    protected void doFillIntoGrid(Composite parent, int numColumns) {
      super.doFillIntoGrid(parent, numColumns);
     
      Button discover = new Button(parent, SWT.NONE);
      discover.setText("Discover");
      discover.setToolTipText("Discover running Hudson instances on the local network");
     
      GridData gd = new GridData();
      gd.horizontalSpan = 3;
      gd.horizontalAlignment = SWT.RIGHT;
      discover.setLayoutData(gd);
      discover.addSelectionListener(new SelectionListener() {
        public void widgetDefaultSelected(SelectionEvent arg0) {}

        public void widgetSelected(SelectionEvent arg0) {
          DiggerDialog dialog = new DiggerDialog(getShell());
          if (Window.OK == dialog.open() && dialog.getSelection() != null) {
            setStringValue(dialog.getSelection());
          }
        }
      });
     
    }
  }
}
TOP

Related Classes of dk.contix.eclipse.hudson.preference.HudsonPreferencesPage$HudsonUrlField

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.