1 19 20 package org.netbeans.modules.languages.features; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import org.netbeans.api.languages.LanguagesManager; 25 import org.netbeans.api.languages.LanguagesManager; 26 import org.netbeans.api.languages.LanguagesManager; 27 import org.netbeans.api.languages.ParseException; 28 import java.awt.Color ; 29 import org.netbeans.api.lexer.Token; 30 import org.netbeans.api.lexer.TokenHierarchy; 31 import org.netbeans.editor.DrawContext; 32 import org.netbeans.editor.DrawLayer; 33 import org.netbeans.editor.MarkFactory; 34 import java.awt.*; 35 import java.util.Map ; 36 import org.netbeans.api.lexer.TokenHierarchy; 37 import org.netbeans.api.lexer.TokenSequence; 38 import org.netbeans.api.languages.ParseException; 39 import org.netbeans.modules.editor.NbEditorDocument; 40 import org.netbeans.modules.languages.Feature; 41 import org.netbeans.modules.languages.Feature; 42 import org.netbeans.modules.languages.Language; 43 import org.netbeans.modules.languages.LanguagesManagerImpl; 44 import org.netbeans.modules.languages.LanguagesManagerImpl; 45 46 47 51 public class MySecondDrawLayer implements DrawLayer { 52 53 private String mimeType; 54 55 56 public MySecondDrawLayer (String mimeType) { 57 this.mimeType = mimeType; 58 } 59 60 public String getName () { 61 return getClass ().getName (); 62 } 63 64 public boolean extendsEOL () { 65 return true; 66 } 67 68 public boolean extendsEmptyLine () { 69 return false; 70 } 71 72 public int getNextActivityChangeOffset (DrawContext ctx) { 73 return nextActivityChangeOffset; 74 } 75 76 private TokenSequence tokenSequence; 77 78 public void init (DrawContext ctx) { 79 TokenHierarchy th = TokenHierarchy.get (ctx.getEditorUI ().getDocument ()); 80 if (th == null) return; 81 tokenSequence = th.tokenSequence (); 82 } 83 84 public int updateLineNumberContext (int lineNumber, DrawContext ctx) { 85 return lineNumber; 86 } 87 88 public String toString () { 89 return "Layer " + getClass (); } 91 92 public boolean isActive (DrawContext ctx, MarkFactory.DrawMark mark) { 93 setNextActivityChangeOffset (ctx.getTokenOffset () + ctx.getTokenLength ()); 94 return true; 95 } 96 97 public void updateContext (DrawContext ctx) { 98 NbEditorDocument doc = (NbEditorDocument) ctx.getEditorUI ().getDocument (); 99 TokenHierarchy tokenHierarchy = TokenHierarchy.get (doc); 100 if (tokenHierarchy == null) return; 101 TokenSequence ts = tokenHierarchy.tokenSequence (); 102 mark (ts, ctx.getFragmentOffset (), ctx); 103 } 104 105 108 private int nextActivityChangeOffset = Integer.MAX_VALUE; 109 110 private void setNextActivityChangeOffset (int nextActivityChangeOffset) { 111 this.nextActivityChangeOffset = nextActivityChangeOffset; 112 } 113 114 private void mark (TokenSequence ts, int offset, DrawContext ctx) { 115 ts.move (offset); 116 if (!ts.moveNext ()) return; 117 Token token = ts.token (); 118 TokenSequence ts2 = ts.embedded (); 119 if (ts2 == null) return; 120 String mimeTypeOut = ts.language ().mimeType (); 121 String mimeTypeIn = ts2.language ().mimeType (); 122 if (token.id ().name ().equals ("PE")) { 123 Color c = getPreprocessorImportsColor (mimeTypeIn); 124 if (c != null) ctx.setBackColor (c); 125 } else 126 if (!mimeTypeOut.equals (mimeTypeIn)) { 127 Color c = getTokenImportsColor (mimeTypeOut, mimeTypeIn, token.id ().name ()); 128 if (c != null) ctx.setBackColor (c); 129 } 130 mark (ts2, offset, ctx); 131 } 132 133 private Map <String ,Map <String ,Color >> tokenImportColors = new HashMap <String ,Map <String ,Color >> (); 134 135 private Color getPreprocessorImportsColor (String mimeTypeIn) { 136 if (preprocessorImportColors == null) { 137 preprocessorImportColors = new HashMap <String ,Color > (); 138 try { 139 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()). 140 getLanguage (mimeType); 141 Feature properties = l.getPreprocessorImport (); 142 if (properties != null) { 143 String mimeType = (String ) properties.getValue ("mimeType"); 144 Color color = ColorsManager.readColor ( 145 (String ) properties.getValue ("background_color") 146 ); 147 if (color != null) 148 preprocessorImportColors.put (mimeType, color); 149 } 150 } catch (ParseException ex) { 151 } 152 } 153 return preprocessorImportColors.get (mimeTypeIn); 154 } 155 156 private Map <String ,Color > preprocessorImportColors; 157 158 private Color getTokenImportsColor (String mimeTypeOut, String mimeTypeIn, String tokenTypeIn) { 159 Map <String ,Color > m = tokenImportColors.get (mimeTypeOut); 160 if (m == null) { 161 m = new HashMap <String ,Color > (); 162 tokenImportColors.put (mimeTypeOut, m); 163 try { 164 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()). 165 getLanguage (mimeTypeOut); 166 Map <String ,Feature> m2 = l.getTokenImports (); 167 Iterator <String > it = m2.keySet ().iterator (); 168 while (it.hasNext ()) { 169 String tokenType = it.next (); 170 Feature properties = m2.get (tokenType); 171 Color color = ColorsManager.readColor ( 172 (String ) properties.getValue ("background_color") 173 ); 174 if (color != null) 175 m.put (tokenType, color); 176 } 177 } catch (ParseException ex) { 178 } 179 } 180 if (m.containsKey (tokenTypeIn)) 181 return m.get (tokenTypeIn); 182 return m.get (mimeTypeIn); 183 } 184 } 185 186 187 | Popular Tags |