1 11 package org.eclipse.jface.text; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.swt.custom.StyledText; 19 import org.eclipse.swt.events.KeyEvent; 20 import org.eclipse.swt.events.KeyListener; 21 import org.eclipse.swt.events.MouseEvent; 22 import org.eclipse.swt.events.MouseListener; 23 import org.eclipse.swt.widgets.Control; 24 25 import org.eclipse.jface.viewers.ISelectionChangedListener; 26 import org.eclipse.jface.viewers.ISelectionProvider; 27 import org.eclipse.jface.viewers.SelectionChangedEvent; 28 29 30 38 public final class PaintManager implements KeyListener, MouseListener, ISelectionChangedListener, ITextListener, ITextInputListener { 39 40 45 static class PaintPositionUpdater extends DefaultPositionUpdater { 46 47 52 protected PaintPositionUpdater(String category) { 53 super(category); 54 } 55 56 62 protected void adaptToInsert() { 63 64 int myStart= fPosition.offset; 65 int myEnd= fPosition.offset + fPosition.length; 66 myEnd= Math.max(myStart, myEnd); 67 68 int yoursStart= fOffset; 69 int yoursEnd= fOffset + fReplaceLength; yoursEnd= Math.max(yoursStart, yoursEnd); 71 72 if (myEnd < yoursStart) 73 return; 74 75 if (myStart <= yoursStart) 76 fPosition.length += fReplaceLength; 77 else 78 fPosition.offset += fReplaceLength; 79 } 80 } 81 82 87 static class PositionManager implements IPaintPositionManager { 88 89 private IDocument fDocument; 91 92 private IPositionUpdater fPositionUpdater; 93 94 private String fCategory; 95 96 100 public PositionManager() { 101 fCategory= getClass().getName() + hashCode(); 102 fPositionUpdater= new PaintPositionUpdater(fCategory); 103 } 104 105 112 public void install(IDocument document) { 113 fDocument= document; 114 fDocument.addPositionCategory(fCategory); 115 fDocument.addPositionUpdater(fPositionUpdater); 116 } 117 118 123 public void dispose() { 124 uninstall(fDocument); 125 } 126 127 133 public void uninstall(IDocument document) { 134 if (document == fDocument && document != null) { 135 try { 136 fDocument.removePositionUpdater(fPositionUpdater); 137 fDocument.removePositionCategory(fCategory); 138 } catch (BadPositionCategoryException x) { 139 } 141 fDocument= null; 142 } 143 } 144 145 148 public void managePosition(Position position) { 149 try { 150 fDocument.addPosition(fCategory, position); 151 } catch (BadPositionCategoryException x) { 152 } catch (BadLocationException x) { 154 } 156 } 157 158 161 public void unmanagePosition(Position position) { 162 try { 163 fDocument.removePosition(fCategory, position); 164 } catch (BadPositionCategoryException x) { 165 } 167 } 168 } 169 170 171 172 private List fPainters= new ArrayList (2); 173 174 private PositionManager fManager; 175 176 private ITextViewer fTextViewer; 177 178 183 public PaintManager(ITextViewer textViewer) { 184 fTextViewer= textViewer; 185 } 186 187 188 195 public void addPainter(IPainter painter) { 196 if (!fPainters.contains(painter)) { 197 fPainters.add(painter); 198 if (fPainters.size() == 1) 199 install(); 200 painter.setPositionManager(fManager); 201 painter.paint(IPainter.INTERNAL); 202 } 203 } 204 205 212 public void removePainter(IPainter painter) { 213 if (fPainters.remove(painter)) { 214 painter.deactivate(true); 215 painter.setPositionManager(null); 216 } 217 if (fPainters.size() == 0) 218 dispose(); 219 } 220 221 225 private void install() { 226 227 fManager= new PositionManager(); 228 if (fTextViewer.getDocument() != null) 229 fManager.install(fTextViewer.getDocument()); 230 231 fTextViewer.addTextInputListener(this); 232 233 addListeners(); 234 } 235 236 240 private void addListeners() { 241 ISelectionProvider provider= fTextViewer.getSelectionProvider(); 242 provider.addSelectionChangedListener(this); 243 244 fTextViewer.addTextListener(this); 245 246 StyledText text= fTextViewer.getTextWidget(); 247 text.addKeyListener(this); 248 text.addMouseListener(this); 249 } 250 251 256 public void dispose() { 257 258 if (fManager != null) { 259 fManager.dispose(); 260 fManager= null; 261 } 262 263 for (Iterator e = fPainters.iterator(); e.hasNext();) 264 ((IPainter) e.next()).dispose(); 265 fPainters.clear(); 266 267 fTextViewer.removeTextInputListener(this); 268 269 removeListeners(); 270 } 271 272 276 private void removeListeners() { 277 ISelectionProvider provider= fTextViewer.getSelectionProvider(); 278 if (provider != null) 279 provider.removeSelectionChangedListener(this); 280 281 fTextViewer.removeTextListener(this); 282 283 StyledText text= fTextViewer.getTextWidget(); 284 if (text != null && !text.isDisposed()) { 285 text.removeKeyListener(this); 286 text.removeMouseListener(this); 287 } 288 } 289 290 296 private void paint(int reason) { 297 for (Iterator e = fPainters.iterator(); e.hasNext();) 298 ((IPainter) e.next()).paint(reason); 299 } 300 301 304 public void keyPressed(KeyEvent e) { 305 paint(IPainter.KEY_STROKE); 306 } 307 308 311 public void keyReleased(KeyEvent e) { 312 } 313 314 317 public void mouseDoubleClick(MouseEvent e) { 318 } 319 320 323 public void mouseDown(MouseEvent e) { 324 paint(IPainter.MOUSE_BUTTON); 325 } 326 327 330 public void mouseUp(MouseEvent e) { 331 } 332 333 336 public void selectionChanged(SelectionChangedEvent event) { 337 paint(IPainter.SELECTION); 338 } 339 340 343 public void textChanged(TextEvent event) { 344 345 if (!event.getViewerRedrawState()) 346 return; 347 348 Control control= fTextViewer.getTextWidget(); 349 if (control != null) { 350 control.getDisplay().asyncExec(new Runnable () { 351 public void run() { 352 if (fTextViewer != null) 353 paint(IPainter.TEXT_CHANGE); 354 } 355 }); 356 } 357 } 358 359 362 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { 363 if (oldInput != null) { 364 for (Iterator e = fPainters.iterator(); e.hasNext();) 365 ((IPainter) e.next()).deactivate(false); 366 fManager.uninstall(oldInput); 367 removeListeners(); 368 } 369 } 370 371 374 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { 375 if (newInput != null) { 376 fManager.install(newInput); 377 paint(IPainter.TEXT_CHANGE); 378 addListeners(); 379 } 380 } 381 } 382 383 | Popular Tags |