<commit_before>package nl.tudelft.watchdog.ui.preferences;

import nl.tudelft.watchdog.ui.util.UIUtils;
import nl.tudelft.watchdog.util.WatchDogGlobals;

import org.apache.commons.validator.routines.UrlValidator;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * The WatchDog preference page in the Eclipse preference settings.
 */
public class PreferencePage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	/** The length of a WatchDog id. */
	private static final int ID_LENGTH = 40;

	/** The project ID input field for this workspace. */
	private Text projectIDInput;

	/** Whether WatchDog should be enabled in this workspace. */
	private Button enableWatchdogInput;

	/** WatchDog preferences. */
	private Preferences preferences = Preferences.getInstance();

	/** This workspace. */
	private String workspace = UIUtils.getWorkspaceName();

	/** The Id of this preference page as set in plugin.xml */
	public final static String ID = "WatchDog.PreferencePage";

	@Override
	public void init(IWorkbench workbench) {
		setPreferenceStore(Preferences.getInstance().getStore());
	}

	@Override
	protected Control createContents(Composite parent) {
		Color colorRed = new Color(getShell().getDisplay(), 255, 0, 0);
		UIUtils.createLabel(
				"Changes in the settings become active after a restart of Eclipse.",
				parent, colorRed);
		UIUtils.createLabel("", parent);
		Group localGroup = createGroup(parent, "Local Settings", 1);
		UIUtils.createLabel(
				"Here you can define the local WatchDog settings for this workspace.",
				localGroup);
		UIUtils.createLabel("", localGroup);

		Composite projectComposite = UIUtils.createGridedComposite(localGroup,
				2);
		projectComposite.setLayoutData(UIUtils.createFullGridUsageData());
		UIUtils.createLabel("Project-ID ", projectComposite);
		projectIDInput = UIUtils.createTextInput(projectComposite);
		projectIDInput.setTextLimit(ID_LENGTH);
		enableWatchdogInput = new Button(localGroup, SWT.CHECK);
		enableWatchdogInput.setText("Monitor this workspace with WatchDog ");

		WorkspacePreferenceSetting workspaceSetting = preferences
				.getOrCreateWorkspaceSetting(workspace);
		projectIDInput.setText(workspaceSetting.projectId);
		enableWatchdogInput.setSelection(workspaceSetting.enableWatchdog);

		UIUtils.createLabel("", parent);

		Group globalGroup = createGroup(parent, "Global Settings", 1);
		UIUtils.createLabel(
				"Here you can enter settings that will affect WatchDog no matter which workspace you have opened.  ",
				globalGroup);
		UIUtils.createLabel("", globalGroup);

		return super.createContents(globalGroup);
	}

	@Override
	public boolean performOk() {
		boolean returnStatus = super.performOk();
		preferences.registerWorkspaceProject(workspace,
				projectIDInput.getText());
		preferences.registerWorkspaceUse(workspace,
				enableWatchdogInput.getSelection());
		return returnStatus;
	}

	@Override
	protected void createFieldEditors() {
		addField(new IDFieldEditor(Preferences.USERID_KEY, "User-ID"));
		addField(new URLFieldEditor(Preferences.SERVER_KEY, "Server-URL"));
		addField(new BooleanFieldEditor(Preferences.AUTHENTICATION_ENABLED_KEY,
				"Enable Authentication", getFieldEditorParent()));
		addField(new BooleanFieldEditor(Preferences.LOGGING_ENABLED_KEY,
				"Enable Logging", getFieldEditorParent()));
	}

	/** A specific field editor allowing input of valid user URLs only. */
	class URLFieldEditor extends StringFieldEditor {
		/** Constructor, delegating call to parent's constructor. */
		public URLFieldEditor(String key, String description) {
			super(key, description, getFieldEditorParent());
		}

		/**
		 * This not too nice implementation allows for a modification of the
		 * inputed value in the text field, by first saving the unchecked value
		 * in the preferences, then loading it in again, then performing the URL
		 * checks for a trailing / and then saving it.
		 * 
		 * Finally, it reloads the value to give imminent user-feedback.
		 */
		@Override
		protected void doStore() {
			super.doStore();
			super.doLoad();
			String url = oldValue;
			url = url.trim();
			if (!url.endsWith("/")) {
				url = url.concat("/");
			}
			if (!UrlValidator.getInstance().isValid(url)) {
				url = WatchDogGlobals.DEFAULT_SERVER_URI;
			}
			getPreferenceStore().setValue(getPreferenceName(), url);
			super.doLoad();
		}
	}

	/** A specific field editor allowing input of valid user IDs only. */
	class IDFieldEditor extends StringFieldEditor {
		/** Constructor, delegating call to parent's constructor. */
		public IDFieldEditor(String key, String description) {
			super(key, description, getFieldEditorParent());
		}

		@Override
		protected void createControl(Composite parent) {
			super.createControl(parent);
			setTextLimit(40);
		}
	}

	/**
	 * Creates and returns a {@link Group} with an enclosed GridData layout with
	 * the given number of columns.
	 */
	private Group createGroup(Composite area, String name, int columns) {
		Group group = new Group(area, SWT.None);
		group.setText(name);
		group.setLayout(new GridLayout(columns, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL));
		return group;
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
		preferences.setDefaults();
	}
}
<commit_msg>Adds warning box in case of invalid URL.<commit_after>package nl.tudelft.watchdog.ui.preferences;

import nl.tudelft.watchdog.ui.util.UIUtils;

import org.apache.commons.validator.routines.UrlValidator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * The WatchDog preference page in the Eclipse preference settings.
 */
public class PreferencePage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	/** The length of a WatchDog id. */
	private static final int ID_LENGTH = 40;

	/** The project ID input field for this workspace. */
	private Text projectIDInput;

	/** Whether WatchDog should be enabled in this workspace. */
	private Button enableWatchdogInput;

	/** WatchDog preferences. */
	private Preferences preferences = Preferences.getInstance();

	/** This workspace. */
	private String workspace = UIUtils.getWorkspaceName();

	/** The Id of this preference page as set in plugin.xml */
	public final static String ID = "WatchDog.PreferencePage";

	@Override
	public void init(IWorkbench workbench) {
		setPreferenceStore(Preferences.getInstance().getStore());
	}

	@Override
	protected Control createContents(Composite parent) {
		Color colorRed = new Color(getShell().getDisplay(), 255, 0, 0);
		UIUtils.createLabel(
				"Changes in the settings become active after a restart of Eclipse.",
				parent, colorRed);
		UIUtils.createLabel("", parent);
		Group localGroup = createGroup(parent, "Local Settings", 1);
		UIUtils.createLabel(
				"Here you can define the local WatchDog settings for this workspace.",
				localGroup);
		UIUtils.createLabel("", localGroup);

		Composite projectComposite = UIUtils.createGridedComposite(localGroup,
				2);
		projectComposite.setLayoutData(UIUtils.createFullGridUsageData());
		UIUtils.createLabel("Project-ID ", projectComposite);
		projectIDInput = UIUtils.createTextInput(projectComposite);
		projectIDInput.setTextLimit(ID_LENGTH);
		enableWatchdogInput = new Button(localGroup, SWT.CHECK);
		enableWatchdogInput.setText("Monitor this workspace with WatchDog ");

		WorkspacePreferenceSetting workspaceSetting = preferences
				.getOrCreateWorkspaceSetting(workspace);
		projectIDInput.setText(workspaceSetting.projectId);
		enableWatchdogInput.setSelection(workspaceSetting.enableWatchdog);

		UIUtils.createLabel("", parent);

		Group globalGroup = createGroup(parent, "Global Settings", 1);
		UIUtils.createLabel(
				"Here you can enter settings that will affect WatchDog no matter which workspace you have opened.  ",
				globalGroup);
		UIUtils.createLabel("", globalGroup);

		return super.createContents(globalGroup);
	}

	@Override
	public boolean performOk() {
		boolean returnStatus = super.performOk();
		preferences.registerWorkspaceProject(workspace,
				projectIDInput.getText());
		preferences.registerWorkspaceUse(workspace,
				enableWatchdogInput.getSelection());
		return returnStatus;
	}

	@Override
	protected void createFieldEditors() {
		addField(new IDFieldEditor(Preferences.USERID_KEY, "User-ID"));
		addField(new URLFieldEditor(Preferences.SERVER_KEY, "Server-URL"));
		addField(new BooleanFieldEditor(Preferences.AUTHENTICATION_ENABLED_KEY,
				"Enable Authentication", getFieldEditorParent()));
		addField(new BooleanFieldEditor(Preferences.LOGGING_ENABLED_KEY,
				"Enable Logging", getFieldEditorParent()));
	}

	/** A specific field editor allowing input of valid user URLs only. */
	class URLFieldEditor extends StringFieldEditor {
		/** Constructor, delegating call to parent's constructor. */
		public URLFieldEditor(String key, String description) {
			super(key, description, getFieldEditorParent());
		}

		/**
		 * This not too nice implementation allows for a modification of the
		 * inputed value in the text field, by first saving the unchecked value
		 * in the preferences, then loading it in again, then performing the URL
		 * checks for a trailing / and then saving it.
		 * 
		 * Finally, it reloads the value to give imminent user-feedback.
		 */
		@Override
		protected void doStore() {
			super.doStore();
			super.doLoad();
			String url = oldValue;
			url = url.trim();
			if (!url.endsWith("/")) {
				url = url.concat("/");
			}
			if (!UrlValidator.getInstance().isValid(url)) {
				new MessageDialog(
						getShell(),
						"Invalid Server",
						null,
						"The URL you entered for the WatchDog server seems to be invalid!",
						MessageDialog.ERROR, new String[] { "OK" }, 0).open();
			}
			getPreferenceStore().setValue(getPreferenceName(), url);
			super.doLoad();
		}
	}

	/** A specific field editor allowing input of valid user IDs only. */
	class IDFieldEditor extends StringFieldEditor {
		/** Constructor, delegating call to parent's constructor. */
		public IDFieldEditor(String key, String description) {
			super(key, description, getFieldEditorParent());
		}

		@Override
		protected void createControl(Composite parent) {
			super.createControl(parent);
			setTextLimit(40);
		}
	}

	/**
	 * Creates and returns a {@link Group} with an enclosed GridData layout with
	 * the given number of columns.
	 */
	private Group createGroup(Composite area, String name, int columns) {
		Group group = new Group(area, SWT.None);
		group.setText(name);
		group.setLayout(new GridLayout(columns, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL));
		return group;
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
		preferences.setDefaults();
	}
}

{'fraction_non_alphanumeric': 0.09978096860550012, 'fraction_numerical': 0.002028068467591466, 'mean_word_length': 4.113231024471173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4944751', 'n_tokens_mistral': 3864, 'n_tokens_neox': 3407, 'n_words': 910}