1 19 20 package org.netbeans.modules.diff; 21 22 import java.awt.Component ; 23 import java.io.IOException ; 24 import java.io.Reader ; 25 import java.util.ArrayList ; 26 import javax.swing.SwingUtilities ; 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 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 55 57 63 public class DiffAction extends NodeAction { 64 65 66 public DiffAction() { 67 putValue("noIconInMenu", Boolean.TRUE); } 69 70 public String 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 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 111 protected boolean asynchronous() { 112 return true; 113 } 114 115 public void performAction(Node[] nodes) { 116 ArrayList <FileObject> fos = new ArrayList <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 133 public static void performAction(final FileObject fo1, final FileObject fo2) { 134 performAction(fo1, fo2, null); 135 } 136 141 static void performAction(FileObject fo1, FileObject fo2, FileObject type) { 142 Diff diff = Diff.getDefault(); 145 if (diff == null) { 147 DialogDisplayer.getDefault().notify( 148 new NotifyDescriptor.Message(NbBundle.getMessage(DiffAction.class, 149 "MSG_NoDiffVisualizer"))); 150 return ; 151 } 152 Component tp; 153 Reader r1 = null; 154 Reader 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 mimeType; 165 if (type != null) { 166 mimeType = type.getMIMEType(); 167 } else { 168 mimeType = fo1.getMIMEType(); 169 } 170 171 final Thread victim = Thread.currentThread(); 172 Cancellable killer = new Cancellable() { 173 public boolean cancel() { 174 victim.interrupt(); 175 return true; 176 } 177 }; 178 String 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 ioex) { 190 ErrorManager.getDefault().notify(ioex); 191 return ; 192 } finally { 193 try { 194 if (r1 != null) r1.close(); 195 } catch (IOException ioex) {} 196 try { 197 if (r2 != null) r2.close(); 198 } catch (IOException ioex) {} 199 } 200 if (tp != null) { 202 final Component ftp = tp; 203 SwingUtilities.invokeLater(new Runnable () { 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 |