KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > desktop > JDICDesktop


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.desktop;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import javax.swing.JOptionPane JavaDoc;
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 JavaDoc getMimeType(File JavaDoc file) {
46         String JavaDoc 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 JavaDoc e) {
55         }
56
57         return mimetype;
58     }
59
60     public String JavaDoc getMimeType(String JavaDoc ext) {
61         String JavaDoc 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 JavaDoc 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 JavaDoc file) {
91         Association association = new AssociationService()
92                 .getMimeTypeAssociation("text/plain");
93         Action action = association.getActionByVerb("open");
94
95         String JavaDoc command = action.getCommand();
96
97         // replace "%1" parameter with file argument ...
98
command = TextUtil.replaceAll(command, "%1", file.getPath());
99
100         // ... or, add the file in case there was no "%1" used
101
if (command.indexOf(file.getPath()) == -1) {
102             command = command + " " + file.getPath();
103         }
104
105         // if win32 platform, prepend cmd.exe
106
// necessary for system environment variables usage
107
if ( OSInfo.isWin32Platform() ) {
108             command = "cmd.exe /C "+"\""+command+"\"";
109         }
110
111         Process JavaDoc child;
112         try {
113             child = Runtime.getRuntime().exec(command);
114         } catch (IOException JavaDoc 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             // Wait for external editor to quit
127
child.waitFor();
128
129         } catch (InterruptedException JavaDoc 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 JavaDoc 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