[SourceEditor] Move AbstractTextStylesConfigurationBlock to LTK

Change-Id: Ida3648bc019d11b2eb4e7650391fe28455d62907
diff --git a/ltk/org.eclipse.statet.ltk.ui/META-INF/MANIFEST.MF b/ltk/org.eclipse.statet.ltk.ui/META-INF/MANIFEST.MF
index a4ff798..450d151 100644
--- a/ltk/org.eclipse.statet.ltk.ui/META-INF/MANIFEST.MF
+++ b/ltk/org.eclipse.statet.ltk.ui/META-INF/MANIFEST.MF
@@ -61,6 +61,7 @@
  org.eclipse.statet.ltk.ui.sourceediting.actions,
  org.eclipse.statet.ltk.ui.sourceediting.assist,
  org.eclipse.statet.ltk.ui.sourceediting.folding,
+ org.eclipse.statet.ltk.ui.sourceediting.presentation,
  org.eclipse.statet.ltk.ui.sourceediting.swt,
  org.eclipse.statet.ltk.ui.sourceediting.util,
  org.eclipse.statet.ltk.ui.templates,
diff --git a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ecommons/text/ui/presentation/AbstractTextStylesConfigurationBlock.java b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/presentation/AbstractTextStylesConfigurationBlock.java
similarity index 91%
rename from ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ecommons/text/ui/presentation/AbstractTextStylesConfigurationBlock.java
rename to ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/presentation/AbstractTextStylesConfigurationBlock.java
index 7d0debd..d390811 100644
--- a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ecommons/text/ui/presentation/AbstractTextStylesConfigurationBlock.java
+++ b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/presentation/AbstractTextStylesConfigurationBlock.java
@@ -12,7 +12,10 @@
  #     Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
  #=============================================================================*/
 
-package org.eclipse.statet.ecommons.text.ui.presentation;
+package org.eclipse.statet.ltk.ui.sourceediting.presentation;
+
+import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullAssert;
+import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullLateInit;
 
 import static org.eclipse.statet.ltk.ui.LtkUI.BUNDLE_ID;
 
@@ -56,7 +59,6 @@
 import org.eclipse.jface.viewers.ComboViewer;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.LabelProvider;
-import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.jface.viewers.ViewerCell;
 import org.eclipse.osgi.util.NLS;
@@ -74,6 +76,9 @@
 
 import org.eclipse.statet.jcommons.collections.ImCollections;
 import org.eclipse.statet.jcommons.collections.ImList;
+import org.eclipse.statet.jcommons.lang.NonNull;
+import org.eclipse.statet.jcommons.lang.NonNullByDefault;
+import org.eclipse.statet.jcommons.lang.Nullable;
 
 import org.eclipse.statet.ecommons.databinding.core.conversion.ClassTypedConverter;
 import org.eclipse.statet.ecommons.preferences.core.Preference;
@@ -82,7 +87,7 @@
 import org.eclipse.statet.ecommons.preferences.ui.OverlayStorePreference;
 import org.eclipse.statet.ecommons.preferences.ui.PreferenceStoreBeanWrapper;
 import org.eclipse.statet.ecommons.preferences.ui.RGBPref;
-import org.eclipse.statet.ecommons.text.ui.presentation.AbstractTextStylesConfigurationBlock.SyntaxNode.UseStyle;
+import org.eclipse.statet.ecommons.text.ui.presentation.ITextPresentationConstants;
 import org.eclipse.statet.ecommons.text.ui.settings.JFaceTextStyleManager;
 import org.eclipse.statet.ecommons.text.ui.settings.PreferenceStoreTextStyleManager;
 import org.eclipse.statet.ecommons.ui.util.LayoutUtils;
@@ -95,13 +100,14 @@
 import org.eclipse.statet.internal.ecommons.text.ui.Messages;
 import org.eclipse.statet.internal.ltk.ui.PreviewSourceViewer;
 import org.eclipse.statet.ltk.ui.sourceediting.SourceEditorViewerConfiguration;
+import org.eclipse.statet.ltk.ui.sourceediting.presentation.AbstractTextStylesConfigurationBlock.SyntaxNode.UseStyle;
 
 
 /**
  * Common UI to configure the text style of syntax tokens (tree, options, preview).
  */
+@NonNullByDefault
 public abstract class AbstractTextStylesConfigurationBlock extends OverlayStoreConfigurationBlock {
-	// even in text, it currently depends on ltk SourceEditorViewerConfiguration;
 	
 	
 	/**
@@ -109,7 +115,7 @@
 	 * 
 	 * Note: getter and setters in all nodes for easy DataBinding.
 	 */
-	protected static abstract class SyntaxNode extends Node {
+	protected static abstract class SyntaxNode extends Node<SyntaxNode> {
 		
 		
 		public static class UseStyle {
@@ -170,26 +176,25 @@
 		public static final String PROP_UNDERLINE= "underline"; //$NON-NLS-1$
 		
 		
-		private final String description;
+		private final @Nullable String description;
 		
 		private final ImList<UseStyle> availableStyles;
 		
-		private IPreferenceStore preferenceStore;
+		private IPreferenceStore preferenceStore= nonNullLateInit();
 		
 		private ImList<PrefProperty> preferences= ImCollections.emptyList();
 		
-		private PreferenceStoreBeanWrapper beanSupport;
+		private PreferenceStoreBeanWrapper beanSupport= nonNullLateInit();
 		
 		
-		private SyntaxNode(final String name, final String description,
-				final SyntaxNode[] children,
+		private SyntaxNode(final String name, final @Nullable String description,
+				final @Nullable ImList<SyntaxNode> children,
 				final ImList<UseStyle> availableStyles) {
 			super(name, children);
-			assert (availableStyles != null);
 			
 			this.description= description;
 			
-			this.availableStyles= availableStyles;
+			this.availableStyles= nonNullAssert(availableStyles);
 		}
 		
 		protected void setPreferences(final List<PrefProperty> prefs) {
@@ -197,7 +202,7 @@
 		}
 		
 		
-		public String getDescription() {
+		public @Nullable String getDescription() {
 			return this.description;
 		}
 		
@@ -246,10 +251,10 @@
 		public ImList<UseStyle> getAvailableUseStyles() {
 			return this.availableStyles;
 		}
-		public UseStyle getUseStyle() {
+		public @Nullable UseStyle getUseStyle() {
 			return (!this.availableStyles.isEmpty()) ? this.availableStyles.get(0) : null;
 		}
-		protected UseStyle getUseStyle(final String value) {
+		protected UseStyle getUseStyle(final @Nullable String value) {
 			for (final UseStyle style : this.availableStyles) {
 				if (style.getRefRootKey().equals(value)) {
 					return style;
@@ -260,7 +265,7 @@
 		public void setUseStyle(final UseStyle useStyle) {
 		}
 		
-		public RGB getColor() {
+		public @Nullable RGB getColor() {
 			return null;
 		}
 		public void setColor(final RGB color) {
@@ -297,8 +302,8 @@
 	 */
 	protected static class CategoryNode extends SyntaxNode {
 		
-		public CategoryNode(final String name, final SyntaxNode[] children) {
-			super(name, null, children, ImCollections.<UseStyle>emptyList());
+		public CategoryNode(final String name, final @NonNull SyntaxNode... children) {
+			super(name, null, ImCollections.newList(children), ImCollections.emptyList());
 		}
 		
 		
@@ -318,7 +323,7 @@
 				return UseStyle.class;
 			}
 			@Override
-			public UseStyle store2Usage(final String storeValue) {
+			public UseStyle store2Usage(final @Nullable String storeValue) {
 				return getUseStyle(storeValue);
 			}
 			@Override
@@ -331,8 +336,11 @@
 		
 		
 		public StyleNode(final String name, final String description, final String rootKey,
-				final UseStyle[] availableStyles, final SyntaxNode[] children) {
-			super(name, description, children, ImCollections.newList(availableStyles));
+				final ImList<UseStyle> availableStyles,
+				final @NonNull SyntaxNode @Nullable... children) {
+			super(name, description,
+					(children != null) ? ImCollections.newList(children) : null,
+					availableStyles );
 			this.rootKey= rootKey;
 			
 			final List<PrefProperty> prefs= new ArrayList<>();
@@ -466,13 +474,13 @@
 			return true;
 		}
 		@Override
-		public String getToolTipText(final Object element) {
-			return ((SyntaxNode) element).getDescription();
+		public @Nullable String getToolTipText(final Object element) {
+			return ((SyntaxNode)element).getDescription();
 		}
 		
 		@Override
 		public void update(final ViewerCell cell) {
-			cell.setText(((Node) cell.getElement()).getName());
+			cell.setText(((SyntaxNode)cell.getElement()).getName());
 		}
 		
 	}
@@ -481,41 +489,33 @@
 		
 		@Override
 		public String getText(final Object element) {
-			final UseStyle style= (UseStyle) element;
+			final UseStyle style= (UseStyle)element;
 			return style.getLabel();
 		}
 		
 	}
 	
 	
-	private static void collectKeys(final List<OverlayStorePreference> keys, final Node[] nodes) {
-		for (final Node node : nodes) {
-			if (node instanceof SyntaxNode) {
-				((SyntaxNode) node).gatherPreferenceKeys(keys);
-			}
-			final Node[] children= node.getChildren();
-			if (children != null) {
-				collectKeys(keys, children);
-			}
+	private static void collectKeys(final List<OverlayStorePreference> keys,
+			final ImList<? extends SyntaxNode> nodes) {
+		for (final var node : nodes) {
+			node.gatherPreferenceKeys(keys);
+			collectKeys(keys, node.getChildren());
 		}
 	}
 	
-	private static void connectStore(final Node[] nodes, final IPreferenceStore store) {
-		for (final Node node: nodes) {
-			if (node instanceof SyntaxNode) {
-				((SyntaxNode) node).connectPreferenceStore(store);
-			}
-			final Node[] children= node.getChildren();
-			if (children != null) {
-				connectStore(children, store);
-			}
+	private static void connectStore(final ImList<? extends SyntaxNode> nodes,
+			final IPreferenceStore store) {
+		for (final var node: nodes) {
+			node.connectPreferenceStore(store);
+			connectStore(node.getChildren(), store);
 		}
 	}
 	
 	private static final Class<List<UseStyle>> List_UseStyle_TYPE= (Class)List.class;
 	
 	
-	private SyntaxNode[] rootNodes;
+	private ImList<? extends SyntaxNode> rootNodes;
 	private DataBindingContext dbc;
 	
 	private TreeViewer selectionViewer;
@@ -540,7 +540,7 @@
 	}
 	
 	
-	protected abstract SyntaxNode[] createItems();
+	protected abstract ImList<? extends SyntaxNode> createItems();
 	protected abstract String getSettingsGroup();
 	
 	
@@ -592,7 +592,8 @@
 		configComposite.setLayout(LayoutUtils.newCompositeGrid(2));
 		{	final Control selectionControl= createTreeViewer(configComposite);
 			final GridData gd= new GridData(SWT.FILL, SWT.FILL, false, true);
-			final Point size= ViewerUtils.calculateTreeSizeHint(this.selectionViewer.getControl(), this.rootNodes, 9);
+			final Point size= ViewerUtils.calculateTreeSizeHint(this.selectionViewer.getControl(),
+					this.rootNodes, 9 );
 			gd.widthHint= size.x;
 			gd.heightHint= size.y;
 			selectionControl.setLayoutData(gd);
@@ -621,14 +622,7 @@
 		initFields();
 		initBindings();
 		
-		UIAccess.getDisplay().asyncExec(new Runnable() {
-			@Override
-			public void run() {
-				if (UIAccess.isOkToUse(AbstractTextStylesConfigurationBlock.this.selectionViewer)) {
-					AbstractTextStylesConfigurationBlock.this.selectionViewer.setSelection(new StructuredSelection(AbstractTextStylesConfigurationBlock.this.rootNodes[0]));
-				}
-			}
-		});
+		ViewerUtils.scheduleStandardSelection(this.selectionViewer);
 	}
 	
 	
@@ -789,10 +783,10 @@
 		useStyleValue.addValueChangeListener(new IValueChangeListener<UseStyle>() {
 			@Override
 			public void handleValueChange(final ValueChangeEvent<? extends UseStyle> event) {
-				final IStructuredSelection selection= (IStructuredSelection) AbstractTextStylesConfigurationBlock.this
+				final IStructuredSelection selection= (IStructuredSelection)AbstractTextStylesConfigurationBlock.this
 						.selectionViewer.getSelection();
 				final UseStyle newUse= event.diff.getNewValue();
-				updateEnablement((SyntaxNode) selection.getFirstElement(), newUse);
+				updateEnablement((SyntaxNode)selection.getFirstElement(), newUse);
 			}
 		});
 		this.dbc.bindValue(