KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > datatransfer > ArchiveFileManipulations


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial implementation
10  * Red Hat, Inc - Extracted methods from WizardArchiveFileResourceImportPage1
11  *******************************************************************************/

12
13 package org.eclipse.ui.internal.wizards.datatransfer;
14
15 import java.io.IOException JavaDoc;
16 import java.util.zip.ZipFile JavaDoc;
17
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
21
22 /**
23  * @since 3.1
24  */

25 public class ArchiveFileManipulations {
26
27     private static ZipLeveledStructureProvider zipProviderCache;
28
29     private static TarLeveledStructureProvider tarProviderCache;
30
31     /**
32      * Determine whether the file with the given filename is in .tar.gz or .tar
33      * format.
34      *
35      * @param fileName
36      * file to test
37      * @return true if the file is in tar format
38      */

39     public static boolean isTarFile(String JavaDoc fileName) {
40         if (fileName.length() == 0) {
41             return false;
42         }
43
44         try {
45             new TarFile(fileName);
46         } catch (TarException tarException) {
47             return false;
48         } catch (IOException JavaDoc ioException) {
49             return false;
50         }
51
52         return true;
53     }
54
55     /**
56      * Determine whether the file with the given filename is in .zip or .jar
57      * format.
58      *
59      * @param fileName
60      * file to test
61      * @return true if the file is in tar format
62      */

63     public static boolean isZipFile(String JavaDoc fileName) {
64         if (fileName.length() == 0) {
65             return false;
66         }
67
68         try {
69             new ZipFile JavaDoc(fileName);
70         } catch (IOException JavaDoc ioException) {
71             return false;
72         }
73
74         return true;
75     }
76
77     /**
78      * Clears the cached structure provider after first finalizing it properly.
79      *
80      * @param shell
81      * The shell to display any possible Dialogs in
82      */

83     public static void clearProviderCache(Shell shell) {
84         if (zipProviderCache != null) {
85             closeZipFile(zipProviderCache.getZipFile(), shell);
86             zipProviderCache = null;
87         }
88         tarProviderCache = null;
89     }
90
91     /**
92      * @param targetZip
93      * @param shell
94      * @return the structure provider
95      */

96     public static ZipLeveledStructureProvider getZipStructureProvider(
97             ZipFile JavaDoc targetZip, Shell shell) {
98         if (zipProviderCache == null) {
99             zipProviderCache = new ZipLeveledStructureProvider(targetZip);
100         } else if (!zipProviderCache.getZipFile().getName().equals(
101                 targetZip.getName())) {
102             clearProviderCache(shell);
103             // ie.- new value, so finalize&remove old value
104
zipProviderCache = new ZipLeveledStructureProvider(targetZip);
105         } else if (!zipProviderCache.getZipFile().equals(targetZip)) {
106             // duplicate handle to same zip
107
// dispose old zip and use new one in case old one is closed
108
zipProviderCache = new ZipLeveledStructureProvider(targetZip);
109         }
110
111         return zipProviderCache;
112     }
113
114     /**
115      * Attempts to close the passed zip file, and answers a boolean indicating
116      * success.
117      *
118      * @param file
119      * The zip file to attempt to close
120      * @param shell
121      * The shell to display error dialogs in
122      * @return Returns true if the operation was successful
123      */

124     public static boolean closeZipFile(ZipFile JavaDoc file, Shell shell) {
125         try {
126             file.close();
127         } catch (IOException JavaDoc e) {
128             displayErrorDialog(DataTransferMessages.ZipImport_couldNotClose,
129                     shell);
130             return false;
131         }
132
133         return true;
134     }
135
136     /**
137      * Returns a structure provider for the specified tar file.
138      *
139      * @param targetTar
140      * The specified tar file
141      * @param shell
142      * The shell to display dialogs in
143      * @return the structure provider
144      */

145     public static TarLeveledStructureProvider getTarStructureProvider(
146             TarFile targetTar, Shell shell) {
147         if (tarProviderCache == null) {
148             tarProviderCache = new TarLeveledStructureProvider(targetTar);
149         } else if (!tarProviderCache.getTarFile().getName().equals(
150                 targetTar.getName())) {
151             ArchiveFileManipulations.clearProviderCache(shell);
152             // ie.- new value, so finalize&remove old value
153
tarProviderCache = new TarLeveledStructureProvider(targetTar);
154         }
155
156         return tarProviderCache;
157     }
158
159     /**
160      * Display an error dialog with the specified message.
161      *
162      * @param message
163      * the error message
164      */

165     protected static void displayErrorDialog(String JavaDoc message, Shell shell) {
166         MessageDialog.openError(shell, getErrorDialogTitle(), message);
167     }
168
169     /**
170      * Get the title for an error dialog. Subclasses should override.
171      */

172     protected static String JavaDoc getErrorDialogTitle() {
173         return IDEWorkbenchMessages.WizardExportPage_internalErrorTitle;
174     }
175 }
176
Popular Tags