1 7 8 package com.sun.java.swing.plaf.windows; 9 10 import java.awt.Color ; 11 import java.awt.Graphics ; 12 import java.awt.Rectangle ; 13 import java.awt.Shape ; 14 import javax.swing.plaf.basic.*; 15 import javax.swing.*; 16 import javax.swing.plaf.TextUI ; 17 import javax.swing.plaf.UIResource ; 18 import javax.swing.text.*; 19 20 30 public abstract class WindowsTextUI extends BasicTextUI { 31 39 protected Caret createCaret() { 40 return new WindowsCaret(); 41 } 42 43 44 static LayeredHighlighter.LayerPainter WindowsPainter = new WindowsHighlightPainter(null); 45 46 47 static class WindowsCaret extends DefaultCaret 48 implements UIResource { 49 54 protected Highlighter.HighlightPainter getSelectionPainter() { 55 return WindowsTextUI.WindowsPainter; 56 } 57 } 58 59 60 static class WindowsHighlightPainter extends 61 DefaultHighlighter.DefaultHighlightPainter { 62 WindowsHighlightPainter(Color c) { 63 super(c); 64 } 65 66 68 77 public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) { 78 Rectangle alloc = bounds.getBounds(); 79 try { 80 TextUI mapper = c.getUI(); 82 Rectangle p0 = mapper.modelToView(c, offs0); 83 Rectangle p1 = mapper.modelToView(c, offs1); 84 85 Color color = getColor(); 87 88 if (color == null) { 89 g.setColor(c.getSelectionColor()); 90 } 91 else { 92 g.setColor(color); 93 } 94 boolean firstIsDot = false; 95 boolean secondIsDot = false; 96 if (c.isEditable()) { 97 int dot = c.getCaretPosition(); 98 firstIsDot = (offs0 == dot); 99 secondIsDot = (offs1 == dot); 100 } 101 if (p0.y == p1.y) { 102 Rectangle r = p0.union(p1); 104 if (r.width > 0) { 105 if (firstIsDot) { 106 r.x++; 107 r.width--; 108 } 109 else if (secondIsDot) { 110 r.width--; 111 } 112 } 113 g.fillRect(r.x, r.y, r.width, r.height); 114 } else { 115 int p0ToMarginWidth = alloc.x + alloc.width - p0.x; 117 if (firstIsDot && p0ToMarginWidth > 0) { 118 p0.x++; 119 p0ToMarginWidth--; 120 } 121 g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height); 122 if ((p0.y + p0.height) != p1.y) { 123 g.fillRect(alloc.x, p0.y + p0.height, alloc.width, 124 p1.y - (p0.y + p0.height)); 125 } 126 if (secondIsDot && p1.x > alloc.x) { 127 p1.x--; 128 } 129 g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height); 130 } 131 } catch (BadLocationException e) { 132 } 134 } 135 136 149 public Shape paintLayer(Graphics g, int offs0, int offs1, 150 Shape bounds, JTextComponent c, View view) { 151 Color color = getColor(); 152 153 if (color == null) { 154 g.setColor(c.getSelectionColor()); 155 } 156 else { 157 g.setColor(color); 158 } 159 boolean firstIsDot = false; 160 boolean secondIsDot = false; 161 if (c.isEditable()) { 162 int dot = c.getCaretPosition(); 163 firstIsDot = (offs0 == dot); 164 secondIsDot = (offs1 == dot); 165 } 166 if (offs0 == view.getStartOffset() && 167 offs1 == view.getEndOffset()) { 168 Rectangle alloc; 170 if (bounds instanceof Rectangle ) { 171 alloc = (Rectangle )bounds; 172 } 173 else { 174 alloc = bounds.getBounds(); 175 } 176 if (firstIsDot && alloc.width > 0) { 177 g.fillRect(alloc.x + 1, alloc.y, alloc.width - 1, 178 alloc.height); 179 } 180 else if (secondIsDot && alloc.width > 0) { 181 g.fillRect(alloc.x, alloc.y, alloc.width - 1, 182 alloc.height); 183 } 184 else { 185 g.fillRect(alloc.x, alloc.y, alloc.width, alloc.height); 186 } 187 return alloc; 188 } 189 else { 190 try { 192 Shape shape = view.modelToView(offs0, Position.Bias.Forward, 194 offs1,Position.Bias.Backward, 195 bounds); 196 Rectangle r = (shape instanceof Rectangle ) ? 197 (Rectangle )shape : shape.getBounds(); 198 if (firstIsDot && r.width > 0) { 199 g.fillRect(r.x + 1, r.y, r.width - 1, r.height); 200 } 201 else if (secondIsDot && r.width > 0) { 202 g.fillRect(r.x, r.y, r.width - 1, r.height); 203 } 204 else { 205 g.fillRect(r.x, r.y, r.width, r.height); 206 } 207 return r; 208 } catch (BadLocationException e) { 209 } 211 } 212 return null; 214 } 215 216 } 217 218 } 219 220 | Popular Tags |