1 18 19 package org.apache.batik.swing.gvt; 20 21 import java.awt.Color ; 22 import java.awt.Cursor ; 23 import java.awt.Graphics ; 24 import java.awt.Graphics2D ; 25 import java.awt.Rectangle ; 26 import java.awt.Shape ; 27 import java.awt.geom.AffineTransform ; 28 29 import org.apache.batik.gvt.Selectable; 30 import org.apache.batik.gvt.event.AWTEventDispatcher; 31 import org.apache.batik.gvt.event.GraphicsNodeMouseEvent; 32 import org.apache.batik.gvt.event.GraphicsNodeMouseListener; 33 import org.apache.batik.gvt.event.SelectionEvent; 34 import org.apache.batik.gvt.event.SelectionListener; 35 import org.apache.batik.gvt.text.ConcreteTextSelector; 36 import org.apache.batik.gvt.text.Mark; 37 38 44 public class TextSelectionManager { 45 46 49 public final static Cursor TEXT_CURSOR = new Cursor (Cursor.TEXT_CURSOR); 50 51 54 protected ConcreteTextSelector textSelector; 55 56 59 protected JGVTComponent component; 60 61 64 protected Overlay selectionOverlay = new SelectionOverlay(); 65 66 69 protected MouseListener mouseListener; 70 71 74 protected Cursor previousCursor; 75 76 79 protected Shape selectionHighlight; 80 81 84 protected SelectionListener textSelectionListener; 85 86 89 protected Color selectionOverlayColor = new Color (100, 100, 255, 100); 90 91 94 protected Color selectionOverlayStrokeColor = new Color (255, 255, 255, 255); 95 96 100 protected boolean xorMode = false; 101 102 105 Object selection = null; 106 107 110 public TextSelectionManager(JGVTComponent comp, 111 AWTEventDispatcher ed) { 112 textSelector = new ConcreteTextSelector(); 113 textSelectionListener = new TextSelectionListener(); 114 textSelector.addSelectionListener(textSelectionListener); 115 mouseListener = new MouseListener(); 116 117 component = comp; 118 component.getOverlays().add(selectionOverlay); 119 120 ed.addGraphicsNodeMouseListener(mouseListener); 121 } 122 123 127 public void addSelectionListener(SelectionListener sl) { 128 textSelector.addSelectionListener(sl); 129 } 130 131 135 public void removeSelectionListener(SelectionListener sl) { 136 textSelector.removeSelectionListener(sl); 137 } 138 139 144 public void setSelectionOverlayColor(Color color) { 145 this.selectionOverlayColor = color; 146 } 147 148 151 public Color getSelectionOverlayColor() { 152 return selectionOverlayColor; 153 } 154 155 161 public void setSelectionOverlayStrokeColor(Color color) { 162 this.selectionOverlayStrokeColor = color; 163 } 164 165 168 public Color getSelectionOverlayStrokeColor() { 169 return selectionOverlayStrokeColor; 170 } 171 172 178 public void setSelectionOverlayXORMode(boolean state) { 179 this.xorMode = state; 180 } 181 182 186 public boolean isSelectionOverlayXORMode() { 187 return xorMode; 188 } 189 190 193 public Overlay getSelectionOverlay() { 194 return selectionOverlay; 195 } 196 197 200 public Object getSelection() { 201 return selection; 202 } 203 204 207 public void setSelection(Mark start, Mark end) { 208 textSelector.setSelection(start, end); 209 } 210 211 214 public void clearSelection() { 215 textSelector.clearSelection(); 216 } 217 218 221 protected class MouseListener implements GraphicsNodeMouseListener { 222 public void mouseClicked(GraphicsNodeMouseEvent evt) { 223 if (evt.getSource() instanceof Selectable) { 224 textSelector.mouseClicked(evt); 225 } 226 } 227 228 public void mousePressed(GraphicsNodeMouseEvent evt) { 229 if (evt.getSource() instanceof Selectable) { 230 textSelector.mousePressed(evt); 231 } else if (selectionHighlight != null) { 232 textSelector.clearSelection(); 233 } 234 } 235 236 public void mouseReleased(GraphicsNodeMouseEvent evt) { 237 textSelector.mouseReleased(evt); 238 } 239 240 public void mouseEntered(GraphicsNodeMouseEvent evt) { 241 if (evt.getSource() instanceof Selectable) { 242 textSelector.mouseEntered(evt); 243 previousCursor = component.getCursor(); 244 if (previousCursor.getType() == Cursor.DEFAULT_CURSOR) { 245 component.setCursor(TEXT_CURSOR); 246 } 247 } 248 } 249 250 public void mouseExited(GraphicsNodeMouseEvent evt) { 251 if (evt.getSource() instanceof Selectable) { 252 textSelector.mouseExited(evt); 253 if (component.getCursor() == TEXT_CURSOR) { 254 component.setCursor(previousCursor); 255 } 256 } 257 } 258 259 public void mouseDragged(GraphicsNodeMouseEvent evt) { 260 if (evt.getSource() instanceof Selectable) { 261 textSelector.mouseDragged(evt); 262 } 263 } 264 265 public void mouseMoved(GraphicsNodeMouseEvent evt) { } 266 } 267 268 271 protected class TextSelectionListener implements SelectionListener { 272 public void selectionDone(SelectionEvent e) { 273 selectionChanged(e); 274 selection = e.getSelection(); 275 } 276 public void selectionCleared(SelectionEvent e) { 277 selectionStarted(e); 278 } 279 public void selectionStarted(SelectionEvent e) { 280 if (selectionHighlight != null) { 281 Rectangle r = getHighlightBounds(); 282 selectionHighlight = null; 283 component.repaint(r); 284 } 285 selection = null; 286 } 287 public void selectionChanged(SelectionEvent e) { 288 Rectangle r = null; 289 AffineTransform at = component.getRenderingTransform(); 290 if (selectionHighlight != null) { 291 r = at.createTransformedShape(selectionHighlight).getBounds(); 292 outset(r, 1); 293 } 294 295 selectionHighlight = e.getHighlightShape(); 296 if (selectionHighlight != null) { 297 if (r != null) { 298 Rectangle r2 = getHighlightBounds(); 299 component.repaint(r.union(r2)); 300 } else { 301 component.repaint(getHighlightBounds()); 302 } 303 } else if (r != null) { 304 component.repaint(r); 305 } 306 } 307 308 } 309 310 protected Rectangle outset(Rectangle r, int amount) { 311 r.x -= amount; 312 r.y -= amount; 313 r.width += 2*amount; 314 r.height += 2*amount; 315 return r; 316 } 317 318 321 protected Rectangle getHighlightBounds() { 322 AffineTransform at = component.getRenderingTransform(); 323 Shape s = at.createTransformedShape(selectionHighlight); 324 return outset(s.getBounds(), 1); 325 } 326 327 330 protected class SelectionOverlay implements Overlay { 331 332 335 public void paint(Graphics g) { 336 if (selectionHighlight != null) { 337 AffineTransform at = component.getRenderingTransform(); 338 Shape s = at.createTransformedShape(selectionHighlight); 339 340 Graphics2D g2d = (Graphics2D )g; 341 if (xorMode) { 342 g2d.setColor(Color.black); 343 g2d.setXORMode(Color.white); 344 g2d.fill(s); 345 } else { 346 g2d.setColor(selectionOverlayColor); 347 g2d.fill(s); 348 if (selectionOverlayStrokeColor != null) { 349 g2d.setStroke(new java.awt.BasicStroke (1.0f)); 350 g2d.setColor(selectionOverlayStrokeColor); 351 g2d.draw(s); 352 } 353 } 354 } 355 } 356 } 357 } 358 | Popular Tags |