Commit 8bfed157 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Improved code preview for syntax highlighting color preferences


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 68789a9d
......@@ -56,7 +56,7 @@ public final class ColorManager {
* the system.
*/
public Color getForegroundColor(final String aName, final IPreferenceStore store) {
Color color = COLOR_TABLE.get(aName);
Color color = store == Activator.getDefault().getPreferenceStore() ? COLOR_TABLE.get(aName) : null;
if (color == null) {
final RGB rgb = getColor(store, aName);
if (rgb == null) {
......@@ -64,7 +64,9 @@ public final class ColorManager {
} else {
color = new Color(getDisplay(), rgb);
}
COLOR_TABLE.put(aName, color);
if (store == Activator.getDefault().getPreferenceStore()) {
COLOR_TABLE.put(aName, color);
}
}
return color;
}
......@@ -78,7 +80,7 @@ public final class ColorManager {
* or if one can not be found, then white.
*/
public Color getBackgroundColor(final String aName, final IPreferenceStore store) {
Color color = COLOR_TABLE.get(aName);
Color color = store == Activator.getDefault().getPreferenceStore() ? COLOR_TABLE.get(aName) : null;
if (color == null) {
final RGB rgb = getColor(store, aName);
if (rgb == null) {
......@@ -86,7 +88,9 @@ public final class ColorManager {
} else {
color = new Color(getDisplay(), rgb);
}
COLOR_TABLE.put(aName, color);
if (store == Activator.getDefault().getPreferenceStore()) {
COLOR_TABLE.put(aName, color);
}
}
return color;
}
......
......@@ -7,7 +7,9 @@
******************************************************************************/
package org.eclipse.titan.designer.preferences.pages;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jface.preference.BooleanFieldEditor;
......@@ -22,6 +24,7 @@ import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
......@@ -41,6 +44,8 @@ import org.eclipse.ui.IWorkbenchPreferencePage;
* coloring.
*
* @author Kristof Szabados
* @author Adam Knapp
* @author Miklos Magyari
*/
public final class SyntaxHighlightPage extends PreferencePage implements IWorkbenchPreferencePage {
private static final String TEMPLATE_CODE =
......@@ -88,6 +93,7 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
+ " execute(tc_HelloW());\n"
+ "}\n"
+ "}\n";
private Map<String,List<StyleRange>> rangeMap = new HashMap<>();
private static final String ENABLE_SEMANTIC_HIGHLIGHTING = "Enable semantic highlighting (EXPERIMENTAL)";
private Composite pageComposite;
......@@ -172,9 +178,7 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
isUnderline.load();
if (element.getWords() != null) {
textViewer.setText(element.getWords());
textViewer.setStyleRange(colorManager.createStyleRangeFromPreference(
element.getBasePreferenceKey(), tempstore, 0, element.getWords().length()));
updateStyleRanges();
} else {
textViewer.setText("");
}
......@@ -331,8 +335,7 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
possiblyChangedPreferences.put(foregroundColorEditor.getPreferenceName(), null);
foregroundColorEditor.store();
if (element != null) {
textViewer.setStyleRange(colorManager.createStyleRangeFromPreference(
element.getBasePreferenceKey(), tempstore, 0, element.getWords().length()));
updateStyleRanges();
}
}
});
......@@ -351,8 +354,7 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
possiblyChangedPreferences.put(useBackgroundColor.getPreferenceName(), null);
useBackgroundColor.store();
backgroundColorEditor.setEnabled(useBackgroundColor.getBooleanValue(), colorEditorsComposite);
textViewer.setStyleRange(colorManager.createStyleRangeFromPreference(
element.getBasePreferenceKey(), tempstore, 0, element.getWords().length()));
updateStyleRanges();
}
});
......@@ -370,8 +372,7 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
possiblyChangedPreferences.put(backgroundColorEditor.getPreferenceName(), null);
backgroundColorEditor.store();
if (element != null) {
textViewer.setStyleRange(colorManager.createStyleRangeFromPreference(
element.getBasePreferenceKey(), tempstore, 0, element.getWords().length()));
updateStyleRanges();
}
}
});
......@@ -390,8 +391,7 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
possiblyChangedPreferences.put(isBold.getPreferenceName(), null);
isBold.store();
if (element != null) {
textViewer.setStyleRange(colorManager.createStyleRangeFromPreference(
element.getBasePreferenceKey(), tempstore, 0, element.getWords().length()));
updateStyleRanges();
}
}
});
......@@ -410,8 +410,7 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
possiblyChangedPreferences.put(isItalic.getPreferenceName(), null);
isItalic.store();
if (element != null) {
textViewer.setStyleRange(colorManager.createStyleRangeFromPreference(
element.getBasePreferenceKey(), tempstore, 0, element.getWords().length()));
updateStyleRanges();
}
}
});
......@@ -430,8 +429,7 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
possiblyChangedPreferences.put(isStrikethrough.getPreferenceName(), null);
isStrikethrough.store();
if (element != null) {
textViewer.setStyleRange(colorManager.createStyleRangeFromPreference(
element.getBasePreferenceKey(), tempstore, 0, element.getWords().length()));
updateStyleRanges();
}
}
});
......@@ -450,8 +448,7 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
possiblyChangedPreferences.put(isUnderline.getPreferenceName(), null);
isUnderline.store();
if (element != null) {
textViewer.setStyleRange(colorManager.createStyleRangeFromPreference(
element.getBasePreferenceKey(), tempstore, 0, element.getWords().length()));
updateStyleRanges();
}
}
});
......@@ -478,6 +475,41 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
textViewer.setLayoutData(wordsGridData);
textViewer.setBackground(getShell().getBackground());
textViewer.setFont(getFont());
addTemplateText(PreferenceConstants.COLOR_COMMENTS, "/**\n"
+ " * @desc Demo module\n"
+ " * @status deprecated\n"
+ " * @url https://projects.eclipse.org/projects/tools.titan\n"
+ " * @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_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_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");
}
private void createSemanticHighlightingCheckbox(final Composite parent) {
......@@ -630,4 +662,30 @@ public final class SyntaxHighlightPage extends PreferencePage implements IWorkbe
return root;
}
private void addTemplateText(String key, String text) {
int start = textViewer.getText().length();
textViewer.append(text);
final StyleRange range = colorManager.createStyleRangeFromPreference(key, tempstore, start, text.length());
textViewer.setStyleRange(range);
List<StyleRange> list = rangeMap.containsKey(key) ? rangeMap.get(key) : new ArrayList<StyleRange>();
list.add(range);
rangeMap.put(key, list);
}
private void updateStyleRanges() {
try {
for (Map.Entry<String,List<StyleRange>> entry : rangeMap.entrySet()) {
if (entry.getKey().equals(element.getBasePreferenceKey())) {
final List<StyleRange> list = entry.getValue();
for (StyleRange oldRange : list) {
textViewer.setStyleRange(colorManager.createStyleRangeFromPreference(
element.getBasePreferenceKey(), tempstore, oldRange.start, oldRange.length));
}
}
}
} catch (Exception e) {
}
}
}
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