1 19 20 package org.netbeans.modules.diff.builtin.visualizer; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.FocusEvent ; 24 import java.awt.event.FocusListener ; 25 import java.awt.*; 26 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyChangeListener ; 29 import java.util.*; 30 import java.io.*; 31 import javax.swing.*; 32 import javax.swing.text.*; 33 import org.netbeans.api.editor.fold.FoldHierarchy; 34 import org.netbeans.api.editor.fold.FoldUtilities; 35 import org.netbeans.editor.EditorUI; 36 import org.netbeans.editor.ext.ExtCaret; 37 import org.netbeans.modules.diff.builtin.DiffPresenter; 38 39 import org.openide.actions.CopyAction; 40 import org.openide.util.actions.ActionPerformer; 41 import org.openide.util.actions.CallbackSystemAction; 42 import org.openide.util.actions.SystemAction; 43 import org.openide.ErrorManager; 44 import org.openide.text.CloneableEditorSupport; 45 48 53 public class DiffPanel extends javax.swing.JPanel implements javax.swing.event.CaretListener { 54 55 private int totalHeight = 0; 57 private int totalLines = 0; 58 59 62 private int horizontalScroll1ChangedValue = -1; 63 private int horizontalScroll2ChangedValue = -1; 64 65 private LinesComponent linesComp1; 66 private LinesComponent linesComp2; 67 68 static final long serialVersionUID =3683458237532937983L; 69 private static final String PLAIN_TEXT_MIME = "text/plain"; 70 71 72 public DiffPanel() { 73 initComponents (); 75 76 commandPanel.remove(prevButton); 79 commandPanel.remove(nextButton); 80 JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.TRAILING, 5, 0)); 81 toolbar.setBorder(BorderFactory.createEmptyBorder()); 82 toolbar.add(prevButton); 83 toolbar.add(nextButton); 84 remove(commandPanel); 85 putClientProperty(DiffPresenter.PROP_TOOLBAR, toolbar); 86 87 setName(org.openide.util.NbBundle.getMessage(DiffPanel.class, "DiffComponent.title")); 89 initActions(); 91 jSplitPane1.setResizeWeight(0.5); 92 putClientProperty("PersistenceType", "Never"); 93 getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DiffPanel.class, "ACS_DiffPanelA11yName")); getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPanel.class, "ACS_DiffPanelA11yDesc")); jEditorPane1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DiffPanel.class, "ACS_EditorPane1A11yName")); jEditorPane1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPanel.class, "ACS_EditorPane1A11yDescr")); jEditorPane2.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DiffPanel.class, "ACS_EditorPane2A11yName")); jEditorPane2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPanel.class, "ACS_EditorPane2A11yDescr")); } 100 101 public void addNotify() { 102 super.addNotify(); 103 104 jEditorPane1.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); jEditorPane2.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); 107 JComponent parent = (JComponent) getParent(); 108 109 Action nextAction = new ButtonAction(nextButton); 110 Action prevAction = new ButtonAction(prevButton); 111 parent.getActionMap().put("jumpNext", nextAction); parent.getActionMap().put("jumpPrev", prevAction); } 114 115 116 private static class ButtonAction extends AbstractAction { 117 118 final JButton button; 119 120 public ButtonAction(JButton button) { 121 this.button = button; 122 } 123 124 public void actionPerformed(ActionEvent e) { 125 button.doClick(); 126 } 127 } 128 129 134 private void initComponents() { 136 java.awt.GridBagConstraints gridBagConstraints; 137 138 setLayout(new java.awt.GridBagLayout ()); 139 140 commandPanel.setLayout(new java.awt.GridBagLayout ()); 141 142 prevButton.setIcon(new ImageIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/prev.gif", true))); 143 prevButton.setToolTipText(org.openide.util.NbBundle.getBundle(DiffPanel.class).getString("DiffComponent.prevButton.toolTipText")); 144 prevButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 145 gridBagConstraints = new java.awt.GridBagConstraints (); 146 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE; 147 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 148 commandPanel.add(prevButton, gridBagConstraints); 149 150 nextButton.setIcon(new ImageIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/next.gif", true))); 151 nextButton.setToolTipText(org.openide.util.NbBundle.getBundle(DiffPanel.class).getString("DiffComponent.nextButton.toolTipText")); 152 nextButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 153 gridBagConstraints = new java.awt.GridBagConstraints (); 154 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE; 155 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 156 gridBagConstraints.insets = new java.awt.Insets (0, 2, 0, 0); 157 commandPanel.add(nextButton, gridBagConstraints); 158 159 gridBagConstraints = new java.awt.GridBagConstraints (); 160 gridBagConstraints.gridy = 0; 161 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 162 gridBagConstraints.weightx = 1.0; 163 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 164 add(commandPanel, gridBagConstraints); 165 166 editorPanel.setLayout(new java.awt.GridBagLayout ()); 167 168 editorPanel.setPreferredSize(new java.awt.Dimension (700, 600)); 169 jSplitPane1.setDividerSize(4); 170 filePanel1.setLayout(new java.awt.GridBagLayout ()); 171 172 jEditorPane1.addCaretListener(this); 173 174 jScrollPane1.setViewportView(jEditorPane1); 175 176 gridBagConstraints = new java.awt.GridBagConstraints (); 177 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 178 gridBagConstraints.weightx = 1.0; 179 gridBagConstraints.weighty = 1.0; 180 filePanel1.add(jScrollPane1, gridBagConstraints); 181 182 fileLabel1.setText("jLabel1"); 183 gridBagConstraints = new java.awt.GridBagConstraints (); 184 gridBagConstraints.gridy = 1; 185 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 186 gridBagConstraints.weightx = 1.0; 187 gridBagConstraints.insets = new java.awt.Insets (4, 4, 4, 4); 188 filePanel1.add(fileLabel1, gridBagConstraints); 189 190 jSplitPane1.setLeftComponent(filePanel1); 191 192 filePanel2.setLayout(new java.awt.GridBagLayout ()); 193 194 jEditorPane2.addCaretListener(this); 195 196 jScrollPane2.setViewportView(jEditorPane2); 197 198 gridBagConstraints = new java.awt.GridBagConstraints (); 199 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 200 gridBagConstraints.weightx = 1.0; 201 gridBagConstraints.weighty = 1.0; 202 filePanel2.add(jScrollPane2, gridBagConstraints); 203 204 fileLabel2.setText("jLabel2"); 205 gridBagConstraints = new java.awt.GridBagConstraints (); 206 gridBagConstraints.gridy = 1; 207 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 208 gridBagConstraints.weightx = 1.0; 209 gridBagConstraints.insets = new java.awt.Insets (4, 4, 4, 4); 210 filePanel2.add(fileLabel2, gridBagConstraints); 211 212 jSplitPane1.setRightComponent(filePanel2); 213 214 gridBagConstraints = new java.awt.GridBagConstraints (); 215 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 216 gridBagConstraints.weightx = 1.0; 217 gridBagConstraints.weighty = 1.0; 218 editorPanel.add(jSplitPane1, gridBagConstraints); 219 220 gridBagConstraints = new java.awt.GridBagConstraints (); 221 gridBagConstraints.gridy = 1; 222 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 223 gridBagConstraints.weightx = 1.0; 224 gridBagConstraints.weighty = 1.0; 225 add(editorPanel, gridBagConstraints); 226 227 } 228 229 231 public void caretUpdate(javax.swing.event.CaretEvent evt) { 232 if (evt.getSource() == jEditorPane1) { 233 DiffPanel.this.jEditorPane1CaretUpdate(evt); 234 } 235 else if (evt.getSource() == jEditorPane2) { 236 DiffPanel.this.jEditorPane2CaretUpdate(evt); 237 } 238 } 240 private void jEditorPane1CaretUpdate(javax.swing.event.CaretEvent evt) { 250 } 252 private void jEditorPane2CaretUpdate(javax.swing.event.CaretEvent evt) { 262 } 264 public void setCurrentLine(int line, int diffLength) { 265 if (line > 0) showLine(line, diffLength); 266 } 267 268 public void addPrevLineButtonListener(java.awt.event.ActionListener listener) { 269 prevButton.addActionListener(listener); 270 } 271 public void addNextLineButtonListener(java.awt.event.ActionListener listener) { 272 nextButton.addActionListener(listener); 273 } 274 275 280 281 private void jScrollBar1AdjustmentValueChanged (java.awt.event.AdjustmentEvent evt) { } 285 private void closeButtonActionPerformed (java.awt.event.ActionEvent evt) { exitForm(null); 288 } 290 291 private void exitForm(java.awt.event.WindowEvent evt) { 305 } 307 private void initActions() { 308 jEditorPane1.addFocusListener(new FocusListener () { 309 public void focusGained(FocusEvent e) { 310 editorActivated(jEditorPane1); 311 } 312 public void focusLost(FocusEvent e) { 313 editorDeactivated(jEditorPane1); 314 } 315 }); 316 jEditorPane2.addFocusListener(new FocusListener () { 317 public void focusGained(FocusEvent e) { 318 editorActivated(jEditorPane2); 319 } 320 public void focusLost(FocusEvent e) { 321 editorDeactivated(jEditorPane2); 322 } 323 }); 324 } 325 326 private Hashtable<JEditorPane, Hashtable<Object , Action>> kitActions; 327 328 private PropertyChangeListener copyL; 329 private PropertyChangeListener copyP; 330 331 private Action getAction (String s, JEditorPane editor) { 332 if (kitActions == null) { 333 kitActions = new Hashtable<JEditorPane, Hashtable<Object , Action>>(); 334 } 335 Hashtable<Object , Action> actions = kitActions.get(editor); 336 if (actions == null) { 337 EditorKit kit = editor.getEditorKit(); 338 if (kit == null) { 339 return null; 340 } 341 342 Action[] a = kit.getActions (); 343 actions = new Hashtable<Object , Action> (a.length); 344 int k = a.length; 345 for (int i = 0; i < k; i++) 346 actions.put (a[i].getValue (Action.NAME), a[i]); 347 kitActions.put(editor, actions); 348 } 349 return actions.get (s); 350 } 351 352 private void editorActivated(final JEditorPane editor) { 353 final Action copy = getAction (DefaultEditorKit.copyAction, editor); 355 if (copy != null) { 356 final CallbackSystemAction sysCopy 357 = ((CallbackSystemAction) SystemAction.get (CopyAction.class)); 358 final ActionPerformer perf = new ActionPerformer () { 359 public void performAction (SystemAction action) { 360 copy.actionPerformed (new ActionEvent (editor, 0, "")); } 362 }; 363 sysCopy.setActionPerformer(copy.isEnabled() ? perf : null); 364 PropertyChangeListener copyListener; 365 copy.addPropertyChangeListener(copyListener = new PropertyChangeListener () { 366 public void propertyChange(PropertyChangeEvent evt) { 367 if ("enabled".equals(evt.getPropertyName())) { if (((Boolean )evt.getNewValue()).booleanValue()) { 369 sysCopy.setActionPerformer(perf); 370 } else if (sysCopy.getActionPerformer() == perf) { 371 sysCopy.setActionPerformer(null); 372 } 373 } 374 } 375 }); 376 if (editor.equals(jEditorPane1)) copyL = copyListener; 377 else copyP = copyListener; 378 } 379 } 380 381 private void editorDeactivated(JEditorPane editor) { 382 Action copy = getAction (DefaultEditorKit.copyAction, editor); 384 PropertyChangeListener copyListener; 385 if (editor.equals(jEditorPane1)) copyListener = copyL; 386 else copyListener = copyP; 387 if (copy != null) { 388 copy.removePropertyChangeListener(copyListener); 389 } 390 } 391 392 393 public void open() { 394 javax.swing.SwingUtilities.invokeLater(new Runnable () { 395 public void run() { 396 jSplitPane1.setDividerLocation(0.5); 397 openPostProcess(); 398 } 399 }); 400 } 401 402 protected void openPostProcess() { 403 javax.swing.SwingUtilities.invokeLater(new Runnable () { 404 public void run() { 405 expandFolds(); 406 initGlobalSizes(); 407 addChangeListeners(); 409 421 } 422 }); 423 } 424 425 private void expandFolds() { 426 FoldHierarchy fh = FoldHierarchy.get(jEditorPane1); 427 FoldUtilities.expandAll(fh); 428 fh = FoldHierarchy.get(jEditorPane2); 429 FoldUtilities.expandAll(fh); 430 } 431 432 439 private void initGlobalSizes() { 440 StyledDocument doc1 = (StyledDocument) jEditorPane1.getDocument(); 441 StyledDocument doc2 = (StyledDocument) jEditorPane2.getDocument(); 442 int numLines1 = org.openide.text.NbDocument.findLineNumber(doc1, doc1.getEndPosition().getOffset()); 443 int numLines2 = org.openide.text.NbDocument.findLineNumber(doc2, doc2.getEndPosition().getOffset()); 444 int numLines = Math.max(numLines1, numLines2); 445 if (numLines < 1) numLines = 1; 446 this.totalLines = numLines; 447 int totHeight = jEditorPane1.getSize().height; 449 int value = jEditorPane2.getSize().height; 451 if (value > totHeight) totHeight = value; 452 this.totalHeight = totHeight; 453 } 454 455 private void showLine(int line, int diffLength) { 456 this.linesComp1.setActiveLine(line); 458 this.linesComp2.setActiveLine(line); 459 linesComp2.repaint(); 460 linesComp1.repaint(); 461 int padding = 5; 462 if (line <= 5) padding = line/2; 463 int off1, off2; 464 int ypos; 465 int viewHeight = jViewport1.getExtentSize().height; 466 java.awt.Point p1, p2; 467 initGlobalSizes(); p1 = jViewport1.getViewPosition(); 469 p2 = jViewport2.getViewPosition(); 470 ypos = (totalHeight*(line - padding - 1))/(totalLines + 1); 471 472 try { 473 off1 = org.openide.text.NbDocument.findLineOffset((StyledDocument) jEditorPane1.getDocument(), line - 1); 474 off2 = org.openide.text.NbDocument.findLineOffset((StyledDocument) jEditorPane2.getDocument(), line - 1); 475 476 jEditorPane1.setCaretPosition(off1); 477 jEditorPane2.setCaretPosition(off2); 478 } catch (IndexOutOfBoundsException ex) { 479 ErrorManager.getDefault().notify(ex); 480 } 481 482 if (ypos < p1.y || ypos + ((diffLength + padding)*totalHeight)/totalLines > p1.y + viewHeight) { 483 p1.y = ypos; 484 jViewport1.setViewPosition(p1); } 486 } 487 488 private void joinScrollBars() { 489 final JScrollBar scrollBarH1 = jScrollPane1.getHorizontalScrollBar(); 490 final JScrollBar scrollBarV1 = jScrollPane1.getVerticalScrollBar(); 491 final JScrollBar scrollBarH2 = jScrollPane2.getHorizontalScrollBar(); 492 final JScrollBar scrollBarV2 = jScrollPane2.getVerticalScrollBar(); 493 scrollBarV1.getModel().addChangeListener(new javax.swing.event.ChangeListener () { 494 public void stateChanged(javax.swing.event.ChangeEvent e) { 495 int value = scrollBarV1.getValue(); 496 int oldValue = scrollBarV2.getValue(); 497 if (oldValue != value) { 498 scrollBarV2.setValue(value); 499 } 502 } 503 }); 504 jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0)); 508 scrollBarV2.getModel().addChangeListener(new javax.swing.event.ChangeListener () { 509 public void stateChanged(javax.swing.event.ChangeEvent e) { 510 int value = scrollBarV2.getValue(); 511 int oldValue = scrollBarV1.getValue(); 512 if (oldValue != value) { 513 scrollBarV1.setValue(value); 514 } 516 } 517 }); 518 scrollBarH1.getModel().addChangeListener(new javax.swing.event.ChangeListener () { 519 public void stateChanged(javax.swing.event.ChangeEvent e) { 520 int value = scrollBarH1.getValue(); 521 if (value == horizontalScroll1ChangedValue) return; 523 int max1 = scrollBarH1.getMaximum(); 524 int max2 = scrollBarH2.getMaximum(); 525 int ext1 = scrollBarH1.getModel().getExtent(); 526 int ext2 = scrollBarH2.getModel().getExtent(); 527 if (max1 == ext1) horizontalScroll2ChangedValue = 0; 528 else horizontalScroll2ChangedValue = (value*(max2 - ext2))/(max1 - ext1); 529 horizontalScroll1ChangedValue = -1; 530 scrollBarH2.setValue(horizontalScroll2ChangedValue); 532 } 533 }); 534 scrollBarH2.getModel().addChangeListener(new javax.swing.event.ChangeListener () { 535 public void stateChanged(javax.swing.event.ChangeEvent e) { 536 int value = scrollBarH2.getValue(); 537 if (value == horizontalScroll2ChangedValue) return; 539 int max1 = scrollBarH1.getMaximum(); 540 int max2 = scrollBarH2.getMaximum(); 541 int ext1 = scrollBarH1.getModel().getExtent(); 542 int ext2 = scrollBarH2.getModel().getExtent(); 543 if (max2 == ext2) horizontalScroll1ChangedValue = 0; 544 else horizontalScroll1ChangedValue = (value*(max1 - ext1))/(max2 - ext2); 545 horizontalScroll2ChangedValue = -1; 546 scrollBarH1.setValue(horizontalScroll1ChangedValue); 548 } 549 }); 550 jSplitPane1.setDividerLocation(0.5); 551 } 552 553 private String strCharacters(char c, int num) { 554 StringBuffer s = new StringBuffer (); 555 while(num-- > 0) { 556 s.append(c); 557 } 558 return s.toString(); 559 } 560 561 private void customizeEditor(JEditorPane editor) { 562 EditorKit kit = editor.getEditorKit(); 563 570 StyledDocument doc; 571 Document document = editor.getDocument(); 572 575 try { 576 doc = (StyledDocument) editor.getDocument(); 577 } catch(ClassCastException e) { 578 doc = new DefaultStyledDocument(); 579 try { 580 doc.insertString(0, document.getText(0, document.getLength()), null); 581 } catch (BadLocationException ble) { 582 } 584 editor.setDocument(doc); 585 } 586 int lastOffset = doc.getEndPosition().getOffset(); 587 int numLines = org.openide.text.NbDocument.findLineNumber(doc, lastOffset); 588 int numLength = Integer.toString(numLines).length(); 589 for (int line = 0; line <= numLines; line++) { 592 int offset = org.openide.text.NbDocument.findLineOffset(doc, line); 593 String lineStr = Integer.toString(line+1); 594 if (lineStr.length() < numLength) lineStr = strCharacters(' ', numLength - lineStr.length()) + lineStr; 595 603 } 604 } 606 607 621 690 691 694 701 702 private void addChangeListeners() { 703 jEditorPane1.addPropertyChangeListener("font", new java.beans.PropertyChangeListener () { 704 public void propertyChange(java.beans.PropertyChangeEvent evt) { 705 javax.swing.SwingUtilities.invokeLater(new Runnable () { 707 public void run() { 708 initGlobalSizes(); 709 linesComp1.repaint(); 710 } 711 }); 712 } 713 }); 714 jEditorPane2.addPropertyChangeListener("font", new java.beans.PropertyChangeListener () { 715 public void propertyChange(java.beans.PropertyChangeEvent evt) { 716 javax.swing.SwingUtilities.invokeLater(new Runnable () { 718 public void run() { 719 initGlobalSizes(); 720 linesComp2.repaint(); 721 } 722 }); 723 } 724 }); 725 } 726 727 public void setSource1(Reader r) throws IOException { 728 EditorKit kit = jEditorPane1.getEditorKit(); 731 if (kit == null) throw new IOException("Missing Editor Kit"); Document doc = kit.createDefaultDocument(); 733 if (!(doc instanceof StyledDocument)) { 734 doc = new DefaultStyledDocument(new StyleContext()); 735 kit = new StyledEditorKit(); 736 jEditorPane1.setEditorKit(kit); 737 } 738 try { 739 kit.read(r, doc, 0); 740 } catch (javax.swing.text.BadLocationException e) { 741 throw new IOException("Can not locate the beginning of the document."); } finally { 743 r.close(); 744 } 745 kit.install(jEditorPane1); 746 jEditorPane1.setDocument(doc); 747 jEditorPane1.setEditable(false); 749 customizeEditor(jEditorPane1); 750 linesComp1 = new LinesComponent(jEditorPane1); 751 jScrollPane1.setRowHeaderView(linesComp1); 752 jViewport1 = jScrollPane1.getViewport(); 753 } 754 755 public void setSource2(Reader r) throws IOException { 756 EditorKit kit = jEditorPane2.getEditorKit(); 758 if (kit == null) throw new IOException("Missing Editor Kit"); Document doc = kit.createDefaultDocument(); 760 if (!(doc instanceof StyledDocument)) { 761 doc = new DefaultStyledDocument(new StyleContext()); 762 kit = new StyledEditorKit(); 763 jEditorPane2.setEditorKit(kit); 764 } 765 try { 766 kit.read(r, doc, 0); 767 } catch (javax.swing.text.BadLocationException e) { 768 throw new IOException("Can not locate the beginning of the document."); } finally { 770 r.close(); 771 } 772 kit.install(jEditorPane2); 773 jEditorPane2.setDocument(doc); 774 jEditorPane2.setEditable(false); 776 777 customizeEditor(jEditorPane2); 778 linesComp2 = new LinesComponent(jEditorPane2); 779 jScrollPane2.setRowHeaderView(linesComp2); 780 jViewport2 = jScrollPane2.getViewport(); 781 joinScrollBars(); 783 } 784 785 public void setSource1Title(String title) { 786 fileLabel1.setText(title); 787 fileLabel1.setMinimumSize(new Dimension(3, fileLabel1.getMinimumSize().height)); 789 } 790 791 public void setSource2Title(String title) { 792 fileLabel2.setText(title); 793 fileLabel2.setMinimumSize(new Dimension(3, fileLabel2.getMinimumSize().height)); 795 } 796 797 public void setMimeType1(String mime) { 798 EditorKit kit = CloneableEditorSupport.getEditorKit(mime); 799 jEditorPane1.setEditorKit(kit); 800 } 803 804 public void setMimeType2(String mime) { 805 EditorKit kit = CloneableEditorSupport.getEditorKit(mime); 806 jEditorPane2.setEditorKit(kit); 807 } 810 811 public void setDocument1(Document doc) { 812 if (doc != null) { 813 jEditorPane1.setDocument(doc); 814 } 815 } 816 817 public void setDocument2(Document doc) { 818 if (doc != null) { 819 jEditorPane2.setDocument(doc); 820 } 821 } 822 823 String getDocumentText1() { 824 return jEditorPane1.getText(); 825 } 826 827 String getDocumentText2() { 828 return jEditorPane2.getText(); 829 } 830 831 private void setHighlight(StyledDocument doc, int line1, int line2, java.awt.Color color) { 832 for(int line = line1-1; line < line2; line++) { 838 if (line < 0) continue; 839 try { 840 int offset = org.openide.text.NbDocument.findLineOffset(doc, line); 841 if (offset >= 0) { 843 Style s = doc.getLogicalStyle(offset); 844 if (s == null) { 845 s = doc.addStyle("diff-style("+color+"):1500", null); } 848 s.addAttribute(StyleConstants.ColorConstants.Background, color); 849 doc.setLogicalStyle(offset, s); 850 } 852 } catch (IndexOutOfBoundsException ex) { 853 ErrorManager.getDefault().annotate(ex, "#67631 reappreared. Please reopen with details."); ErrorManager.getDefault().notify(ex); 856 } 857 } 858 } 860 861 private void unhighlight(StyledDocument doc) { 862 int endOffset = doc.getEndPosition().getOffset(); 863 int endLine = org.openide.text.NbDocument.findLineNumber(doc, endOffset); 864 Style s = doc.addStyle("diff-style(white):1500", null); s.addAttribute(StyleConstants.ColorConstants.Background, java.awt.Color.white); 866 for(int line = 0; line <= endLine; line++) { 867 int offset = org.openide.text.NbDocument.findLineOffset(doc, line); 868 doc.setLogicalStyle(offset, s); 869 } 870 } 871 872 public void unhighlightAll() { 873 unhighlight((StyledDocument) jEditorPane1.getDocument()); 874 unhighlight((StyledDocument) jEditorPane2.getDocument()); 875 } 876 877 public void highlightRegion1(int line1, int line2, java.awt.Color color) { 878 StyledDocument doc = (StyledDocument) jEditorPane1.getDocument(); 879 setHighlight(doc, line1, line2, color); 880 } 881 882 public void highlightRegion2(int line1, int line2, java.awt.Color color) { 883 StyledDocument doc = (StyledDocument) jEditorPane2.getDocument(); 884 setHighlight(doc, line1, line2, color); 885 } 886 887 private void addEmptyLines(StyledDocument doc, int line, int numLines) { 888 int lastOffset = doc.getEndPosition().getOffset(); 889 int totLines = org.openide.text.NbDocument.findLineNumber(doc, lastOffset); 890 int offset = lastOffset-1; 892 if (line <= totLines) { 893 offset = org.openide.text.NbDocument.findLineOffset(doc, line); 894 } 896 String insStr = strCharacters('\n', numLines); 899 try { 900 doc.insertString(offset, insStr, null); 901 } catch (BadLocationException e) { 902 org.openide.ErrorManager.getDefault().notify(e); 903 } 904 } 906 907 public void addEmptyLines1(int line, int numLines) { 908 StyledDocument doc = (StyledDocument) jEditorPane1.getDocument(); 909 addEmptyLines(doc, line, numLines); 911 linesComp1.addEmptyLines(line, numLines); 912 } 913 914 public void addEmptyLines2(int line, int numLines) { 915 StyledDocument doc = (StyledDocument) jEditorPane2.getDocument(); 916 addEmptyLines(doc, line, numLines); 918 linesComp2.addEmptyLines(line, numLines); 919 } 920 921 922 private javax.swing.JViewport jViewport1; 923 private javax.swing.JViewport jViewport2; 924 931 final javax.swing.JPanel commandPanel = new javax.swing.JPanel (); 933 final javax.swing.JPanel editorPanel = new javax.swing.JPanel (); 934 final javax.swing.JLabel fileLabel1 = new javax.swing.JLabel (); 935 final javax.swing.JLabel fileLabel2 = new javax.swing.JLabel (); 936 final javax.swing.JPanel filePanel1 = new javax.swing.JPanel (); 937 final javax.swing.JPanel filePanel2 = new javax.swing.JPanel (); 938 final org.netbeans.modules.diff.builtin.visualizer.DEditorPane jEditorPane1 = new DEditorPane(); 939 final org.netbeans.modules.diff.builtin.visualizer.DEditorPane jEditorPane2 = new DEditorPane(); 940 final javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane (); 941 final javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane (); 942 final javax.swing.JSplitPane jSplitPane1 = new javax.swing.JSplitPane (); 943 final javax.swing.JButton nextButton = new javax.swing.JButton (); 944 final javax.swing.JButton prevButton = new javax.swing.JButton (); 945 947 1001 1002} 1003 | Popular Tags |