1 19 20 package org.netbeans.modules.merge.builtin.visualizer; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.FocusEvent ; 25 import java.awt.event.FocusListener ; 26 import java.awt.Dimension ; 27 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.modules.diff.builtin.visualizer.*; 34 35 import org.openide.actions.CopyAction; 36 import org.openide.actions.SaveAction; 37 import org.openide.util.actions.ActionPerformer; 38 import org.openide.util.actions.CallbackSystemAction; 39 import org.openide.util.actions.SystemAction; 40 41 import org.netbeans.modules.diff.builtin.visualizer.LinesComponent; 42 import org.netbeans.editor.EditorUI; 43 import org.netbeans.editor.ext.ExtCaret; 44 import org.openide.text.CloneableEditorSupport; 45 46 51 public class MergePanel extends javax.swing.JPanel implements java.awt.event.ActionListener , javax.swing.event.CaretListener { 52 53 public static final String ACTION_FIRST_CONFLICT = "firstConflict"; public static final String ACTION_LAST_CONFLICT = "lastConflict"; public static final String ACTION_PREVIOUS_CONFLICT = "previousConflict"; public static final String ACTION_NEXT_CONFLICT = "nextConflict"; public static final String ACTION_ACCEPT_RIGHT = "acceptRight"; public static final String ACTION_ACCEPT_LEFT = "acceptLeft"; 62 public static final String PROP_CAN_BE_SAVED = "canBeSaved"; public static final String PROP_CAN_NOT_BE_SAVED = "canNotBeSaved"; 65 private int totalHeight = 0; 67 private int totalLines = 0; 68 69 private int horizontalScroll1ChangedValue = -1; 70 private int horizontalScroll2ChangedValue = -1; 71 private int horizontalScroll3ChangedValue = -1; 72 private int verticalScroll1ChangedValue = -1; 73 private int verticalScroll3ChangedValue = -1; 74 75 private LinesComponent linesComp1; 76 private LinesComponent linesComp2; 77 private LinesComponent linesComp3; 78 79 89 private int[] resultLineNumbers; 90 91 private int numConflicts; 92 private int numUnresolvedConflicts; 93 private int currentConflictPos; 94 private List<Integer > resolvedLeftConflictsLineNumbers = new ArrayList<Integer >(); 95 private List<Integer > resolvedRightConflictsLineNumbers = new ArrayList<Integer >(); 96 97 private ArrayList<ActionListener > controlListeners = new ArrayList<ActionListener >(); 98 99 private SystemAction[] systemActions = new SystemAction[] { SaveAction.get(SaveAction.class), 100 null, 101 CloseMergeViewAction.get(CloseMergeViewAction.class) }; 102 103 static final long serialVersionUID =3683458237532937983L; 104 private static final String PLAIN_TEXT_MIME = "text/plain"; 105 106 107 public MergePanel() { 108 initComponents (); 110 firstConflictButton.setVisible(false); 112 lastConflictButton.setVisible(false); 113 prevConflictButton.setIcon(new ImageIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/prev.gif", true))); 114 nextConflictButton.setIcon(new ImageIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/next.gif", true))); 115 initActions(); 121 diffSplitPane.setResizeWeight(0.5); 122 mergeSplitPane.setResizeWeight(0.5); 123 putClientProperty("PersistenceType", "Never"); 124 jEditorPane1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane1A11yName")); jEditorPane1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane1A11yDescr")); jEditorPane2.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane2A11yName")); jEditorPane2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane2A11yDescr")); jEditorPane3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane3A11yName")); jEditorPane3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane3A11yDescr")); } 131 132 133 public void addNotify() { 134 super.addNotify(); 135 136 jEditorPane1.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); jEditorPane2.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); jEditorPane3.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); } 140 141 142 147 private void initComponents() { 149 java.awt.GridBagConstraints gridBagConstraints; 150 151 setLayout(new java.awt.GridBagLayout ()); 152 153 commandPanel.setLayout(new java.awt.GridBagLayout ()); 154 155 firstConflictButton.setPreferredSize(new java.awt.Dimension (24, 24)); 156 firstConflictButton.addActionListener(this); 157 commandPanel.add(firstConflictButton, new java.awt.GridBagConstraints ()); 158 159 prevConflictButton.setToolTipText(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.prevButton.toolTipText")); prevConflictButton.setMargin(new java.awt.Insets (1, 1, 0, 1)); 161 prevConflictButton.setMaximumSize(new java.awt.Dimension (24, 24)); 162 prevConflictButton.setMinimumSize(new java.awt.Dimension (24, 24)); 163 prevConflictButton.setPreferredSize(new java.awt.Dimension (24, 24)); 164 prevConflictButton.addActionListener(this); 165 gridBagConstraints = new java.awt.GridBagConstraints (); 166 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 167 gridBagConstraints.insets = new java.awt.Insets (0, 2, 0, 1); 168 commandPanel.add(prevConflictButton, gridBagConstraints); 169 170 nextConflictButton.setToolTipText(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.nextButton.toolTipText")); nextConflictButton.setMargin(new java.awt.Insets (1, 1, 0, 1)); 172 nextConflictButton.setMaximumSize(new java.awt.Dimension (24, 24)); 173 nextConflictButton.setMinimumSize(new java.awt.Dimension (24, 24)); 174 nextConflictButton.setPreferredSize(new java.awt.Dimension (24, 24)); 175 nextConflictButton.addActionListener(this); 176 gridBagConstraints = new java.awt.GridBagConstraints (); 177 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 178 gridBagConstraints.insets = new java.awt.Insets (0, 1, 0, 1); 179 commandPanel.add(nextConflictButton, gridBagConstraints); 180 181 lastConflictButton.setPreferredSize(new java.awt.Dimension (24, 24)); 182 lastConflictButton.addActionListener(this); 183 gridBagConstraints = new java.awt.GridBagConstraints (); 184 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 185 gridBagConstraints.insets = new java.awt.Insets (0, 1, 0, 2); 186 commandPanel.add(lastConflictButton, gridBagConstraints); 187 188 org.openide.awt.Mnemonics.setLocalizedText(statusLabel, "jLabel1"); 189 gridBagConstraints = new java.awt.GridBagConstraints (); 190 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 191 gridBagConstraints.weightx = 1.0; 192 gridBagConstraints.insets = new java.awt.Insets (0, 4, 0, 1); 193 commandPanel.add(statusLabel, gridBagConstraints); 194 195 gridBagConstraints = new java.awt.GridBagConstraints (); 196 gridBagConstraints.gridy = 0; 197 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 198 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 199 gridBagConstraints.weightx = 1.0; 200 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 201 add(commandPanel, gridBagConstraints); 202 203 editorPanel.setPreferredSize(new java.awt.Dimension (700, 600)); 204 editorPanel.setLayout(new java.awt.GridBagLayout ()); 205 206 mergeSplitPane.setDividerSize(4); 207 mergeSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); 208 209 diffSplitPane.setDividerSize(4); 210 211 filePanel1.setLayout(new java.awt.GridBagLayout ()); 212 213 leftCommandPanel.setLayout(new java.awt.GridBagLayout ()); 214 215 org.openide.awt.Mnemonics.setLocalizedText(acceptLeftButton, org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptLeftButton.text")); acceptLeftButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptLeftButton.textA11yDesc")); acceptLeftButton.addActionListener(this); 218 gridBagConstraints = new java.awt.GridBagConstraints (); 219 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE; 220 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 221 gridBagConstraints.insets = new java.awt.Insets (0, 2, 0, 1); 222 leftCommandPanel.add(acceptLeftButton, gridBagConstraints); 223 224 org.openide.awt.Mnemonics.setLocalizedText(acceptAndNextLeftButton, org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptAndNextLeftButton")); acceptAndNextLeftButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptAndNextLeftButtonA11yDesc")); acceptAndNextLeftButton.addActionListener(this); 227 gridBagConstraints = new java.awt.GridBagConstraints (); 228 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 229 gridBagConstraints.weightx = 1.0; 230 gridBagConstraints.insets = new java.awt.Insets (0, 4, 0, 2); 231 leftCommandPanel.add(acceptAndNextLeftButton, gridBagConstraints); 232 233 gridBagConstraints = new java.awt.GridBagConstraints (); 234 gridBagConstraints.gridy = 0; 235 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 236 gridBagConstraints.weightx = 1.0; 237 filePanel1.add(leftCommandPanel, gridBagConstraints); 238 239 jEditorPane1.addCaretListener(this); 240 jScrollPane1.setViewportView(jEditorPane1); 241 242 gridBagConstraints = new java.awt.GridBagConstraints (); 243 gridBagConstraints.gridy = 1; 244 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 245 gridBagConstraints.weightx = 1.0; 246 gridBagConstraints.weighty = 1.0; 247 filePanel1.add(jScrollPane1, gridBagConstraints); 248 249 org.openide.awt.Mnemonics.setLocalizedText(fileLabel1, "jLabel1"); 250 gridBagConstraints = new java.awt.GridBagConstraints (); 251 gridBagConstraints.gridy = 2; 252 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 253 gridBagConstraints.weightx = 1.0; 254 gridBagConstraints.insets = new java.awt.Insets (4, 4, 4, 4); 255 filePanel1.add(fileLabel1, gridBagConstraints); 256 257 diffSplitPane.setLeftComponent(filePanel1); 258 259 filePanel2.setLayout(new java.awt.GridBagLayout ()); 260 261 rightCommandPanel.setLayout(new java.awt.GridBagLayout ()); 262 263 org.openide.awt.Mnemonics.setLocalizedText(acceptRightButton, org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptRightButton.text")); acceptRightButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptRightButton.textA11yDesc")); acceptRightButton.addActionListener(this); 266 gridBagConstraints = new java.awt.GridBagConstraints (); 267 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 268 gridBagConstraints.insets = new java.awt.Insets (0, 2, 0, 1); 269 rightCommandPanel.add(acceptRightButton, gridBagConstraints); 270 271 org.openide.awt.Mnemonics.setLocalizedText(acceptAndNextRightButton, org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptAndNextRightButton")); acceptAndNextRightButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptAndNextRightButtonA11yDesc")); acceptAndNextRightButton.addActionListener(this); 274 gridBagConstraints = new java.awt.GridBagConstraints (); 275 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE; 276 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 277 gridBagConstraints.weightx = 1.0; 278 gridBagConstraints.insets = new java.awt.Insets (0, 4, 0, 2); 279 rightCommandPanel.add(acceptAndNextRightButton, gridBagConstraints); 280 281 gridBagConstraints = new java.awt.GridBagConstraints (); 282 gridBagConstraints.gridy = 0; 283 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 284 gridBagConstraints.weightx = 1.0; 285 filePanel2.add(rightCommandPanel, gridBagConstraints); 286 287 jEditorPane2.addCaretListener(this); 288 jScrollPane2.setViewportView(jEditorPane2); 289 290 gridBagConstraints = new java.awt.GridBagConstraints (); 291 gridBagConstraints.gridy = 1; 292 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 293 gridBagConstraints.weightx = 1.0; 294 gridBagConstraints.weighty = 1.0; 295 filePanel2.add(jScrollPane2, gridBagConstraints); 296 297 org.openide.awt.Mnemonics.setLocalizedText(fileLabel2, "jLabel2"); 298 gridBagConstraints = new java.awt.GridBagConstraints (); 299 gridBagConstraints.gridy = 2; 300 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 301 gridBagConstraints.weightx = 1.0; 302 gridBagConstraints.insets = new java.awt.Insets (4, 4, 4, 4); 303 filePanel2.add(fileLabel2, gridBagConstraints); 304 305 diffSplitPane.setRightComponent(filePanel2); 306 307 mergeSplitPane.setLeftComponent(diffSplitPane); 308 309 resultPanel.setLayout(new java.awt.GridBagLayout ()); 310 311 resultScrollPane.setViewportView(jEditorPane3); 312 313 gridBagConstraints = new java.awt.GridBagConstraints (); 314 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 315 gridBagConstraints.weightx = 1.0; 316 gridBagConstraints.weighty = 1.0; 317 resultPanel.add(resultScrollPane, gridBagConstraints); 318 319 org.openide.awt.Mnemonics.setLocalizedText(resultLabel, "jLabel1"); 320 gridBagConstraints = new java.awt.GridBagConstraints (); 321 gridBagConstraints.gridy = 1; 322 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 323 gridBagConstraints.insets = new java.awt.Insets (4, 4, 4, 4); 324 resultPanel.add(resultLabel, gridBagConstraints); 325 326 mergeSplitPane.setRightComponent(resultPanel); 327 328 gridBagConstraints = new java.awt.GridBagConstraints (); 329 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 330 gridBagConstraints.weightx = 1.0; 331 gridBagConstraints.weighty = 1.0; 332 editorPanel.add(mergeSplitPane, gridBagConstraints); 333 334 gridBagConstraints = new java.awt.GridBagConstraints (); 335 gridBagConstraints.gridy = 1; 336 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 337 gridBagConstraints.weightx = 1.0; 338 gridBagConstraints.weighty = 1.0; 339 add(editorPanel, gridBagConstraints); 340 } 341 342 344 public void actionPerformed(java.awt.event.ActionEvent evt) { 345 if (evt.getSource() == firstConflictButton) { 346 MergePanel.this.firstConflictButtonActionPerformed(evt); 347 } 348 else if (evt.getSource() == prevConflictButton) { 349 MergePanel.this.prevConflictButtonActionPerformed(evt); 350 } 351 else if (evt.getSource() == nextConflictButton) { 352 MergePanel.this.nextConflictButtonActionPerformed(evt); 353 } 354 else if (evt.getSource() == lastConflictButton) { 355 MergePanel.this.lastConflictButtonActionPerformed(evt); 356 } 357 else if (evt.getSource() == acceptLeftButton) { 358 MergePanel.this.acceptLeftButtonActionPerformed(evt); 359 } 360 else if (evt.getSource() == acceptAndNextLeftButton) { 361 MergePanel.this.acceptAndNextLeftButtonActionPerformed(evt); 362 } 363 else if (evt.getSource() == acceptRightButton) { 364 MergePanel.this.acceptRightButtonActionPerformed(evt); 365 } 366 else if (evt.getSource() == acceptAndNextRightButton) { 367 MergePanel.this.acceptAndNextRightButtonActionPerformed(evt); 368 } 369 } 370 371 public void caretUpdate(javax.swing.event.CaretEvent evt) { 372 if (evt.getSource() == jEditorPane1) { 373 MergePanel.this.jEditorPane1CaretUpdate(evt); 374 } 375 else if (evt.getSource() == jEditorPane2) { 376 MergePanel.this.jEditorPane2CaretUpdate(evt); 377 } 378 } 380 private void firstConflictButtonActionPerformed(java.awt.event.ActionEvent evt) { fireControlActionCommand(ACTION_FIRST_CONFLICT); 383 } 385 private void prevConflictButtonActionPerformed(java.awt.event.ActionEvent evt) { fireControlActionCommand(ACTION_PREVIOUS_CONFLICT); 388 } 390 private void nextConflictButtonActionPerformed(java.awt.event.ActionEvent evt) { fireControlActionCommand(ACTION_NEXT_CONFLICT); 393 } 395 private void lastConflictButtonActionPerformed(java.awt.event.ActionEvent evt) { fireControlActionCommand(ACTION_LAST_CONFLICT); 398 } 400 private void acceptRightButtonActionPerformed(java.awt.event.ActionEvent evt) { fireControlActionCommand(ACTION_ACCEPT_RIGHT); 403 } 405 private void acceptAndNextRightButtonActionPerformed(java.awt.event.ActionEvent evt) { fireControlActionCommand(ACTION_ACCEPT_RIGHT); 408 fireControlActionCommand(ACTION_NEXT_CONFLICT); 409 } 411 private void acceptAndNextLeftButtonActionPerformed(java.awt.event.ActionEvent evt) { fireControlActionCommand(ACTION_ACCEPT_LEFT); 414 fireControlActionCommand(ACTION_NEXT_CONFLICT); 415 } 417 private void acceptLeftButtonActionPerformed(java.awt.event.ActionEvent evt) { fireControlActionCommand(ACTION_ACCEPT_LEFT); 420 } 422 private void jEditorPane1CaretUpdate(javax.swing.event.CaretEvent evt) { 432 } 434 private void jEditorPane2CaretUpdate(javax.swing.event.CaretEvent evt) { 444 } 446 public void setNumConflicts(int numConflicts) { 447 this.numConflicts = numConflicts; 448 this.numUnresolvedConflicts = numConflicts; 449 } 450 451 public int getNumUnresolvedConflicts(){ 452 return numUnresolvedConflicts; 453 } 454 455 459 public void setCurrentLine(final int line, final int diffLength, final int conflictPos, 460 final int resultLine) { 461 if (line > 0) { 462 SwingUtilities.invokeLater(new Runnable () { 463 public void run() { 464 showLine12(line, diffLength); 465 showLine3(resultLine, diffLength); 466 if (conflictPos >= 0) MergePanel.this.currentConflictPos = conflictPos; 467 updateStatusLine(); 468 updateAcceptButtons(line); 469 } 470 }); 471 } 472 } 473 474 public void setNeedsSaveState(boolean needsSave) { 475 firePropertyChange((needsSave) ? PROP_CAN_BE_SAVED : PROP_CAN_NOT_BE_SAVED, null, null); 476 } 477 478 public synchronized void addControlActionListener(ActionListener listener) { 479 controlListeners.add(listener); 480 } 481 482 public synchronized void removeControlActionListener(ActionListener listener) { 483 controlListeners.remove(listener); 484 } 485 486 private void updateStatusLine() { 487 statusLabel.setText(org.openide.util.NbBundle.getMessage(MergePanel.class, 488 "MergePanel.statusLine", Integer.toString(currentConflictPos + 1), 489 Integer.toString(numConflicts), Integer.toString(numUnresolvedConflicts))); 490 } 491 492 private void updateAcceptButtons(int linePos) { 493 Integer conflictPos = new Integer (linePos); 494 boolean left = resolvedLeftConflictsLineNumbers.contains(conflictPos); 495 boolean right = resolvedRightConflictsLineNumbers.contains(conflictPos); 496 acceptLeftButton.setEnabled(!left); 497 acceptAndNextLeftButton.setEnabled(!left); 498 acceptRightButton.setEnabled(!right); 499 acceptAndNextRightButton.setEnabled(!right); 500 } 501 502 private void fireControlActionCommand(String command) { 503 ArrayList<ActionListener > listeners; 504 synchronized (this) { 505 listeners = new ArrayList<ActionListener >(controlListeners); 506 } 507 ActionEvent evt = new ActionEvent (this, 0, command); 508 for (ActionListener l: listeners) { 509 l.actionPerformed(evt); 510 } 511 } 512 513 private void jScrollBar1AdjustmentValueChanged (java.awt.event.AdjustmentEvent evt) { } 517 private void closeButtonActionPerformed (java.awt.event.ActionEvent evt) { exitForm(null); 520 } 522 523 private void exitForm(java.awt.event.WindowEvent evt) { 537 } 539 public void setSystemActions(SystemAction[] actions) { 540 this.systemActions = actions; 541 } 542 543 public SystemAction[] getSystemActions() { 544 return systemActions; 545 } 546 547 private void initActions() { 548 jEditorPane1.addFocusListener(new FocusListener () { 549 public void focusGained(FocusEvent e) { 550 editorActivated(jEditorPane1); 551 } 552 public void focusLost(FocusEvent e) { 553 editorDeactivated(jEditorPane1); 554 } 555 }); 556 jEditorPane2.addFocusListener(new FocusListener () { 557 public void focusGained(FocusEvent e) { 558 editorActivated(jEditorPane2); 559 } 560 public void focusLost(FocusEvent e) { 561 editorDeactivated(jEditorPane2); 562 } 563 }); 564 jEditorPane3.addFocusListener(new FocusListener () { 565 public void focusGained(FocusEvent e) { 566 editorActivated(jEditorPane3); 567 } 568 public void focusLost(FocusEvent e) { 569 editorDeactivated(jEditorPane3); 570 } 571 }); 572 } 573 574 private Hashtable<JEditorPane, Hashtable<Object , Action>> kitActions; 575 576 private PropertyChangeListener copyL; 577 private PropertyChangeListener copyP; 578 579 private Action getAction (String s, JEditorPane editor) { 580 if (kitActions == null) { 581 kitActions = new Hashtable<JEditorPane, Hashtable<Object , Action>>(); 582 } 583 Hashtable<Object , Action> actions = kitActions.get(editor); 584 if (actions == null) { 585 EditorKit kit = editor.getEditorKit(); 586 if (kit == null) { 587 return null; 588 } 589 590 Action[] a = kit.getActions (); 591 actions = new Hashtable<Object , Action> (a.length); 592 int k = a.length; 593 for (int i = 0; i < k; i++) 594 actions.put (a[i].getValue (Action.NAME), a[i]); 595 kitActions.put(editor, actions); 596 } 597 return actions.get (s); 598 } 599 600 private void editorActivated(final JEditorPane editor) { 601 final Action copy = getAction (DefaultEditorKit.copyAction, editor); 603 if (copy != null) { 604 final CallbackSystemAction sysCopy 605 = ((CallbackSystemAction) SystemAction.get (CopyAction.class)); 606 final ActionPerformer perf = new ActionPerformer () { 607 public void performAction (SystemAction action) { 608 copy.actionPerformed (new ActionEvent (editor, 0, "")); } 610 }; 611 sysCopy.setActionPerformer(copy.isEnabled() ? perf : null); 612 PropertyChangeListener copyListener; 613 copy.addPropertyChangeListener(copyListener = new PropertyChangeListener () { 614 public void propertyChange(PropertyChangeEvent evt) { 615 if ("enabled".equals(evt.getPropertyName())) { if (((Boolean )evt.getNewValue()).booleanValue()) { 617 sysCopy.setActionPerformer(perf); 618 } else if (sysCopy.getActionPerformer() == perf) { 619 sysCopy.setActionPerformer(null); 620 } 621 } 622 } 623 }); 624 if (editor.equals(jEditorPane1)) copyL = copyListener; 625 else copyP = copyListener; 626 } 627 } 628 629 private void editorDeactivated(JEditorPane editor) { 630 Action copy = getAction (DefaultEditorKit.copyAction, editor); 632 PropertyChangeListener copyListener; 633 if (editor.equals(jEditorPane1)) copyListener = copyL; 634 else copyListener = copyP; 635 if (copy != null) { 636 copy.removePropertyChangeListener(copyListener); 637 } 638 } 639 640 641 public void open() { 642 javax.swing.SwingUtilities.invokeLater(new Runnable () { 643 public void run() { 644 diffSplitPane.setDividerLocation(0.5); 645 mergeSplitPane.setDividerLocation(0.5); 646 openPostProcess(); 647 } 648 }); 649 } 650 651 protected void openPostProcess() { 652 javax.swing.SwingUtilities.invokeLater(new Runnable () { 653 public void run() { 654 initGlobalSizes(); 655 addChangeListeners(); 657 669 } 670 }); 671 } 672 673 680 private void initGlobalSizes() { 681 StyledDocument doc1 = (StyledDocument) jEditorPane1.getDocument(); 682 StyledDocument doc2 = (StyledDocument) jEditorPane2.getDocument(); 683 int numLines1 = org.openide.text.NbDocument.findLineNumber(doc1, doc1.getEndPosition().getOffset()); 684 int numLines2 = org.openide.text.NbDocument.findLineNumber(doc2, doc2.getEndPosition().getOffset()); 685 int numLines = Math.max(numLines1, numLines2); 686 if (numLines < 1) numLines = 1; 687 this.totalLines = numLines; 688 int totHeight = jEditorPane1.getSize().height; 690 int value = jEditorPane2.getSize().height; 692 if (value > totHeight) totHeight = value; 693 this.totalHeight = totHeight; 694 } 695 696 private void showLine12(int line, int diffLength) { 697 assert SwingUtilities.isEventDispatchThread(); 698 this.linesComp1.setActiveLine(line); 700 this.linesComp2.setActiveLine(line); 701 linesComp1.repaint(); 702 linesComp2.repaint(); 703 int padding = 5; 704 if (line <= 5) padding = line/2; 705 int off1, off2; 706 int ypos; 707 int viewHeight = jViewport1.getExtentSize().height; 708 java.awt.Point p1, p2; 709 initGlobalSizes(); p1 = jViewport1.getViewPosition(); 711 p2 = jViewport2.getViewPosition(); 712 ypos = (totalHeight*(line - padding - 1))/(totalLines + 1); 713 int viewSize = jViewport1.getViewRect().y; 714 if (ypos < p1.y || ypos + ((diffLength + padding)*totalHeight)/totalLines > p1.y + viewHeight) { 715 p1.y = ypos; 717 p2.y = ypos; 718 setViewPosition(p1, p2); 719 } 720 off1 = org.openide.text.NbDocument.findLineOffset((StyledDocument) jEditorPane1.getDocument(), line); 721 off2 = org.openide.text.NbDocument.findLineOffset((StyledDocument) jEditorPane2.getDocument(), line); 722 jEditorPane1.setCaretPosition(off1); 723 jEditorPane2.setCaretPosition(off2); 724 } 727 728 private void showLine3(int line, int diffLength) { 729 linesComp3.setActiveLine(line); 730 linesComp3.repaint(); 731 } 732 733 private void setViewPosition(java.awt.Point p1, java.awt.Point p2) { 734 assert SwingUtilities.isEventDispatchThread(); 735 jViewport1.setViewPosition(p1); 736 jViewport1.repaint(jViewport1.getViewRect()); 737 jViewport2.setViewPosition(p2); 738 jViewport2.repaint(jViewport2.getViewRect()); 739 } 740 741 private void joinScrollBars() { 742 final JScrollBar scrollBarH1 = jScrollPane1.getHorizontalScrollBar(); 743 final JScrollBar scrollBarV1 = jScrollPane1.getVerticalScrollBar(); 744 final JScrollBar scrollBarH2 = jScrollPane2.getHorizontalScrollBar(); 745 final JScrollBar scrollBarV2 = jScrollPane2.getVerticalScrollBar(); 746 final JScrollBar scrollBarH3 = resultScrollPane.getHorizontalScrollBar(); 747 final JScrollBar scrollBarV3 = resultScrollPane.getVerticalScrollBar(); 748 scrollBarV1.getModel().addChangeListener(new javax.swing.event.ChangeListener () { 749 public void stateChanged(javax.swing.event.ChangeEvent e) { 750 int value = scrollBarV1.getValue(); 751 int oldValue = scrollBarV2.getValue(); 752 if (oldValue != value) { 753 scrollBarV2.setValue(value); 754 } 757 if (value == verticalScroll1ChangedValue) return ; 759 int max1 = scrollBarV1.getMaximum(); 760 int max2 = scrollBarV3.getMaximum(); 761 int ext1 = scrollBarV1.getModel().getExtent(); 762 int ext2 = scrollBarV3.getModel().getExtent(); 763 if (max1 == ext1) verticalScroll3ChangedValue = 0; 764 else verticalScroll3ChangedValue = (value*(max2 - ext2))/(max1 - ext1); 765 verticalScroll1ChangedValue = -1; 766 scrollBarV3.setValue(verticalScroll3ChangedValue); 767 } 768 }); 769 jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension (0, 0)); 773 scrollBarV2.getModel().addChangeListener(new javax.swing.event.ChangeListener () { 774 public void stateChanged(javax.swing.event.ChangeEvent e) { 775 int value = scrollBarV2.getValue(); 776 int oldValue = scrollBarV1.getValue(); 777 if (oldValue != value) { 778 scrollBarV1.setValue(value); 779 } 781 } 782 }); 783 799 scrollBarH1.getModel().addChangeListener(new javax.swing.event.ChangeListener () { 800 public void stateChanged(javax.swing.event.ChangeEvent e) { 801 int value = scrollBarH1.getValue(); 802 if (value == horizontalScroll1ChangedValue) return; 804 int max1 = scrollBarH1.getMaximum(); 805 int max2 = scrollBarH2.getMaximum(); 806 int ext1 = scrollBarH1.getModel().getExtent(); 807 int ext2 = scrollBarH2.getModel().getExtent(); 808 if (max1 == ext1) horizontalScroll2ChangedValue = 0; 809 else horizontalScroll2ChangedValue = (value*(max2 - ext2))/(max1 - ext1); 810 horizontalScroll1ChangedValue = -1; 811 scrollBarH2.setValue(horizontalScroll2ChangedValue); 813 } 814 }); 815 scrollBarH2.getModel().addChangeListener(new javax.swing.event.ChangeListener () { 816 public void stateChanged(javax.swing.event.ChangeEvent e) { 817 int value = scrollBarH2.getValue(); 818 if (value == horizontalScroll2ChangedValue) return; 820 int max1 = scrollBarH1.getMaximum(); 821 int max2 = scrollBarH2.getMaximum(); 822 int max3 = scrollBarH3.getMaximum(); 823 int ext1 = scrollBarH1.getModel().getExtent(); 824 int ext2 = scrollBarH2.getModel().getExtent(); 825 int ext3 = scrollBarH3.getModel().getExtent(); 826 if (max2 == ext2) { 827 horizontalScroll1ChangedValue = 0; 828 horizontalScroll3ChangedValue = 0; 829 } else { 830 horizontalScroll1ChangedValue = (value*(max1 - ext1))/(max2 - ext2); 831 horizontalScroll3ChangedValue = (value*(max3 - ext3))/(max2 - ext2); 832 } 833 horizontalScroll2ChangedValue = -1; 834 scrollBarH1.setValue(horizontalScroll1ChangedValue); 836 scrollBarH3.setValue(horizontalScroll3ChangedValue); 837 } 838 }); 839 scrollBarH3.getModel().addChangeListener(new javax.swing.event.ChangeListener () { 840 public void stateChanged(javax.swing.event.ChangeEvent e) { 841 int value = scrollBarH3.getValue(); 842 if (value == horizontalScroll3ChangedValue) return; 844 int max1 = scrollBarH1.getMaximum(); 845 int max2 = scrollBarH2.getMaximum(); 846 int max3 = scrollBarH3.getMaximum(); 847 int ext1 = scrollBarH1.getModel().getExtent(); 848 int ext2 = scrollBarH2.getModel().getExtent(); 849 int ext3 = scrollBarH3.getModel().getExtent(); 850 if (max3 == ext3) { 851 horizontalScroll1ChangedValue = 0; 852 horizontalScroll2ChangedValue = 0; 853 } else { 854 horizontalScroll1ChangedValue = (value*(max1 - ext1))/(max3 - ext3); 855 horizontalScroll2ChangedValue = (value*(max2 - ext2))/(max3 - ext3); 856 } 857 horizontalScroll3ChangedValue = -1; 858 scrollBarH1.setValue(horizontalScroll1ChangedValue); 860 scrollBarH2.setValue(horizontalScroll2ChangedValue); 861 } 862 }); 863 diffSplitPane.setDividerLocation(0.5); 864 mergeSplitPane.setDividerLocation(0.5); 865 } 866 867 private String strCharacters(char c, int num) { 868 StringBuffer s = new StringBuffer (); 869 while(num-- > 0) { 870 s.append(c); 871 } 872 return s.toString(); 873 } 874 875 private void customizeEditor(JEditorPane editor) { 876 EditorKit kit = editor.getEditorKit(); 877 884 StyledDocument doc; 885 Document document = editor.getDocument(); 886 889 try { 890 doc = (StyledDocument) editor.getDocument(); 891 } catch(ClassCastException e) { 892 doc = new DefaultStyledDocument(); 893 try { 894 doc.insertString(0, document.getText(0, document.getLength()), null); 895 } catch (BadLocationException ble) { 896 } 898 editor.setDocument(doc); 899 } 900 919 } 921 922 private void addChangeListeners() { 923 jEditorPane1.addPropertyChangeListener("font", new java.beans.PropertyChangeListener () { 924 public void propertyChange(java.beans.PropertyChangeEvent evt) { 925 javax.swing.SwingUtilities.invokeLater(new Runnable () { 927 public void run() { 928 initGlobalSizes(); 929 linesComp1.changedAll(); 930 } 931 }); 932 } 933 }); 934 jEditorPane2.addPropertyChangeListener("font", new java.beans.PropertyChangeListener () { 935 public void propertyChange(java.beans.PropertyChangeEvent evt) { 936 javax.swing.SwingUtilities.invokeLater(new Runnable () { 938 public void run() { 939 initGlobalSizes(); 940 linesComp2.changedAll(); 941 } 942 }); 943 } 944 }); 945 jEditorPane3.addPropertyChangeListener("font", new java.beans.PropertyChangeListener () { 946 public void propertyChange(java.beans.PropertyChangeEvent evt) { 947 javax.swing.SwingUtilities.invokeLater(new Runnable () { 949 public void run() { 950 initGlobalSizes(); 951 linesComp3.changedAll(); 952 } 953 }); 954 } 955 }); 956 } 957 958 public void setSource1(Reader r) throws IOException { 959 EditorKit kit = jEditorPane1.getEditorKit(); 962 if (kit == null) throw new IOException("Missing Editor Kit"); Document doc = kit.createDefaultDocument(); 964 if (!(doc instanceof StyledDocument)) { 965 doc = new DefaultStyledDocument(new StyleContext()); 966 kit = new StyledEditorKit(); 967 jEditorPane1.setEditorKit(kit); 968 } 969 try { 970 kit.read(r, doc, 0); 971 } catch (javax.swing.text.BadLocationException e) { 972 throw new IOException("Can not locate the beginning of the document."); } finally { 974 r.close(); 975 } 976 kit.install(jEditorPane1); 977 jEditorPane1.setDocument(doc); 978 jEditorPane1.setEditable(false); 980 customizeEditor(jEditorPane1); 981 linesComp1 = new LinesComponent(jEditorPane1); 982 jScrollPane1.setRowHeaderView(linesComp1); 983 jViewport1 = jScrollPane1.getViewport(); 984 } 985 986 public void setSource2(Reader r) throws IOException { 987 EditorKit kit = jEditorPane2.getEditorKit(); 989 if (kit == null) throw new IOException("Missing Editor Kit"); Document doc = kit.createDefaultDocument(); 991 if (!(doc instanceof StyledDocument)) { 992 doc = new DefaultStyledDocument(new StyleContext()); 993 kit = new StyledEditorKit(); 994 jEditorPane2.setEditorKit(kit); 995 } 996 try { 997 kit.read(r, doc, 0); 998 } catch (javax.swing.text.BadLocationException e) { 999 throw new IOException("Can not locate the beginning of the document."); } finally { 1001 r.close(); 1002 } 1003 kit.install(jEditorPane2); 1004 jEditorPane2.setDocument(doc); 1005 jEditorPane2.setEditable(false); 1007 1008 customizeEditor(jEditorPane2); 1009 linesComp2 = new LinesComponent(jEditorPane2); 1010 jScrollPane2.setRowHeaderView(linesComp2); 1011 jViewport2 = jScrollPane2.getViewport(); 1012 joinScrollBars(); 1014 } 1015 1016 public void setResultSource(Reader r) throws IOException { 1017 EditorKit kit = jEditorPane3.getEditorKit(); 1018 if (kit == null) throw new IOException("Missing Editor Kit"); Document doc = kit.createDefaultDocument(); 1020 if (!(doc instanceof StyledDocument)) { 1021 doc = new DefaultStyledDocument(new StyleContext()); 1022 kit = new StyledEditorKit(); 1023 jEditorPane3.setEditorKit(kit); 1024 } 1025 try { 1026 kit.read(r, doc, 0); 1027 } catch (javax.swing.text.BadLocationException e) { 1028 throw new IOException("Can not locate the beginning of the document."); } finally { 1030 r.close(); 1031 } 1032 kit.install(jEditorPane3); 1033 jEditorPane3.setDocument(doc); 1034 jEditorPane3.setEditable(false); 1036 customizeEditor(jEditorPane3); 1037 linesComp3 = new LinesComponent(jEditorPane3); 1038 resultScrollPane.setRowHeaderView(linesComp3); 1039 resultLineNumbers = new int[1]; 1040 assureResultLineNumbersLength( 1041 org.openide.text.NbDocument.findLineNumber((StyledDocument) doc, 1042 doc.getEndPosition().getOffset()) + 1); 1043 for (int i = 0; i < resultLineNumbers.length; i++) resultLineNumbers[i] = i; 1044 } 1045 1046 private static final int EXTRA_CAPACITY = 5; 1047 private void assureResultLineNumbersLength(int length) { 1048 if (length > resultLineNumbers.length) { 1049 int[] newrln = new int[length + EXTRA_CAPACITY]; 1050 System.arraycopy(resultLineNumbers, 0, newrln, 0, resultLineNumbers.length); 1051 resultLineNumbers = newrln; 1052 } 1053 } 1054 1055 1062 public void copySource1ToResult(int line1, int line2, int line3) { 1063 StyledDocument doc1 = (StyledDocument) jEditorPane1.getDocument(); 1064 StyledDocument doc2 = (StyledDocument) jEditorPane3.getDocument(); 1065 try { 1066 copy(doc1, line1, line2, doc2, line3); 1067 } catch (BadLocationException e) { 1068 org.openide.ErrorManager.getDefault().notify(e); 1069 } 1070 } 1071 1072 1079 public void copySource2ToResult(int line1, int line2, int line3) { 1080 StyledDocument doc1 = (StyledDocument) jEditorPane2.getDocument(); 1081 StyledDocument doc2 = (StyledDocument) jEditorPane3.getDocument(); 1082 try { 1083 copy(doc1, line1, line2, doc2, line3); 1084 } catch (BadLocationException e) { 1085 org.openide.ErrorManager.getDefault().notify(e); 1086 } 1087 } 1088 1089 1090 private void copy(StyledDocument doc1, int line1, int line2, StyledDocument doc2, int line3) throws BadLocationException { 1091 int offset1 = org.openide.text.NbDocument.findLineOffset(doc1, line1 - 1); 1092 int offset2 = (line2 >= 0) ? org.openide.text.NbDocument.findLineOffset(doc1, line2) 1093 : (doc1.getLength() - 1); 1094 if (offset1 >= offset2) return ; 1095 int offset3 = org.openide.text.NbDocument.findLineOffset(doc2, line3-1); 1096 int length = offset2 - offset1; 1097 if (line2 < 0) length++; 1098 String text = doc1.getText(offset1, length); 1099 doc2.insertString(offset3, text, null); 1102 if (line2 < 0) line2 = org.openide.text.NbDocument.findLineNumber(doc1, doc1.getLength()); 1104 int numLines = line2 - line1 + 1; 1105 assureResultLineNumbersLength(line3 + numLines); 1107 if (resultLineNumbers[line3] == 0 && line3 > 0) resultLineNumbers[line3] = resultLineNumbers[line3 - 1] + 1; 1108 int resultLine = resultLineNumbers[line3]; 1109 linesComp3.insertNumbers(line3 - 1, resultLine, numLines); 1112 linesComp3.changedAll(); 1113 for (int i = 0; i < numLines; i++) resultLineNumbers[line3 + i] = resultLine + i; 1114 } 1115 1116 1124 public void replaceSource1InResult(int line1, int line2, int line3, int line4) { 1125 Integer conflictLine = new Integer ((line1 > 0) ? line1 : 1); 1127 if (resolvedLeftConflictsLineNumbers.contains(conflictLine)) return ; 1129 StyledDocument doc1 = (StyledDocument) jEditorPane1.getDocument(); 1130 StyledDocument doc2 = (StyledDocument) jEditorPane3.getDocument(); 1131 try { 1132 replace(doc1, line1, line2, doc2, line3, line4); 1133 } catch (BadLocationException e) { 1134 org.openide.ErrorManager.getDefault().notify(e); 1135 } 1136 if (resolvedRightConflictsLineNumbers.contains(conflictLine)) { 1137 resolvedRightConflictsLineNumbers.remove(conflictLine); 1138 } else { 1139 numUnresolvedConflicts--; 1141 updateStatusLine(); 1142 } 1143 resolvedLeftConflictsLineNumbers.add(conflictLine); 1144 updateAcceptButtons(line1); 1145 } 1146 1147 1155 public void replaceSource2InResult(int line1, int line2, int line3, int line4) { 1156 Integer conflictLine = new Integer ((line1 > 0) ? line1 : 1); 1158 if (resolvedRightConflictsLineNumbers.contains(conflictLine)) return ; 1160 StyledDocument doc1 = (StyledDocument) jEditorPane2.getDocument(); 1161 StyledDocument doc2 = (StyledDocument) jEditorPane3.getDocument(); 1162 try { 1163 replace(doc1, line1, line2, doc2, line3, line4); 1164 } catch (BadLocationException e) { 1165 org.openide.ErrorManager.getDefault().notify(e); 1166 } 1167 if (resolvedLeftConflictsLineNumbers.contains(conflictLine)) { 1168 resolvedLeftConflictsLineNumbers.remove(conflictLine); 1169 } else { 1170 numUnresolvedConflicts--; 1172 updateStatusLine(); 1173 } 1174 resolvedRightConflictsLineNumbers.add(conflictLine); 1175 updateAcceptButtons(line1); 1176 } 1177 1178 private void replace(StyledDocument doc1, int line1, int line2, 1179 StyledDocument doc2, int line3, int line4) throws BadLocationException { 1180 int offset1 = (line1 > 0) ? org.openide.text.NbDocument.findLineOffset(doc1, line1 - 1) 1183 : 0; 1184 int offset2 = (line2 >= 0) ? org.openide.text.NbDocument.findLineOffset(doc1, line2) 1185 : (doc1.getLength() - 1); 1186 int offset3 = (line3 > 0) ? org.openide.text.NbDocument.findLineOffset(doc2, line3 - 1) 1187 : 0; 1188 int offset4 = (line4 >= 0) ? org.openide.text.NbDocument.findLineOffset(doc2, line4) 1189 : (doc2.getLength() - 1); 1190 int length = offset2 - offset1; 1192 if (line2 < 0) length++; 1193 String text = doc1.getText(offset1, length); 1194 doc2.remove(offset3, offset4 - offset3); 1195 doc2.insertString(offset3, text, null); 1196 assureResultLineNumbersLength(line4); 1198 int physicalLineDiff = line2 - line1 - (line4 - line3); 1200 if (physicalLineDiff > 0) { 1201 System.arraycopy(resultLineNumbers, line4 + 1, 1202 resultLineNumbers, line4 + physicalLineDiff + 1, 1203 resultLineNumbers.length - line4 - physicalLineDiff - 1); 1204 } 1207 int lineDiff = (resultLineNumbers[line3] <= resultLineNumbers[line3 - 1]) 1208 ? (line2 - line1 + 1) 1209 : (line2 - line1 - (line4 - line3)); 1210 int n = resultLineNumbers[line3 - 1]; 1214 for (int i = line3; i <= line4 + physicalLineDiff; i++) { 1215 resultLineNumbers[i] = ++n; 1216 } 1217 1223 linesComp3.insertNumbers(line3 - 1, resultLineNumbers[line3], line2 - line1 + 1); 1226 linesComp3.changedAll(); 1227 if (physicalLineDiff < 0) { 1232 System.arraycopy(resultLineNumbers, line4 + 1, 1233 resultLineNumbers, line4 + physicalLineDiff + 1, 1234 resultLineNumbers.length - line4 - 1); 1235 } 1238 adjustLineNumbers(line4 + physicalLineDiff + 1, lineDiff); 1239 1240 int line = -1; 1242 for (int i = 0; i< resultLineNumbers.length; i++) { 1243 if (resultLineNumbers[i] < line) { 1244 resultLineNumbers[i] = line; 1245 } 1246 line = resultLineNumbers[i]; 1247 } 1248 } 1249 1250 1265 1266 private void adjustLineNumbers(int startLine, int shift) { 1267 int end = resultLineNumbers.length; 1269 while (end > 0 && resultLineNumbers[end - 1] == 0) end--; 1270 int startSetLine = -1; 1271 int endSetLine = -1; 1272 for (int i = startLine; i < end; i++) { 1274 resultLineNumbers[i] += shift; 1275 if (resultLineNumbers[i] <= resultLineNumbers[i - 1]) { 1276 if (startSetLine > 0) { 1277 linesComp3.insertNumbers(startSetLine - 1, resultLineNumbers[startSetLine], i - startSetLine); 1279 linesComp3.changedAll(); 1280 startSetLine = -1; 1282 } 1283 if (endSetLine < 0) { 1284 endSetLine = i; 1285 } 1286 } else { 1287 if (endSetLine > 0) { 1288 linesComp3.removeNumbers(endSetLine - 1, i - endSetLine); 1290 linesComp3.changedAll(); 1291 endSetLine = -1; 1293 } 1294 if (startSetLine < 0) { 1295 startSetLine = i; 1296 } 1297 } 1298 } 1299 if (startSetLine > 0) { 1300 linesComp3.insertNumbers(startSetLine - 1, resultLineNumbers[startSetLine], end - startSetLine); 1302 linesComp3.shrink(end - 1); 1303 linesComp3.changedAll(); 1304 } 1306 if (endSetLine > 0) { 1307 linesComp3.removeNumbers(endSetLine - 1, end - endSetLine); 1309 linesComp3.shrink(end - 1); 1310 linesComp3.changedAll(); 1311 } 1313 } 1314 1315 public void setSource1Title(String title) { 1316 fileLabel1.setText(title); 1317 } 1318 1319 public void setSource2Title(String title) { 1320 fileLabel2.setText(title); 1321 } 1322 1323 public void setResultSourceTitle(String title) { 1324 resultLabel.setText(title); 1325 } 1326 1327 public void setStatusLabel(String status) { 1328 statusLabel.setText(status); 1329 } 1330 1331 public void setMimeType1(String mime) { 1332 EditorKit kit = CloneableEditorSupport.getEditorKit(mime); 1333 jEditorPane1.setEditorKit(kit); 1334 } 1337 1338 public void setMimeType2(String mime) { 1339 EditorKit kit = CloneableEditorSupport.getEditorKit(mime); 1340 jEditorPane2.setEditorKit(kit); 1341 } 1344 1345 public void setMimeType3(String mime) { 1346 EditorKit kit = CloneableEditorSupport.getEditorKit(mime); 1347 jEditorPane3.setEditorKit(kit); 1348 } 1349 1350 1357 1358 public void setResultDocument(Document doc) { 1359 if (doc != null) { 1360 jEditorPane3.setDocument(doc); 1361 jEditorPane3.setEditable(false); 1362 linesComp3 = new LinesComponent(jEditorPane3); 1363 resultScrollPane.setRowHeaderView(linesComp3); 1364 } 1365 } 1366 1367 1375 1376 1381 public void writeResult(Writer w, boolean stripLastNewline) throws IOException { 1382 1391 int end = resultLineNumbers.length; 1392 while (end > 0 && resultLineNumbers[end - 1] == 0) end--; 1393 int startSetLine = -1; 1394 StyledDocument doc = (StyledDocument) jEditorPane3.getDocument(); 1395 try { 1396 for (int i = 1; i < end; i++) { 1397 if (resultLineNumbers[i] <= resultLineNumbers[i - 1]) { 1398 if (startSetLine > 0) { 1399 int offsetStart = org.openide.text.NbDocument.findLineOffset(doc, startSetLine - 1); 1401 int offsetEnd = org.openide.text.NbDocument.findLineOffset(doc, i - 1); 1402 try { 1404 writeText(w, doc.getText(offsetStart, offsetEnd - offsetStart)); 1406 } catch (BadLocationException blex) { 1407 throw new IOException(blex.getLocalizedMessage()); 1408 } 1409 startSetLine = -1; 1411 } 1412 } else { 1413 if (startSetLine < 0) { 1414 startSetLine = i; 1415 } 1416 } 1417 } 1418 if (startSetLine > 0) { 1419 int offsetStart = org.openide.text.NbDocument.findLineOffset(doc, startSetLine - 1); 1421 int offsetEnd = doc.getLength(); 1422 try { 1423 String text = doc.getText(offsetStart, offsetEnd - offsetStart); 1424 if (stripLastNewline && text.endsWith("\n")) { 1425 text = text.substring(0, text.length() - 1); 1426 } 1427 writeText(w, text); 1428 } catch (BadLocationException blex) { 1429 throw new IOException(blex.getLocalizedMessage()); 1430 } 1431 } 1433 } finally { 1434 w.close(); 1435 } 1436 } 1437 1438 private void writeText(Writer w, String text) throws IOException { 1439 text = text.replaceAll("\n", System.getProperty("line.separator")); 1440 w.write(text); 1441 } 1442 1443 private void setHighlight(final StyledDocument doc, final int line1, 1444 final int line2, final java.awt.Color color) { 1445 if (!SwingUtilities.isEventDispatchThread()) { 1446 SwingUtilities.invokeLater(new Runnable () { 1447 public void run() { 1448 setHighlight(doc, line1, line2, color); 1449 } 1450 }); 1451 } 1452 Style s = doc.getStyle("diff-style("+color+"):1500"); 1458 if (s == null) { 1460 s = doc.addStyle("diff-style("+color+"):1500", null); s.addAttribute(StyleConstants.ColorConstants.Background, color); 1463 } 1464 for(int line = line1-1; line < line2; line++) { 1465 if (line < 0) continue; 1466 int offset = org.openide.text.NbDocument.findLineOffset(doc, line); 1467 if (offset >= 0) { 1469 1480 doc.setLogicalStyle(offset, s); 1481 } 1483 } 1484 } 1486 1487 private void unhighlight(StyledDocument doc) { 1488 int endOffset = doc.getEndPosition().getOffset(); 1489 int endLine = org.openide.text.NbDocument.findLineNumber(doc, endOffset); 1490 Style s = doc.addStyle("diff-style(white):1500", null); s.addAttribute(StyleConstants.ColorConstants.Background, java.awt.Color.white); 1492 for(int line = 0; line <= endLine; line++) { 1493 int offset = org.openide.text.NbDocument.findLineOffset(doc, line); 1494 doc.setLogicalStyle(offset, s); 1495 } 1496 } 1497 1498 public void unhighlightAll() { 1499 unhighlight((StyledDocument) jEditorPane1.getDocument()); 1500 unhighlight((StyledDocument) jEditorPane2.getDocument()); 1501 } 1502 1503 public void highlightRegion1(int line1, int line2, java.awt.Color color) { 1504 StyledDocument doc = (StyledDocument) jEditorPane1.getDocument(); 1505 setHighlight(doc, line1, line2, color); 1506 } 1507 1508 public void highlightRegion2(int line1, int line2, java.awt.Color color) { 1509 StyledDocument doc = (StyledDocument) jEditorPane2.getDocument(); 1510 setHighlight(doc, line1, line2, color); 1511 } 1512 1513 public void highlightRegion3(int line1, int line2, java.awt.Color color) { 1514 StyledDocument doc = (StyledDocument) jEditorPane3.getDocument(); 1515 setHighlight(doc, line1, line2, color); 1516 } 1517 1518 public void unhighlightRegion3(int line1, int line2) { 1519 StyledDocument doc = (StyledDocument) jEditorPane3.getDocument(); 1520 setHighlight(doc, line1, line2, jEditorPane3.getBackground()); 1521 } 1522 1523 private void addEmptyLines(StyledDocument doc, int line, int numLines) { 1524 int lastOffset = doc.getEndPosition().getOffset(); 1525 int totLines = org.openide.text.NbDocument.findLineNumber(doc, lastOffset); 1526 int offset = lastOffset; 1528 if (line <= totLines) { 1529 offset = org.openide.text.NbDocument.findLineOffset(doc, line); 1530 } 1532 String insStr = strCharacters('\n', numLines); 1535 try { 1537 doc.insertString(offset, insStr, null); 1538 } catch (BadLocationException e) { 1539 org.openide.ErrorManager.getDefault().notify(e); 1540 } 1541 } 1543 1544 public void addEmptyLines1(int line, int numLines) { 1545 StyledDocument doc = (StyledDocument) jEditorPane1.getDocument(); 1546 addEmptyLines(doc, line, numLines); 1548 linesComp1.addEmptyLines(line, numLines); 1549 } 1550 1551 public void addEmptyLines2(int line, int numLines) { 1552 StyledDocument doc = (StyledDocument) jEditorPane2.getDocument(); 1553 addEmptyLines(doc, line, numLines); 1555 linesComp2.addEmptyLines(line, numLines); 1556 } 1557 1558 public void addEmptyLines3(int line, int numLines) { 1559 StyledDocument doc = (StyledDocument) jEditorPane3.getDocument(); 1560 addEmptyLines(doc, line - 1, numLines); 1562 linesComp3.addEmptyLines(line - 1, numLines); 1563 assureResultLineNumbersLength(line + numLines); 1564 if (resultLineNumbers[line] == 0 && line > 0) resultLineNumbers[line] = resultLineNumbers[line - 1]; 1565 int resultLine = resultLineNumbers[line]; 1566 for (int i = 1; i < numLines; i++) resultLineNumbers[line + i] = resultLine; 1567 } 1568 1569 1570 private javax.swing.JViewport jViewport1; 1571 private javax.swing.JViewport jViewport2; 1572 1573 final javax.swing.JButton acceptAndNextLeftButton = new javax.swing.JButton (); 1575 final javax.swing.JButton acceptAndNextRightButton = new javax.swing.JButton (); 1576 final javax.swing.JButton acceptLeftButton = new javax.swing.JButton (); 1577 final javax.swing.JButton acceptRightButton = new javax.swing.JButton (); 1578 final javax.swing.JPanel commandPanel = new javax.swing.JPanel (); 1579 final javax.swing.JSplitPane diffSplitPane = new javax.swing.JSplitPane (); 1580 final javax.swing.JPanel editorPanel = new javax.swing.JPanel (); 1581 final javax.swing.JLabel fileLabel1 = new javax.swing.JLabel (); 1582 final javax.swing.JLabel fileLabel2 = new javax.swing.JLabel (); 1583 final javax.swing.JPanel filePanel1 = new javax.swing.JPanel (); 1584 final javax.swing.JPanel filePanel2 = new javax.swing.JPanel (); 1585 final javax.swing.JButton firstConflictButton = new javax.swing.JButton (); 1586 final javax.swing.JEditorPane jEditorPane1 = new DEditorPane(); 1587 final javax.swing.JEditorPane jEditorPane2 = new DEditorPane(); 1588 final javax.swing.JEditorPane jEditorPane3 = new DEditorPane(); 1589 final javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane (); 1590 final javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane (); 1591 final javax.swing.JButton lastConflictButton = new javax.swing.JButton (); 1592 final javax.swing.JPanel leftCommandPanel = new javax.swing.JPanel (); 1593 final javax.swing.JSplitPane mergeSplitPane = new javax.swing.JSplitPane (); 1594 final javax.swing.JButton nextConflictButton = new javax.swing.JButton (); 1595 final javax.swing.JButton prevConflictButton = new javax.swing.JButton (); 1596 final javax.swing.JLabel resultLabel = new javax.swing.JLabel (); 1597 final javax.swing.JPanel resultPanel = new javax.swing.JPanel (); 1598 final javax.swing.JScrollPane resultScrollPane = new javax.swing.JScrollPane (); 1599 final javax.swing.JPanel rightCommandPanel = new javax.swing.JPanel (); 1600 final javax.swing.JLabel statusLabel = new javax.swing.JLabel (); 1601 1603} 1604 | Popular Tags |