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

Last change on this file since 6630 was 6630, checked in by jmt12, 20 years ago

File associations are now stored in the user specific config. Also, to make everyones life a little easier, the config has a version number which if no match is found, causes the current config to be backed-up, and a new default config created.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 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.msm.MSMUtils;
35import org.greenstone.gatherer.util.StaticStrings;
36import org.greenstone.gatherer.util.Utility;
37import org.greenstone.gatherer.util.WinRegistry;
38import org.w3c.dom.*;
39
40public class FileAssociationManager
41 extends AbstractTableModel {
42 static final public String FILENAME_ARG = "%1";
43 static final private String ESCAPE = "\\\\"; // '\'
44 static final private String ESCAPED_ESCAPE = "\\\\\\\\"; // '\\'
45 private Element associations_element;
46 private File data_file;
47
48 public FileAssociationManager() {
49 // Retrieve the associations_element from the config
50 associations_element = Gatherer.config.getFileAssociations();
51 // Initialize the associations. This involves looking through all current associations searching for those with a command of "".
52 if(associations_element != null) {
53 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
54 for(int i = 0; i < entries.getLength(); i++) {
55 Element entry = (Element) entries.item(i);
56 String extension = entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
57 String command = MSMUtils.getValue(entry);
58 // If we encounter a command of ""...
59 if(command.length() == 0) {
60 // and if we are on windows, we try to automatically set this command.
61 if(Utility.isWindows()) {
62 // Create a dummy filename with the appropriate extension
63 String dummy_file = "dummy." + extension;
64 command = WinRegistry.openCommand(dummy_file);
65 // If this succeeded add the association.
66 if(command != null) {
67 // Remember to replace the dummy filename with %1
68 command = command.replaceAll(dummy_file, FILENAME_ARG);
69 // Replace the text in this node.
70 MSMUtils.setValue(entry, command);
71 }
72 dummy_file = null;
73 }
74 // and if we are on windows, we default to the open program
75 if(Utility.isMac()) {
76 MSMUtils.setValue(entry, StaticStrings.MAC_OPEN_COMMAND);
77 }
78 }
79 command = null;
80 extension = null;
81 entry = null;
82 }
83 entries = null;
84 }
85 else {
86 Gatherer.println("Didn't parse anything. About to crash.");
87 }
88 }
89
90 public void edit() {
91 FileAssociationDialog dialog = new FileAssociationDialog(this);
92 dialog.display(null);
93 dialog = null;
94 }
95
96 public String getBrowserCommand(String url) {
97 Gatherer.println("Get browser command: " + url);
98 // First off we try to retrieve one from the configuration
99 String command = Gatherer.config.getPreviewCommand();
100 // If that worked, substitute in the url and return
101 if(command != null && command.length() > 0) {
102 command = command.replaceAll("%1", url);
103 Gatherer.println("Result = " + command);
104 return command;
105 }
106 // Failing that we have to prompt the user for what they want to do, but we do our best to provide a sensible default (if available).
107 // For windows we can have a pretty good guess
108 if(Utility.isWindows()) {
109 // we use cmd and start
110 if (Utility.isWindows9x()) {
111 command = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
112 } else {
113 command = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
114 }
115 }
116
117 // Now prompt the user
118 FileAssociationDialog dialog = new FileAssociationDialog(this);
119 command = dialog.display(true, command);
120 dialog.dispose();
121 dialog = null;
122
123 // Store the result if any
124 if(command != null) {
125 Gatherer.config.setPreviewCommand(command);
126 }
127
128 // for now uder linux get the fileassoc thing. but eventually want a separate thing.
129 // pretend we are trying to open an html file
130 // Mmmoooo. - jmt12
131
132 // if we haven't got a command by now, we'll never get one
133 if (command == null) {
134 Gatherer.println("Result = " + command);
135 return null;
136 }
137 // Replace %1 with the url in quotes
138 command = command.replaceAll(FILENAME_ARG, url);
139 Gatherer.println("Result = " + command);
140 return command;
141 }
142
143 public int getColumnCount() {
144 return 2;
145 }
146
147 public String getColumnName(int column) {
148 String name;
149 switch(column) {
150 case 0:
151 name = Dictionary.get("FileAssociationDialog.Table.Extension");
152 break;
153 default:
154 name = Dictionary.get("FileAssociationDialog.Table.Command");
155 }
156 return name;
157 }
158
159 public String getCommand(File file) {
160 String command = null;
161 if(file.isFile()) {
162 // Determine extension
163 String filename = file.getAbsolutePath();
164 String extension = filename.substring(filename.lastIndexOf(".") + 1);
165 // Try to retrieve a value from cache
166 Element entry = getCommand(extension);
167 if(entry != null) {
168 ///ystem.err.println("Retrieved Value From Cache");
169 command = MSMUtils.getValue(entry);
170 }
171 if(command == null || command.length() == 0) {
172 ///ystem.err.println("No Existing Command");
173 // If command is null, and we are on windows try searching the registry.
174 if(Utility.isWindows()) {
175 ///ystem.err.println("Is Windows");
176 //command = WinRegistry.openCommand(filename);
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 ///ystem.err.println("Is Mac");
189 command = StaticStrings.MAC_OPEN_COMMAND;
190 }
191
192 // Otherwise display the dialog and ask the user to enter launching command.
193 if(command == null || command.length() == 0) {
194 ///ystem.err.println("Show Dialog");
195 // Show the dialog which forces a user to select the launch command for a certain file.
196 FileAssociationDialog dialog = new FileAssociationDialog(this);
197 command = dialog.display(extension);
198 dialog = null;
199 }
200 // Hopefully by now we have a command, or else we're never going to get one. Add the association.
201 if(command != null) {
202 // If no previous entry existed create one.
203 if(entry == null) {
204 entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
205 entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
206 associations_element.appendChild(entry);
207 }
208 // 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
209 //MSMUtils.setValue(entry, command.replaceAll(filename, FILENAME_ARG));
210 MSMUtils.setValue(entry, command);
211 }
212 }
213 if(command != null) {
214 // We have to fix filename under windows to escape the backslashes.
215 filename = filename.replaceAll(ESCAPE, ESCAPED_ESCAPE);
216 // Replace %1 with the appropriate filename
217 command = command.replaceAll(FILENAME_ARG, filename);
218 }
219
220 entry = null;
221 extension = null;
222 filename = null;
223 }
224 return command;
225 }
226
227 public Element getCommand(String target_extension) {
228 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
229 for(int i = 0; i < entries.getLength(); i++) {
230 Element entry = (Element) entries.item(i);
231 String extension = entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
232 if(extension.equalsIgnoreCase(target_extension)) {
233 entries = null;
234 extension = null;
235 return entry;
236 }
237 }
238 entries = null;
239 return null;
240 }
241
242 public String getCommandString(String target_extension) {
243 Element entry = getCommand(target_extension);
244 if(entry != null) {
245 return MSMUtils.getValue(entry);
246 }
247 else {
248 return "";
249 }
250 }
251
252 public String getExtension(int index) {
253 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
254 if(0 <= index && index < entries.getLength()) {
255 Element entry = (Element) entries.item(index);
256 return entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
257 }
258 return "";
259 }
260
261 public int getRowCount() {
262 return size();
263 }
264
265 public Object getValueAt(int row, int column) {
266 String extension = getExtension(row);
267 switch(column) {
268 case 0:
269 return extension;
270 default:
271 return getCommandString(extension);
272 }
273 }
274
275 public void save() {
276 }
277
278 public void setCommand(String extension, String command) {
279 Gatherer.println("Set Launch: " + extension + " with " + command);
280 // Retrieve any existing entry for this extension
281 Element entry = getCommand(extension);
282 // If no previous entry existed create one.
283 if(entry == null && command != null) {
284 entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
285 entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
286 associations_element.appendChild(entry);
287 }
288
289 if(command != null) {
290 // Replace the text in this node. If the user has used filename instead of %1 then too bad.
291 MSMUtils.setValue(entry, command);
292 }
293 else {
294 // Remove the entry
295 associations_element.removeChild(entry);
296 }
297 entry = null;
298 fireTableDataChanged(); // Can't be anymore efficient as DOM does not gareuntee ordering of new child nodes is consistant
299 }
300
301 public int size() {
302 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
303 return entries.getLength();
304 }
305}
Note: See TracBrowser for help on using the repository browser.