1 18 package org.apache.batik.gvt.text; 19 20 import java.awt.Shape ; 21 import java.awt.Toolkit ; 22 import java.awt.datatransfer.Clipboard ; 23 import java.awt.datatransfer.StringSelection ; 24 import java.awt.geom.AffineTransform ; 25 import java.awt.geom.NoninvertibleTransformException ; 26 import java.awt.geom.Point2D ; 27 import java.text.CharacterIterator ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 31 import org.apache.batik.gvt.GraphicsNode; 32 import org.apache.batik.gvt.Selectable; 33 import org.apache.batik.gvt.Selector; 34 import org.apache.batik.gvt.TextNode; 35 import org.apache.batik.gvt.event.GraphicsNodeChangeEvent; 36 import org.apache.batik.gvt.event.GraphicsNodeEvent; 37 import org.apache.batik.gvt.event.GraphicsNodeKeyEvent; 38 import org.apache.batik.gvt.event.GraphicsNodeMouseEvent; 39 import org.apache.batik.gvt.event.SelectionEvent; 40 import org.apache.batik.gvt.event.SelectionListener; 41 42 49 50 public class ConcreteTextSelector implements Selector { 51 52 private ArrayList listeners = null; 53 private GraphicsNode selectionNode = null; 54 private GraphicsNode currentNode = null; 55 private int firstHit; 56 private int lastHit; 57 58 public ConcreteTextSelector() { 59 } 60 61 public void mouseClicked(GraphicsNodeMouseEvent evt) { 62 checkSelectGesture(evt); 63 } 64 65 public void mouseDragged(GraphicsNodeMouseEvent evt) { 66 checkSelectGesture(evt); 67 } 68 69 public void mouseEntered(GraphicsNodeMouseEvent evt) { 70 currentNode = evt.getGraphicsNode(); 71 checkSelectGesture(evt); 72 } 73 74 public void mouseExited(GraphicsNodeMouseEvent evt) { 75 checkSelectGesture(evt); 76 currentNode = null; 77 } 78 79 public void mouseMoved(GraphicsNodeMouseEvent evt) { 80 } 81 82 public void mousePressed(GraphicsNodeMouseEvent evt) { 83 checkSelectGesture(evt); 84 } 85 86 public void mouseReleased(GraphicsNodeMouseEvent evt) { 87 checkSelectGesture(evt); 88 } 89 90 public void keyPressed(GraphicsNodeKeyEvent evt) { 91 report(evt, "keyPressed"); 92 } 93 94 public void keyReleased(GraphicsNodeKeyEvent evt) { 95 report(evt, "keyReleased"); 96 } 97 98 public void keyTyped(GraphicsNodeKeyEvent evt) { 99 report(evt, "keyTyped"); 100 } 101 102 public void changeStarted (GraphicsNodeChangeEvent gnce) { 103 } 104 public void changeCompleted (GraphicsNodeChangeEvent gnce) { 105 if (selectionNode == null) return; 106 Shape newShape = 107 ((Selectable)selectionNode).getHighlightShape(); 108 dispatchSelectionEvent 109 (new SelectionEvent(getSelection(), 110 SelectionEvent.SELECTION_CHANGED, 111 newShape)); 112 } 113 114 public void setSelection(Mark begin, Mark end) { 115 TextNode node = begin.getTextNode(); 116 if (node != end.getTextNode()) 117 throw new Error ("Markers not from same TextNode"); 118 node.setSelection(begin, end); 119 selectionNode = node; 120 Object selection = getSelection(); 121 Shape shape = node.getHighlightShape(); 122 dispatchSelectionEvent(new SelectionEvent 123 (selection, SelectionEvent.SELECTION_DONE, shape)); 124 } 125 126 public void clearSelection() { 127 if (selectionNode == null) 128 return; 129 dispatchSelectionEvent(new SelectionEvent 130 (null, SelectionEvent.SELECTION_CLEARED, null)); 131 selectionNode = null; 132 } 133 134 141 protected void checkSelectGesture(GraphicsNodeEvent evt) { 142 143 GraphicsNodeMouseEvent mevt = null; 144 if (evt instanceof GraphicsNodeMouseEvent) { 145 mevt = (GraphicsNodeMouseEvent) evt; 146 } 147 148 GraphicsNode source = evt.getGraphicsNode(); 149 if (isDeselectGesture(evt)) { 150 if (selectionNode != null) 151 selectionNode.getRoot() 152 .removeTreeGraphicsNodeChangeListener(this); 153 154 clearSelection(); 155 } else if (mevt != null) { 156 157 Point2D p = new Point2D.Double (mevt.getX(), mevt.getY()); 158 AffineTransform t = source.getGlobalTransform(); 159 if (t == null) { 160 t = new AffineTransform (); 161 } 162 else { 163 try { 164 t = t.createInverse(); 165 } catch (NoninvertibleTransformException ni) { 166 } 167 } 168 p = t.transform(p, null); 169 170 if ((source instanceof Selectable) && 171 (isSelectStartGesture(evt))) { 172 if (selectionNode != source) { 173 if (selectionNode != null) 174 selectionNode.getRoot() 175 .removeTreeGraphicsNodeChangeListener(this); 176 if (source != null) 177 source.getRoot() 178 .addTreeGraphicsNodeChangeListener(this); 179 } 180 181 selectionNode = source; 182 ((Selectable) source).selectAt(p.getX(), p.getY()); 183 dispatchSelectionEvent( 184 new SelectionEvent(null, 185 SelectionEvent.SELECTION_STARTED, 186 null)); 187 188 } else if (isSelectEndGesture(evt)) { 189 if (selectionNode == source) 190 ((Selectable) source).selectTo(p.getX(), p.getY()); 191 Object oldSelection = getSelection(); 192 if (selectionNode != null) { 193 Shape newShape; 194 newShape = ((Selectable)selectionNode).getHighlightShape(); 195 dispatchSelectionEvent 196 (new SelectionEvent(oldSelection, 197 SelectionEvent.SELECTION_DONE, 198 newShape)); 199 } 200 } else if (isSelectContinueGesture(evt)) { 201 202 if (selectionNode == source) { 203 boolean result = ((Selectable) source).selectTo(p.getX(), 204 p.getY()); 205 if (result) { 206 Shape newShape = 207 ((Selectable) selectionNode).getHighlightShape(); 208 209 dispatchSelectionEvent( 210 new SelectionEvent(null, 211 SelectionEvent.SELECTION_CHANGED, 212 newShape)); 213 } 214 } 215 } else if ((source instanceof Selectable) && 216 (isSelectAllGesture(evt))) { 217 if (selectionNode != source) { 218 if (selectionNode != null) 219 selectionNode.getRoot() 220 .removeTreeGraphicsNodeChangeListener(this); 221 if (source != null) 222 source.getRoot() 223 .addTreeGraphicsNodeChangeListener(this); 224 } 225 selectionNode = source; 226 ((Selectable) source).selectAll(p.getX(), p.getY()); 227 Object oldSelection = getSelection(); 228 Shape newShape = 229 ((Selectable) source).getHighlightShape(); 230 dispatchSelectionEvent( 231 new SelectionEvent(oldSelection, 232 SelectionEvent.SELECTION_DONE, 233 newShape)); 234 } 235 } 236 } 237 238 private boolean isDeselectGesture(GraphicsNodeEvent evt) { 239 return ((evt.getID() == GraphicsNodeMouseEvent.MOUSE_CLICKED) 240 && (((GraphicsNodeMouseEvent) evt).getClickCount() == 1)); 241 } 242 243 private boolean isSelectStartGesture(GraphicsNodeEvent evt) { 244 return (evt.getID() == GraphicsNodeMouseEvent.MOUSE_PRESSED); 245 } 246 247 private boolean isSelectEndGesture(GraphicsNodeEvent evt) { 248 return ((evt.getID() == GraphicsNodeMouseEvent.MOUSE_RELEASED)); 249 } 250 251 private boolean isSelectContinueGesture(GraphicsNodeEvent evt) { 252 return (evt.getID() == GraphicsNodeMouseEvent.MOUSE_DRAGGED); 253 } 254 255 private boolean isSelectAllGesture(GraphicsNodeEvent evt) { 256 return ((evt.getID() == GraphicsNodeMouseEvent.MOUSE_CLICKED) 257 && (((GraphicsNodeMouseEvent) evt).getClickCount() == 2)); 258 } 259 260 263 public Object getSelection() { 264 Object value = null; 265 if (selectionNode instanceof Selectable) { 266 value = ((Selectable) selectionNode).getSelection(); 267 } 268 return value; 269 } 270 271 274 public boolean isEmpty() { 275 return (getSelection() == null); 276 } 277 278 281 public void dispatchSelectionEvent(SelectionEvent e) { 282 if (listeners != null) { 283 Iterator iter = listeners.iterator(); 284 switch(e.getID()) { 285 case SelectionEvent.SELECTION_DONE: 286 while (iter.hasNext()) { 287 ((SelectionListener)iter.next()).selectionDone(e); 288 } 289 break; 290 case SelectionEvent.SELECTION_CHANGED: 291 while (iter.hasNext()) { 292 ((SelectionListener)iter.next()).selectionChanged(e); 293 } 294 break; 295 case SelectionEvent.SELECTION_CLEARED: 296 while (iter.hasNext()) { 297 ((SelectionListener)iter.next()).selectionCleared(e); 298 } 299 break; 300 case SelectionEvent.SELECTION_STARTED: 301 while (iter.hasNext()) { 302 ((SelectionListener)iter.next()).selectionStarted(e); 303 } 304 break; 305 } 306 } 307 } 308 309 313 public void addSelectionListener(SelectionListener l) { 314 if (listeners == null) { 315 listeners = new ArrayList (); 316 } 317 listeners.add(l); 318 } 319 320 324 public void removeSelectionListener(SelectionListener l) { 325 if (listeners != null) { 326 listeners.remove(l); 327 } 328 } 329 330 private void report(GraphicsNodeEvent evt, String message) { 331 GraphicsNode source = evt.getGraphicsNode(); 332 String label = "(non-text node)"; 333 if (source instanceof TextNode) { 334 char[] cbuff; 335 java.text.CharacterIterator iter = 336 ((TextNode) source).getAttributedCharacterIterator(); 337 cbuff = new char[iter.getEndIndex()]; 338 if (cbuff.length > 0) cbuff[0] = iter.first(); 339 for (int i=1; i<cbuff.length;++i) { 340 cbuff[i] = iter.next(); 341 } 342 label = new String (cbuff); 343 } 344 System.out.println("Mouse "+message+" in "+label); 345 } 346 } 347 348 349 | Popular Tags |