1 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 ; 29 import java.io.IOException ; 30 import java.awt.*; 31 32 35 class DiffTooltipContentPanel extends JComponent { 36 37 public DiffTooltipContentPanel(JTextComponent parentPane, String 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 (diff.getFirstText()), doc, 0); 54 } catch (IOException 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 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; originalTextPane.setPreferredSize(new Dimension(maxWidth * 7 / 6, height)); 86 87 if (!originalTextPane.isEditable()) { 88 originalTextPane.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); } 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 |