001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.desktop;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022:
023: import javax.swing.JOptionPane;
024:
025: import org.columba.api.desktop.IDesktop;
026: import org.columba.core.base.OSInfo;
027: import org.columba.core.base.TextUtil;
028: import org.columba.core.gui.frame.FrameManager;
029: import org.columba.core.logging.Logging;
030: import org.columba.core.resourceloader.GlobalResourceLoader;
031: import org.jdesktop.jdic.desktop.Desktop;
032: import org.jdesktop.jdic.desktop.DesktopException;
033: import org.jdesktop.jdic.filetypes.Action;
034: import org.jdesktop.jdic.filetypes.Association;
035: import org.jdesktop.jdic.filetypes.AssociationService;
036:
037: public class JDICDesktop implements IDesktop {
038:
039: private AssociationService associationService;
040:
041: public JDICDesktop() {
042: associationService = new AssociationService();
043: }
044:
045: public String getMimeType(File file) {
046: String mimetype = "application/octet-stream";
047:
048: try {
049: Association a = associationService
050: .getAssociationByContent(file.toURL());
051: if (a != null) {
052: return a.getMimeType();
053: }
054: } catch (MalformedURLException e) {
055: }
056:
057: return mimetype;
058: }
059:
060: public String getMimeType(String ext) {
061: String mimetype = "application/octet-stream";
062:
063: Association a = associationService
064: .getFileExtensionAssociation(ext);
065: if (a != null) {
066: return a.getMimeType();
067: }
068:
069: return mimetype;
070: }
071:
072: public boolean supportsOpen() {
073: return true;
074: }
075:
076: public boolean open(File file) {
077: try {
078: Desktop.open(file);
079: } catch (DesktopException e) {
080: JOptionPane.showMessageDialog(FrameManager.getInstance()
081: .getActiveFrame(), GlobalResourceLoader.getString(
082: "org.columba.core.i18n.dialog", "error",
083: "no_viewer"), "Error", JOptionPane.ERROR_MESSAGE);
084:
085: return false;
086: }
087:
088: return true;
089: }
090:
091: public boolean openAndWait(File file) {
092: Association association = new AssociationService()
093: .getMimeTypeAssociation("text/plain");
094: Action action = association.getActionByVerb("open");
095:
096: String command = action.getCommand();
097:
098: // replace "%1" parameter with file argument ...
099: command = TextUtil.replaceAll(command, "%1", file.getPath());
100:
101: // ... or, add the file in case there was no "%1" used
102: if (command.indexOf(file.getPath()) == -1) {
103: command = command + " " + file.getPath();
104: }
105:
106: // if win32 platform, prepend cmd.exe
107: // necessary for system environment variables usage
108: if (OSInfo.isWin32Platform()) {
109: command = "cmd.exe /C " + "\"" + command + "\"";
110: }
111:
112: Process child;
113: try {
114: child = Runtime.getRuntime().exec(command);
115: } catch (IOException e) {
116: if (Logging.DEBUG)
117: e.printStackTrace();
118:
119: return false;
120: }
121:
122: if (child == null) {
123: return false;
124: }
125:
126: try {
127: // Wait for external editor to quit
128: child.waitFor();
129:
130: } catch (InterruptedException ex) {
131: return false;
132: }
133:
134: return true;
135: }
136:
137: public boolean supportsBrowse() {
138: return true;
139: }
140:
141: public void browse(URL url) {
142: try {
143: Desktop.browse(url);
144: } catch (DesktopException e) {
145: JOptionPane.showMessageDialog(FrameManager.getInstance()
146: .getActiveFrame(), GlobalResourceLoader.getString(
147: "org.columba.core.i18n.dialog", "error",
148: "no_browser"), "Error", JOptionPane.ERROR_MESSAGE);
149: }
150: }
151:
152: }
|