KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > diff > DiffTooltipContentPanel


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.versioning.diff;
20
21 import org.netbeans.editor.EditorUI;
22 import org.netbeans.editor.ext.ExtCaret;
23 import org.netbeans.api.diff.Difference;
24 import org.openide.text.CloneableEditorSupport;
25
26 import javax.swing.*;
27 import javax.swing.text.*;
28 import java.io.StringReader JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.awt.*;
31
32 /**
33  * @author Maros Sandor
34  */

35 class DiffTooltipContentPanel extends JComponent {
36
37     public DiffTooltipContentPanel(JTextComponent parentPane, String JavaDoc mimeType, Difference diff) {
38         
39         JEditorPane originalTextPane = new JEditorPane();
40
41         EditorKit kit = CloneableEditorSupport.getEditorKit(mimeType);
42         originalTextPane.setEditorKit(kit);
43
44         Document xdoc = kit.createDefaultDocument();
45         if (!(xdoc instanceof StyledDocument)) {
46             xdoc = new DefaultStyledDocument(new StyleContext());
47             kit = new StyledEditorKit();
48             originalTextPane.setEditorKit(kit);
49         }
50
51         StyledDocument doc = (StyledDocument) xdoc;
52         try {
53             kit.read(new StringReader JavaDoc(diff.getFirstText()), doc, 0);
54         } catch (IOException JavaDoc e) {
55             e.printStackTrace();
56         } catch (BadLocationException e) {
57             e.printStackTrace();
58         }
59
60         originalTextPane.setDocument(doc);
61         originalTextPane.setEditable(false);
62         Color color = (diff.getType() == Difference.DELETE) ? new Color(255, 225, 232) : new Color(233, 241, 255);
63         originalTextPane.setBackground(color);
64
65         EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(originalTextPane);
66
67         Element rootElement = org.openide.text.NbDocument.findLineRootElement(doc);
68         int lineCount = rootElement.getElementCount();
69         int height = eui.getLineHeight() * lineCount;
70
71         int maxWidth = 0;
72         for(int line = 0; line < lineCount; line++) {
73             Element lineElement = rootElement.getElement(line);
74             String JavaDoc text = null;
75             try {
76                 text = doc.getText(lineElement.getStartOffset(), lineElement.getEndOffset() - lineElement.getStartOffset());
77             } catch (BadLocationException e) {
78                 e.printStackTrace();
79             }
80             int lineLength = parentPane.getFontMetrics(parentPane.getFont()).stringWidth(text);
81             if (lineLength > maxWidth) maxWidth = lineLength;
82         }
83
84         if (maxWidth < 50) maxWidth = 50; // too thin component causes repaint problems
85
originalTextPane.setPreferredSize(new Dimension(maxWidth * 7 / 6, height));
86
87         if (!originalTextPane.isEditable()) {
88             originalTextPane.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); //NOI18N
89
}
90
91         JScrollPane jsp = new JScrollPane(originalTextPane);
92         jsp.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
93
94         setLayout(new BorderLayout());
95         add(jsp);
96     }
97 }
98
Popular Tags