KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > merge > builtin > visualizer > MergePanel


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
20 package org.netbeans.modules.merge.builtin.visualizer;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.FocusEvent JavaDoc;
25 import java.awt.event.FocusListener JavaDoc;
26 import java.awt.Dimension JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
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 /**
47  * This class displays two editor panes with two files and marks the differences
48  * by a different color.
49  * @author Martin Entlicher
50  */

51 public class MergePanel extends javax.swing.JPanel JavaDoc implements java.awt.event.ActionListener JavaDoc, javax.swing.event.CaretListener JavaDoc {
52     
53     public static final String JavaDoc ACTION_FIRST_CONFLICT = "firstConflict"; // NOI18N
54
public static final String JavaDoc ACTION_LAST_CONFLICT = "lastConflict"; // NOI18N
55
public static final String JavaDoc ACTION_PREVIOUS_CONFLICT = "previousConflict"; // NOI18N
56
public static final String JavaDoc ACTION_NEXT_CONFLICT = "nextConflict"; // NOI18N
57
public static final String JavaDoc ACTION_ACCEPT_RIGHT = "acceptRight"; // NOI18N
58
//public static final String ACTION_ACCEPT_RIGHT_AND_NEXT = "acceptRightAndNext"; // NOI18N
59
public static final String JavaDoc ACTION_ACCEPT_LEFT = "acceptLeft"; // NOI18N
60
//public static final String ACTION_ACCEPT_LEFT_AND_NEXT = "acceptLeftAndNext"; // NOI18N
61

62     public static final String JavaDoc PROP_CAN_BE_SAVED = "canBeSaved"; // NOI18N
63
public static final String JavaDoc PROP_CAN_NOT_BE_SAVED = "canNotBeSaved"; // NOI18N
64

65 // private AbstractDiff diff = null;
66
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     /**
80      * Line numbers in the result document. The indexes are "physical" document line numbers,
81      * and values are "logical" document line numbers. If there is a space inserted (a conflict),
82      * the corresponding document content is not defined and logical document line numbers
83      * do not grow.
84      * If the conflict starts from the beginning of the file, the logical line numbers are '0',
85      * if the conflict is in the middle of the file, the logical line numbers are euqal to
86      * the last logical line before this conflict.
87      * The line numbers start from '1'.
88      */

89     private int[] resultLineNumbers;
90     
91     private int numConflicts;
92     private int numUnresolvedConflicts;
93     private int currentConflictPos;
94     private List<Integer JavaDoc> resolvedLeftConflictsLineNumbers = new ArrayList<Integer JavaDoc>();
95     private List<Integer JavaDoc> resolvedRightConflictsLineNumbers = new ArrayList<Integer JavaDoc>();
96
97     private ArrayList<ActionListener JavaDoc> controlListeners = new ArrayList<ActionListener JavaDoc>();
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 JavaDoc PLAIN_TEXT_MIME = "text/plain";
105
106     /** Creates new DiffComponent from AbstractDiff object*/
107     public MergePanel() {
108 // this.diff = diff;
109
initComponents ();
110         // TODO Get icons for these buttons
111
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         //prevConflictButton.setIcon(new ImageIcon(getClass().getResource("/org/netbeans/modules/diff/builtin/visualizer/prev.gif")));
116
//nextConflictButton.setIcon(new ImageIcon(getClass().getResource("/org/netbeans/modules/diff/builtin/visualizer/next.gif")));
117
//setTitle(org.openide.util.NbBundle.getBundle(DiffComponent.class).getString("DiffComponent.title"));
118
//setName(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.title"));
119
//HelpCtx.setHelpIDString (getRootPane (), DiffComponent.class.getName ());
120
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")); // NOI18N
125
jEditorPane1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane1A11yDescr")); // NOI18N
126
jEditorPane2.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane2A11yName")); // NOI18N
127
jEditorPane2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane2A11yDescr")); // NOI18N
128
jEditorPane3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane3A11yName")); // NOI18N
129
jEditorPane3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane3A11yDescr")); // NOI18N
130
}
131
132
133     public void addNotify() {
134         super.addNotify();
135
136         jEditorPane1.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); //NOI18N
137
jEditorPane2.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); //NOI18N
138
jEditorPane3.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$"); //NOI18N
139
}
140
141
142     /** This method is called from within the constructor to
143      * initialize the form.
144      * WARNING: Do NOT modify this code. The content of this method is
145      * always regenerated by the FormEditor.
146      */

147     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
148
private void initComponents() {
149         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
150
151         setLayout(new java.awt.GridBagLayout JavaDoc());
152
153         commandPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
154
155         firstConflictButton.setPreferredSize(new java.awt.Dimension JavaDoc(24, 24));
156         firstConflictButton.addActionListener(this);
157         commandPanel.add(firstConflictButton, new java.awt.GridBagConstraints JavaDoc());
158
159         prevConflictButton.setToolTipText(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.prevButton.toolTipText")); // NOI18N
160
prevConflictButton.setMargin(new java.awt.Insets JavaDoc(1, 1, 0, 1));
161         prevConflictButton.setMaximumSize(new java.awt.Dimension JavaDoc(24, 24));
162         prevConflictButton.setMinimumSize(new java.awt.Dimension JavaDoc(24, 24));
163         prevConflictButton.setPreferredSize(new java.awt.Dimension JavaDoc(24, 24));
164         prevConflictButton.addActionListener(this);
165         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
166         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
167         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 2, 0, 1);
168         commandPanel.add(prevConflictButton, gridBagConstraints);
169
170         nextConflictButton.setToolTipText(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.nextButton.toolTipText")); // NOI18N
171
nextConflictButton.setMargin(new java.awt.Insets JavaDoc(1, 1, 0, 1));
172         nextConflictButton.setMaximumSize(new java.awt.Dimension JavaDoc(24, 24));
173         nextConflictButton.setMinimumSize(new java.awt.Dimension JavaDoc(24, 24));
174         nextConflictButton.setPreferredSize(new java.awt.Dimension JavaDoc(24, 24));
175         nextConflictButton.addActionListener(this);
176         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
177         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
178         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 1, 0, 1);
179         commandPanel.add(nextConflictButton, gridBagConstraints);
180
181         lastConflictButton.setPreferredSize(new java.awt.Dimension JavaDoc(24, 24));
182         lastConflictButton.addActionListener(this);
183         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
184         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
185         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 1, 0, 2);
186         commandPanel.add(lastConflictButton, gridBagConstraints);
187
188         org.openide.awt.Mnemonics.setLocalizedText(statusLabel, "jLabel1");
189         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
190         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
191         gridBagConstraints.weightx = 1.0;
192         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 4, 0, 1);
193         commandPanel.add(statusLabel, gridBagConstraints);
194
195         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(5, 5, 5, 5);
201         add(commandPanel, gridBagConstraints);
202
203         editorPanel.setPreferredSize(new java.awt.Dimension JavaDoc(700, 600));
204         editorPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
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 JavaDoc());
212
213         leftCommandPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
214
215         org.openide.awt.Mnemonics.setLocalizedText(acceptLeftButton, org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptLeftButton.text")); // NOI18N
216
acceptLeftButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptLeftButton.textA11yDesc")); // NOI18N
217
acceptLeftButton.addActionListener(this);
218         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
219         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
220         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
221         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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")); // NOI18N
225
acceptAndNextLeftButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptAndNextLeftButtonA11yDesc")); // NOI18N
226
acceptAndNextLeftButton.addActionListener(this);
227         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
228         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
229         gridBagConstraints.weightx = 1.0;
230         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 4, 0, 2);
231         leftCommandPanel.add(acceptAndNextLeftButton, gridBagConstraints);
232
233         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc();
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 JavaDoc();
251         gridBagConstraints.gridy = 2;
252         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
253         gridBagConstraints.weightx = 1.0;
254         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
255         filePanel1.add(fileLabel1, gridBagConstraints);
256
257         diffSplitPane.setLeftComponent(filePanel1);
258
259         filePanel2.setLayout(new java.awt.GridBagLayout JavaDoc());
260
261         rightCommandPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
262
263         org.openide.awt.Mnemonics.setLocalizedText(acceptRightButton, org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptRightButton.text")); // NOI18N
264
acceptRightButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptRightButton.textA11yDesc")); // NOI18N
265
acceptRightButton.addActionListener(this);
266         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
267         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
268         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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")); // NOI18N
272
acceptAndNextRightButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptAndNextRightButtonA11yDesc")); // NOI18N
273
acceptAndNextRightButton.addActionListener(this);
274         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(0, 4, 0, 2);
279         rightCommandPanel.add(acceptAndNextRightButton, gridBagConstraints);
280
281         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc();
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 JavaDoc();
299         gridBagConstraints.gridy = 2;
300         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
301         gridBagConstraints.weightx = 1.0;
302         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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 JavaDoc());
310
311         resultScrollPane.setViewportView(jEditorPane3);
312
313         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc();
321         gridBagConstraints.gridy = 1;
322         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
323         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
324         resultPanel.add(resultLabel, gridBagConstraints);
325
326         mergeSplitPane.setRightComponent(resultPanel);
327
328         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc();
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     // Code for dispatching events from components to event handlers.
343

344     public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc 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     }// </editor-fold>//GEN-END:initComponents
379

380     private void firstConflictButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_firstConflictButtonActionPerformed
381
// Add your handling code here:
382
fireControlActionCommand(ACTION_FIRST_CONFLICT);
383     }//GEN-LAST:event_firstConflictButtonActionPerformed
384

385     private void prevConflictButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_prevConflictButtonActionPerformed
386
// Add your handling code here:
387
fireControlActionCommand(ACTION_PREVIOUS_CONFLICT);
388     }//GEN-LAST:event_prevConflictButtonActionPerformed
389

390     private void nextConflictButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_nextConflictButtonActionPerformed
391
// Add your handling code here:
392
fireControlActionCommand(ACTION_NEXT_CONFLICT);
393     }//GEN-LAST:event_nextConflictButtonActionPerformed
394

395     private void lastConflictButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_lastConflictButtonActionPerformed
396
// Add your handling code here:
397
fireControlActionCommand(ACTION_LAST_CONFLICT);
398     }//GEN-LAST:event_lastConflictButtonActionPerformed
399

400     private void acceptRightButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_acceptRightButtonActionPerformed
401
// Add your handling code here:
402
fireControlActionCommand(ACTION_ACCEPT_RIGHT);
403     }//GEN-LAST:event_acceptRightButtonActionPerformed
404

405     private void acceptAndNextRightButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_acceptAndNextRightButtonActionPerformed
406
// Add your handling code here:
407
fireControlActionCommand(ACTION_ACCEPT_RIGHT);
408         fireControlActionCommand(ACTION_NEXT_CONFLICT);
409     }//GEN-LAST:event_acceptAndNextRightButtonActionPerformed
410

411     private void acceptAndNextLeftButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_acceptAndNextLeftButtonActionPerformed
412
// Add your handling code here:
413
fireControlActionCommand(ACTION_ACCEPT_LEFT);
414         fireControlActionCommand(ACTION_NEXT_CONFLICT);
415     }//GEN-LAST:event_acceptAndNextLeftButtonActionPerformed
416

417     private void acceptLeftButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_acceptLeftButtonActionPerformed
418
// Add your handling code here:
419
fireControlActionCommand(ACTION_ACCEPT_LEFT);
420     }//GEN-LAST:event_acceptLeftButtonActionPerformed
421

422   private void jEditorPane1CaretUpdate(javax.swing.event.CaretEvent JavaDoc evt) {//GEN-FIRST:event_jEditorPane1CaretUpdate
423
// Add your handling code here:
424
/* int pos = evt.getDot();
425       int line = org.openide.text.NbDocument.findLineNumber((StyledDocument) jEditorPane1.getDocument(), pos);
426       StyledDocument linesDoc = (StyledDocument)jEditorPane1.getDocument();
427       int numLines = org.openide.text.NbDocument.findLineNumber(linesDoc, linesDoc.getEndPosition().getOffset());
428       if (line <= numLines) {
429           jEditorPane1.setCaretPosition(org.openide.text.NbDocument.findLineOffset(linesDoc, line));
430       }
431  */

432   }//GEN-LAST:event_jEditorPane1CaretUpdate
433

434   private void jEditorPane2CaretUpdate(javax.swing.event.CaretEvent JavaDoc evt) {//GEN-FIRST:event_jEditorPane2CaretUpdate
435
// Add your handling code here:
436
/* int pos = evt.getDot();
437       int line = org.openide.text.NbDocument.findLineNumber((StyledDocument) jEditorPane2.getDocument(), pos);
438       StyledDocument linesDoc = (StyledDocument)jEditorPane2.getDocument();
439       int numLines = org.openide.text.NbDocument.findLineNumber(linesDoc, linesDoc.getEndPosition().getOffset());
440       if (line <= numLines) {
441           jEditorPane2.setCaretPosition(org.openide.text.NbDocument.findLineOffset(linesDoc, line));
442       }
443  */

444   }//GEN-LAST:event_jEditorPane2CaretUpdate
445

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   /**
456    * Instruct view to move to given line. It actually
457    * moves when AWT processes posted event.
458    */

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 JavaDoc() {
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 JavaDoc listener) {
479       controlListeners.add(listener);
480   }
481   
482   public synchronized void removeControlActionListener(ActionListener JavaDoc 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 JavaDoc conflictPos = new Integer JavaDoc(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 JavaDoc command) {
503       ArrayList<ActionListener JavaDoc> listeners;
504       synchronized (this) {
505           listeners = new ArrayList<ActionListener JavaDoc>(controlListeners);
506       }
507       ActionEvent JavaDoc evt = new ActionEvent JavaDoc(this, 0, command);
508       for (ActionListener JavaDoc l: listeners) {
509           l.actionPerformed(evt);
510       }
511   }
512
513     private void jScrollBar1AdjustmentValueChanged (java.awt.event.AdjustmentEvent JavaDoc evt) {//GEN-FIRST:event_jScrollBar1AdjustmentValueChanged
514
// Add your handling code here:
515
}//GEN-LAST:event_jScrollBar1AdjustmentValueChanged
516

517     private void closeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_closeButtonActionPerformed
518
// Add your handling code here:
519
exitForm(null);
520     }//GEN-LAST:event_closeButtonActionPerformed
521

522     /** Exit the Application */
523     private void exitForm(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_exitForm
524
/* try {
525             org.netbeans.editor.Settings.setValue(null, org.netbeans.editor.SettingsNames.LINE_NUMBER_VISIBLE, lineNumbersVisible);
526         } catch (Throwable exc) {
527             // editor module not found
528         }
529         //System.out.println("exitForm() called.");
530         //diff.closing();
531         //close();
532         //dispose ();
533         for(Iterator it = closeListeners.iterator(); it.hasNext(); ) {
534             ((TopComponentCloseListener) it.next()).closing();
535         }
536  */

537     }//GEN-LAST:event_exitForm
538

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 JavaDoc() {
549             public void focusGained(FocusEvent JavaDoc e) {
550                 editorActivated(jEditorPane1);
551             }
552             public void focusLost(FocusEvent JavaDoc e) {
553                 editorDeactivated(jEditorPane1);
554             }
555         });
556         jEditorPane2.addFocusListener(new FocusListener JavaDoc() {
557             public void focusGained(FocusEvent JavaDoc e) {
558                 editorActivated(jEditorPane2);
559             }
560             public void focusLost(FocusEvent JavaDoc e) {
561                 editorDeactivated(jEditorPane2);
562             }
563         });
564         jEditorPane3.addFocusListener(new FocusListener JavaDoc() {
565             public void focusGained(FocusEvent JavaDoc e) {
566                 editorActivated(jEditorPane3);
567             }
568             public void focusLost(FocusEvent JavaDoc e) {
569                 editorDeactivated(jEditorPane3);
570             }
571         });
572     }
573     
574     private Hashtable<JEditorPane, Hashtable<Object JavaDoc, Action>> kitActions;
575             /** Listener for copy action enabling */
576     private PropertyChangeListener JavaDoc copyL;
577     private PropertyChangeListener JavaDoc copyP;
578     
579     private Action getAction (String JavaDoc s, JEditorPane editor) {
580         if (kitActions == null) {
581             kitActions = new Hashtable<JEditorPane, Hashtable<Object JavaDoc, Action>>();
582         }
583         Hashtable<Object JavaDoc, 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 JavaDoc, 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         //System.out.println("editor("+editor+") activated.");
602
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 JavaDoc (editor, 0, "")); // NOI18N
609
}
610             };
611             sysCopy.setActionPerformer(copy.isEnabled() ? perf : null);
612             PropertyChangeListener JavaDoc copyListener;
613             copy.addPropertyChangeListener(copyListener = new PropertyChangeListener JavaDoc() {
614                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
615                     if ("enabled".equals(evt.getPropertyName())) { // NOI18N
616
if (((Boolean JavaDoc)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         //System.out.println("editorDeactivated ("+editor+")");
631
Action copy = getAction (DefaultEditorKit.copyAction, editor);
632         PropertyChangeListener JavaDoc 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 JavaDoc() {
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 JavaDoc() {
653             public void run() {
654                 initGlobalSizes();
655                 //showLine(1, 0);
656
addChangeListeners();
657 /* javax.swing.SwingUtilities.invokeLater(new Runnable() {
658                     public void run() {
659                         initGlobalSizes(); // do that again to be sure that components are initialized.
660                         javax.swing.SwingUtilities.invokeLater(new Runnable() {
661                             public void run() {
662                                 syncFont(); // Components have to be fully initialized before font syncing
663                                 addChangeListeners();
664                             }
665                         });
666                     }
667                 });
668  */

669             }
670         });
671     }
672
673     /*
674     public void removeNotify() {
675         System.out.println("removeNotify() called");
676         exitForm(null);
677         super.removeNotify();
678     }
679      */

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 = editorPanel1.getSize().height;
689
int totHeight = jEditorPane1.getSize().height;
690         // int value = editorPanel2.getSize().height;
691
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         //System.out.println("showLine("+line+", "+diffLength+")");
699
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 JavaDoc p1, p2;
709         initGlobalSizes(); // The window might be resized in the mean time.
710
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             //System.out.println("resetting posision=" + ypos);
716
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         //D.deb("off1 = "+off1+", off2 = "+off2+", totalHeight = "+totalHeight+", totalLines = "+totalLines+", ypos = "+ypos);
725
//System.out.println("off1 = "+off1+", off2 = "+off2+", totalHeight = "+totalHeight+", totalLines = "+totalLines+", ypos = "+ypos);
726
}
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 JavaDoc p1, java.awt.Point JavaDoc 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 JavaDoc() {
749             public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
750                 int value = scrollBarV1.getValue();
751                 int oldValue = scrollBarV2.getValue();
752                 if (oldValue != value) {
753                     scrollBarV2.setValue(value);
754 // System.out.println("setting v2=" + value);
755
// Thread.dumpStack();
756
}
757                 // TODO use a better algorithm to adjust scrollbars, if there are large changes, this will not work optimally.
758
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.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
770
// The vertical scroll bar must be there for mouse wheel to work correctly.
771
// However it's not necessary to be seen (but must be visible so that the wheel will work).
772
jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension JavaDoc(0, 0));
773         scrollBarV2.getModel().addChangeListener(new javax.swing.event.ChangeListener JavaDoc() {
774             public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
775                 int value = scrollBarV2.getValue();
776                 int oldValue = scrollBarV1.getValue();
777                 if (oldValue != value) {
778                     scrollBarV1.setValue(value);
779 // System.out.println("setting v1 to=" + value);
780
}
781             }
782         });
783         /* don't not let the result source vertical scrolling to influence the diff panels.
784         scrollBarV3.getModel().addChangeListener(new javax.swing.event.ChangeListener() {
785             public void stateChanged(javax.swing.event.ChangeEvent e) {
786                 int value = scrollBarV3.getValue();
787                 if (value == verticalScroll3ChangedValue) return ;
788                 int max1 = scrollBarV3.getMaximum();
789                 int max2 = scrollBarV1.getMaximum();
790                 int ext1 = scrollBarV3.getModel().getExtent();
791                 int ext2 = scrollBarV1.getModel().getExtent();
792                 if (max1 == ext1) verticalScroll1ChangedValue = 0;
793                 else verticalScroll1ChangedValue = (value*(max2 - ext2))/(max1 - ext1);
794                 verticalScroll3ChangedValue = -1;
795                 scrollBarV1.setValue(verticalScroll1ChangedValue);
796             }
797         });
798          */

799         scrollBarH1.getModel().addChangeListener(new javax.swing.event.ChangeListener JavaDoc() {
800             public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
801                 int value = scrollBarH1.getValue();
802                 // System.out.println("stateChangedH1:value = "+value+", horizontalScroll1ChangedValue = "+horizontalScroll1ChangedValue);
803
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                 // System.out.println("H1 value = "+value+" => H2 value = "+horizontalScroll2ChangedValue+"\t\tmax1 = "+max1+", max2 = "+max2);
812
scrollBarH2.setValue(horizontalScroll2ChangedValue);
813             }
814         });
815         scrollBarH2.getModel().addChangeListener(new javax.swing.event.ChangeListener JavaDoc() {
816             public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
817                 int value = scrollBarH2.getValue();
818                 // System.out.println("stateChangedH2:value = "+value+", horizontalScroll2ChangedValue = "+horizontalScroll2ChangedValue);
819
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                 // System.out.println("H2 value = "+value+" => H1 value = "+horizontalScroll1ChangedValue+"\t\tmax1 = "+max1+", max2 = "+max2);
835
scrollBarH1.setValue(horizontalScroll1ChangedValue);
836                 scrollBarH3.setValue(horizontalScroll3ChangedValue);
837             }
838         });
839         scrollBarH3.getModel().addChangeListener(new javax.swing.event.ChangeListener JavaDoc() {
840             public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
841                 int value = scrollBarH3.getValue();
842                 // System.out.println("stateChangedH1:value = "+value+", horizontalScroll1ChangedValue = "+horizontalScroll1ChangedValue);
843
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                 // System.out.println("H1 value = "+value+" => H2 value = "+horizontalScroll2ChangedValue+"\t\tmax1 = "+max1+", max2 = "+max2);
859
scrollBarH1.setValue(horizontalScroll1ChangedValue);
860                 scrollBarH2.setValue(horizontalScroll2ChangedValue);
861             }
862         });
863         diffSplitPane.setDividerLocation(0.5);
864         mergeSplitPane.setDividerLocation(0.5);
865     }
866     
867     private String JavaDoc strCharacters(char c, int num) {
868         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
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         /*
878         try {
879             org.netbeans.editor.Settings.setValue(null, org.netbeans.editor.SettingsNames.LINE_NUMBER_VISIBLE, Boolean.FALSE);
880         } catch (Throwable exc) {
881             // editor module not found
882         }
883          */

884         StyledDocument doc;
885         Document document = editor.getDocument();
886 /* StyledDocument docLines = new DefaultStyledDocument();
887         textLines.setStyledDocument(docLines);
888  */

889         try {
890             doc = (StyledDocument) editor.getDocument();
891         } catch(ClassCastException JavaDoc e) {
892             doc = new DefaultStyledDocument();
893             try {
894                 doc.insertString(0, document.getText(0, document.getLength()), null);
895             } catch (BadLocationException ble) {
896                 // leaving the document empty
897
}
898             editor.setDocument(doc);
899         }
900         //int lastOffset = doc.getEndPosition().getOffset();
901
//int numLines = org.openide.text.NbDocument.findLineNumber(doc, lastOffset);
902
//int numLength = Integer.toString(numLines).length();
903
// textLines.setForeground(numForegroundColor);
904
// textLines.setBackground(numBackgroundColor);
905
/*
906         for (int line = 0; line <= numLines; line++) {
907             int offset = org.openide.text.NbDocument.findLineOffset(doc, line);
908             String lineStr = Integer.toString(line+1);
909             if (lineStr.length() < numLength) lineStr = strCharacters(' ', numLength - lineStr.length()) + lineStr;
910             //lineStr = " "+lineStr+" "; // NOI18N
911             try {
912                 if (line < numLines) lineStr += "\n"; // NOI18N
913                 docLines.insertString(docLines.getLength(), lineStr, null);
914             } catch (BadLocationException e) {
915                 E.deb("Internal ERROR: "+e.getMessage()); // NOI18N
916             }
917         }
918          */

919         // joinScrollBars();
920
}
921     
922     private void addChangeListeners() {
923         jEditorPane1.addPropertyChangeListener("font", new java.beans.PropertyChangeListener JavaDoc() {
924             public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
925                 //System.out.println("1:evt = "+evt+", Property NAME = "+evt.getPropertyName());
926
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
927                     public void run() {
928                         initGlobalSizes();
929                         linesComp1.changedAll();
930                     }
931                 });
932             }
933         });
934         jEditorPane2.addPropertyChangeListener("font", new java.beans.PropertyChangeListener JavaDoc() {
935             public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
936                 //System.out.println("2:evt = "+evt+", Property NAME = "+evt.getPropertyName());
937
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
938                     public void run() {
939                         initGlobalSizes();
940                         linesComp2.changedAll();
941                     }
942                 });
943             }
944         });
945         jEditorPane3.addPropertyChangeListener("font", new java.beans.PropertyChangeListener JavaDoc() {
946             public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
947                 //System.out.println("2:evt = "+evt+", Property NAME = "+evt.getPropertyName());
948
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
949                     public void run() {
950                         initGlobalSizes();
951                         linesComp3.changedAll();
952                     }
953                 });
954             }
955         });
956     }
957     
958     public void setSource1(Reader r) throws IOException {
959         //D.deb("setFile("+url+")"); // NOI18N
960
//System.out.println("setFile1("+url+")");
961
EditorKit kit = jEditorPane1.getEditorKit();
962         if (kit == null) throw new IOException("Missing Editor Kit"); // NOI18N
963
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 JavaDoc e) {
972             throw new IOException("Can not locate the beginning of the document."); // NOI18N
973
} finally {
974             r.close();
975         }
976         kit.install(jEditorPane1);
977         jEditorPane1.setDocument(doc);
978         //jEditorPane1.setPage(url);
979
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         //D.deb("setFile("+url+")"); // NOI18N
988
EditorKit kit = jEditorPane2.getEditorKit();
989         if (kit == null) throw new IOException("Missing Editor Kit"); // NOI18N
990
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 JavaDoc e) {
999             throw new IOException("Can not locate the beginning of the document."); // NOI18N
1000
} finally {
1001            r.close();
1002        }
1003        kit.install(jEditorPane2);
1004        jEditorPane2.setDocument(doc);
1005        //jEditorPane2.setPage(url);
1006
jEditorPane2.setEditable(false);
1007        
1008        customizeEditor(jEditorPane2);
1009        linesComp2 = new LinesComponent(jEditorPane2);
1010        jScrollPane2.setRowHeaderView(linesComp2);
1011        jViewport2 = jScrollPane2.getViewport();
1012        // add scrollbar listeners..
1013
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"); // NOI18N
1019
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 JavaDoc e) {
1028            throw new IOException("Can not locate the beginning of the document."); // NOI18N
1029
} finally {
1030            r.close();
1031        }
1032        kit.install(jEditorPane3);
1033        jEditorPane3.setDocument(doc);
1034        //jEditorPane2.setPage(url);
1035
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    /**
1056     * Copy a part of first document into the result document.
1057     * @param line1 The starting line in the first source
1058     * @param line2 The ending line in the first source or <code>null</code>
1059     * when the part ends at the end of the document
1060     * @param line3 The starting line in the result
1061     */

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    /**
1073     * Copy a part of second document into the result document.
1074     * @param line1 The starting line in the second source
1075     * @param line2 The ending line in the second source or <code>null</code>
1076     * when the part ends at the end of the document
1077     * @param line3 The starting line in the result
1078     */

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    /** Copies a part of one document into another. */
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 JavaDoc text = doc1.getText(offset1, length);
1099        //System.out.println("copy: offset1 = "+offset1+", offset2 = "+offset2);
1100
//System.out.println(">> copy text: at "+offset3+" <<\n"+text+">> <<");
1101
doc2.insertString(offset3, text, null);
1102        // Adjust the line numbers
1103
if (line2 < 0) line2 = org.openide.text.NbDocument.findLineNumber(doc1, doc1.getLength());
1104        int numLines = line2 - line1 + 1;
1105        //System.out.println("copy("+line1+", "+line2+", "+line3+"): resultLineNumbers.length = "+resultLineNumbers.length);
1106
assureResultLineNumbersLength(line3 + numLines);
1107        if (resultLineNumbers[line3] == 0 && line3 > 0) resultLineNumbers[line3] = resultLineNumbers[line3 - 1] + 1;
1108        int resultLine = resultLineNumbers[line3];
1109        //System.out.println("resultLine = rln["+line3+"] = "+resultLine);
1110
//System.out.println("insertNumbers("+line3+", "+resultLine+", "+numLines+")");
1111
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    /**
1117     * Replace a part of result with a part of the first source.
1118     * @param line1 The starting line in the first source
1119     * @param line2 The ending line in the first source or <code>null</code>
1120     * when the part ends at the end of the document
1121     * @param line3 The starting line in the result
1122     * @param line4 The ending line in the result
1123     */

1124    public void replaceSource1InResult(int line1, int line2, int line3, int line4) {
1125        //System.out.println("replaceSource1InResult("+line1+", "+line2+", "+line3+", "+line4+")");
1126
Integer JavaDoc conflictLine = new Integer JavaDoc((line1 > 0) ? line1 : 1);
1127        // If trying to resolve the conflict twice simply return .
1128
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            // We've resolved the conflict.
1140
numUnresolvedConflicts--;
1141            updateStatusLine();
1142        }
1143        resolvedLeftConflictsLineNumbers.add(conflictLine);
1144        updateAcceptButtons(line1);
1145    }
1146    
1147    /**
1148     * Replace a part of result with a part of the second source.
1149     * @param line1 The starting line in the second source
1150     * @param line2 The ending line in the second source or <code>null</code>
1151     * when the part ends at the end of the document
1152     * @param line3 The starting line in the result
1153     * @param line4 The ending line in the result
1154     */

1155    public void replaceSource2InResult(int line1, int line2, int line3, int line4) {
1156        //System.out.println("replaceSource2InResult("+line1+", "+line2+", "+line3+", "+line4+")");
1157
Integer JavaDoc conflictLine = new Integer JavaDoc((line1 > 0) ? line1 : 1);
1158        // If trying to resolve the conflict twice simply return .
1159
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            // We've resolved the conflict.
1171
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        //dumpResultLineNumbers();
1181
//System.out.println("replace("+line1+", "+line2+", "+line3+", "+line4+")");
1182
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        //System.out.println("replace: offsets = "+offset1+", "+offset2+", "+offset3+", "+offset4);
1191
int length = offset2 - offset1;
1192        if (line2 < 0) length++;
1193        String JavaDoc text = doc1.getText(offset1, length);
1194        doc2.remove(offset3, offset4 - offset3);
1195        doc2.insertString(offset3, text, null);
1196        // Adjust the line numbers
1197
assureResultLineNumbersLength(line4);
1198        //int lineDiff;
1199
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            //System.out.println("arraycopy("+line4+", "+(line4 + physicalLineDiff)+")");
1205
//dumpResultLineNumbers();
1206
}
1207        int lineDiff = (resultLineNumbers[line3] <= resultLineNumbers[line3 - 1])
1208                       ? (line2 - line1 + 1)
1209                       : (line2 - line1 - (line4 - line3));
1210        //if (resultLineNumbers[line3] <= resultLineNumbers[line3 - 1]) {
1211
// There are no line numbers defined.
1212
//lineDiff = line2 - line1 + 1;
1213
int n = resultLineNumbers[line3 - 1];
1214        for (int i = line3; i <= line4 + physicalLineDiff; i++) {
1215            resultLineNumbers[i] = ++n;
1216        }
1217            /*
1218            for (int i = line4 + lineDiff + 1; i < resultLineNumbers.length; i++) {
1219                if (resultLineNumbers[i] != 0) resultLineNumbers[i] += lineDiff;
1220                else break;
1221            }
1222             */

1223        //lineDiff = line2 - line1 + 1;
1224
//System.out.println("insertNumbers("+line3+", "+resultLineNumbers[line3]+", "+(line2 - line1 + 1)+")");
1225
linesComp3.insertNumbers(line3 - 1, resultLineNumbers[line3], line2 - line1 + 1);
1226        linesComp3.changedAll();
1227        //dumpResultLineNumbers();
1228
//} else {
1229
// lineDiff = line2 - line1 - (line4 - line3);
1230
//}
1231
if (physicalLineDiff < 0) {
1232            System.arraycopy(resultLineNumbers, line4 + 1,
1233            resultLineNumbers, line4 + physicalLineDiff + 1,
1234            resultLineNumbers.length - line4 - 1);
1235            //System.out.println("arraycopy("+line4+", "+(line4 + physicalLineDiff)+")");
1236
//dumpResultLineNumbers();
1237
}
1238        adjustLineNumbers(line4 + physicalLineDiff + 1, lineDiff);
1239
1240        // #65970 workaround, resultLineNumbers content must be primitive only raising
1241
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    /*
1251    private void dumpResultLineNumbers() {
1252        System.out.print("resultLineNum[] = ");
1253        boolean was = false;
1254        for (int i = 0; i < resultLineNumbers.length; i++) {
1255            if (resultLineNumbers[i] == 0 && was) break;
1256            if (resultLineNumbers[i] != 0) was = true;
1257            System.out.print(resultLineNumbers[i]+", ");
1258        }
1259        System.out.println("");
1260        try {
1261            Thread.currentThread().sleep(1000);
1262        } catch (InterruptedException iex) {}
1263    }
1264     */

1265    
1266    private void adjustLineNumbers(int startLine, int shift) {
1267        //System.out.println("adjustLineNumbers("+startLine+", "+shift+")");
1268
int end = resultLineNumbers.length;
1269        while (end > 0 && resultLineNumbers[end - 1] == 0) end--;
1270        int startSetLine = -1;
1271        int endSetLine = -1;
1272        //resultLineNumbers[startLine] += shift;
1273
for (int i = startLine; i < end; i++) {
1274            resultLineNumbers[i] += shift;
1275            if (resultLineNumbers[i] <= resultLineNumbers[i - 1]) {
1276                if (startSetLine > 0) {
1277                    //System.out.println("insertNumbers("+startSetLine+", "+resultLineNumbers[startSetLine]+", "+(i - startSetLine)+")");
1278
linesComp3.insertNumbers(startSetLine - 1, resultLineNumbers[startSetLine], i - startSetLine);
1279                    linesComp3.changedAll();
1280                    //dumpResultLineNumbers();
1281
startSetLine = -1;
1282                }
1283                if (endSetLine < 0) {
1284                    endSetLine = i;
1285                }
1286            } else {
1287                if (endSetLine > 0) {
1288                    //System.out.println("removeNumbers("+endSetLine+", "+(i - endSetLine)+")");
1289
linesComp3.removeNumbers(endSetLine - 1, i - endSetLine);
1290                    linesComp3.changedAll();
1291                    //dumpResultLineNumbers();
1292
endSetLine = -1;
1293                }
1294                if (startSetLine < 0) {
1295                    startSetLine = i;
1296                }
1297            }
1298        }
1299        if (startSetLine > 0) {
1300            //System.out.println("insertNumbers("+startSetLine+", "+resultLineNumbers[startSetLine]+", "+(end - startSetLine)+" (END))");
1301
linesComp3.insertNumbers(startSetLine - 1, resultLineNumbers[startSetLine], end - startSetLine);
1302            linesComp3.shrink(end - 1);
1303            linesComp3.changedAll();
1304            //dumpResultLineNumbers();
1305
}
1306        if (endSetLine > 0) {
1307            //System.out.println("removeNumbers("+endSetLine+", "+(end - endSetLine)+" (END))");
1308
linesComp3.removeNumbers(endSetLine - 1, end - endSetLine);
1309            linesComp3.shrink(end - 1);
1310            linesComp3.changedAll();
1311            //dumpResultLineNumbers();
1312
}
1313    }
1314    
1315    public void setSource1Title(String JavaDoc title) {
1316        fileLabel1.setText(title);
1317    }
1318    
1319    public void setSource2Title(String JavaDoc title) {
1320        fileLabel2.setText(title);
1321    }
1322    
1323    public void setResultSourceTitle(String JavaDoc title) {
1324        resultLabel.setText(title);
1325    }
1326    
1327    public void setStatusLabel(String JavaDoc status) {
1328        statusLabel.setText(status);
1329    }
1330    
1331    public void setMimeType1(String JavaDoc mime) {
1332        EditorKit kit = CloneableEditorSupport.getEditorKit(mime);
1333        jEditorPane1.setEditorKit(kit);
1334        //Document doc = jEditorPane1.getDocument();
1335
//if (!(doc instanceof StyledDocument)) jEditorPane1.setDocument(new DefaultStyledDocument());
1336
}
1337    
1338    public void setMimeType2(String JavaDoc mime) {
1339        EditorKit kit = CloneableEditorSupport.getEditorKit(mime);
1340        jEditorPane2.setEditorKit(kit);
1341        //Document doc = jEditorPane2.getDocument();
1342
//if (!(doc instanceof StyledDocument)) jEditorPane2.setDocument(new DefaultStyledDocument());
1343
}
1344    
1345    public void setMimeType3(String JavaDoc mime) {
1346        EditorKit kit = CloneableEditorSupport.getEditorKit(mime);
1347        jEditorPane3.setEditorKit(kit);
1348    }
1349    
1350    /*
1351    public void setDocument1(Document doc) {
1352        if (doc != null) {
1353            jEditorPane1.setDocument(doc);
1354        }
1355    }
1356     */

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    /*
1368     * Whether all conflicts are resolved and the panel can be closed.
1369     * @return <code>true</code> when the panel can be closed, <code>false</code> otherwise.
1370     *
1371    public boolean canClose() {
1372        return true;
1373    }
1374     */

1375    
1376    /**
1377     * Write the result content into the given writer. Skip all unresolved conflicts.
1378     * @param w The writer to write the result into.
1379     * @throws IOException When the writing process fails.
1380     */

1381    public void writeResult(Writer w, boolean stripLastNewline) throws IOException {
1382        //System.out.println("writeResult()");
1383
/*
1384        try {
1385            jEditorPane3.getEditorKit().write(w, jEditorPane3.getDocument(),
1386                                              0, jEditorPane3.getDocument().getLength());
1387        } catch (BadLocationException blex) {
1388            throw new IOException(blex.getLocalizedMessage());
1389        }
1390         */

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                        //System.out.println("write("+startSetLine+", "+i+")");
1400
int offsetStart = org.openide.text.NbDocument.findLineOffset(doc, startSetLine - 1);
1401                        int offsetEnd = org.openide.text.NbDocument.findLineOffset(doc, i - 1);
1402                        //System.out.println(" Have text(<l="+(startSetLine-1)+",off="+offsetStart+";l="+(i-1)+",off="+offsetEnd+">), length = "+doc.getLength());
1403
try {
1404                            //System.out.println("'"+doc.getText(offsetStart, offsetEnd - offsetStart)+"'");
1405
writeText(w, doc.getText(offsetStart, offsetEnd - offsetStart));
1406                        } catch (BadLocationException blex) {
1407                            throw new IOException(blex.getLocalizedMessage());
1408                        }
1409                        //dumpResultLineNumbers();
1410
startSetLine = -1;
1411                    }
1412                } else {
1413                    if (startSetLine < 0) {
1414                        startSetLine = i;
1415                    }
1416                }
1417            }
1418            if (startSetLine > 0) {
1419                //System.out.println("write("+startSetLine+", "+end+" (END))");
1420
int offsetStart = org.openide.text.NbDocument.findLineOffset(doc, startSetLine - 1);
1421                int offsetEnd = doc.getLength();
1422                try {
1423                    String JavaDoc 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                //dumpResultLineNumbers();
1432
}
1433        } finally {
1434            w.close();
1435        }
1436    }
1437    
1438    private void writeText(Writer w, String JavaDoc 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 JavaDoc color) {
1445        if (!SwingUtilities.isEventDispatchThread()) {
1446            SwingUtilities.invokeLater(new Runnable JavaDoc() {
1447                public void run() {
1448                    setHighlight(doc, line1, line2, color);
1449                }
1450            });
1451        }
1452        //System.out.println("setHighlight(): <"+line1+", "+line2+">, color = "+color); // NOI18N
1453
//Style s = doc.addStyle("diff-style("+color+"):1500", null); // NOI18N
1454
// SimpleAttributeSet attrSet = new SimpleAttributeSet();
1455
// attrSet.addAttribute(StyleConstants.ColorConstants.Background, java.awt.Color.green);
1456
//s.addAttribute(StyleConstants.ColorConstants.Background, color);
1457
Style s = doc.getStyle("diff-style("+color+"):1500");
1458        //if (s == null) s = doc.getLogicalStyle(offset);
1459
if (s == null) {
1460            //System.out.println("setHighlight(): logical style is NULL"); // NOI18N
1461
s = doc.addStyle("diff-style("+color+"):1500", null); // NOI18N
1462
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            //System.out.println("setHighlight(): I got offset = "+offset); // NOI18N
1468
if (offset >= 0) {
1469                /*
1470                Style ls = doc.getLogicalStyle(offset);
1471                //if (ls != null) ls.addAttributes(s.copyAttributes());
1472                if (ls == null) {
1473                    ls = s;//new javax.swing.text.StyleContext.NamedStyle("diff-style("+color+"):1500", null);
1474                } else {
1475                    //ls.addAttributes(s.copyAttributes());
1476                }
1477                ls.removeAttribute(StyleConstants.ColorConstants.Background);
1478                ls.addAttribute(StyleConstants.ColorConstants.Background, color);
1479                 */

1480                doc.setLogicalStyle(offset, s);
1481                //doc.setParagraphAttributes(offset, 1, s, false);
1482
}
1483        }
1484        //doc.setParagraphAttributes(offset, 100, s, true);
1485
}
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); // NOI18N
1491
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 totLines = doc.getDefaultRootElement().getElementIndex(lastOffset);
1527
int offset = lastOffset;
1528        if (line <= totLines) {
1529            offset = org.openide.text.NbDocument.findLineOffset(doc, line);
1530            //offset = doc.getDefaultRootElement().getElement(line).getStartOffset();
1531
}
1532        //int endOffset = doc.getEndPosition().getOffset();
1533
//if (offset > endOffset) offset = endOffset;
1534
String JavaDoc insStr = strCharacters('\n', numLines);
1535        //System.out.println("addEmptyLines = '"+insStr+"'");
1536
try {
1537            doc.insertString(offset, insStr, null);
1538        } catch (BadLocationException e) {
1539            org.openide.ErrorManager.getDefault().notify(e);
1540        }
1541        //initScrollBars();
1542
}
1543    
1544    public void addEmptyLines1(int line, int numLines) {
1545        StyledDocument doc = (StyledDocument) jEditorPane1.getDocument();
1546        //System.out.println("addEmptyLines1: line = "+line+", numLines = "+numLines); // NOI18N
1547
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        //System.out.println("addEmptyLines2: line = "+line+", numLines = "+numLines); // NOI18N
1554
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        //System.out.println("addEmptyLines3: line = "+line+", numLines = "+numLines); // NOI18N
1561
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 JavaDoc jViewport1;
1571    private javax.swing.JViewport JavaDoc jViewport2;
1572    
1573    // Variables declaration - do not modify//GEN-BEGIN:variables
1574
final javax.swing.JButton JavaDoc acceptAndNextLeftButton = new javax.swing.JButton JavaDoc();
1575    final javax.swing.JButton JavaDoc acceptAndNextRightButton = new javax.swing.JButton JavaDoc();
1576    final javax.swing.JButton JavaDoc acceptLeftButton = new javax.swing.JButton JavaDoc();
1577    final javax.swing.JButton JavaDoc acceptRightButton = new javax.swing.JButton JavaDoc();
1578    final javax.swing.JPanel JavaDoc commandPanel = new javax.swing.JPanel JavaDoc();
1579    final javax.swing.JSplitPane JavaDoc diffSplitPane = new javax.swing.JSplitPane JavaDoc();
1580    final javax.swing.JPanel JavaDoc editorPanel = new javax.swing.JPanel JavaDoc();
1581    final javax.swing.JLabel JavaDoc fileLabel1 = new javax.swing.JLabel JavaDoc();
1582    final javax.swing.JLabel JavaDoc fileLabel2 = new javax.swing.JLabel JavaDoc();
1583    final javax.swing.JPanel JavaDoc filePanel1 = new javax.swing.JPanel JavaDoc();
1584    final javax.swing.JPanel JavaDoc filePanel2 = new javax.swing.JPanel JavaDoc();
1585    final javax.swing.JButton JavaDoc firstConflictButton = new javax.swing.JButton JavaDoc();
1586    final javax.swing.JEditorPane JavaDoc jEditorPane1 = new DEditorPane();
1587    final javax.swing.JEditorPane JavaDoc jEditorPane2 = new DEditorPane();
1588    final javax.swing.JEditorPane JavaDoc jEditorPane3 = new DEditorPane();
1589    final javax.swing.JScrollPane JavaDoc jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
1590    final javax.swing.JScrollPane JavaDoc jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
1591    final javax.swing.JButton JavaDoc lastConflictButton = new javax.swing.JButton JavaDoc();
1592    final javax.swing.JPanel JavaDoc leftCommandPanel = new javax.swing.JPanel JavaDoc();
1593    final javax.swing.JSplitPane JavaDoc mergeSplitPane = new javax.swing.JSplitPane JavaDoc();
1594    final javax.swing.JButton JavaDoc nextConflictButton = new javax.swing.JButton JavaDoc();
1595    final javax.swing.JButton JavaDoc prevConflictButton = new javax.swing.JButton JavaDoc();
1596    final javax.swing.JLabel JavaDoc resultLabel = new javax.swing.JLabel JavaDoc();
1597    final javax.swing.JPanel JavaDoc resultPanel = new javax.swing.JPanel JavaDoc();
1598    final javax.swing.JScrollPane JavaDoc resultScrollPane = new javax.swing.JScrollPane JavaDoc();
1599    final javax.swing.JPanel JavaDoc rightCommandPanel = new javax.swing.JPanel JavaDoc();
1600    final javax.swing.JLabel JavaDoc statusLabel = new javax.swing.JLabel JavaDoc();
1601    // End of variables declaration//GEN-END:variables
1602

1603}
1604
Popular Tags