View Javadoc

1   /*
2    * @(#) $Id$
3    *
4    * Copyright (c) 2010 Steven Drinovsky. All Rights Reserved.
5    *
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   *
19   */
20  
21  package org.sdrinovsky.sdsvn.diff;
22  
23  import java.awt.*;
24  import java.awt.event.WindowAdapter;
25  import java.awt.event.WindowEvent;
26  import java.awt.event.WindowListener;
27  
28  import javax.swing.*;
29  
30  import org.jmeld.settings.EditorSettings;
31  import org.jmeld.settings.JMeldSettings;
32  import org.jmeld.ui.JMeldPanel;
33  import org.jmeld.util.prefs.WindowPreference;
34  
35  import org.openide.util.lookup.ServiceProvider;
36  
37  /***
38   * Use components from the JMeld project to render a comparison of two files in a JFrame.
39   */
40  @ServiceProvider(service = DiffView.class)
41  public class JMeldDiffView implements DiffView {
42    /***
43     * The frame to place the JMeldPanel in
44     */
45    private JFrame frame;
46  
47    /***
48     * Create an instance of JMeld to compare two files.
49     *
50     * @param leftFile      is the file on the left hand side of the comparison
51     * @param rightFile     is the file on the right hand side of the comparison
52     * @param leftReadOnly  true will make the left file read-only in the diff, false will allow edits to the file
53     * @param rightReadOnly true will make the right file read-only in the diff, false will allow edits to the file
54     *
55     * @throws Exception
56     *
57     */
58    public void executeDiff(String leftFile, String rightFile, boolean leftReadOnly,
59                            boolean rightReadOnly) throws Exception {
60      final EditorSettings editorSettings = JMeldSettings.getInstance().getEditor();
61  
62      editorSettings.setLeftsideReadonly(leftReadOnly);
63      editorSettings.setRightsideReadonly(rightReadOnly);
64  
65      //TODO: Move this to a setting
66      Font monospacedFont = new Font("Courier New", Font.PLAIN, 11);
67  
68      editorSettings.setFont(monospacedFont);
69  
70      frame = new JFrame();
71  
72      final JMeldPanel panel = new NonExitingJMeldPanel();
73  
74      panel.SHOW_TABBEDPANE_OPTION.disable();
75      panel.SHOW_TOOLBAR_OPTION.disable();
76      frame.add(panel);
77      frame.addWindowListener(new WindowCloseListener());
78      new WindowPreference(frame.getTitle(), frame);
79      frame.addWindowListener(panel.getWindowListener());
80      frame.setVisible(true);
81      frame.toFront();
82      panel.openComparison(leftFile, rightFile);
83    }
84  
85    /***
86     * A WindowListener that responds to windowClosed events by hiding the JMeld frame and setting it to null
87     * so it can be garbage-collected.
88     */
89    private class WindowCloseListener extends WindowAdapter {
90      /***
91       * @see java.awt.event.WindowAdapter#windowClosed(java.awt.event.WindowEvent)
92       *
93       * @param e
94       */
95      @Override
96      public void windowClosed(WindowEvent e) {
97        if(frame != null) {
98          frame.setVisible(false);
99  
100         frame = null;
101       }
102     }
103   }
104 
105   /***
106    * The JMeldPanel registers a window listener that exits the JVM when it detects that the frame it resides
107    * in was closed. This subclass overrides this behavior with a no-op window listener.
108    * Thanks to Rob Manning.
109    */
110   private class NonExitingJMeldPanel extends JMeldPanel {
111     /***
112      * @see org.jmeld.ui.JMeldPanel#getWindowListener()
113      *
114      * @return
115      */
116     @Override
117     public WindowListener getWindowListener() {
118       return new WindowAdapter() {}
119       ;
120     }
121   }
122 }