EOS 2  1.1.0
Einfache Objektbasierte Sprache
CodeColoring.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.common.gui;
2 
3 import java.awt.Color;
4 import java.util.LinkedList;
5 
6 import javax.swing.SwingUtilities;
7 import javax.swing.text.SimpleAttributeSet;
8 import javax.swing.text.StyleConstants;
9 
10 import de.lathanda.eos.base.util.GuiToolkit;
11 import de.lathanda.eos.baseparser.InfoToken;
12 import de.lathanda.eos.vm.Marker;
13 
14 
21 public class CodeColoring implements CodeColorHook {
25  private final SimpleAttributeSet attributeSetBase = new SimpleAttributeSet();
29  private final SimpleAttributeSet attributeSetMark = new SimpleAttributeSet();
33  private final SimpleAttributeSet attributeSetUnmark = new SimpleAttributeSet();
37  private final SimpleAttributeSet attributeSetError = new SimpleAttributeSet();
41  private final SimpleAttributeSet attributeSetComment = new SimpleAttributeSet();
45  private final SimpleAttributeSet attributeSetLiteral = new SimpleAttributeSet();
49  private final SimpleAttributeSet attributeSetKeyword = new SimpleAttributeSet();
53  private SourceCode sourceCode;
57  private Marker newCodePointer;
61  private Marker codePointer;
65  private boolean codePointerDirty = false;
66 
70  public CodeColoring() {
71  this.codePointer = new Marker();
72  StyleConstants.setBackground(attributeSetBase, Color.WHITE);
73  StyleConstants.setForeground(attributeSetBase, Color.BLACK);
74  StyleConstants.setUnderline(attributeSetBase, false);
75  StyleConstants.setBold(attributeSetBase, false);
76  StyleConstants.setForeground(attributeSetComment, new Color(0, 160, 0));
77  StyleConstants.setForeground(attributeSetLiteral, Color.BLUE);
78  StyleConstants.setForeground(attributeSetKeyword, Color.BLACK);
79  StyleConstants.setForeground(attributeSetError, Color.RED);
80  StyleConstants.setBold(attributeSetKeyword, true);
81  StyleConstants.setBackground(attributeSetMark, Color.YELLOW);
82  StyleConstants.setBackground(attributeSetUnmark, Color.WHITE);
83  }
84 
89  @Override
90  public void setFontSize(int size) {
91  StyleConstants.setFontSize(attributeSetBase, GuiToolkit.getFontSize(size));
92  StyleConstants.setFontSize(attributeSetComment, GuiToolkit.getFontSize(size));
93  StyleConstants.setFontSize(attributeSetLiteral, GuiToolkit.getFontSize(size));
94  StyleConstants.setFontSize(attributeSetKeyword, GuiToolkit.getFontSize(size));
95  StyleConstants.setFontSize(attributeSetMark, GuiToolkit.getFontSize(size));
96  StyleConstants.setFontSize(attributeSetUnmark, GuiToolkit.getFontSize(size));
97  }
98 
102  @Override
103  public void doColoring() {
104  if (sourceCode.getProgram() == null) {
105  sourceCode.setCharacterAttributes(0, sourceCode.getLength(), attributeSetBase, true);
106  return;
107  }
108  LinkedList<InfoToken> tokens = sourceCode.getProgram().getTokenList();
109  // remove previous coloring
110  // change size
111  sourceCode.setCharacterAttributes(0, sourceCode.getLength(), attributeSetBase, true);
112  for (InfoToken t : tokens) {
113  switch (t.getFormat()) {
114  case COMMENT:
115  sourceCode.setCharacterAttributes(t.getBegin(), t.getLength(), attributeSetComment, true);
116  break;
117  case LITERAL:
118  sourceCode.setCharacterAttributes(t.getBegin(), t.getLength(), attributeSetLiteral, true);
119  break;
120  case KEYWORD:
121  sourceCode.setCharacterAttributes(t.getBegin(), t.getLength(), attributeSetKeyword, true);
122  break;
123  case PLAIN:
124  // no formating
125  default:
126  // no formating
127  }
128  }
129  }
130 
134  private synchronized void markExecutionPoint() {
135  if (codePointer != null) {
136  sourceCode.setCharacterAttributes(codePointer.getBeginPosition(), codePointer.getLength(),
137  attributeSetUnmark, false);
138  }
139  codePointer = newCodePointer;
140  codePointerDirty = false;
141 
142  if (codePointer != null) {
143  sourceCode.setCharacterAttributes(codePointer.getBeginPosition(), codePointer.getLength(), attributeSetMark,
144  false);
145  }
146  }
147 
151  @Override
152  public synchronized void unmarkExecutionPoint() {
153  if (codePointer != null) {
154  int begin = codePointer.getBeginPosition();
155  int length = codePointer.getLength();
156  sourceCode.setCharacterAttributes(begin, length, attributeSetUnmark, false);
157  }
158  }
159 
164  @Override
165  public void markError(Marker code) {
166  sourceCode.setCharacterAttributes(code.getBeginPosition(), code.getLength(), attributeSetError, false);
167  }
168 
176  @Override
177  public synchronized void markExecutionPoint(Marker codeRange) {
178  newCodePointer = codeRange;
179  if (!codePointerDirty) {
180  codePointerDirty = true;
181  SwingUtilities.invokeLater(() -> markExecutionPoint());
182  }
183  }
184 
185  @Override
186  public void init(SourceCode source) {
187  this.sourceCode = source;
188  }
189 }
static int getFontSize(int size)
Definition: GuiToolkit.java:93
synchronized void markExecutionPoint(Marker codeRange)
LinkedList< InfoToken > getTokenList()
Impressum