KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > LocalSiteSelector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.ui.wizards;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.zip.ZipEntry JavaDoc;
19 import java.util.zip.ZipFile JavaDoc;
20
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.swt.custom.BusyIndicator;
23 import org.eclipse.swt.widgets.DirectoryDialog;
24 import org.eclipse.swt.widgets.FileDialog;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.update.internal.ui.UpdateUI;
27 import org.eclipse.update.internal.ui.UpdateUIMessages;
28 import org.eclipse.update.internal.ui.model.SiteBookmark;
29
30 /**
31  */

32 public class LocalSiteSelector {
33
34     /**
35      *
36      */

37     static String JavaDoc lastLocation = null;
38     public LocalSiteSelector() {
39         super();
40     }
41     public static SiteBookmark getLocaLSite(Shell parent, SiteBookmark[] siteBookmarks) {
42         DirectoryDialog dialog = new DirectoryDialog(parent);
43         dialog.setMessage(
44             UpdateUIMessages.LocalSiteSelector_dialogMessage);
45         dialog.setFilterPath(lastLocation);
46         String JavaDoc dir = dialog.open();
47
48         SiteBookmark siteBookmark = null;
49         while (dir != null && siteBookmark == null) {
50             File JavaDoc dirFile = new File JavaDoc(dir);
51             if (isDirSite(dirFile)) {
52                 if (!isDuplicate( dirFile, siteBookmarks)) {
53                     siteBookmark = createDirSite(dirFile);
54                     lastLocation = dir;
55                 } else {
56                     MessageDialog.openInformation(parent,
57                                                   UpdateUIMessages.LocalSiteSelector_dirInfoTitle,
58                                                   UpdateUIMessages.LocalSiteSelector_dirDuplicateDefinition);
59                     dialog.setFilterPath(dir);
60                     dir = dialog.open();
61                 }
62             } else {
63                 MessageDialog.openInformation(
64                     parent,
65                     UpdateUIMessages.LocalSiteSelector_dirInfoTitle,
66                     UpdateUIMessages.LocalSiteSelector_dirInfoMessage);
67                 dialog.setFilterPath(dir);
68                 dir = dialog.open();
69             }
70         }
71         return siteBookmark;
72     }
73     public static SiteBookmark getLocaLZippedSite(Shell parent, SiteBookmark[] siteBookmarks) {
74         FileDialog dialog = new FileDialog(parent);
75         dialog.setText(
76             UpdateUIMessages.LocalSiteSelector_dialogMessagezip);
77         //dialog.setFilterExtensions(new String[] { "*.zip", "*.jar" });
78
// //$NON-NLS-1$
79
dialog.setFilterExtensions(new String JavaDoc[] { "*.jar;*.zip" }); //$NON-NLS-1$
80

81         SiteBookmark siteBookmark = null;
82
83         String JavaDoc zip = dialog.open();
84         while (zip != null && siteBookmark == null) {
85             File JavaDoc zipF = new File JavaDoc(zip);
86             if (isZipSite(zipF)) {
87                 siteBookmark = createZipSite(zipF);
88                 if (isDuplicate( siteBookmark.getURL(), siteBookmarks)) {
89                     MessageDialog.openInformation(parent,
90                               UpdateUIMessages.LocalSiteSelector_zipInfoTitle,
91                               UpdateUIMessages.LocalSiteSelector_zipDuplicateDefinition);
92                     siteBookmark = null;
93                     zip = dialog.open();
94                 }
95             } else {
96                 MessageDialog.openInformation(
97                     parent,
98                     UpdateUIMessages.LocalSiteSelector_zipInfoTitle,
99                     UpdateUIMessages.LocalSiteSelector_zipInfoMessage);
100                 zip = dialog.open();
101             }
102         }
103         return siteBookmark;
104     }
105     /**
106      * Returns true the zip file contains an update site
107      *
108      * @param file
109      * @return
110      */

111     static boolean isZipSite(File JavaDoc file) {
112         if (!file.getName().toLowerCase().endsWith(".zip") //$NON-NLS-1$
113
&& !file.getName().toLowerCase().endsWith(".jar")) { //$NON-NLS-1$
114
return false;
115         }
116
117         ZippedSiteValidator validator = new ZippedSiteValidator(file);
118         BusyIndicator.showWhile(
119             UpdateUI.getActiveWorkbenchShell().getDisplay(),
120             validator);
121         return validator.isValid();
122     }
123
124     /**
125      * Returns true if the specified dir contains an update site
126      *
127      * @param dir
128      * @return
129      */

130     static boolean isDirSite(File JavaDoc dir) {
131         File JavaDoc siteXML = new File JavaDoc(dir, "site.xml"); //$NON-NLS-1$
132
File JavaDoc featuresDir = new File JavaDoc(dir, "features"); //$NON-NLS-1$
133
File JavaDoc pluginsDir = new File JavaDoc(dir, "plugins"); //$NON-NLS-1$
134
return siteXML.exists()
135             || featuresDir.exists()
136             && featuresDir.isDirectory()
137             && pluginsDir.exists()
138             && pluginsDir.isDirectory();
139     }
140
141     /**
142      * Creates a bookmark to a zipped site
143      *
144      * @param file
145      * @return
146      */

147     static SiteBookmark createZipSite(File JavaDoc file) {
148         try {
149             URL JavaDoc fileURL = new URL JavaDoc("file", null, file.getAbsolutePath()); //$NON-NLS-1$
150
URL JavaDoc url =
151                 new URL JavaDoc(
152                     "jar:" //$NON-NLS-1$
153
+ fileURL.toExternalForm().replace('\\', '/')
154                         + "!/"); //$NON-NLS-1$
155
SiteBookmark site = new SiteBookmark(file.getName(), url, false);
156             site.setLocal(true);
157             return site;
158         } catch (Exception JavaDoc e) {
159             return null;
160         }
161     }
162
163     /**
164      * Creates a bookmark to a site on the file system
165      *
166      * @param file
167      * @return
168      */

169     static SiteBookmark createDirSite(File JavaDoc file) {
170         try {
171             URL JavaDoc url = file.toURL();
172             String JavaDoc parent = file.getParent();
173             if(parent == null)
174                 parent = ""; //$NON-NLS-1$
175
else
176                 parent = new File JavaDoc(parent).getName();
177             String JavaDoc siteName = parent + "/" + file.getName(); //$NON-NLS-1$
178
SiteBookmark site = new SiteBookmark(siteName, url, false);
179             site.setLocal(true);
180             return site;
181         } catch (Exception JavaDoc e) {
182             return null;
183         }
184     }
185     static class ZippedSiteValidator implements Runnable JavaDoc {
186         File JavaDoc file;
187         boolean valid = false;
188         public ZippedSiteValidator(File JavaDoc file) {
189             this.file = file;
190         }
191         public void run() {
192             ZipFile JavaDoc siteZip = null;
193             try {
194                 // check if the zip file contains site.xml
195
siteZip = new ZipFile JavaDoc(file);
196                 if (siteZip.getEntry("site.xml") != null) { //$NON-NLS-1$
197
valid = true;
198                     return;
199                 }
200
201                 boolean hasFeatures = false;
202                 boolean hasPlugins = false;
203                 for (Enumeration JavaDoc iterator = siteZip.entries();
204                     iterator.hasMoreElements();
205                     ) {
206                     ZipEntry JavaDoc zEntry = (ZipEntry JavaDoc) iterator.nextElement();
207                     if (!hasFeatures
208                         && zEntry.getName().startsWith("features")) { //$NON-NLS-1$
209
hasFeatures = true;
210                     }
211                     if (!hasPlugins
212                         && zEntry.getName().startsWith("plugins")) { //$NON-NLS-1$
213
hasPlugins = true;
214                     }
215                     if (hasFeatures && hasPlugins) {
216                         valid = true;
217                         return;
218                     }
219                 }
220             } catch (Exception JavaDoc e) {
221             } finally {
222                 try {
223                     if (siteZip != null) {
224                         siteZip.close();
225                     }
226                 } catch (IOException JavaDoc ioe) {
227                 }
228             }
229
230         }
231         /**
232          * @return Returns the valid. */

233         public boolean isValid() {
234             return valid;
235         }
236     }
237     
238     private static boolean isDuplicate( File JavaDoc file, SiteBookmark[] siteBookmarks) {
239         
240         try {
241             return isDuplicate(file.toURL(), siteBookmarks);
242         } catch (MalformedURLException JavaDoc mue) {
243             // this should not ever happen
244
UpdateUI.logException(mue);
245         }
246         
247         return false;
248     }
249     
250     private static boolean isDuplicate( URL JavaDoc url, SiteBookmark[] siteBookmarks) {
251         
252         if ( siteBookmarks == null)
253             return false;
254
255         for( int i = 0; i < siteBookmarks.length; i++) {
256             if (siteBookmarks[i].getURL().equals(url))
257                 return true;
258
259         }
260         return false;
261         
262     }
263
264 }
265
Popular Tags