KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > diff > builtin > visualizer > editable > DecoratedEditorPane


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
33  * Editor pane with added decorations (diff lines).
34  *
35  * @author Maros Sandor
36  */

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         // compensate for cursor drawing, it is needed for catching a difference on the cursor line
69
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                     // find the nearest visible line with an annotation
91
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++; // make it 1-based
100

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++; // make it 1-based
107
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++; // make it 1-based
132
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