1 package org.columba.core.desktop; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 23 import javax.swing.JOptionPane ; 24 25 import org.columba.api.desktop.IDesktop; 26 import org.columba.core.base.OSInfo; 27 import org.columba.core.base.TextUtil; 28 import org.columba.core.gui.frame.FrameManager; 29 import org.columba.core.logging.Logging; 30 import org.columba.core.resourceloader.GlobalResourceLoader; 31 import org.jdesktop.jdic.desktop.Desktop; 32 import org.jdesktop.jdic.desktop.DesktopException; 33 import org.jdesktop.jdic.filetypes.Action; 34 import org.jdesktop.jdic.filetypes.Association; 35 import org.jdesktop.jdic.filetypes.AssociationService; 36 37 public class JDICDesktop implements IDesktop { 38 39 private AssociationService associationService; 40 41 public JDICDesktop() { 42 associationService = new AssociationService(); 43 } 44 45 public String getMimeType(File file) { 46 String mimetype = "application/octet-stream"; 47 48 try { 49 Association a = associationService.getAssociationByContent(file 50 .toURL()); 51 if (a != null) { 52 return a.getMimeType(); 53 } 54 } catch (MalformedURLException e) { 55 } 56 57 return mimetype; 58 } 59 60 public String getMimeType(String ext) { 61 String mimetype = "application/octet-stream"; 62 63 Association a = associationService.getFileExtensionAssociation(ext); 64 if (a != null) { 65 return a.getMimeType(); 66 } 67 68 return mimetype; 69 } 70 71 public boolean supportsOpen() { 72 return true; 73 } 74 75 public boolean open(File file) { 76 try { 77 Desktop.open(file); 78 } catch (DesktopException e) { 79 JOptionPane.showMessageDialog(FrameManager.getInstance() 80 .getActiveFrame(), GlobalResourceLoader.getString( 81 "org.columba.core.i18n.dialog", "error", "no_viewer"), 82 "Error", JOptionPane.ERROR_MESSAGE); 83 84 return false; 85 } 86 87 return true; 88 } 89 90 public boolean openAndWait(File file) { 91 Association association = new AssociationService() 92 .getMimeTypeAssociation("text/plain"); 93 Action action = association.getActionByVerb("open"); 94 95 String command = action.getCommand(); 96 97 command = TextUtil.replaceAll(command, "%1", file.getPath()); 99 100 if (command.indexOf(file.getPath()) == -1) { 102 command = command + " " + file.getPath(); 103 } 104 105 if ( OSInfo.isWin32Platform() ) { 108 command = "cmd.exe /C "+"\""+command+"\""; 109 } 110 111 Process child; 112 try { 113 child = Runtime.getRuntime().exec(command); 114 } catch (IOException e) { 115 if (Logging.DEBUG) 116 e.printStackTrace(); 117 118 return false; 119 } 120 121 if (child == null) { 122 return false; 123 } 124 125 try { 126 child.waitFor(); 128 129 } catch (InterruptedException ex) { 130 return false; 131 } 132 133 return true; 134 } 135 136 public boolean supportsBrowse() { 137 return true; 138 } 139 140 public void browse(URL url) { 141 try { 142 Desktop.browse(url); 143 } catch (DesktopException e) { 144 JOptionPane.showMessageDialog(FrameManager.getInstance() 145 .getActiveFrame(), GlobalResourceLoader.getString( 146 "org.columba.core.i18n.dialog", "error", "no_browser"), 147 "Error", JOptionPane.ERROR_MESSAGE); 148 } 149 } 150 151 } 152 | Popular Tags |