KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > filetypes > internal > WinAppAssociationWriter


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.filetypes.internal;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.jdesktop.jdic.filetypes.Association;
26 import org.jdesktop.jdic.filetypes.RegisterFailedException;
27
28
29 /**
30  * Concrete implementation of the AppAssociationWriter class for Windows platform.
31  */

32 public class WinAppAssociationWriter implements AppAssociationWriter {
33
34     /**
35      * Inline class for restoreing association registration/unregistration failure.
36      */

37     protected class BackupAssociation {
38         // Mime type retrieved from the specified association object.
39
private String JavaDoc curMimeType;
40         // File extension retrieved from the specified association object.
41
private String JavaDoc curFileExt;
42         private boolean curMimeTypeExisted;
43         private boolean curFileExtExisted;
44         private String JavaDoc backupMimeType;
45         private String JavaDoc backupClassID;
46         private String JavaDoc backupFileExt;
47
48         /**
49          * Suppresses default constructor for noninstantiability.
50          */

51         private BackupAssociation() {}
52
53         /**
54          * Constructor for class BackupAssociation
55          *
56          * @param assoc the given Association (not null).
57          * @param regLevel the given registeration level.
58          */

59         protected BackupAssociation(Association assoc, int regLevel) {
60             curMimeType = assoc.getMimeType();
61
62             Iterator JavaDoc iter = null;
63             List JavaDoc temFileExtList = assoc.getFileExtList();
64             if (temFileExtList != null) {
65                 iter = temFileExtList.iterator();
66             }
67             if (iter != null) {
68                 if (iter.hasNext()) {
69                     curFileExt = (String JavaDoc) iter.next();
70                 }
71             }
72             
73             if (curMimeType != null) {
74                 curMimeTypeExisted = WinRegistryUtil.isMimeTypeExist(curMimeType, regLevel);
75             } else {
76                 curMimeTypeExisted = false;
77             }
78             
79             if (curFileExt != null) {
80                 curFileExtExisted = WinRegistryUtil.isFileExtExist(curFileExt, regLevel);
81             } else {
82                 curFileExtExisted = false;
83             }
84     
85             if (curMimeTypeExisted) {
86                 backupFileExt = WinRegistryUtil.getFileExtByMimeType(curMimeType, regLevel);
87             }
88     
89             if (curFileExtExisted) {
90                 backupClassID = WinRegistryUtil.getClassIDByFileExt(curFileExt, regLevel);
91                 backupMimeType = WinRegistryUtil.getMimeTypeByFileExt(curFileExt, regLevel);
92             }
93         }
94
95         /**
96          * Retrieve mime type
97          *
98          * @return mime type
99          */

100         protected String JavaDoc getCurMimeType() {
101             return curMimeType;
102         }
103         
104         /**
105          * Retrieves file extension
106          *
107          * @return file extension
108          */

109         protected String JavaDoc getCurFileExt() {
110             return curFileExt;
111         }
112     
113         /**
114          * Return true if the mime type existed in the Windows registry table
115          *
116          * @return true if the mime type existed
117          */

118         protected boolean getCurMimeTypeExisted() {
119             return curMimeTypeExisted;
120         }
121
122         /**
123          * Returns true if the file extension existed in the Windows registry table
124          *
125          * @return true if the file extension existed
126          */

127         protected boolean getCurFileExtExisted() {
128             return curFileExtExisted;
129         }
130             
131         /**
132          * Returns the backup mime type information
133          *
134          * @return backup mime type information
135          */

136         protected String JavaDoc getBackupMimeType() {
137             return backupMimeType;
138         }
139
140         /**
141          * Returns the backup class ID
142          *
143          * @return backup class ID information
144          */

145         protected String JavaDoc getBackupClassID() {
146             return backupClassID;
147         }
148
149         /**
150          * Returns backup file extension
151          *
152          * @return backup file extensio
153          */

154         protected String JavaDoc getBackupFileExt() {
155             return backupFileExt;
156         }
157     }
158
159     /**
160      * Restores association registration failure.
161      *
162      * @param backupAssoc backup association (not null)
163      * @param regLevel given regLevel
164      */

165     private void restoreAssociationRegistration(BackupAssociation backupAssoc, int regLevel) {
166         try {
167             String JavaDoc curMimeType = backupAssoc.getCurMimeType();
168             String JavaDoc curFileExt = backupAssoc.getCurFileExt();
169             
170             
171             if (!backupAssoc.getCurMimeTypeExisted()) {
172                 if (curMimeType != null) {
173                     WinRegistryUtil.removeMimeType(curMimeType, regLevel);
174                 }
175             } else {
176                 String JavaDoc backupFileExt = backupAssoc.getBackupFileExt();
177                 if (backupFileExt != null) {
178                     WinRegistryUtil.setFileExtByMimeType(backupFileExt,
179                         curMimeType, regLevel);
180                 }
181             }
182
183             if (!backupAssoc.getCurFileExtExisted()) {
184                 if (curFileExt != null) {
185                     WinRegistryUtil.removeFileExt(curFileExt, regLevel);
186                 }
187             } else {
188                 String JavaDoc backupMimeType = backupAssoc.getBackupMimeType();
189                 if (backupMimeType != null) {
190                     WinRegistryUtil.setMimeTypeByFileExt(backupMimeType,
191                         curFileExt, regLevel);
192                 }
193                 String JavaDoc backupClassID = backupAssoc.getBackupClassID();
194                 if (backupClassID != null) {
195                     WinRegistryUtil.setClassIDByFileExt(curFileExt,
196                         backupClassID, regLevel);
197                 }
198             }
199         } catch (RegisterFailedException e) {
200         }
201     }
202
203     /**
204      * Restores association unregistration failure.
205      *
206      * @param backupAssoc backup association
207      * @param regLevel given regLevel
208      */

209     private void restoreAssociationUnregistration(BackupAssociation backupAssoc, int regLevel) {
210         try {
211             String JavaDoc curMimeType = backupAssoc.getCurMimeType();
212             String JavaDoc curFileExt = backupAssoc.getCurFileExt();
213             
214             if (backupAssoc.getCurMimeTypeExisted()) {
215                 WinRegistryUtil.addMimeType(curMimeType, regLevel);
216                 String JavaDoc backupFileExt = backupAssoc.getBackupFileExt();
217                 if (backupFileExt != null) {
218                     WinRegistryUtil.setFileExtByMimeType(backupFileExt,
219                         curMimeType, regLevel);
220                 }
221             }
222         
223             if (backupAssoc.getCurFileExtExisted()) {
224                 WinRegistryUtil.addFileExt(curFileExt, regLevel);
225                 String JavaDoc backupMimeType = backupAssoc.getBackupMimeType();
226                 String JavaDoc backupClassID = backupAssoc.getBackupClassID();
227                 
228                 if (backupMimeType != null) {
229                     WinRegistryUtil.setMimeTypeByFileExt(backupMimeType,
230                         curFileExt, regLevel);
231                 }
232                 if (backupClassID != null) {
233                     WinRegistryUtil.setClassIDByFileExt(curFileExt,
234                         backupClassID, regLevel);
235                 }
236             }
237         } catch (RegisterFailedException e) {
238         }
239     }
240
241     /**
242      * Checks whether the given assocation is valid for registration.
243      * <PRE>
244      * 1. The file extension list and mime type can't all be null
245      * 2. If any of the fileds: description, iconFile, actionList is not null,
246      * then fileExtensionList should not be empty
247      * </PRE>
248      *
249      * @param assoc a given Association object.
250      * @throws IllegalArgumentException if the given association is not valid for registration.
251      */

252     public void checkAssociationValidForRegistration(Association assoc)
253         throws IllegalArgumentException JavaDoc {
254         boolean isActionListEmpty = true;
255         boolean isFileExtensionEmpty = true;
256         boolean isValid = false;
257             
258         // Check if actionlist is empty
259
if (assoc.getActionList() != null) {
260             isActionListEmpty = assoc.getActionList().isEmpty();
261         }
262         // Check if file extension list is empty
263
if (assoc.getFileExtList() != null) {
264             isFileExtensionEmpty = assoc.getFileExtList().isEmpty();
265         }
266     
267         if (isFileExtensionEmpty && (assoc.getMimeType() == null)) {
268             isValid = false;
269         } else if ((assoc.getDescription() != null) || (assoc.getIconFileName() != null) ||
270             (!isActionListEmpty)) {
271             isValid = !isFileExtensionEmpty;
272         } else {
273             isValid = true;
274         }
275     
276         if (!isValid) {
277             throw new IllegalArgumentException JavaDoc("The given association is invalid. It should " +
278                 "specify both the mimeType and fileExtensionList fields to perform this operation.");
279         }
280     }
281
282     /**
283      * Checks whether the given assocation is valid for unregistration.
284      * If both the mimeType and fileExtensionList field is null, throw exception.
285      *
286      * @param assoc a given Association object.
287      * @throws IllegalArgumentException if the given association is not valid for unregistration.
288      */

289     public void checkAssociationValidForUnregistration(Association assoc)
290         throws IllegalArgumentException JavaDoc {
291         boolean isFileExtListEmpty = true;
292         if (assoc.getFileExtList() != null) {
293             isFileExtListEmpty = assoc.getFileExtList().isEmpty();
294         }
295         if ((assoc.getMimeType() == null) && isFileExtListEmpty) {
296             throw new IllegalArgumentException JavaDoc("The given association is invalid. It should " +
297                 "specify both the mimeType and fileExtensionList fields to perform this " +
298                 "operation.");
299         }
300     }
301   
302     /**
303      * Checks whether the given assocation already existed in Windows registry.
304      * <PRE>
305      * The evaluation will based on the following rule:
306      * 1. mimetype == null && fileExt == null
307      * return false
308      * 2. mimetype == null && fileExt != null
309      * return !(fileExt existed in the reg table)
310      * 3. mimetype != null && fileExt == null
311      * return !(mimetype existed in the reg table)
312      * 4. mimetype != null && fileExt != null
313      * return ( (mimetype existed in the reg table) &&
314      * (fileExt existed in the reg table) &&
315      * (getFileExtByMimeType(mimetype) == fileExt) &&
316      * (getMimeTypeByFileExt(fileExt) == mimetype) )
317      * </PRE>
318      *
319      * @param assoc given association (not null)
320      * @param regLevel given registry level
321      * @return true if the given association existed
322      *
323      */

324     public boolean isAssociationExist(Association assoc, int regLevel) {
325         String JavaDoc temFileExt = null;
326         String JavaDoc temMimeType = assoc.getMimeType();
327         Iterator JavaDoc temFileExtIter;
328         if (assoc.getFileExtList() != null) {
329             temFileExtIter = assoc.getFileExtList().iterator();
330         } else {
331             temFileExtIter = null;
332         }
333
334         if (temFileExtIter != null) {
335             if (temFileExtIter.hasNext()) {
336                 temFileExt = (String JavaDoc) temFileExtIter.next();
337             }
338         }
339
340         //Check if there is the same file extension define in Win2k
341
//HKEY_CURRENT_USER\\software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts
342
if (WinRegistryUtil.isWin2kUserDefinedFileExtExist(temFileExt)) {
343             return true;
344         }
345
346         if ((temMimeType == null) && (temFileExt == null)) {
347             return false;
348         } else if ((temMimeType == null) && (temFileExt != null)) {
349             return WinRegistryUtil.isFileExtExist(temFileExt, regLevel);
350         } else if ((temMimeType != null) && (temFileExt == null)) {
351             return WinRegistryUtil.isMimeTypeExist(temMimeType, regLevel);
352         } else {
353             String JavaDoc regMimeType = WinRegistryUtil.getMimeTypeByFileExt(temFileExt, regLevel);
354             String JavaDoc regFileExt = WinRegistryUtil.getFileExtByMimeType(temMimeType, regLevel);
355     
356             return ((WinRegistryUtil.isMimeTypeExist(temMimeType, regLevel))
357                     && (WinRegistryUtil.isFileExtExist(temFileExt, regLevel))
358                     && (temFileExt == null
359                             ? regFileExt == null
360                             : temFileExt.equals(regFileExt))
361                     && (temMimeType == null
362                             ? regMimeType == null
363                             : temMimeType.equals(regMimeType)));
364         }
365     }
366   
367     /**
368      * Registers the given association info in the specified level.
369      * <PRE>
370      *
371      * The register process will following the rules below
372      * 1. mimetype == null && fileExt != null
373      * Since fileExt does not exist in this case
374      * 1.1 adds the fileExt
375      * 1.2 adds description, icon file and action list for this fileExt
376      * 2. mimetype != null && fileExt == null
377      * Since mimetype does not exist in this case
378      * just adds the mimetype in the reg table
379      * 3. mimetype != null && fileExt != null
380      * 3.1 Adds the mime type into the reg table if necessary
381      * 3.2 Adds the file extension into the reg table if necessary
382      * 3.3 Adds the description, icon file and action list for the fileExt
383      * 3.4 Sets the mutual reference of the mime type and file extension
384      * </PRE>
385      *
386      * @param assoc given association (not null)
387      * @param regLevel given registry level
388      * @throws RegisterFailedException if the operation fails.
389      */

390     public void registerAssociation(Association assoc, int regLevel)
391         throws RegisterFailedException {
392         //LOCAL_MACHINE. Association, in this case, should only be written into LOCAL_MACHINE rather
393
//than the CURRENT_USER.
394
boolean isOldWindows = false;
395         String JavaDoc osName = System.getProperty("os.name").toLowerCase();
396         if ((osName.indexOf("98") != -1) ||
397             (osName.indexOf("me") != -1) ||
398             (osName.indexOf("nt") != -1)) {
399             isOldWindows = true;
400         }
401         if (isOldWindows) {
402             regLevel = AppConstants.SYSTEM_LEVEL;
403         }
404         BackupAssociation backupAssoc = new BackupAssociation(assoc, regLevel);
405         String JavaDoc curMimeType = backupAssoc.getCurMimeType();
406         String JavaDoc curFileExt = backupAssoc.getCurFileExt();
407         String JavaDoc curDescription = assoc.getDescription();
408         String JavaDoc curIconFileName = assoc.getIconFileName();
409         List JavaDoc curActionList = assoc.getActionList();
410         boolean curMimeTypeExisted = backupAssoc.getCurMimeTypeExisted();
411         boolean curFileExtExisted = backupAssoc.getCurFileExtExisted();
412         
413     
414         try {
415             if ((curMimeType == null) && (curFileExt != null)) {
416                 WinRegistryUtil.addFileExt(curFileExt, regLevel);
417                 if (curDescription != null) {
418                     WinRegistryUtil.setDescriptionByFileExt(curDescription,
419                             curFileExt, regLevel);
420                 }
421                 if (curIconFileName != null) {
422                     WinRegistryUtil.setIconFileNameByFileExt(curIconFileName,
423                         curFileExt, regLevel);
424                 }
425                 if (curActionList != null) {
426                     WinRegistryUtil.setActionListByFileExt(curActionList,
427                         curFileExt, regLevel);
428                 }
429                 //Mark the classID generator field
430
WinRegistryUtil.markGeneratorByFileExt(curFileExt, regLevel);
431             } else if ((curMimeType != null) && (curFileExt == null)) {
432                 WinRegistryUtil.addMimeType(curMimeType, regLevel);
433             } else if ((curMimeType != null) && (curFileExt != null)) {
434                 if (!curMimeTypeExisted) {
435                     WinRegistryUtil.addMimeType(curMimeType, regLevel);
436                 }
437                 if (!curFileExtExisted) {
438                     WinRegistryUtil.addFileExt(curFileExt, regLevel);
439                 }
440                 if (curDescription != null) {
441                     WinRegistryUtil.setDescriptionByFileExt(curDescription,
442                         curFileExt, regLevel);
443                 }
444                 if (curIconFileName != null) {
445                     WinRegistryUtil.setIconFileNameByFileExt(curIconFileName,
446                         curFileExt, regLevel);
447                 }
448                 if (curActionList != null) {
449                     WinRegistryUtil.setActionListByFileExt(curActionList,
450                         curFileExt, regLevel);
451                 }
452                 //Mark the classID generator field.
453
WinRegistryUtil.markGeneratorByFileExt(curFileExt, regLevel);
454                 WinRegistryUtil.setMutualRef(curFileExt, curMimeType, regLevel);
455             }
456         } catch (RegisterFailedException e) {
457             restoreAssociationRegistration(backupAssoc, regLevel);
458                 
459             throw e;
460         }
461     }
462
463     /**
464      * Unregisters the given association in the specified level.
465      *
466      * @param assoc given association (not null)
467      * @param regLevel given registry level
468      * @throws RegisterFailedException if the operation fails.
469      */

470     public void unregisterAssociation(Association assoc, int regLevel)
471         throws RegisterFailedException {
472         //Note: Windows 98, Windows ME & Windows NT will only take care of registry information from
473
//LOCAL_MACHINE. Association, in this case, should only be unregistered from LOCAL_MACHINE rather
474
//than the CURRENT_USER.
475
boolean isOldWindows = false;
476         String JavaDoc osName = System.getProperty("os.name").toLowerCase();
477         if ((osName.indexOf("98") != -1) ||
478             (osName.indexOf("me") != -1) ||
479             (osName.indexOf("nt") != -1)) {
480             isOldWindows = true;
481         }
482         if (isOldWindows) {
483             regLevel = AppConstants.SYSTEM_LEVEL;
484         }
485         
486         BackupAssociation backupAssoc = new BackupAssociation(assoc, regLevel);
487         String JavaDoc curMimeType = backupAssoc.getCurMimeType();
488         String JavaDoc curFileExt = backupAssoc.getCurFileExt();
489         boolean curMimeTypeExisted = backupAssoc.getCurMimeTypeExisted();
490         boolean curFileExtExisted = backupAssoc.getCurFileExtExisted();
491     
492         try {
493             if (curMimeTypeExisted) {
494                 WinRegistryUtil.removeMimeType(curMimeType, regLevel);
495             }
496             if (curFileExtExisted) {
497                 WinRegistryUtil.removeFileExt(curFileExt, regLevel);
498             }
499         } catch (RegisterFailedException e) {
500             restoreAssociationUnregistration(backupAssoc, regLevel);
501             
502             throw e;
503         }
504     }
505 }
506
Popular Tags