[297714]: Fix ArrayOutOfBoundIndex problem with preferences
https://bugs.eclipse.org/bugs/show_bug.cgi?id=297714
diff --git a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/preferences/AbstractValidationSettingsPage.java b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/preferences/AbstractValidationSettingsPage.java
index a969dc2..0aa0a27 100644
--- a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/preferences/AbstractValidationSettingsPage.java
+++ b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/preferences/AbstractValidationSettingsPage.java
@@ -9,12 +9,14 @@
  *     David Carver (STAR) - bug 230072 - initial API and implementation
  *                                        based on org.eclipse.wst.html.ui class of
  *                                        the same name.
+ *    David Carver - bug 297714 - Values not being loaded from preferences.
  *******************************************************************************/
 package org.eclipse.wst.xsl.ui.internal.preferences;
 
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.ProjectScope;
@@ -68,8 +70,8 @@
 	
 	public AbstractValidationSettingsPage() {
 		super();
-		fCombos = new ArrayList();
-		fExpandables = new ArrayList();
+		fCombos = new CopyOnWriteArrayList<Combo>();
+		fExpandables = new CopyOnWriteArrayList<ExpandableComposite>();
 		fPreferencesService = Platform.getPreferencesService();
 		fValidation = ValidationFramework.getDefault();
 	}
@@ -287,6 +289,7 @@
 	
 	@Override
 	public boolean performOk() {
+		storeValues();
 		if(super.performOk() && shouldRevalidateOnSettingsChange()) {
 			MessageBox mb = new MessageBox(this.getShell(), SWT.APPLICATION_MODAL | SWT.YES | SWT.NO | SWT.CANCEL | SWT.ICON_INFORMATION | SWT.RIGHT);
 			mb.setText(Messages.AbstractValidationSettingsPageMbText);
diff --git a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/preferences/XSLValidationPreferencePage.java b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/preferences/XSLValidationPreferencePage.java
index 096f5e9..3cdb8f2 100644
--- a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/preferences/XSLValidationPreferencePage.java
+++ b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/preferences/XSLValidationPreferencePage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *Copyright (c) 2008 Standards for Technology in Automotive Retail and others.
+ *Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail and others.
  *All rights reserved. This program and the accompanying materials
  *are made available under the terms of the Eclipse Public License v1.0
  *which accompanies this distribution, and is available at
@@ -9,16 +9,24 @@
  *    David Carver - bug 230072 - initial API and implementation based on code from
  *                                Doug Satchwell, Jesper Moeller, and the
  *                                HTML Validation PreferencePage.
+ *    David Carver - bug 297714 - Values not being loaded from preferences.
  *******************************************************************************/
 package org.eclipse.wst.xsl.ui.internal.preferences;
 
-import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.List;
 import java.util.Map;
 
 import org.eclipse.core.resources.IMarker;
-import org.eclipse.core.runtime.Preferences;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ProjectScope;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.preferences.DefaultScope;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
 import org.eclipse.jface.dialogs.IDialogSettings;
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.swt.SWT;
@@ -37,118 +45,141 @@
 import org.eclipse.ui.forms.events.ExpansionEvent;
 import org.eclipse.ui.forms.widgets.ExpandableComposite;
 import org.eclipse.wst.sse.ui.internal.preferences.ui.ScrolledPageContent;
-import org.eclipse.wst.sse.ui.internal.util.PixelConverter;
+import org.eclipse.jface.layout.PixelConverter;
 import org.eclipse.wst.xsl.core.ValidationPreferences;
 import org.eclipse.wst.xsl.core.internal.XSLCorePlugin;
 import org.eclipse.wst.xsl.ui.internal.Messages;
 import org.eclipse.wst.xsl.ui.internal.XSLUIPlugin;
 
-public class XSLValidationPreferencePage extends AbstractValidationSettingsPage implements ModifyListener {
+public class XSLValidationPreferencePage extends AbstractValidationSettingsPage
+		implements ModifyListener {
 
 	private static final String XSL_UI_PROPERTY_PAGE_PROJECT_VALIDATION_ID = "org.eclipse.wst.xsl.ui.propertyPage.project.validation"; //$NON-NLS-1$
 	private static final String XSL_UI_PREFERENCES_VALIDATION_ID = "org.eclipse.wst.xsl.ui.preferences.Validation"; //$NON-NLS-1$
-	private static final String[] ERRORS = new String[] { Messages.ErrorLevelText, Messages.WarningLevelText, Messages.IgnoreLevelText };
-	private static final int[] ERROR_VALUES = new int[] { IMarker.SEVERITY_ERROR, IMarker.SEVERITY_WARNING, IMarker.SEVERITY_INFO };
-	private static final Map<Integer, Integer> ERROR_MAP = new HashMap<Integer, Integer>();
+	private static final String[] ERRORS = new String[] {
+			Messages.ErrorLevelText, Messages.WarningLevelText,
+			Messages.IgnoreLevelText };
+	private static final int[] ERROR_VALUES = new int[] {
+			IMarker.SEVERITY_ERROR, IMarker.SEVERITY_WARNING,
+			IMarker.SEVERITY_INFO };
+	private static final Map<Integer, Integer> ERROR_MAP = new ConcurrentHashMap<Integer, Integer>();
 	private Text maxErrorsText;
-	private Map<String, Combo> combos = new HashMap<String, Combo>();
-	private List<ExpandableComposite> Expandables = new ArrayList<ExpandableComposite>();
+	private Map<String, Combo> combos = new ConcurrentHashMap<String, Combo>();
+	private List<ExpandableComposite> Expandables = new CopyOnWriteArrayList<ExpandableComposite>();
 	private static final String SETTINGS_SECTION_NAME = "XSLValidationSeverities";//$NON-NLS-1$
 	private PixelConverter fPixelConverter;
 
-
-	static
-	{
+	static {
 		ERROR_MAP.put(IMarker.SEVERITY_ERROR, 0);
 		ERROR_MAP.put(IMarker.SEVERITY_WARNING, 1);
 		ERROR_MAP.put(IMarker.SEVERITY_INFO, 2);
 	}
-	
+
 	@Override
 	protected Control createCommonContents(Composite parent) {
 		final Composite page = new Composite(parent, SWT.NULL);
-		
-		//GridLayout
+
+		// GridLayout
 		GridLayout layout = new GridLayout();
 		layout.numColumns = 1;
 		page.setLayout(layout);
-		
+
 		fPixelConverter = new PixelConverter(parent);
-		
+
 		final Composite content = createValidationSection(page);
 
 		loadPreferences();
-		restoreSectionExpansionStates(getDialogSettings().getSection(SETTINGS_SECTION_NAME));
-		
-		GridData gridData= new GridData(GridData.FILL, GridData.FILL, true, true);
+		restoreSectionExpansionStates(getDialogSettings().getSection(
+				SETTINGS_SECTION_NAME));
+
+		GridData gridData = new GridData(GridData.FILL, GridData.FILL, true,
+				true);
 		gridData.heightHint = fPixelConverter.convertHeightInCharsToPixels(20);
 		content.setLayoutData(gridData);
-		
+
 		return page;
 	}
-	
-	protected Composite createValidationSection(Composite parent)
-	{
-		GridLayout layout= new GridLayout();
-		layout.numColumns= 2;
-		layout.marginHeight= 0;
-		layout.marginWidth= 0;
-		
+
+	protected Composite createValidationSection(Composite parent) {
+		GridLayout layout = new GridLayout();
+		layout.numColumns = 2;
+		layout.marginHeight = 0;
+		layout.marginWidth = 0;
+
 		final ScrolledPageContent pageContent = new ScrolledPageContent(parent);
 		pageContent.setLayoutData(new GridData(GridData.FILL_BOTH));
 		pageContent.setExpandHorizontal(true);
 		pageContent.setExpandVertical(true);
-		
+
 		Composite body = pageContent.getBody();
 		body.setLayout(layout);
 
-		GridData gd= new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1);
-		gd.horizontalIndent= 0;
-		
+		GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false,
+				2, 1);
+		gd.horizontalIndent = 0;
+
 		createLabel(body, Messages.XSLValidationPreferenceMaxErrorsLabel);
 		maxErrorsText = createTextField(body);
 		maxErrorsText.addModifyListener(this);
 
-
 		Label description = new Label(body, SWT.NONE);
 		description.setText(Messages.XSLValidationPageSeverityLevel);
 		description.setFont(pageContent.getFont());
 		description.setLayoutData(gd);
-		
-
 
 		ExpandableComposite twistie;
-		
+
 		int columns = 3;
-		twistie = createTwistie(body, Messages.XSLValidationPreferenceImportsIncludesLabel,columns);
+		twistie = createTwistie(body,
+				Messages.XSLValidationPreferenceImportsIncludesLabel, columns);
 		Composite inner = createInnerComposite(parent, twistie, columns);
-		
+
 		String label = Messages.XSLValidationPreferenceUnresolveImportIncludeLabel;
 		createCombo(inner, label, ValidationPreferences.MISSING_INCLUDE);
 
 		inner = createInnerComposite(parent, twistie, columns);
 		createCombo(inner, label, ValidationPreferences.MISSING_INCLUDE);
-		createCombo(inner, Messages.XSLValidationPreferenceCircularReferencesLabel, ValidationPreferences.CIRCULAR_REF);
+		createCombo(inner,
+				Messages.XSLValidationPreferenceCircularReferencesLabel,
+				ValidationPreferences.CIRCULAR_REF);
 
-		twistie = createTwistie(body, Messages.XSLValidationPreferenceNamedTemplatesLabel,columns);
+		twistie = createTwistie(body,
+				Messages.XSLValidationPreferenceNamedTemplatesLabel, columns);
 		inner = createInnerComposite(parent, twistie, columns);
-		
-		createCombo(inner, Messages.XSLValidationPreferenceTemplateConflictsLabel, ValidationPreferences.TEMPLATE_CONFLICT);
-		createCombo(inner, Messages.XSLValidationPreferenceDuplicateParameterLabel, ValidationPreferences.DUPLICATE_PARAMETER);
-		createCombo(inner, Messages.XSLValidationPreferenceParamtersWithoutValueLabel, ValidationPreferences.NAME_ATTRIBUTE_MISSING);
-		createCombo(inner, Messages.XSLValidationPreferenceMissingParameterAttributeLabel, ValidationPreferences.NAME_ATTRIBUTE_EMPTY);
-		
-		twistie = createTwistie(body,Messages.XSLValidationPreferenceCallTemplatesLabel,columns);
-		inner = createInnerComposite(parent, twistie, columns);
-		
-		createCombo(inner, Messages.XSLValidationPreferenceUnresolvedTemplatesLabel, ValidationPreferences.CALL_TEMPLATES);
-		createCombo(inner, Messages.XSLValidationPreferenceMissingParamtersLabel, ValidationPreferences.MISSING_PARAM);
-		createCombo(inner, Messages.XSLValidationPreferenceMissingParameterAttributeLabel, ValidationPreferences.EMPTY_PARAM);
 
-		twistie = createTwistie(body, Messages.XSLValidationPreferenceXPathLabel,columns);
+		createCombo(inner,
+				Messages.XSLValidationPreferenceTemplateConflictsLabel,
+				ValidationPreferences.TEMPLATE_CONFLICT);
+		createCombo(inner,
+				Messages.XSLValidationPreferenceDuplicateParameterLabel,
+				ValidationPreferences.DUPLICATE_PARAMETER);
+		createCombo(inner,
+				Messages.XSLValidationPreferenceParamtersWithoutValueLabel,
+				ValidationPreferences.NAME_ATTRIBUTE_MISSING);
+		createCombo(inner,
+				Messages.XSLValidationPreferenceMissingParameterAttributeLabel,
+				ValidationPreferences.NAME_ATTRIBUTE_EMPTY);
+
+		twistie = createTwistie(body,
+				Messages.XSLValidationPreferenceCallTemplatesLabel, columns);
 		inner = createInnerComposite(parent, twistie, columns);
-		createCombo(inner, Messages.XSLValidationPreferenceXPathSyntaxLabel, ValidationPreferences.XPATHS);
-		
+
+		createCombo(inner,
+				Messages.XSLValidationPreferenceUnresolvedTemplatesLabel,
+				ValidationPreferences.CALL_TEMPLATES);
+		createCombo(inner,
+				Messages.XSLValidationPreferenceMissingParamtersLabel,
+				ValidationPreferences.MISSING_PARAM);
+		createCombo(inner,
+				Messages.XSLValidationPreferenceMissingParameterAttributeLabel,
+				ValidationPreferences.EMPTY_PARAM);
+
+		twistie = createTwistie(body,
+				Messages.XSLValidationPreferenceXPathLabel, columns);
+		inner = createInnerComposite(parent, twistie, columns);
+		createCombo(inner, Messages.XSLValidationPreferenceXPathSyntaxLabel,
+				ValidationPreferences.XPATHS);
+
 		return parent;
 	}
 
@@ -160,17 +191,18 @@
 		twistie.setClient(inner);
 		return inner;
 	}
-	
+
 	protected Combo createCombo(Composite parent, String label, String key) {
-		return addComboBox(parent, label, key, ERROR_VALUES, ERRORS, 0);
+		Combo combo = addComboBox(parent, label, key, ERROR_VALUES, ERRORS, 0);
+		combos.put(key, combo);
+		return combo;
 	}
-	
+
 	protected Label createLabel(Composite parent, String text) {
 		Label label = new Label(parent, SWT.LEFT);
 		label.setText(text);
 
-
-		//GridData
+		// GridData
 		GridData data = new GridData(GridData.FILL);
 		data.verticalAlignment = GridData.CENTER;
 		data.horizontalAlignment = GridData.FILL;
@@ -178,11 +210,11 @@
 
 		return label;
 	}
-	
+
 	protected Text createTextField(Composite parent) {
 		Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
 
-		//GridData
+		// GridData
 		GridData data = new GridData();
 		data.verticalAlignment = GridData.CENTER;
 		data.horizontalAlignment = GridData.FILL;
@@ -191,14 +223,19 @@
 
 		return text;
 	}
-	
+
 	@Override
-	protected ExpandableComposite createTwistie(Composite parent, String label, int nColumns) {
-		ExpandableComposite excomposite= new ExpandableComposite(parent, SWT.NONE, ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT);
+	protected ExpandableComposite createTwistie(Composite parent, String label,
+			int nColumns) {
+		ExpandableComposite excomposite = new ExpandableComposite(parent,
+				SWT.NONE, ExpandableComposite.TWISTIE
+						| ExpandableComposite.CLIENT_INDENT);
 		excomposite.setText(label);
 		excomposite.setExpanded(false);
-		excomposite.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT));
-		excomposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, nColumns, 1));
+		excomposite.setFont(JFaceResources.getFontRegistry().getBold(
+				JFaceResources.DIALOG_FONT));
+		excomposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL,
+				true, false, nColumns, 1));
 		excomposite.addExpansionListener(new ExpansionAdapter() {
 			@Override
 			public void expansionStateChanged(ExpansionEvent e) {
@@ -209,26 +246,25 @@
 		makeScrollableCompositeAware(excomposite);
 		return excomposite;
 	}
-	
-	private void makeScrollableCompositeAware(Control control)
-	{
+
+	private void makeScrollableCompositeAware(Control control) {
 		ScrolledPageContent parentScrolledComposite = getParentScrolledComposite(control);
-		if (parentScrolledComposite != null)
-		{
+		if (parentScrolledComposite != null) {
 			parentScrolledComposite.adaptChild(control);
 		}
 	}
-	
+
 	@Override
 	public void dispose() {
-		storeSectionExpansionStates(getDialogSettings().addNewSection(SETTINGS_SECTION_NAME));
+		storeSectionExpansionStates(getDialogSettings().addNewSection(
+				SETTINGS_SECTION_NAME));
 		super.dispose();
 	}
-	
+
 	protected String getQualifier() {
 		return XSLCorePlugin.getDefault().getBundle().getSymbolicName();
 	}
-	
+
 	@Override
 	protected String getPreferenceNodeQualifier() {
 		return XSLCorePlugin.getDefault().getBundle().getSymbolicName();
@@ -243,10 +279,10 @@
 	protected String getProjectSettingsKey() {
 		return XSLCorePlugin.USE_PROJECT_SETTINGS;
 	}
-	
+
 	protected IDialogSettings getDialogSettings() {
 		return XSLUIPlugin.getDefault().getDialogSettings();
-	}	
+	}
 
 	@Override
 	protected String getPropertyPageID() {
@@ -268,23 +304,32 @@
 		enableValues();
 	}
 
-
 	@Override
-	protected void storeValues()
-	{
-		int maxErrors = Integer.parseInt(maxErrorsText.getText());
-		getModelPreferences().setValue(ValidationPreferences.MAX_ERRORS, maxErrors);
-		for (Map.Entry<String, Combo> entry : combos.entrySet())
-		{
-			int index = entry.getValue().getSelectionIndex();
-			getModelPreferences().setValue(entry.getKey(), ERROR_VALUES[index]);
-		}
+	protected void storeValues() {
 		super.storeValues();
+		int maxErrors = Integer.parseInt(maxErrorsText.getText());
+		Preferences prefs = getModelPreferences();
+		
+		prefs.putInt(ValidationPreferences.MAX_ERRORS, maxErrors);
+		
+		try {
+			prefs.flush();
+		} catch (BackingStoreException ex) {
+			XSLUIPlugin.log(ex);
+		}
 	}
-	
-	protected Preferences getModelPreferences()
-	{
-		return XSLCorePlugin.getDefault().getPluginPreferences();
+
+	protected Preferences getModelPreferences() {
+		IEclipsePreferences prefs = Platform.getPreferencesService().getRootNode();
+		IProject project = getProject();
+		if (project != null) {
+			return prefs.node(ProjectScope.SCOPE).node(getPreferenceNodeQualifier());
+		}
+		Preferences instanceScope = prefs.node(InstanceScope.SCOPE).node(getPreferenceNodeQualifier());
+		if (instanceScope != null) {
+			return instanceScope;
+		}
+		return prefs.node(DefaultScope.SCOPE).node(getPreferenceNodeQualifier());
 	}
 
 	protected boolean loadPreferences() {
@@ -297,45 +342,45 @@
 		});
 		return true;
 	}
-	
-	protected void initializeValues()
-	{
-		int maxErrors = getModelPreferences().getInt(ValidationPreferences.MAX_ERRORS);
+
+	protected void initializeValues() {
+		Preferences prefs = getModelPreferences();
+		int maxErrors = prefs.getInt(ValidationPreferences.MAX_ERRORS, 100);
 		maxErrorsText.setText(String.valueOf(maxErrors));
-		for (Map.Entry<String, Combo> entry : combos.entrySet())
-		{
-			int val = getModelPreferences().getInt(entry.getKey());
+		for (Map.Entry<String, Combo> entry : combos.entrySet()) {
+			int val = prefs.getInt(entry.getKey(), IMarker.SEVERITY_WARNING);
+			if (val < 0) {
+				val = IMarker.SEVERITY_WARNING;
+			}
 			entry.getValue().select(ERROR_MAP.get(val));
 		}
 	}
-	
-	protected void validateValues()
-	{
+
+	protected void validateValues() {
 		String errorMessage = null;
-		try
-		{
+		try {
 			int maxErrors = Integer.parseInt(maxErrorsText.getText());
 			if (maxErrors < 0)
 				errorMessage = Messages.XSLValidationPreferenceMaxErrorsMsgError;
-		}
-		catch (NumberFormatException e)
-		{
+		} catch (NumberFormatException e) {
 			errorMessage = Messages.XSLValidationPreferenceMaxErrorsMsgError;
 		}
 		setErrorMessage(errorMessage);
 		setValid(errorMessage == null);
-	}	
-	
+	}
+
 	protected void enableValues() {
-	}	
-	
+	}
+
 	@Override
 	protected void performDefaults() {
 		resetSeverities();
 		super.performDefaults();
 	}
-	
-	/** (non-Javadoc)
+
+	/**
+	 * (non-Javadoc)
+	 * 
 	 * @see org.eclipse.wst.xsl.ui.internal.preferences.AbstractValidationSettingsPage#performOk()
 	 */
 	@Override
@@ -343,5 +388,5 @@
 		boolean result = super.performOk();
 		storeValues();
 		return result;
-	}	
+	}
 }