Commit 35785331 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Whitespace handling for syntax highlighting prefs preview


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent b68f64d0
/******************************************************************************
* Copyright (c) 2000-2022 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
package org.eclipse.titan.designer.preferences;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.swt.graphics.Color;
import org.eclipse.ui.PlatformUI;
import org.osgi.service.prefs.Preferences;
/**
* Helper class for handling different preferences
*
* @author Miklos Magyari
*/
public final class PreferenceHandler {
private static final String EDITOR_BACKGROUND_NODE = "org.eclipse.ui.editors";
private static final String EDITOR_BACKGROUND_ENTRY = "AbstractTextEditor.Color.Background";
public static String getPreferenceEntry(final String node, final String entry) {
Preferences preferences = InstanceScope.INSTANCE.getNode(node);
if (preferences == null) {
return null;
}
String value = preferences.get(entry, null);
if (value == null) {
preferences = DefaultScope.INSTANCE.getNode(node);
value= preferences.get(entry, null);
}
return value;
}
public static Color getEditorBackgroundColor() {
final String color = getPreferenceEntry(EDITOR_BACKGROUND_NODE, EDITOR_BACKGROUND_ENTRY);
if (color == null) {
return null;
}
return new Color(PlatformUI.getWorkbench().getDisplay(), StringConverter.asRGB(color));
}
}
......@@ -12,15 +12,12 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.ColorFieldEditor;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelectionChangedListener;
......@@ -40,11 +37,10 @@ import org.eclipse.titan.designer.Activator;
import org.eclipse.titan.designer.AST.TTCN3.definitions.ICommentable;
import org.eclipse.titan.designer.editors.ColorManager;
import org.eclipse.titan.designer.preferences.PreferenceConstants;
import org.eclipse.titan.designer.preferences.PreferenceHandler;
import org.eclipse.titan.designer.preferences.PreferenceInitializer;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;
import org.osgi.service.prefs.Preferences;
/**
* This preference page hold the controls and functionality related to syntax
......@@ -480,14 +476,9 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
textViewer = new StyledText(colorEditorsComposite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.READ_ONLY);
textViewer.setEditable(false);
textViewer.setLayoutData(wordsGridData);
Preferences preferences = InstanceScope.INSTANCE.getNode("org.eclipse.ui.editors");
String bgColor = preferences.get("AbstractTextEditor.Color.Background", null);
if (bgColor == null) {
preferences = DefaultScope.INSTANCE.getNode("org.eclipse.ui.editors");
bgColor = preferences.get("AbstractTextEditor.Color.Background", null);
}
final Color bgColor = PreferenceHandler.getEditorBackgroundColor();
if (bgColor != null) {
textViewer.setBackground(new Color(PlatformUI.getWorkbench().getDisplay(), StringConverter.asRGB(bgColor)));
textViewer.setBackground(bgColor);
}
textViewer.setFont(JFaceResources.getTextFont());
addTemplateText(PreferenceConstants.COLOR_COMMENTS, "/**\n"
......@@ -497,34 +488,37 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
+ " * @author AK, MM\n"
+ " */\n");
addTemplateText(PreferenceConstants.COLOR_COMMENTS, "// TTCN-3 version of \"Hello, world!\"\n");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "module ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "MyExample ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "{\n");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "\ttype port ");
addTemplateText(PreferenceConstants.COLOR_AST_DEFTYPE, "PCOType ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "message ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "{\n");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "\t\tinout charstring ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, ";\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "}\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "type component ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "MTCType ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "{\n");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "\tport ");
addTemplateText(PreferenceConstants.COLOR_AST_DEFTYPE, "PCOType ");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "module", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "MyExample", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "{", "\n\t");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "type", " ");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "port", " ");
addTemplateText(PreferenceConstants.COLOR_AST_DEFTYPE, "PCOType", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "message", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "{", "\n\t\t");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "inout", " ");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "charstring");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, ";", "\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "}", "\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "type", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "component", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "MTCType", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "{", "\n\t");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "port", " ");
addTemplateText(PreferenceConstants.COLOR_AST_DEFTYPE, "PCOType", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "MyPCO_PT");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, ";\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "}\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "type record ");
addTemplateText(PreferenceConstants.COLOR_AST_DEFTYPE, "MyRecord ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "{\n");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "\tinteger ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, ";", "\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "}", "\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "type", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "record", " ");
addTemplateText(PreferenceConstants.COLOR_AST_DEFTYPE, "MyRecord", " ");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "{", "\n\t");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "integer", " ");
addTemplateText(PreferenceConstants.COLOR_AST_VARIABLE, "x1");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, ",\n");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "\tcharstring ");
addTemplateText(PreferenceConstants.COLOR_AST_VARIABLE, "c2");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "}\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, ",", "\n\t");
addTemplateText(PreferenceConstants.COLOR_TTCN3_KEYWORDS, "charstring", " ");
addTemplateText(PreferenceConstants.COLOR_AST_VARIABLE, "c2", "\n");
addTemplateText(PreferenceConstants.COLOR_NORMAL_TEXT, "}", "\n");
updateStyleRanges();
}
......@@ -680,6 +674,10 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
}
private void addTemplateText(String key, String text) {
addTemplateText(key, text, null);
}
private void addTemplateText(String key, String text, String optWhiteSpace) {
int start = textViewer.getText().length();
textViewer.append(text);
final StyleRange range = colorManager.createStyleRangeFromPreference(key, Activator.getDefault().getPreferenceStore(), start, text.length());
......@@ -687,8 +685,12 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
List<StyleRange> list = rangeMap.containsKey(key) ? rangeMap.get(key) : new ArrayList<StyleRange>();
list.add(range);
rangeMap.put(key, list);
if (optWhiteSpace != null) {
textViewer.append(optWhiteSpace);
}
}
private void updateStyleRanges() {
try {
for (Map.Entry<String,List<StyleRange>> entry : rangeMap.entrySet()) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment