diff --git a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/.ratings b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/.ratings index c86544c8f773cff92c7e897799fb6592ec6352d0..2d1b98a8f4ee06c2a92ac72d47ec75a6dbf1b2d2 100644 --- a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/.ratings +++ b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/.ratings @@ -8,5 +8,6 @@ DiagramKeyHandler.java cfd15ac8f9fc933739cef5e7039960e19826d1ce GREEN FormsEditorBase.java 4046d340913d951340084ae7240d79f8e75cb8d4 GREEN GEFEditorBase.java e668f596f45f07215994cbbd3929a9438331718f GREEN SourceEditorBase.java 347d6b45d577abd7e23a7234904be118e82469f1 YELLOW +SourceEditorConfigurationBase.java db3898fe1cace33aab0dd83d9711205c235c9861 YELLOW SourceEditorUndoRedo.java 5f5d1b05c8b1287a9e37d866eda3474b6dcbd014 YELLOW TreeViewerEditorBase.java 1c59689ff57c4f3cc180d85f13021fc03461ecb0 GREEN diff --git a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/SourceEditorConfigurationBase.java b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/SourceEditorConfigurationBase.java new file mode 100644 index 0000000000000000000000000000000000000000..db3898fe1cace33aab0dd83d9711205c235c9861 --- /dev/null +++ b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/SourceEditorConfigurationBase.java @@ -0,0 +1,121 @@ +/*-------------------------------------------------------------------------+ +| Copyright 2012 fortiss GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.editor; + +import static org.eclipse.jface.text.IDocument.DEFAULT_CONTENT_TYPE; +import static org.eclipse.wb.swt.SWTResourceManager.getColor; + +import java.util.List; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.jface.text.presentation.IPresentationReconciler; +import org.eclipse.jface.text.presentation.PresentationReconciler; +import org.eclipse.jface.text.rules.DefaultDamagerRepairer; +import org.eclipse.jface.text.rules.IRule; +import org.eclipse.jface.text.rules.IToken; +import org.eclipse.jface.text.rules.ITokenScanner; +import org.eclipse.jface.text.rules.IWordDetector; +import org.eclipse.jface.text.rules.RuleBasedScanner; +import org.eclipse.jface.text.rules.WordRule; +import org.eclipse.jface.text.source.IAnnotationHover; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.source.SourceViewerConfiguration; +import org.eclipse.swt.graphics.Color; +import org.fortiss.tooling.base.ui.editor.SourceEditorBase; +import org.fortiss.tooling.base.ui.editor.annotations.AnnotationHover; + +/** + * Class responsible for proper configuration of the {@link SourceEditorBase}. + * + * @author doebber + */ +public abstract class SourceEditorConfigurationBase<T extends EObject> + extends SourceViewerConfiguration { + + /** Stores the actual editor object. */ + protected SourceEditorBase<T> editor; + + /** Constructor. */ + public SourceEditorConfigurationBase(SourceEditorBase<T> editor) { + this.editor = editor; + } + + /** Color constant used to display code dark red. */ + protected Color DARK_RED = getColor(128, 0, 0); + + /** Color constant used to display code dark blue. */ + protected Color DARK_BLUE = getColor(0, 0, 128); + + /** Detector used to create the scanners. */ + protected IWordDetector detector = new IWordDetector() { + /** {@inheritDoc} */ + @Override + public boolean isWordStart(char c) { + return Character.isLetterOrDigit(c); + } + + /** {@inheritDoc} */ + @Override + public boolean isWordPart(char c) { + return Character.isLetterOrDigit(c) || c == '_'; + } + }; + + /** {@inheritDoc} */ + @Override + public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { + PresentationReconciler reconciler = new PresentationReconciler(); + + DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getScanner()); + reconciler.setDamager(dr, DEFAULT_CONTENT_TYPE); + reconciler.setRepairer(dr, DEFAULT_CONTENT_TYPE); + + return reconciler; + } + + /** Returns the scanner. */ + private ITokenScanner getScanner() { + return getScannerForSyntaxHighlighting(); + } + + /** Returns the annotation hover. */ + @Override + public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { + return new AnnotationHover(editor.getAnnotationModel()); + } + + /** Returns the rule based scanner. */ + private RuleBasedScanner getScannerForSyntaxHighlighting() { + List<WordRule> rules = getCommonRules(); + rules.add(getRuleSpecificToEditor()); + RuleBasedScanner scanner = new RuleBasedScanner(); + scanner.setRules(rules.toArray(new IRule[0])); + return scanner; + } + + /** Returns a list of rules common for the syntax highlighting. */ + abstract protected List<WordRule> getCommonRules(); + + /** Returns a rule specific to the editor. */ + abstract protected WordRule getRuleSpecificToEditor(); + + /** Adds the words to the rule with the given token. */ + protected void addWordsToRule(List<String> words, WordRule rule, IToken token) { + for(String word : words) { + rule.addWord(word, token); + } + } +}