bug 367925: moved handling of preferred style to class VexPreferences
diff --git a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/VexPlugin.java b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/VexPlugin.java
index 109c13b..d7180e7 100644
--- a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/VexPlugin.java
+++ b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/VexPlugin.java
@@ -26,12 +26,13 @@
  */
 public class VexPlugin extends AbstractUIPlugin {
 
-    /** The plugin's ID */
     public static final String ID = "org.eclipse.vex.ui"; //$NON-NLS-1$
 
     private static VexPlugin instance;
     
     private final ConfigurationRegistry configurationRegistry = new ConfigurationRegistryImpl(new ConfigLoaderJob());
+    
+    private VexPreferences preferences = null;
 
     /**
      * Returns the shared instance.
@@ -73,6 +74,12 @@
     public ConfigurationRegistry getConfigurationRegistry() {
     	return configurationRegistry;
     }
+    
+    public VexPreferences getPreferences() {
+    	if (preferences == null)
+    		preferences = new VexPreferences(getPreferenceStore(), getConfigurationRegistry());
+		return preferences;
+	}
 
     /**
      * Override the plugin startup to intialize the resource tracker.
diff --git a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/VexPreferences.java b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/VexPreferences.java
new file mode 100644
index 0000000..7f38eed
--- /dev/null
+++ b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/VexPreferences.java
@@ -0,0 +1,65 @@
+/*******************************************************************************

+ * Copyright (c) 2011 Florian Thienel 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

+ * http://www.eclipse.org/legal/epl-v10.html

+ * 

+ * Contributors:

+ * 		Florian Thienel - initial API and implementation

+ *******************************************************************************/

+package org.eclipse.vex.ui.internal;

+

+import org.eclipse.core.runtime.IStatus;

+import org.eclipse.core.runtime.preferences.InstanceScope;

+import org.eclipse.jface.preference.IPreferenceStore;

+import org.eclipse.vex.ui.internal.config.ConfigurationRegistry;

+import org.eclipse.vex.ui.internal.config.Style;

+import org.eclipse.vex.ui.internal.editor.Messages;

+import org.osgi.service.prefs.BackingStoreException;

+import org.osgi.service.prefs.Preferences;

+

+/**

+ * @author Florian Thienel

+ */

+public class VexPreferences {

+	

+	private final IPreferenceStore preferenceStore;

+	

+	private final ConfigurationRegistry configurationRegistry;

+	

+	public VexPreferences(final IPreferenceStore preferenceStore, final ConfigurationRegistry configurationRegistry) {

+		this.preferenceStore = preferenceStore;

+		this.configurationRegistry = configurationRegistry;

+	}

+	

+	public void setPreferredStyleId(final String publicId, final String styleId) {

+		final Preferences preferences = InstanceScope.INSTANCE.getNode(VexPlugin.ID);

+		final String key = getStylePreferenceKey(publicId);

+		preferences.put(key, styleId);

+		try {

+			preferences.flush();

+		} catch (final BackingStoreException e) {

+			VexPlugin.getInstance().log(IStatus.ERROR, Messages.getString("VexEditor.errorSavingStylePreference"), e); //$NON-NLS-1$

+		}

+	}

+

+	public String getPreferredStyleId(final String publicId) {

+		final Preferences preferences = InstanceScope.INSTANCE.getNode(VexPlugin.ID);

+		final String preferredStyleId = preferences.get(getStylePreferenceKey(publicId), null);

+		return preferredStyleId;

+	}

+

+	private static String getStylePreferenceKey(final String publicId) {

+		return publicId + ".style"; //$NON-NLS-1$

+	}

+

+	public Style getPreferredStyle(final String publicId) {

+		return configurationRegistry.getStyle(publicId, getPreferredStyleId(publicId));

+	}

+

+	public String getIndentationPattern() {

+		return "\t";

+	}

+

+}

diff --git a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexDocumentContentModel.java b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexDocumentContentModel.java
index b09bed6..02a3a0d 100644
--- a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexDocumentContentModel.java
+++ b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexDocumentContentModel.java
@@ -54,7 +54,7 @@
 //			throw new RuntimeException(message);

 //		}

 

-		style = VexEditor.getPreferredStyle(documentType.getPublicId());

+		style = VexPlugin.getInstance().getPreferences().getPreferredStyle(documentType.getPublicId());

 		if (style == null)

 			throw new NoStyleForDoctypeException();

 	}

diff --git a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexEditor.java b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexEditor.java
index b730711..9c58fa3 100644
--- a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexEditor.java
+++ b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexEditor.java
@@ -31,7 +31,6 @@
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.text.IFindReplaceTarget;
@@ -74,7 +73,9 @@
 import org.eclipse.vex.core.internal.validator.WTPVEXValidator;
 import org.eclipse.vex.core.internal.widget.CssWhitespacePolicy;
 import org.eclipse.vex.ui.internal.VexPlugin;
+import org.eclipse.vex.ui.internal.VexPreferences;
 import org.eclipse.vex.ui.internal.config.ConfigEvent;
+import org.eclipse.vex.ui.internal.config.ConfigurationRegistry;
 import org.eclipse.vex.ui.internal.config.DocumentType;
 import org.eclipse.vex.ui.internal.config.IConfigListener;
 import org.eclipse.vex.ui.internal.config.Style;
@@ -83,8 +84,6 @@
 import org.eclipse.vex.ui.internal.outline.DocumentOutlinePage;
 import org.eclipse.vex.ui.internal.property.ElementPropertySource;
 import org.eclipse.vex.ui.internal.swt.VexWidget;
-import org.osgi.service.prefs.BackingStoreException;
-import org.osgi.service.prefs.Preferences;
 import org.xml.sax.SAXParseException;
 
 /**
@@ -96,12 +95,36 @@
 	 * ID of this editor extension.
 	 */
 	public static final String ID = "org.eclipse.vex.ui.internal.editor.VexEditor"; //$NON-NLS-1$
+	
+	private final boolean debugging;
+	private final ConfigurationRegistry configurationRegistry;
+	private final VexPreferences preferences;
+
+	private Composite parentControl;
+	private Label loadingLabel;
+
+	private boolean loaded;
+	private DocumentType doctype;
+	private Document document;
+	private Style style;
+
+	private VexWidget vexWidget;
+
+	private int savedUndoDepth;
+	private boolean wasDirty;
+
+	private final ListenerList<IVexEditorListener, VexEditorEvent> vexEditorListeners = new ListenerList<IVexEditorListener, VexEditorEvent>(
+			IVexEditorListener.class);
+
+	private final SelectionProvider selectionProvider = new SelectionProvider();
 
 	/**
 	 * Class constructor.
 	 */
 	public VexEditor() {
 		debugging = VexPlugin.getInstance().isDebugging() && "true".equalsIgnoreCase(Platform.getDebugOption(VexPlugin.ID + "/debug/layout")); //$NON-NLS-1$ //$NON-NLS-2$
+		configurationRegistry = VexPlugin.getInstance().getConfigurationRegistry();
+		preferences = VexPlugin.getInstance().getPreferences();
 	}
 
 	/**
@@ -121,7 +144,7 @@
 		if (parentControl != null)
 			// createPartControl was called, so we must de-register from config
 			// events
-			VexPlugin.getInstance().getConfigurationRegistry().removeConfigListener(configListener);
+			configurationRegistry.removeConfigListener(configListener);
 
 		if (getEditorInput() instanceof IFileEditorInput)
 			ResourcesPlugin.getWorkspace().removeResourceChangeListener(resourceChangeListener);
@@ -135,8 +158,7 @@
 		OutputStream os = null;
 		try {
 			resourceChangeListener.setSaving(true);
-			final DocumentWriter writer = new DocumentWriter();
-			writer.setWhitespacePolicy(new CssWhitespacePolicy(style.getStyleSheet()));
+			final DocumentWriter writer = createDocumentWriter();
 
 			if (input instanceof IFileEditorInput) {
 				final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -169,6 +191,13 @@
 		}
 	}
 
+	private DocumentWriter createDocumentWriter() {
+		final DocumentWriter result = new DocumentWriter();
+		result.setWhitespacePolicy(new CssWhitespacePolicy(style.getStyleSheet()));
+		result.setIndent(preferences.getIndentationPattern());
+		return result;
+	}
+	
 	@Override
 	public void doSaveAs() {
 		final SaveAsDialog dlg = new SaveAsDialog(getSite().getShell());
@@ -178,8 +207,7 @@
 			try {
 				resourceChangeListener.setSaving(true);
 				final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-				final DocumentWriter writer = new DocumentWriter();
-				writer.setWhitespacePolicy(new CssWhitespacePolicy(style.getStyleSheet()));
+				final DocumentWriter writer = createDocumentWriter();
 				writer.write(document, baos);
 				baos.close();
 
@@ -214,14 +242,8 @@
 	 * @param publicId
 	 *            Public ID for which to return the style.
 	 */
-	public static Style getPreferredStyle(final String publicId) {
-		return VexPlugin.getInstance().getConfigurationRegistry().getStyle(publicId, getPreferredStyleId(publicId));
-	}
-
-	private static String getPreferredStyleId(final String publicId) {
-		final Preferences prefs = new InstanceScope().getNode(VexPlugin.ID);
-		final String preferredStyleId = prefs.get(getStylePreferenceKey(publicId), null);
-		return preferredStyleId;
+	public Style getPreferredStyle(final String publicId) {
+		return configurationRegistry.getStyle(publicId, preferences.getPreferredStyleId(publicId));
 	}
 
 	/**
@@ -400,8 +422,8 @@
 
 		parentControl = parent;
 
-		VexPlugin.getInstance().getConfigurationRegistry().addConfigListener(configListener);
-		if (VexPlugin.getInstance().getConfigurationRegistry().isLoaded())
+		configurationRegistry.addConfigListener(configListener);
+		if (configurationRegistry.isLoaded())
 			loadInput();
 		else
 			showLabel(Messages.getString("VexEditor.loading")); //$NON-NLS-1$
@@ -448,52 +470,10 @@
 		this.style = style;
 		if (vexWidget != null) {
 			vexWidget.setStyleSheet(style.getStyleSheet());
-			setPreferredStyleId(document.getPublicID(), style.getUniqueId());
+			preferences.setPreferredStyleId(document.getPublicID(), style.getUniqueId());
 		}
 	}
 
-	private static void setPreferredStyleId(final String publicId, final String styleId) {
-		final Preferences prefs = new InstanceScope().getNode(VexPlugin.ID);
-		final String key = getStylePreferenceKey(publicId);
-		prefs.put(key, styleId);
-		try {
-			prefs.flush();
-		} catch (final BackingStoreException e) {
-			VexPlugin.getInstance().log(IStatus.ERROR, Messages.getString("VexEditor.errorSavingStylePreference"), e); //$NON-NLS-1$
-		}
-	}
-
-	// ========================================================= PRIVATE
-
-	private final boolean debugging;
-
-	private Composite parentControl;
-	private Label loadingLabel;
-
-	private boolean loaded;
-	private DocumentType doctype;
-	private Document document;
-	private Style style;
-
-	private VexWidget vexWidget;
-
-	private int savedUndoDepth;
-	private boolean wasDirty;
-	// private Label statusLabel;
-
-	private final ListenerList<IVexEditorListener, VexEditorEvent> vexEditorListeners = new ListenerList<IVexEditorListener, VexEditorEvent>(
-			IVexEditorListener.class);
-
-	private final SelectionProvider selectionProvider = new SelectionProvider();
-
-	/**
-	 * Returns the preference key used to access the style ID for documents with
-	 * the same public ID as the current document.
-	 */
-	private static String getStylePreferenceKey(final String publicId) {
-		return publicId + ".style"; //$NON-NLS-1$
-	}
-
 	private void showLabel(final String message) {
 		if (loadingLabel == null) {
 			if (vexWidget != null) {
@@ -642,7 +622,7 @@
 						return;
 
 					final String styleId = style.getUniqueId();
-					final Style newStyle = VexPlugin.getInstance().getConfigurationRegistry().getStyle(styleId);
+					final Style newStyle = configurationRegistry.getStyle(styleId);
 					if (newStyle == null) {
 						// Oops, style went bye-bye
 						// Let's just hold on to it in case it comes back later
diff --git a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/wizards/NewDocumentWizard.java b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/wizards/NewDocumentWizard.java
index d82c8d8..dd69041 100644
--- a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/wizards/NewDocumentWizard.java
+++ b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/wizards/NewDocumentWizard.java
@@ -73,7 +73,7 @@
 		try {
 			final Document doc = createDocument(typePage.getDocumentType(), typePage.getRootElementName());
 
-			final Style style = VexEditor.getPreferredStyle(typePage.getDocumentType().getPublicId());
+			final Style style = VexPlugin.getInstance().getPreferences().getPreferredStyle(typePage.getDocumentType().getPublicId());
 			if (style == null) {
 				MessageDialog.openError(getShell(),
 						Messages.getString("NewDocumentWizard.noStyles.title"), Messages.getString("NewDocumentWizard.noStyles.message")); //$NON-NLS-1$ //$NON-NLS-2$