KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > desktop > internal > impl > WinLaunchService


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.desktop.internal.impl;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import org.jdesktop.jdic.desktop.internal.LaunchFailedException;
27 import org.jdesktop.jdic.desktop.internal.LaunchService;
28
29
30 /**
31  * Concrete implementation of the LaunchService interface for Win.
32  *
33  * @see LaunchService
34  * @see GnomeLaunchService
35  */

36 public class WinLaunchService implements LaunchService {
37     // The shortcut file suffix on Windows.
38
private static final String JavaDoc LINK_FILE_SUFFIX = ".lnk";
39     
40     /**
41      * Checks if the given file is a shortcut.
42      */

43     private boolean isLinkFile(File JavaDoc file) {
44         boolean result;
45
46         return ((file.getPath()).toLowerCase() ).endsWith(LINK_FILE_SUFFIX);
47     }
48     
49     /**
50      * Resolve the link file if the given file is a link file or symbol file.
51      *
52      * @param inputFile the given file to be resolved.
53      * @return the resolved file.
54      */

55     public File JavaDoc resolveLinkFile(File JavaDoc inputFile) {
56         // Get the absolute file path if it's in relative path.
57
File JavaDoc resolvedFile = inputFile;
58         
59         try {
60             resolvedFile = inputFile.getCanonicalFile();
61         } catch (IOException JavaDoc e) {
62         }
63         
64         if (isLinkFile(resolvedFile)) {
65             String JavaDoc fileStr = resolvedFile.toString();
66             String JavaDoc targetFileStr = WinAPIWrapper.WinResolveLinkFile(fileStr);
67             if (targetFileStr != null) {
68                 resolvedFile = new File JavaDoc(targetFileStr);
69             }
70         }
71             
72         return resolvedFile;
73     }
74        
75     /**
76      * Invokes the system default file handler application to open the given file.
77      *
78      * @param file the given file to be opened.
79      * @throws LaunchFailedException if the given file has no associated application, or
80      * the associated application fails to be launched.
81      */

82     public void open(File JavaDoc 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         //First check if we could find command for verb opennew
90
String JavaDoc appCommand = WinUtility.getVerbCommand(file, DesktopConstants.VERB_OPENNEW);
91         if (appCommand != null) {
92             findOpenNew = true;
93         } else {
94             //If no opennew verb command find, then check open verb command
95
appCommand = WinUtility.getVerbCommand(file, DesktopConstants.VERB_OPEN);
96         }
97         if (appCommand != null) {
98             boolean result;
99             if (findOpenNew) {
100                 //If there is opennew verb command, use this one
101
result = WinAPIWrapper.WinShellExecute(file.toString(), DesktopConstants.VERB_OPENNEW);
102             } else {
103                 //else use open verb command
104
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     /**
114      * Checks if the give file is printable.
115      *
116      * @param file The given file
117      * @return true if the file is printable
118      */

119     public boolean isEditable(File JavaDoc file) {
120         String JavaDoc verbCommand = WinUtility.getVerbCommand(file, DesktopConstants.VERB_EDIT);
121         return (verbCommand != null) ? true : false;
122     }
123
124     /**
125      * Launches the relevant application to edit the given file.
126      *
127      * @param file the given file to be edited.
128      * @throws LaunchFailedException if the given file has no associated editor, or
129      * the associated editor fails to be launched.
130      */

131     public void edit(File JavaDoc 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     /**
143      * Checks if the given file is printable.
144      *
145      * @param file The given file.
146      * @return true if the given file is printable.
147      */

148     public boolean isPrintable(File JavaDoc file) {
149         String JavaDoc verbCommand = WinUtility.getVerbCommand(file, DesktopConstants.VERB_PRINT);
150         return (verbCommand != null) ? true : false;
151     }
152
153     /**
154      * Launches the relevant application to print the given file.
155      *
156      * @param file the given file to be printed.
157      * @throws LaunchFailedException if the given file has no associated application, or
158      * the associated application fails to be launched.
159      */

160     public void print(File JavaDoc 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