1 20 21 package org.jdesktop.jdic.desktop.internal.impl; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 26 import org.jdesktop.jdic.desktop.internal.LaunchFailedException; 27 import org.jdesktop.jdic.desktop.internal.LaunchService; 28 29 30 36 public class WinLaunchService implements LaunchService { 37 private static final String LINK_FILE_SUFFIX = ".lnk"; 39 40 43 private boolean isLinkFile(File file) { 44 boolean result; 45 46 return ((file.getPath()).toLowerCase() ).endsWith(LINK_FILE_SUFFIX); 47 } 48 49 55 public File resolveLinkFile(File inputFile) { 56 File resolvedFile = inputFile; 58 59 try { 60 resolvedFile = inputFile.getCanonicalFile(); 61 } catch (IOException e) { 62 } 63 64 if (isLinkFile(resolvedFile)) { 65 String fileStr = resolvedFile.toString(); 66 String targetFileStr = WinAPIWrapper.WinResolveLinkFile(fileStr); 67 if (targetFileStr != null) { 68 resolvedFile = new File (targetFileStr); 69 } 70 } 71 72 return resolvedFile; 73 } 74 75 82 public void open(File file) throws LaunchFailedException { 83 if(file.isDirectory()) { 84 if( !WinAPIWrapper.WinShellExecute(file.toString(), DesktopConstants.VERB_OPEN)) 85 throw new LaunchFailedException("Failed to open the given directory."); 86 return; 87 } 88 boolean findOpenNew = false; 89 String appCommand = WinUtility.getVerbCommand(file, DesktopConstants.VERB_OPENNEW); 91 if (appCommand != null) { 92 findOpenNew = true; 93 } else { 94 appCommand = WinUtility.getVerbCommand(file, DesktopConstants.VERB_OPEN); 96 } 97 if (appCommand != null) { 98 boolean result; 99 if (findOpenNew) { 100 result = WinAPIWrapper.WinShellExecute(file.toString(), DesktopConstants.VERB_OPENNEW); 102 } else { 103 result = WinAPIWrapper.WinShellExecute(file.toString(), DesktopConstants.VERB_OPEN); 105 } 106 if (!result) { 107 throw new LaunchFailedException("Failed to launch the associationed application"); 108 } 109 } else { 110 throw new LaunchFailedException("No application associated with the specified file"); 111 } 112 } 113 119 public boolean isEditable(File file) { 120 String verbCommand = WinUtility.getVerbCommand(file, DesktopConstants.VERB_EDIT); 121 return (verbCommand != null) ? true : false; 122 } 123 124 131 public void edit(File file) throws LaunchFailedException { 132 if (isEditable(file)){ 133 boolean result = WinAPIWrapper.WinShellExecute(file.toString(), DesktopConstants.VERB_EDIT); 134 if (!result) { 135 throw new LaunchFailedException("Failed to edit the file."); 136 } 137 } else { 138 throw new LaunchFailedException("No application associated with the specified file"); 139 } 140 } 141 142 148 public boolean isPrintable(File file) { 149 String verbCommand = WinUtility.getVerbCommand(file, DesktopConstants.VERB_PRINT); 150 return (verbCommand != null) ? true : false; 151 } 152 153 160 public void print(File file) throws LaunchFailedException { 161 if (isPrintable(file)){ 162 boolean result = WinAPIWrapper.WinShellExecute(file.toString(), DesktopConstants.VERB_PRINT); 163 if (!result) { 164 throw new LaunchFailedException("Failed to print the file."); 165 } 166 } else { 167 throw new LaunchFailedException("No application associated with the specified file"); 168 } 169 } 170 } | Popular Tags |