KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > diff > DiffAction


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.diff;
21
22 import java.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27
28 import org.openide.NotifyDescriptor;
29 import org.openide.filesystems.FileObject;
30 import org.openide.loaders.DataObject;
31 import org.openide.loaders.DataShadow;
32 import org.openide.nodes.Node;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35 import org.openide.util.RequestProcessor;
36 import org.openide.util.Cancellable;
37 import org.openide.util.actions.NodeAction;
38 import org.openide.windows.TopComponent;
39
40 //import org.netbeans.modules.diff.cmdline.DiffCommand;
41
//import org.netbeans.modules.vcscore.diff.AbstractDiff;
42

43 import org.netbeans.api.diff.*;
44 import org.netbeans.api.project.Project;
45 import org.netbeans.api.progress.ProgressHandle;
46 import org.netbeans.api.progress.ProgressHandleFactory;
47 import org.openide.DialogDisplayer;
48 import org.openide.ErrorManager;
49 import org.openide.filesystems.FileUtil;
50
51 //import org.netbeans.modules.diff.builtin.provider.BuiltInDiffProvider;
52
//import org.netbeans.modules.diff.cmdline.CmdlineDiffProvider;
53
//import org.netbeans.modules.diff.builtin.visualizer.GraphicalDiffVisualizer;
54

55 //import org.netbeans.modules.diff.io.diff.*;
56

57 /**
58  * Diff Action. It gets the default diff visualizer and diff provider if needed
59  * and display the diff visual representation of two files selected in the IDE.
60  *
61  * @author Martin Entlicher
62  */

63 public class DiffAction extends NodeAction {
64
65     /** Creates new DiffAction */
66     public DiffAction() {
67         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
68
}
69     
70     public String JavaDoc getName() {
71         return NbBundle.getMessage(DiffAction.class, "CTL_DiffActionName");
72     }
73     
74     static FileObject getFileFromNode(Node node) {
75         FileObject fo = (FileObject) node.getLookup().lookup(FileObject.class);
76         if (fo == null) {
77             Project p = (Project) node.getLookup().lookup(Project.class);
78             if (p != null) return p.getProjectDirectory();
79
80             DataObject dobj = (DataObject) node.getCookie(DataObject.class);
81             if (dobj instanceof DataShadow) {
82                 dobj = ((DataShadow) dobj).getOriginal();
83             }
84             if (dobj != null) {
85                 fo = dobj.getPrimaryFile();
86             }
87         }
88         return fo;
89     }
90     
91     public boolean enable(Node[] nodes) {
92         //System.out.println("DiffAction.enable() = "+(nodes.length == 2));
93
if (nodes.length == 2) {
94             FileObject fo1 = getFileFromNode(nodes[0]);
95             FileObject fo2 = getFileFromNode(nodes[1]);
96             if (fo1 != null && fo2 != null) {
97                 if (fo1.isData() && fo2.isData()) {
98                     Diff d = Diff.getDefault();
99                     return d != null;
100                 }
101             }
102         }
103         return false;
104     }
105     
106     /**
107      * This action should not be run in AWT thread, because it opens streams
108      * to files.
109      * @return true not to run in AWT thread!
110      */

111     protected boolean asynchronous() {
112         return true;
113     }
114     
115     public void performAction(Node[] nodes) {
116         ArrayList JavaDoc<FileObject> fos = new ArrayList JavaDoc<FileObject>();
117         for (int i = 0; i < nodes.length; i++) {
118             FileObject fo = getFileFromNode(nodes[i]);
119             if (fo != null) {
120                 fos.add(fo);
121             }
122         }
123         if (fos.size() < 2) return ;
124         final FileObject fo1 = fos.get(0);
125         final FileObject fo2 = fos.get(1);
126         performAction(fo1, fo2);
127     }
128     
129     /**
130      * Shows the diff between two FileObject objects.
131      * This is expected not to be called in AWT thread.
132      */

133     public static void performAction(final FileObject fo1, final FileObject fo2) {
134         performAction(fo1, fo2, null);
135     }
136     /**
137      * Shows the diff between two FileObject objects.
138      * This is expected not to be called in AWT thread.
139      * @param type Use the type of that FileObject to load both files.
140      */

141     static void performAction(FileObject fo1, FileObject fo2, FileObject type) {
142         //System.out.println("performAction("+fo1+", "+fo2+")");
143
//doDiff(fo1, fo2);
144
Diff diff = Diff.getDefault();
145         //System.out.println("dv = "+dv);
146
if (diff == null) {
147             DialogDisplayer.getDefault().notify(
148                 new NotifyDescriptor.Message(NbBundle.getMessage(DiffAction.class,
149                     "MSG_NoDiffVisualizer")));
150             return ;
151         }
152         Component JavaDoc tp;
153         Reader JavaDoc r1 = null;
154         Reader JavaDoc r2 = null;
155         try {
156             EncodedReaderFactory rf = EncodedReaderFactory.getDefault();
157             if (type != null) {
158                 r1 = rf.getReader(fo1, rf.getEncoding(type), type);
159                 r2 = rf.getReader(fo2, rf.getEncoding(type), type);
160             } else {
161                 r1 = rf.getReader(fo1, rf.getEncoding(fo1), fo2.getExt());
162                 r2 = rf.getReader(fo2, rf.getEncoding(fo2), fo1.getExt());
163             }
164             String JavaDoc mimeType;
165             if (type != null) {
166                 mimeType = type.getMIMEType();
167             } else {
168                 mimeType = fo1.getMIMEType();
169             }
170             
171             final Thread JavaDoc victim = Thread.currentThread();
172             Cancellable killer = new Cancellable() {
173                 public boolean cancel() {
174                     victim.interrupt();
175                     return true;
176                 }
177             };
178             String JavaDoc name = NbBundle.getMessage(DiffAction.class, "BK0001");
179             ProgressHandle ph = ProgressHandleFactory.createHandle(name, killer);
180             try {
181                 ph.start();
182                 tp = diff.createDiff(fo1.getNameExt(), FileUtil.getFileDisplayName(fo1),
183                                      r1,
184                                      fo2.getNameExt(), FileUtil.getFileDisplayName(fo2),
185                                      r2, mimeType);
186             } finally {
187                 ph.finish();
188             }
189         } catch (IOException JavaDoc ioex) {
190             ErrorManager.getDefault().notify(ioex);
191             return ;
192         } finally {
193             try {
194                 if (r1 != null) r1.close();
195             } catch (IOException JavaDoc ioex) {}
196             try {
197                 if (r2 != null) r2.close();
198             } catch (IOException JavaDoc ioex) {}
199         }
200         //System.out.println("tp = "+tp);
201
if (tp != null) {
202             final Component JavaDoc ftp = tp;
203             SwingUtilities.invokeLater(new Runnable JavaDoc() {
204                 public void run() {
205                     if (ftp instanceof TopComponent) {
206                         ((TopComponent) ftp).open();
207                         ((TopComponent) ftp).requestActive();
208                     } else {
209                         ftp.setVisible(true);
210                         ftp.requestFocusInWindow();
211                     }
212                 }
213             });
214         }
215     }
216     
217     public HelpCtx getHelpCtx() {
218         return new HelpCtx(DiffAction.class);
219     }
220
221 }
222
Popular Tags