1 19 package org.netbeans.modules.diff.builtin.visualizer.editable; 20 21 import org.netbeans.editor.EditorUI; 22 import org.netbeans.editor.Utilities; 23 import org.netbeans.editor.BaseTextUI; 24 import org.netbeans.api.editor.fold.FoldHierarchy; 25 import org.netbeans.api.diff.Difference; 26 import org.openide.ErrorManager; 27 28 import javax.swing.*; 29 import javax.swing.text.*; 30 import java.awt.*; 31 32 37 class DecoratedEditorPane extends JEditorPane { 38 39 private Difference[] currentDiff; 40 private DiffContentPanel master; 41 42 public DecoratedEditorPane(DiffContentPanel master) { 43 setBorder(null); 44 this.master = master; 45 } 46 47 public boolean isFirst() { 48 return master.isFirst(); 49 } 50 51 public DiffContentPanel getMaster() { 52 return master; 53 } 54 55 void setDifferences(Difference [] diff) { 56 currentDiff = diff; 57 repaint(); 58 } 59 60 protected void paintComponent(Graphics gr) { 61 super.paintComponent(gr); 62 if (currentDiff == null) return; 63 64 EditorUI editorUI = org.netbeans.editor.Utilities.getEditorUI(this); 65 66 Graphics2D g = (Graphics2D) gr.create(); 67 Rectangle clip = g.getClipBounds(); 68 clip.y -= 1; 70 clip.height += 1; 71 72 73 FoldHierarchy foldHierarchy = FoldHierarchy.get(editorUI.getComponent()); 74 JTextComponent component = editorUI.getComponent(); 75 if (component == null) return; 76 View rootView = Utilities.getDocumentView(component); 77 if (rootView == null) return; 78 BaseTextUI textUI = (BaseTextUI)component.getUI(); 79 80 AbstractDocument doc = (AbstractDocument)component.getDocument(); 81 doc.readLock(); 82 try{ 83 foldHierarchy.lock(); 84 try{ 85 int startPos = textUI.getPosFromY(clip.y); 86 int startViewIndex = rootView.getViewIndex(startPos,Position.Bias.Forward); 87 int rootViewCount = rootView.getViewCount(); 88 89 if (startViewIndex >= 0 && startViewIndex < rootViewCount) { 90 Rectangle rec = textUI.modelToView(component, rootView.getView(startViewIndex).getStartOffset()); 92 int y = (rec == null) ? 0 : rec.y; 93 94 int clipEndY = clip.y + clip.height; 95 Element rootElem = textUI.getRootView(component).getElement(); 96 97 View view = rootView.getView(startViewIndex); 98 int line = rootElem.getElementIndex(view.getStartOffset()); 99 line++; 101 g.setColor(master.getMaster().getColorLines()); 102 if (master.isFirst()) { 103 for (int i = startViewIndex; i < rootViewCount; i++){ 104 view = rootView.getView(i); 105 line = rootElem.getElementIndex(view.getStartOffset()); 106 line++; Difference ad = EditableDiffView.getFirstDifference(currentDiff, line); 108 if (ad != null) { 109 int yy = y + editorUI.getLineHeight(); 110 if (ad.getType() == Difference.ADD) { 111 g.drawLine(0, yy, getWidth(), yy); 112 ad = null; 113 } else { 114 if (ad.getFirstStart() == line) { 115 g.drawLine(0, y, getWidth(), y); 116 } 117 if (ad.getFirstEnd() == line) { 118 g.drawLine(0, yy, getWidth(), yy); 119 } 120 } 121 } 122 y += editorUI.getLineHeight(); 123 if (y >= clipEndY) { 124 break; 125 } 126 } 127 } else { 128 for (int i = startViewIndex; i < rootViewCount; i++){ 129 view = rootView.getView(i); 130 line = rootElem.getElementIndex(view.getStartOffset()); 131 line++; Difference ad = EditableDiffView.getSecondDifference(currentDiff, line); 133 if (ad != null) { 134 int yy = y + editorUI.getLineHeight(); 135 if (ad.getType() == Difference.DELETE) { 136 g.drawLine(0, yy, getWidth(), yy); 137 ad = null; 138 } else { 139 if (ad.getSecondStart() == line) { 140 g.drawLine(0, y, getWidth(), y); 141 } 142 if (ad.getSecondEnd() == line) { 143 g.drawLine(0, yy, getWidth(), yy); 144 } 145 } 146 } 147 y += editorUI.getLineHeight(); 148 if (y >= clipEndY) { 149 break; 150 } 151 } 152 } 153 } 154 } finally { 155 foldHierarchy.unlock(); 156 } 157 } catch (BadLocationException ble){ 158 ErrorManager.getDefault().notify(ble); 159 } finally { 160 doc.readUnlock(); 161 } 162 } 163 } | Popular Tags |