|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.merge.builtin.visualizer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.*; import java.io.*; import javax.swing.*; import javax.swing.text.*; import org.openide.actions.CloseViewAction; import org.openide.actions.CopyAction; import org.openide.actions.SaveAction; import org.openide.util.HelpCtx; import org.openide.util.actions.ActionPerformer; import org.openide.util.actions.CallbackSystemAction; import org.openide.util.actions.SystemAction; import org.netbeans.modules.diff.builtin.visualizer.LinesComponent; /** * This class displays two editor panes with two files and marks the differences * by a different color. * @author Martin Entlicher */ public class MergePanel extends javax.swing.JPanel { public static final String ACTION_FIRST_CONFLICT = "firstConflict"; // NOI18N public static final String ACTION_LAST_CONFLICT = "lastConflict"; // NOI18N public static final String ACTION_PREVIOUS_CONFLICT = "previousConflict"; // NOI18N public static final String ACTION_NEXT_CONFLICT = "nextConflict"; // NOI18N public static final String ACTION_ACCEPT_RIGHT = "acceptRight"; // NOI18N //public static final String ACTION_ACCEPT_RIGHT_AND_NEXT = "acceptRightAndNext"; // NOI18N public static final String ACTION_ACCEPT_LEFT = "acceptLeft"; // NOI18N //public static final String ACTION_ACCEPT_LEFT_AND_NEXT = "acceptLeftAndNext"; // NOI18N public static final String PROP_CAN_BE_SAVED = "canBeSaved"; // NOI18N public static final String PROP_CAN_NOT_BE_SAVED = "canNotBeSaved"; // NOI18N // scroll 4 lines vertically private static final double VERTICAL_SCROLL_NUM_LINES = 4.0; // scroll 4 "lines" horizontally private static final double HORIZONTAL_SCROLL_NUM_LINES = 4.0; // private AbstractDiff diff = null; private int totalHeight = 0; private int additionalHeight = 0; private int totalLines = 0; private int horizontalScroll1ChangedValue = -1; private int horizontalScroll2ChangedValue = -1; private int horizontalScroll3ChangedValue = -1; private int verticalScroll1ChangedValue = -1; private int verticalScroll3ChangedValue = -1; private LinesComponent linesComp1; private LinesComponent linesComp2; private LinesComponent linesComp3; /** * Line numbers in the result document. The indexes are "physical" document line numbers, * and values are "logical" document line numbers. If there is a space inserted (a conflict), * the corresponding document content is not defined and logical document line numbers * do not grow. * If the conflict starts from the beginning of the file, the logical line numbers are '0', * if the conflict is in the middle of the file, the logical line numbers are euqal to * the last logical line before this conflict. * The line numbers start from '1'. */ private int[] resultLineNumbers; private int numConflicts; private int numUnresolvedConflicts; private int currentConflictPos; private List resolvedLeftConflictsLineNumbers = new ArrayList(); private List resolvedRightConflictsLineNumbers = new ArrayList(); private ArrayList controlListeners = new ArrayList(); private SystemAction[] systemActions = new SystemAction[] { SaveAction.get(SaveAction.class), null, CloseMergeViewAction.get(CloseMergeViewAction.class) }; /** * Used for deserialization. */ private boolean diffSetSuccess = true; static final long serialVersionUID =3683458237532937983L; private static final String PLAIN_TEXT_MIME = "text/plain"; /** Creates new DiffComponent from AbstractDiff object*/ public MergePanel() { // this.diff = diff; initComponents (); // TODO Get icons for these buttons firstConflictButton.setVisible(false); lastConflictButton.setVisible(false); prevConflictButton.setIcon(new ImageIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/prev.gif"))); nextConflictButton.setIcon(new ImageIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/next.gif"))); //prevConflictButton.setIcon(new ImageIcon(getClass().getResource("/org/netbeans/modules/diff/builtin/visualizer/prev.gif"))); //nextConflictButton.setIcon(new ImageIcon(getClass().getResource("/org/netbeans/modules/diff/builtin/visualizer/next.gif"))); prevConflictButton.setMnemonic (org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.prevButton.mnemonic").charAt (0)); nextConflictButton.setMnemonic (org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.nextButton.mnemonic").charAt (0)); //setTitle(org.openide.util.NbBundle.getBundle(DiffComponent.class).getString("DiffComponent.title")); //setName(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.title")); //HelpCtx.setHelpIDString (getRootPane (), DiffComponent.class.getName ()); initActions(); diffSplitPane.setResizeWeight(0.5); mergeSplitPane.setResizeWeight(0.5); putClientProperty("PersistenceType", "Never"); acceptLeftButton.setMnemonic(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptLeftButton.text_Mnemonic").charAt(0)); // NOI18N acceptAndNextLeftButton.setMnemonic(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptAndNextLeftButton_Mnemonic").charAt(0)); // NOI18N acceptRightButton.setMnemonic(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptRightButton.text_Mnemonic").charAt(0)); // NOI18N acceptAndNextRightButton.setMnemonic(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptAndNextRightButton_Mnemonic").charAt(0)); // NOI18N jEditorPane1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane1A11yName")); // NOI18N jEditorPane1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane1A11yDescr")); // NOI18N jEditorPane2.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane2A11yName")); // NOI18N jEditorPane2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane2A11yDescr")); // NOI18N jEditorPane3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane3A11yName")); // NOI18N jEditorPane3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MergePanel.class, "ACS_EditorPane3A11yDescr")); // NOI18N } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the FormEditor. */ private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; commandPanel = new javax.swing.JPanel(); firstConflictButton = new javax.swing.JButton(); prevConflictButton = new javax.swing.JButton(); nextConflictButton = new javax.swing.JButton(); lastConflictButton = new javax.swing.JButton(); statusLabel = new javax.swing.JLabel(); editorPanel = new javax.swing.JPanel(); mergeSplitPane = new javax.swing.JSplitPane(); diffSplitPane = new javax.swing.JSplitPane(); filePanel1 = new javax.swing.JPanel(); leftCommandPanel = new javax.swing.JPanel(); acceptLeftButton = new javax.swing.JButton(); acceptAndNextLeftButton = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jEditorPane1 = new javax.swing.JEditorPane(); fileLabel1 = new javax.swing.JLabel(); filePanel2 = new javax.swing.JPanel(); rightCommandPanel = new javax.swing.JPanel(); acceptRightButton = new javax.swing.JButton(); acceptAndNextRightButton = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); jEditorPane2 = new javax.swing.JEditorPane(); fileLabel2 = new javax.swing.JLabel(); resultPanel = new javax.swing.JPanel(); resultScrollPane = new javax.swing.JScrollPane(); jEditorPane3 = new javax.swing.JEditorPane(); resultLabel = new javax.swing.JLabel(); setLayout(new java.awt.GridBagLayout()); commandPanel.setLayout(new java.awt.GridBagLayout()); firstConflictButton.setPreferredSize(new java.awt.Dimension(24, 24)); firstConflictButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { firstConflictButtonActionPerformed(evt); } }); commandPanel.add(firstConflictButton, new java.awt.GridBagConstraints()); prevConflictButton.setToolTipText(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.prevButton.toolTipText")); prevConflictButton.setMargin(new java.awt.Insets(1, 1, 0, 1)); prevConflictButton.setMaximumSize(new java.awt.Dimension(24, 24)); prevConflictButton.setMinimumSize(new java.awt.Dimension(24, 24)); prevConflictButton.setPreferredSize(new java.awt.Dimension(24, 24)); prevConflictButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { prevConflictButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 1); commandPanel.add(prevConflictButton, gridBagConstraints); nextConflictButton.setToolTipText(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.nextButton.toolTipText")); nextConflictButton.setMargin(new java.awt.Insets(1, 1, 0, 1)); nextConflictButton.setMaximumSize(new java.awt.Dimension(24, 24)); nextConflictButton.setMinimumSize(new java.awt.Dimension(24, 24)); nextConflictButton.setPreferredSize(new java.awt.Dimension(24, 24)); nextConflictButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { nextConflictButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1); commandPanel.add(nextConflictButton, gridBagConstraints); lastConflictButton.setPreferredSize(new java.awt.Dimension(24, 24)); lastConflictButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { lastConflictButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 2); commandPanel.add(lastConflictButton, gridBagConstraints); statusLabel.setText("jLabel1"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 1); commandPanel.add(statusLabel, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); add(commandPanel, gridBagConstraints); editorPanel.setLayout(new java.awt.GridBagLayout()); editorPanel.setPreferredSize(new java.awt.Dimension(700, 600)); mergeSplitPane.setDividerSize(4); mergeSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); diffSplitPane.setDividerSize(4); filePanel1.setLayout(new java.awt.GridBagLayout()); leftCommandPanel.setLayout(new java.awt.GridBagLayout()); acceptLeftButton.setText(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(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { acceptLeftButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 1); leftCommandPanel.add(acceptLeftButton, gridBagConstraints); acceptAndNextLeftButton.setText(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptAndNextLeftButton")); acceptAndNextLeftButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptAndNextLeftButtonA11yDesc")); acceptAndNextLeftButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { acceptAndNextLeftButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 2); leftCommandPanel.add(acceptAndNextLeftButton, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; filePanel1.add(leftCommandPanel, gridBagConstraints); jEditorPane1.addCaretListener(new javax.swing.event.CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent evt) { jEditorPane1CaretUpdate(evt); } }); jScrollPane1.setViewportView(jEditorPane1); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; filePanel1.add(jScrollPane1, gridBagConstraints); fileLabel1.setText("jLabel1"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridy = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); filePanel1.add(fileLabel1, gridBagConstraints); diffSplitPane.setLeftComponent(filePanel1); filePanel2.setLayout(new java.awt.GridBagLayout()); rightCommandPanel.setLayout(new java.awt.GridBagLayout()); acceptRightButton.setText(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(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { acceptRightButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 1); rightCommandPanel.add(acceptRightButton, gridBagConstraints); acceptAndNextRightButton.setText(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.acceptAndNextRightButton")); acceptAndNextRightButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergePanel.class).getString("ACS_MergePanel.acceptAndNextRightButtonA11yDesc")); acceptAndNextRightButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { acceptAndNextRightButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 2); rightCommandPanel.add(acceptAndNextRightButton, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; filePanel2.add(rightCommandPanel, gridBagConstraints); jEditorPane2.addCaretListener(new javax.swing.event.CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent evt) { jEditorPane2CaretUpdate(evt); } }); jScrollPane2.setViewportView(jEditorPane2); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; filePanel2.add(jScrollPane2, gridBagConstraints); fileLabel2.setText("jLabel2"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridy = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); filePanel2.add(fileLabel2, gridBagConstraints); diffSplitPane.setRightComponent(filePanel2); mergeSplitPane.setLeftComponent(diffSplitPane); resultPanel.setLayout(new java.awt.GridBagLayout()); resultScrollPane.setViewportView(jEditorPane3); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; resultPanel.add(resultScrollPane, gridBagConstraints); resultLabel.setText("jLabel1"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); resultPanel.add(resultLabel, gridBagConstraints); mergeSplitPane.setRightComponent(resultPanel); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; editorPanel.add(mergeSplitPane, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; add(editorPanel, gridBagConstraints); }//GEN-END:initComponents private void firstConflictButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_firstConflictButtonActionPerformed // Add your handling code here: fireControlActionCommand(ACTION_FIRST_CONFLICT); }//GEN-LAST:event_firstConflictButtonActionPerformed private void prevConflictButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prevConflictButtonActionPerformed // Add your handling code here: fireControlActionCommand(ACTION_PREVIOUS_CONFLICT); }//GEN-LAST:event_prevConflictButtonActionPerformed private void nextConflictButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextConflictButtonActionPerformed // Add your handling code here: fireControlActionCommand(ACTION_NEXT_CONFLICT); }//GEN-LAST:event_nextConflictButtonActionPerformed private void lastConflictButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lastConflictButtonActionPerformed // Add your handling code here: fireControlActionCommand(ACTION_LAST_CONFLICT); }//GEN-LAST:event_lastConflictButtonActionPerformed private void acceptRightButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptRightButtonActionPerformed // Add your handling code here: fireControlActionCommand(ACTION_ACCEPT_RIGHT); }//GEN-LAST:event_acceptRightButtonActionPerformed private void acceptAndNextRightButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptAndNextRightButtonActionPerformed // Add your handling code here: fireControlActionCommand(ACTION_ACCEPT_RIGHT); fireControlActionCommand(ACTION_NEXT_CONFLICT); }//GEN-LAST:event_acceptAndNextRightButtonActionPerformed private void acceptAndNextLeftButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptAndNextLeftButtonActionPerformed // Add your handling code here: fireControlActionCommand(ACTION_ACCEPT_LEFT); fireControlActionCommand(ACTION_NEXT_CONFLICT); }//GEN-LAST:event_acceptAndNextLeftButtonActionPerformed private void acceptLeftButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptLeftButtonActionPerformed // Add your handling code here: fireControlActionCommand(ACTION_ACCEPT_LEFT); }//GEN-LAST:event_acceptLeftButtonActionPerformed private void jEditorPane1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jEditorPane1CaretUpdate // Add your handling code here: /* int pos = evt.getDot(); int line = org.openide.text.NbDocument.findLineNumber((StyledDocument) jEditorPane1.getDocument(), pos); StyledDocument linesDoc = (StyledDocument)jEditorPane1.getDocument(); int numLines = org.openide.text.NbDocument.findLineNumber(linesDoc, linesDoc.getEndPosition().getOffset()); if (line <= numLines) { jEditorPane1.setCaretPosition(org.openide.text.NbDocument.findLineOffset(linesDoc, line)); } */ }//GEN-LAST:event_jEditorPane1CaretUpdate private void jEditorPane2CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jEditorPane2CaretUpdate // Add your handling code here: /* int pos = evt.getDot(); int line = org.openide.text.NbDocument.findLineNumber((StyledDocument) jEditorPane2.getDocument(), pos); StyledDocument linesDoc = (StyledDocument)jEditorPane2.getDocument(); int numLines = org.openide.text.NbDocument.findLineNumber(linesDoc, linesDoc.getEndPosition().getOffset()); if (line <= numLines) { jEditorPane2.setCaretPosition(org.openide.text.NbDocument.findLineOffset(linesDoc, line)); } */ }//GEN-LAST:event_jEditorPane2CaretUpdate public void setNumConflicts(int numConflicts) { this.numConflicts = numConflicts; this.numUnresolvedConflicts = numConflicts; } public int getNumUnresolvedConflicts(){ return numUnresolvedConflicts; } public void setCurrentLine(int line, int diffLength, int conflictPos, int resultLine) { if (line > 0) { showLine12(line, diffLength); showLine3(resultLine, diffLength); if (conflictPos >= 0) this.currentConflictPos = conflictPos; updateStatusLine(); updateAcceptButtons(line); } } public void setNeedsSaveState(boolean needsSave) { firePropertyChange((needsSave) ? PROP_CAN_BE_SAVED : PROP_CAN_NOT_BE_SAVED, null, null); } public synchronized void addControlActionListener(ActionListener listener) { controlListeners.add(listener); } public synchronized void removeControlActionListener(ActionListener listener) { controlListeners.remove(listener); } private void updateStatusLine() { statusLabel.setText(org.openide.util.NbBundle.getMessage(MergePanel.class, "MergePanel.statusLine", Integer.toString(currentConflictPos + 1), Integer.toString(numConflicts), Integer.toString(numUnresolvedConflicts))); } private void updateAcceptButtons(int linePos) { Integer conflictPos = new Integer(linePos); boolean left = resolvedLeftConflictsLineNumbers.contains(conflictPos); boolean right = resolvedRightConflictsLineNumbers.contains(conflictPos); acceptLeftButton.setEnabled(!left); acceptAndNextLeftButton.setEnabled(!left); acceptRightButton.setEnabled(!right); acceptAndNextRightButton.setEnabled(!right); } private void fireControlActionCommand(String command) { ArrayList listeners; synchronized (this) { listeners = new ArrayList(controlListeners); } ActionEvent evt = new ActionEvent(this, 0, command); for (Iterator it = listeners.iterator(); it.hasNext(); ) { ActionListener l = (ActionListener) it.next(); l.actionPerformed(evt); } } private void jScrollBar1AdjustmentValueChanged (java.awt.event.AdjustmentEvent evt) {//GEN-FIRST:event_jScrollBar1AdjustmentValueChanged // Add your handling code here: }//GEN-LAST:event_jScrollBar1AdjustmentValueChanged private void closeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed // Add your handling code here: exitForm(null); }//GEN-LAST:event_closeButtonActionPerformed /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm /* try { org.netbeans.editor.Settings.setValue(null, org.netbeans.editor.SettingsNames.LINE_NUMBER_VISIBLE, lineNumbersVisible); } catch (Throwable exc) { // editor module not found } //System.out.println("exitForm() called."); //diff.closing(); //close(); //dispose (); for(Iterator it = closeListeners.iterator(); it.hasNext(); ) { ((TopComponentCloseListener) it.next()).closing(); } */ }//GEN-LAST:event_exitForm public void setSystemActions(SystemAction[] actions) { this.systemActions = actions; } public SystemAction[] getSystemActions() { return systemActions; } private void initActions() { jEditorPane1.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { editorActivated(jEditorPane1); } public void focusLost(FocusEvent e) { editorDeactivated(jEditorPane1); } }); jEditorPane2.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { editorActivated(jEditorPane2); } public void focusLost(FocusEvent e) { editorDeactivated(jEditorPane2); } }); jEditorPane3.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { editorActivated(jEditorPane3); } public void focusLost(FocusEvent e) { editorDeactivated(jEditorPane3); } }); } private Hashtable kitActions; /** Listener for copy action enabling */ private PropertyChangeListener copyL; private PropertyChangeListener copyP; private Action getAction (String s, JEditorPane editor) { if (kitActions == null) { kitActions = new Hashtable(); } Hashtable actions = (Hashtable) kitActions.get(editor); if (actions == null) { EditorKit kit = editor.getEditorKit(); if (kit == null) { return null; } Action[] a = kit.getActions (); actions = new Hashtable (a.length); int k = a.length; for (int i = 0; i < k; i++) actions.put (a[i].getValue (Action.NAME), a[i]); kitActions.put(editor, actions); } return (Action) actions.get (s); } private void editorActivated(final JEditorPane editor) { //System.out.println("editor("+editor+") activated."); final Action copy = getAction (DefaultEditorKit.copyAction, editor); if (copy != null) { final CallbackSystemAction sysCopy = ((CallbackSystemAction) SystemAction.get (CopyAction.class)); final ActionPerformer perf = new ActionPerformer () { public void performAction (SystemAction action) { copy.actionPerformed (new ActionEvent (editor, 0, "")); // NOI18N } }; sysCopy.setActionPerformer(copy.isEnabled() ? perf : null); PropertyChangeListener copyListener; copy.addPropertyChangeListener(copyListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if ("enabled".equals(evt.getPropertyName())) { // NOI18N if (((Boolean)evt.getNewValue()).booleanValue()) { sysCopy.setActionPerformer(perf); } else if (sysCopy.getActionPerformer() == perf) { sysCopy.setActionPerformer(null); } } } }); if (editor.equals(jEditorPane1)) copyL = copyListener; else copyP = copyListener; } } private void editorDeactivated(JEditorPane editor) { //System.out.println("editorDeactivated ("+editor+")"); Action copy = getAction (DefaultEditorKit.copyAction, editor); PropertyChangeListener copyListener; if (editor.equals(jEditorPane1)) copyListener = copyL; else copyListener = copyP; if (copy != null) { copy.removePropertyChangeListener(copyListener); } } public void open() { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { diffSplitPane.setDividerLocation(0.5); mergeSplitPane.setDividerLocation(0.5); openPostProcess(); } }); } protected void openPostProcess() { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { initGlobalSizes(); //showLine(1, 0); addChangeListeners(); /* javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { initGlobalSizes(); // do that again to be sure that components are initialized. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { syncFont(); // Components have to be fully initialized before font syncing addChangeListeners(); } }); } }); */ } }); } /* public void removeNotify() { System.out.println("removeNotify() called"); exitForm(null); super.removeNotify(); } */ private void initGlobalSizes() { StyledDocument doc1 = (StyledDocument) jEditorPane1.getDocument(); StyledDocument doc2 = (StyledDocument) jEditorPane2.getDocument(); int numLines1 = org.openide.text.NbDocument.findLineNumber(doc1, doc1.getEndPosition().getOffset()); int numLines2 = org.openide.text.NbDocument.findLineNumber(doc2, doc2.getEndPosition().getOffset()); int numLines = Math.max(numLines1, numLines2); if (numLines < 1) numLines = 1; this.totalLines = numLines; // int totHeight = editorPanel1.getSize().height; int totHeight = jEditorPane1.getSize().height; // int value = editorPanel2.getSize().height; int value = jEditorPane2.getSize().height; if (value > totHeight) totHeight = value; this.totalHeight = totHeight; } private void showLine12(int line, int diffLength) { //System.out.println("showLine("+line+", "+diffLength+")"); this.linesComp1.setActiveLine(line); this.linesComp2.setActiveLine(line); linesComp1.repaint(); linesComp2.repaint(); int padding = 5; if (line <= 5) padding = line/2; int off1, off2; int ypos; int viewHeight = jViewport1.getExtentSize().height; java.awt.Point p1, p2; initGlobalSizes(); // The window might be resized in the mean time. p1 = jViewport1.getViewPosition(); p2 = jViewport2.getViewPosition(); ypos = (totalHeight*(line - padding - 1))/(totalLines + 1); int viewSize = jViewport1.getViewRect().y; if (ypos < p1.y || ypos + ((diffLength + padding)*totalHeight)/totalLines > p1.y + viewHeight) { //System.out.println("resetting posision=" + ypos); p1.y = ypos; p2.y = ypos; setViewPosition(p1, p2); } off1 = org.openide.text.NbDocument.findLineOffset((StyledDocument) jEditorPane1.getDocument(), line); off2 = org.openide.text.NbDocument.findLineOffset((StyledDocument) jEditorPane2.getDocument(), line); jEditorPane1.setCaretPosition(off1); jEditorPane2.setCaretPosition(off2); //D.deb("off1 = "+off1+", off2 = "+off2+", totalHeight = "+totalHeight+", totalLines = "+totalLines+", ypos = "+ypos); //System.out.println("off1 = "+off1+", off2 = "+off2+", totalHeight = "+totalHeight+", totalLines = "+totalLines+", ypos = "+ypos); } private void showLine3(int line, int diffLength) { linesComp3.setActiveLine(line); linesComp3.repaint(); } private void setViewPosition(java.awt.Point p1, java.awt.Point p2) { jViewport1.setViewPosition(p1); jViewport1.repaint(jViewport1.getViewRect()); jViewport2.setViewPosition(p2); jViewport2.repaint(jViewport2.getViewRect()); } private void joinScrollBars() { final JScrollBar scrollBarH1 = jScrollPane1.getHorizontalScrollBar(); final JScrollBar scrollBarV1 = jScrollPane1.getVerticalScrollBar(); final JScrollBar scrollBarH2 = jScrollPane2.getHorizontalScrollBar(); final JScrollBar scrollBarV2 = jScrollPane2.getVerticalScrollBar(); final JScrollBar scrollBarH3 = resultScrollPane.getHorizontalScrollBar(); final JScrollBar scrollBarV3 = resultScrollPane.getVerticalScrollBar(); scrollBarV1.getModel().addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { int value = scrollBarV1.getValue(); int oldValue = scrollBarV2.getValue(); if (oldValue != value) { scrollBarV2.setValue(value); // System.out.println("setting v2=" + value); // Thread.dumpStack(); } // TODO use a better algorithm to adjust scrollbars, if there are large changes, this will not work optimally. if (value == verticalScroll1ChangedValue) return ; int max1 = scrollBarV1.getMaximum(); int max2 = scrollBarV3.getMaximum(); int ext1 = scrollBarV1.getModel().getExtent(); int ext2 = scrollBarV3.getModel().getExtent(); if (max1 == ext1) verticalScroll3ChangedValue = 0; else verticalScroll3ChangedValue = (value*(max2 - ext2))/(max1 - ext1); verticalScroll1ChangedValue = -1; scrollBarV3.setValue(verticalScroll3ChangedValue); } }); //jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); // The vertical scroll bar must be there for mouse wheel to work correctly. // However it's not necessary to be seen (but must be visible so that the wheel will work). jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0)); scrollBarV2.getModel().addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { int value = scrollBarV2.getValue(); int oldValue = scrollBarV1.getValue(); if (oldValue != value) { scrollBarV1.setValue(value); // System.out.println("setting v1 to=" + value); } } }); /* don't not let the result source vertical scrolling to influence the diff panels. scrollBarV3.getModel().addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { int value = scrollBarV3.getValue(); if (value == verticalScroll3ChangedValue) return ; int max1 = scrollBarV3.getMaximum(); int max2 = scrollBarV1.getMaximum(); int ext1 = scrollBarV3.getModel().getExtent(); int ext2 = scrollBarV1.getModel().getExtent(); if (max1 == ext1) verticalScroll1ChangedValue = 0; else verticalScroll1ChangedValue = (value*(max2 - ext2))/(max1 - ext1); verticalScroll3ChangedValue = -1; scrollBarV1.setValue(verticalScroll1ChangedValue); } }); */ scrollBarH1.getModel().addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { int value = scrollBarH1.getValue(); // System.out.println("stateChangedH1:value = "+value+", horizontalScroll1ChangedValue = "+horizontalScroll1ChangedValue); if (value == horizontalScroll1ChangedValue) return; int max1 = scrollBarH1.getMaximum(); int max2 = scrollBarH2.getMaximum(); int ext1 = scrollBarH1.getModel().getExtent(); int ext2 = scrollBarH2.getModel().getExtent(); if (max1 == ext1) horizontalScroll2ChangedValue = 0; else horizontalScroll2ChangedValue = (value*(max2 - ext2))/(max1 - ext1); horizontalScroll1ChangedValue = -1; // System.out.println("H1 value = "+value+" => H2 value = "+horizontalScroll2ChangedValue+"\t\tmax1 = "+max1+", max2 = "+max2); scrollBarH2.setValue(horizontalScroll2ChangedValue); } }); scrollBarH2.getModel().addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { int value = scrollBarH2.getValue(); // System.out.println("stateChangedH2:value = "+value+", horizontalScroll2ChangedValue = "+horizontalScroll2ChangedValue); if (value == horizontalScroll2ChangedValue) return; int max1 = scrollBarH1.getMaximum(); int max2 = scrollBarH2.getMaximum(); int max3 = scrollBarH3.getMaximum(); int ext1 = scrollBarH1.getModel().getExtent(); int ext2 = scrollBarH2.getModel().getExtent(); int ext3 = scrollBarH3.getModel().getExtent(); if (max2 == ext2) { horizontalScroll1ChangedValue = 0; horizontalScroll3ChangedValue = 0; } else { horizontalScroll1ChangedValue = (value*(max1 - ext1))/(max2 - ext2); horizontalScroll3ChangedValue = (value*(max3 - ext3))/(max2 - ext2); } horizontalScroll2ChangedValue = -1; // System.out.println("H2 value = "+value+" => H1 value = "+horizontalScroll1ChangedValue+"\t\tmax1 = "+max1+", max2 = "+max2); scrollBarH1.setValue(horizontalScroll1ChangedValue); scrollBarH3.setValue(horizontalScroll3ChangedValue); } }); scrollBarH3.getModel().addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { int value = scrollBarH3.getValue(); // System.out.println("stateChangedH1:value = "+value+", horizontalScroll1ChangedValue = "+horizontalScroll1ChangedValue); if (value == horizontalScroll3ChangedValue) return; int max1 = scrollBarH1.getMaximum(); int max2 = scrollBarH2.getMaximum(); int max3 = scrollBarH3.getMaximum(); int ext1 = scrollBarH1.getModel().getExtent(); int ext2 = scrollBarH2.getModel().getExtent(); int ext3 = scrollBarH3.getModel().getExtent(); if (max3 == ext3) { horizontalScroll1ChangedValue = 0; horizontalScroll2ChangedValue = 0; } else { horizontalScroll1ChangedValue = (value*(max1 - ext1))/(max3 - ext3); horizontalScroll2ChangedValue = (value*(max2 - ext2))/(max3 - ext3); } horizontalScroll3ChangedValue = -1; // System.out.println("H1 value = "+value+" => H2 value = "+horizontalScroll2ChangedValue+"\t\tmax1 = "+max1+", max2 = "+max2); scrollBarH1.setValue(horizontalScroll1ChangedValue); scrollBarH2.setValue(horizontalScroll2ChangedValue); } }); diffSplitPane.setDividerLocation(0.5); mergeSplitPane.setDividerLocation(0.5); } private String strCharacters(char c, int num) { StringBuffer s = new StringBuffer(); while(num-- > 0) { s.append(c); } return s.toString(); } private void customizeEditor(JEditorPane editor) { EditorKit kit = editor.getEditorKit(); /* try { org.netbeans.editor.Settings.setValue(null, org.netbeans.editor.SettingsNames.LINE_NUMBER_VISIBLE, Boolean.FALSE); } catch (Throwable exc) { // editor module not found } */ StyledDocument doc; Document document = editor.getDocument(); /* StyledDocument docLines = new DefaultStyledDocument(); textLines.setStyledDocument(docLines); */ try { doc = (StyledDocument) editor.getDocument(); } catch(ClassCastException e) { doc = new DefaultStyledDocument(); try { doc.insertString(0, document.getText(0, document.getLength()), null); } catch (BadLocationException ble) { // leaving the document empty } editor.setDocument(doc); } //int lastOffset = doc.getEndPosition().getOffset(); //int numLines = org.openide.text.NbDocument.findLineNumber(doc, lastOffset); //int numLength = Integer.toString(numLines).length(); // textLines.setForeground(numForegroundColor); // textLines.setBackground(numBackgroundColor); /* for (int line = 0; line <= numLines; line++) { int offset = org.openide.text.NbDocument.findLineOffset(doc, line); String lineStr = Integer.toString(line+1); if (lineStr.length() < numLength) lineStr = strCharacters(' ', numLength - lineStr.length()) + lineStr; //lineStr = " "+lineStr+" "; // NOI18N try { if (line < numLines) lineStr += "\n"; // NOI18N docLines.insertString(docLines.getLength(), lineStr, null); } catch (BadLocationException e) { E.deb("Internal ERROR: "+e.getMessage()); // NOI18N } } */ // joinScrollBars(); } private void setScrollBarsIncrements() { StyledDocument doc = (StyledDocument) jEditorPane1.getDocument(); int lineHeight = jEditorPane1.getSize().height/org.openide.text.NbDocument.findLineNumber(doc, doc.getEndPosition().getOffset()); jScrollPane1.getVerticalScrollBar().setUnitIncrement((int) (VERTICAL_SCROLL_NUM_LINES*lineHeight)); jScrollPane2.getVerticalScrollBar().setUnitIncrement((int) (VERTICAL_SCROLL_NUM_LINES*lineHeight)); jScrollPane1.getHorizontalScrollBar().setUnitIncrement((int) (HORIZONTAL_SCROLL_NUM_LINES*lineHeight)); jScrollPane2.getHorizontalScrollBar().setUnitIncrement((int) (HORIZONTAL_SCROLL_NUM_LINES*lineHeight)); } private void addChangeListeners() { jEditorPane1.addPropertyChangeListener("font", new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { //System.out.println("1:evt = "+evt+", Property NAME = "+evt.getPropertyName()); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { initGlobalSizes(); linesComp1.changedAll(); } }); } }); jEditorPane2.addPropertyChangeListener("font", new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { //System.out.println("2:evt = "+evt+", Property NAME = "+evt.getPropertyName()); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { initGlobalSizes(); linesComp2.changedAll(); } }); } }); jEditorPane3.addPropertyChangeListener("font", new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { //System.out.println("2:evt = "+evt+", Property NAME = "+evt.getPropertyName()); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { initGlobalSizes(); linesComp3.changedAll(); } }); } }); } public void setSource1(Reader r) throws IOException { //D.deb("setFile("+url+")"); // NOI18N //System.out.println("setFile1("+url+")"); EditorKit kit = jEditorPane1.getEditorKit(); if (kit == null) throw new IOException("Missing Editor Kit"); // NOI18N Document doc = kit.createDefaultDocument(); if (!(doc instanceof StyledDocument)) { doc = new DefaultStyledDocument(new StyleContext()); kit = new StyledEditorKit(); jEditorPane1.setEditorKit(kit); } try { kit.read(r, doc, 0); } catch (javax.swing.text.BadLocationException e) { throw new IOException("Can not locate the beginning of the document."); // NOI18N } finally { r.close(); } kit.install(jEditorPane1); jEditorPane1.setDocument(doc); //jEditorPane1.setPage(url); jEditorPane1.setEditable(false); customizeEditor(jEditorPane1); linesComp1 = new LinesComponent(jEditorPane1); jScrollPane1.setRowHeaderView(linesComp1); jViewport1 = jScrollPane1.getViewport(); } public void setSource2(Reader r) throws IOException { //D.deb("setFile("+url+")"); // NOI18N EditorKit kit = jEditorPane2.getEditorKit(); if (kit == null) throw new IOException("Missing Editor Kit"); // NOI18N Document doc = kit.createDefaultDocument(); if (!(doc instanceof StyledDocument)) { doc = new DefaultStyledDocument(new StyleContext()); kit = new StyledEditorKit(); jEditorPane2.setEditorKit(kit); } try { kit.read(r, doc, 0); } catch (javax.swing.text.BadLocationException e) { throw new IOException("Can not locate the beginning of the document."); // NOI18N } finally { r.close(); } kit.install(jEditorPane2); jEditorPane2.setDocument(doc); //jEditorPane2.setPage(url); jEditorPane2.setEditable(false); customizeEditor(jEditorPane2); linesComp2 = new LinesComponent(jEditorPane2); jScrollPane2.setRowHeaderView(linesComp2); jViewport2 = jScrollPane2.getViewport(); // add scrollbar listeners.. joinScrollBars(); } public void setResultSource(Reader r) throws IOException { EditorKit kit = jEditorPane3.getEditorKit(); if (kit == null) throw new IOException("Missing Editor Kit"); // NOI18N Document doc = kit.createDefaultDocument(); if (!(doc instanceof StyledDocument)) { doc = new DefaultStyledDocument(new StyleContext()); kit = new StyledEditorKit(); jEditorPane3.setEditorKit(kit); } try { kit.read(r, doc, 0); } catch (javax.swing.text.BadLocationException e) { throw new IOException("Can not locate the beginning of the document."); // NOI18N } finally { r.close(); } kit.install(jEditorPane3); jEditorPane3.setDocument(doc); //jEditorPane2.setPage(url); jEditorPane3.setEditable(false); customizeEditor(jEditorPane3); linesComp3 = new LinesComponent(jEditorPane3); resultScrollPane.setRowHeaderView(linesComp3); resultLineNumbers = new int[1]; assureResultLineNumbersLength( org.openide.text.NbDocument.findLineNumber((StyledDocument) doc, doc.getEndPosition().getOffset()) + 1); for (int i = 0; i < resultLineNumbers.length; i++) resultLineNumbers[i] = i; } private static final int EXTRA_CAPACITY = 5; private void assureResultLineNumbersLength(int length) { if (length > resultLineNumbers.length) { int[] newrln = new int[length + EXTRA_CAPACITY]; System.arraycopy(resultLineNumbers, 0, newrln, 0, resultLineNumbers.length); resultLineNumbers = newrln; } } /** * Copy a part of first document into the result document. * @param line1 The starting line in the first source * @param line2 The ending line in the first source or |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.