source: trunk/gli/src/org/greenstone/gatherer/file/FileAssociationManager.java@ 7540

Last change on this file since 7540 was 7540, checked in by mdewsnip, 20 years ago

Some minor tidy ups with removing dead code.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.file;
28
29import java.io.*;
30import javax.swing.table.*;
31import org.greenstone.gatherer.Dictionary;
32import org.greenstone.gatherer.Gatherer;
33import org.greenstone.gatherer.gui.FileAssociationDialog;
34import org.greenstone.gatherer.gui.PreviewCommandDialog;
35import org.greenstone.gatherer.msm.MSMUtils;
36import org.greenstone.gatherer.util.ExternalProgram;
37import org.greenstone.gatherer.util.StaticStrings;
38import org.greenstone.gatherer.util.Utility;
39import org.w3c.dom.*;
40
41public class FileAssociationManager
42 extends AbstractTableModel {
43 static final public String FILENAME_ARG = "%1";
44 static final private String ESCAPE = "\\\\"; // '\'
45 static final private String ESCAPED_ESCAPE = "\\\\\\\\"; // '\\'
46 static final private String SPACE = " ";
47 static final private String ESCAPED_SPACE = "\\\\ ";
48 private Element associations_element;
49 private File data_file;
50
51 public FileAssociationManager() {
52 // Retrieve the associations_element from the config
53 associations_element = Gatherer.config.getFileAssociations();
54 // Initialize the associations. This involves looking through all current associations searching for those with a command of "".
55 if(associations_element != null) {
56 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
57 for(int i = 0; i < entries.getLength(); i++) {
58 Element entry = (Element) entries.item(i);
59 String command = MSMUtils.getValue(entry);
60 // If we encounter a command of ""...
61 if(command.length() == 0) {
62 // if we are on windows, we default to the start command
63 if(Utility.isWindows()) {
64 if (Utility.isWindows9x()) {
65 MSMUtils.setValue(entry, StaticStrings.WIN_9X_OPEN_COMMAND);
66 } else {
67 MSMUtils.setValue(entry, StaticStrings.WIN_OPEN_COMMAND);
68 }
69 }
70 // and if we are on mac, we default to the open program
71 else if(Utility.isMac()) {
72 MSMUtils.setValue(entry, StaticStrings.MAC_OPEN_COMMAND);
73 }
74 }
75 command = null;
76 entry = null;
77 }
78 entries = null;
79 }
80 else {
81 Gatherer.println("Didn't parse anything. About to crash.");
82 }
83 }
84
85 public void edit() {
86 FileAssociationDialog dialog = new FileAssociationDialog(this);
87 dialog.display(null);
88 dialog = null;
89 }
90
91 public String getBrowserCommand(String url) {
92 Gatherer.println("Get browser command: " + url);
93 // First off we try to retrieve one from the configuration
94 String command = Gatherer.config.getPreviewCommand();
95 // If that worked, substitute in the url and return
96 if(command != null && command.length() > 0) {
97 command = command.replaceAll("%1", url);
98 Gatherer.println("Result = " + command);
99 return command;
100 }
101 command = null;
102 // Failing that we have a guess at a sensible default
103 if(Utility.isWindows()) {
104 // we use cmd and start
105 if (Utility.isWindows9x()) {
106 command = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
107 } else {
108 command = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
109 }
110 } else if (Utility.isMac()) {
111 command = StaticStrings.MAC_OPEN_COMMAND; // "open %1"
112 } else {
113 // we try to look for a browser
114 String [] browsers = new String [] {"mozilla", "netscape"};
115 for (int i=0; i<browsers.length; i++) {
116 if (isAvailable(browsers[i])) {
117 command = browsers[i]+ " %1";
118 break;
119 }
120 }
121 }
122
123 // if we still haven't found something, prompt the user
124 if (command == null) {
125 PreviewCommandDialog dialog = new PreviewCommandDialog();
126 command = dialog.display();
127 dialog.dispose();
128 dialog = null;
129 }
130
131 // Store the result if any
132 if(command != null && !command.equals("")) {
133 Gatherer.config.setPreviewCommand(command);
134 command = command.replaceAll(FILENAME_ARG, url);
135 Gatherer.println("Result = " + command);
136 return command;
137 }
138 // if we haven't got a command by now, we'll never get one
139 Gatherer.println("Result = null");
140 return null;
141
142 }
143
144 public int getColumnCount() {
145 return 2;
146 }
147
148 public String getColumnName(int column) {
149 String name;
150 switch(column) {
151 case 0:
152 name = Dictionary.get("FileAssociationDialog.Table.Extension");
153 break;
154 default:
155 name = Dictionary.get("FileAssociationDialog.Table.Command");
156 }
157 return name;
158 }
159
160 public String [] getCommand(File file) {
161 String command = null;
162 String [] commands = null;
163 if(file.isFile()) {
164 // Determine extension
165 String filename = file.getAbsolutePath();
166 String extension = filename.substring(filename.lastIndexOf(".") + 1);
167 // Try to retrieve a value from cache
168 Element entry = getCommand(extension);
169 if(entry != null) {
170 ///ystem.err.println("Retrieved Value From Cache");
171 command = MSMUtils.getValue(entry);
172 }
173 if(command == null || command.length() == 0) {
174 ///ystem.err.println("No Existing Command");
175 // If command is null, and we are on windows try searching the registry.
176 if(Utility.isWindows()) {
177 //try the start command
178 if (Utility.isWindows9x()) {
179 command = StaticStrings.WIN_9X_OPEN_COMMAND;
180 } else {
181 command = StaticStrings.WIN_OPEN_COMMAND;
182 }
183
184 }
185
186 // If we are on a mac, default to using the open program
187 else if(Utility.isMac()) {
188 command = StaticStrings.MAC_OPEN_COMMAND;
189 }
190
191 // Otherwise display the dialog and ask the user to enter launching command.
192 if(command == null || command.length() == 0) {
193 ///ystem.err.println("Show Dialog");
194 // Show the dialog which forces a user to select the launch command for a certain file.
195 FileAssociationDialog dialog = new FileAssociationDialog(this);
196 command = dialog.display(extension);
197 dialog = null;
198 }
199 // Hopefully by now we have a command, or else we're never going to get one. Add the association.
200 if(command != null) {
201 // If no previous entry existed create one.
202 if(entry == null) {
203 entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
204 entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
205 associations_element.appendChild(entry);
206 }
207 // Replace the text in this node. Remember to replace the dummy filename with %1 - I dont think the filename will ever be in the comand now
208 //MSMUtils.setValue(entry, command.replaceAll(filename, FILENAME_ARG));
209 MSMUtils.setValue(entry, command);
210 }
211 }
212 if(command != null) {
213 // We have to fix filename under windows to escape the backslashes
214 filename = filename.replaceAll(ESCAPE, ESCAPED_ESCAPE);
215 // Replace %1 with the appropriate filename
216 command = command.replaceAll(FILENAME_ARG, filename);
217
218 // Make the command into a string [] in case the filename has spaces in it
219 commands = command.split(" ");
220 }
221
222 entry = null;
223 extension = null;
224 filename = null;
225 }
226 return commands;
227 }
228
229 public Element getCommand(String target_extension) {
230 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
231 for(int i = 0; i < entries.getLength(); i++) {
232 Element entry = (Element) entries.item(i);
233 String extension = entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
234 if(extension.equalsIgnoreCase(target_extension)) {
235 entries = null;
236 extension = null;
237 return entry;
238 }
239 }
240 entries = null;
241 return null;
242 }
243
244 public String getCommandString(String target_extension) {
245 Element entry = getCommand(target_extension);
246 if(entry != null) {
247 return MSMUtils.getValue(entry);
248 }
249 else {
250 return "";
251 }
252 }
253
254 public String getExtension(int index) {
255 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
256 if(0 <= index && index < entries.getLength()) {
257 Element entry = (Element) entries.item(index);
258 return entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
259 }
260 return "";
261 }
262
263 public int getRowCount() {
264 return size();
265 }
266
267 public Object getValueAt(int row, int column) {
268 String extension = getExtension(row);
269 switch(column) {
270 case 0:
271 return extension;
272 default:
273 return getCommandString(extension);
274 }
275 }
276
277 public void save() {
278 }
279
280 public void setCommand(String extension, String command) {
281 Gatherer.println("Set Launch: " + extension + " with " + command);
282 // Retrieve any existing entry for this extension
283 Element entry = getCommand(extension);
284 // If no previous entry existed create one.
285 if(entry == null && command != null) {
286 entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
287 entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
288 associations_element.appendChild(entry);
289 }
290
291 if(command != null) {
292 // Replace the text in this node. If the user has used filename instead of %1 then too bad.
293 MSMUtils.setValue(entry, command);
294 }
295 else {
296 // Remove the entry
297 associations_element.removeChild(entry);
298 }
299 entry = null;
300 fireTableDataChanged(); // Can't be anymore efficient as DOM does not gareuntee ordering of new child nodes is consistant
301 }
302
303 public int size() {
304 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
305 return entries.getLength();
306 }
307
308
309 protected boolean isAvailable(String program) {
310 try {
311 ExternalProgram e = new ExternalProgram("which", program);
312 e.exitProgram();
313 String out = e.getLineOfProgramOutput();
314 if (out == null) {
315 return false;
316 }
317 return true;
318 } catch (Exception exc) {
319 return false;
320 }
321 }
322}
Note: See TracBrowser for help on using the repository browser.