1 21 22 package org.armedbear.j; 23 24 import java.awt.Color ; 25 import java.awt.Font ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 public abstract class Formatter implements Constants 30 { 31 protected Buffer buffer; 32 33 protected FormatTable formatTable; 34 protected Color colorCaret; 35 protected Color colorBackground; 36 protected Color colorCurrentLineBackground; 37 protected Color colorSelectionBackground; 38 protected Color colorMatchingBracketBackground; 39 40 protected LineSegmentList segmentList = new LineSegmentList(); 41 42 public abstract LineSegmentList formatLine(Line line); 43 public abstract FormatTable getFormatTable(); 44 45 public boolean parseBuffer() 46 { 47 buffer.setNeedsParsing(false); 48 return false; 49 } 50 51 protected final boolean isKeyword(String s) 52 { 53 return buffer.isKeyword(s); 54 } 55 56 public Color getCaretColor() 57 { 58 if (colorCaret == null) { 59 colorCaret = buffer.getMode().getColorProperty(Property.COLOR_CARET); 60 if (colorCaret == null) { 61 colorCaret = buffer.getMode().getColorProperty(Property.COLOR_TEXT); 62 if (colorCaret == null) 63 colorCaret = DefaultTheme.getColor("caret"); 64 } 65 } 66 return colorCaret; 67 } 68 69 public Color getBackgroundColor() 70 { 71 if (colorBackground == null) { 72 colorBackground = buffer.getMode().getColorProperty(Property.COLOR_BACKGROUND); 73 if (colorBackground == null) 74 colorBackground = DefaultTheme.getColor("background"); 75 } 76 return colorBackground; 77 } 78 79 public Color getCurrentLineBackgroundColor() 80 { 81 if (colorCurrentLineBackground == null) { 82 colorCurrentLineBackground = buffer.getMode().getColorProperty(Property.COLOR_CURRENT_LINE_BACKGROUND); 83 if (colorCurrentLineBackground == null) 84 colorCurrentLineBackground = DefaultTheme.getColor("currentLineBackground"); 85 } 86 return colorCurrentLineBackground; 87 } 88 89 public Color getSelectionBackgroundColor() 90 { 91 if (colorSelectionBackground == null) { 92 colorSelectionBackground = buffer.getMode().getColorProperty(Property.COLOR_SELECTION_BACKGROUND); 93 if (colorSelectionBackground == null) 94 colorSelectionBackground = DefaultTheme.getColor("selectionBackground"); 95 } 96 return colorSelectionBackground; 97 } 98 99 public Color getMatchingBracketBackgroundColor() 100 { 101 if (colorMatchingBracketBackground == null) { 102 colorMatchingBracketBackground = buffer.getMode().getColorProperty(Property.COLOR_MATCHING_BRACKET_BACKGROUND); 103 if (colorMatchingBracketBackground == null) 104 colorMatchingBracketBackground = DefaultTheme.getColor("matchingBracketBackground"); 105 } 106 return colorMatchingBracketBackground; 107 } 108 109 public Color getColor(int format) 110 { 111 FormatTableEntry entry = getFormatTable().lookup(format); 112 if (entry != null) 113 return entry.getColor(); 114 return DefaultTheme.getColor("text"); 115 } 116 117 public int getStyle(int format) 118 { 119 FormatTableEntry entry = getFormatTable().lookup(format); 120 if (entry != null) 121 return entry.getStyle(); 122 return Font.PLAIN; 123 } 124 125 public FormatTableEntry getFormatTableEntry(int format) 126 { 127 return getFormatTable().lookup(format); 128 } 129 130 public boolean getUnderline(int format) 131 { 132 return false; 133 } 134 135 public void reset() 136 { 137 colorCaret = null; 138 colorBackground = null; 139 colorCurrentLineBackground = null; 140 colorSelectionBackground = null; 141 colorMatchingBracketBackground = null; 142 formatTable = null; 143 } 144 145 protected final void addSegment(String text, int begin, int end, int format) 146 { 147 segmentList.addSegment(new LineSegment(text, begin, end, format)); 148 } 149 150 protected final void addSegment(String text, int begin, int format) 151 { 152 segmentList.addSegment(new LineSegment(text, begin, text.length(), format)); 153 } 154 155 protected final void addSegment(String text, int format) 156 { 157 segmentList.addSegment(new LineSegment(text, format)); 158 } 159 160 protected final LineSegment getLastSegment() 161 { 162 return segmentList.getLastSegment(); 163 } 164 165 protected final void clearSegmentList() 166 { 167 segmentList.clear(); 168 } 169 170 protected final String getDetabbedText(Line line) 171 { 172 if (Editor.tabsAreVisible()) 173 return Utilities.makeTabsVisible(line.getText(), buffer.getTabWidth()); 174 return Utilities.detab(line.getText(), buffer.getTabWidth()); 175 } 176 } 177 | Popular Tags |