KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > model > SiteBookmark


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 API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.ui.model;
12
13 import java.net.*;
14 import java.util.Vector JavaDoc;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.update.core.*;
19 import org.eclipse.update.internal.ui.*;
20
21 public class SiteBookmark extends NamedModelObject
22                             implements ISiteAdapter {
23
24     private static final long serialVersionUID = 1L;
25     public static final String JavaDoc P_URL="p_url"; //$NON-NLS-1$
26
public static final String JavaDoc P_TYPE="p_type"; //$NON-NLS-1$
27

28     private URL url;
29     transient private ISite site;
30     transient private Vector JavaDoc catalog = new Vector JavaDoc();
31     transient private SiteCategory otherCategory;
32     private boolean webBookmark;
33     private boolean selected;
34     private String JavaDoc [] ignoredCategories = new String JavaDoc[0];
35     private boolean readOnly = false;
36     private boolean local = false;
37     private boolean unavailable = false;
38     private String JavaDoc description;
39
40     public SiteBookmark() {
41     }
42     
43     public SiteBookmark(String JavaDoc name, URL url, boolean webBookmark) {
44         this(name, url, webBookmark, false);
45     }
46     
47     public SiteBookmark(String JavaDoc name, URL url, boolean webBookmark, boolean selected) {
48         super(name);
49         this.url = url;
50         this.webBookmark = webBookmark;
51         this.selected = selected;
52     }
53
54     public boolean equals(Object JavaDoc obj) {
55         if (obj == this)
56             return true;
57         if (!(obj instanceof SiteBookmark))
58             return false;
59         SiteBookmark b = (SiteBookmark)obj;
60         if (url == null)
61             return false;
62         return url.equals(b.url);
63     }
64
65     public int hashCode() {
66         if (url == null)
67             return super.hashCode();
68         else
69             return url.hashCode();
70     }
71
72     public void setSelected(boolean selected) {
73         this.selected = selected;
74     }
75     
76     public boolean isSelected() {
77         return selected;
78     }
79     
80     public String JavaDoc [] getIgnoredCategories() {
81         return ignoredCategories;
82     }
83     
84     public void setIgnoredCategories(String JavaDoc [] categories) {
85         this.ignoredCategories = categories;
86     }
87     
88     public void setWebBookmark(boolean value) {
89         if (isLocal()) return;
90         this.webBookmark = value;
91         notifyObjectChanged(P_TYPE);
92     }
93     
94     public boolean isWebBookmark() {
95         return webBookmark;
96     }
97     
98     public URL getURL() {
99         return url;
100     }
101
102
103     public void setURL(URL url) {
104         this.url = url;
105         site = null;
106         notifyObjectChanged(P_URL);
107     }
108     
109     public ISite getSite(IProgressMonitor monitor) {
110         return getSite(true, monitor);
111     }
112     
113     public ISite getSite(boolean showDialogIfFailed, IProgressMonitor monitor) {
114         if (site==null) {
115             try {
116                 connect(monitor);
117             }
118             catch (CoreException e) {
119                 UpdateUI.logException(e, showDialogIfFailed);
120             }
121         }
122         return site;
123     }
124     
125     public boolean isSiteConnected() {
126         return site!=null;
127     }
128     
129     public void connect(IProgressMonitor monitor) throws CoreException {
130         connect(true, monitor);
131     }
132     
133     public void connect(boolean useCache, IProgressMonitor monitor) throws CoreException {
134         try {
135             if (monitor==null) monitor = new NullProgressMonitor();
136             monitor.beginTask("", 2); //$NON-NLS-1$
137
monitor.subTask(NLS.bind(UpdateUIMessages.SiteBookmark_connecting, url.toString()));
138             site = SiteManager.getSite(url, useCache, new SubProgressMonitor(monitor, 1));
139             if (site!=null) {
140                 createCatalog(new SubProgressMonitor(monitor, 1));
141                 unavailable = false;
142             } else {
143                 catalog = new Vector JavaDoc();
144                 unavailable = true;
145             }
146         } catch (CoreException e) {
147             unavailable = true;
148             throw e;
149         }
150     }
151     
152     public boolean isUnavailable() {
153         return unavailable;
154     }
155     
156     public void setUnavailable(boolean value) {
157         unavailable = value;
158     }
159     
160     private void createCatalog(IProgressMonitor monitor) {
161         catalog = new Vector JavaDoc();
162         otherCategory = new SiteCategory(this, null, null);
163         // Add all the categories
164
ICategory [] categories = site.getCategories();
165         
166         ISiteFeatureReference [] featureRefs;
167         featureRefs = site.getRawFeatureReferences();
168         
169         monitor.beginTask("", featureRefs.length + categories.length); //$NON-NLS-1$
170

171         for (int i=0; i<categories.length; i++) {
172             ICategory category = categories[i];
173             addCategoryToCatalog(category);
174             monitor.worked(1);
175         }
176         // Add features to categories
177

178         for (int i=0; i<featureRefs.length; i++) {
179             ISiteFeatureReference featureRef = featureRefs[i];
180             addFeatureToCatalog(featureRef);
181             monitor.worked(1);
182         }
183         if (otherCategory.getChildCount()>0)
184            catalog.add(otherCategory);
185         
186         // set the site description
187
IURLEntry descURL = site.getDescription();
188         if (descURL != null)
189             description = descURL.getAnnotation();
190     }
191
192     public Object JavaDoc [] getCatalog(boolean withCategories, IProgressMonitor monitor) {
193         if (withCategories)
194             return catalog.toArray();
195         else {
196             // Make a flat catalog
197
Vector JavaDoc flatCatalog = new Vector JavaDoc();
198             for (int i=0; i<catalog.size(); i++) {
199                 SiteCategory category = (SiteCategory)catalog.get(i);
200                 category.addFeaturesTo(flatCatalog);
201             }
202             return flatCatalog.toArray();
203         }
204     }
205     
206     private void addCategoryToCatalog(ICategory category) {
207         String JavaDoc name = category.getName();
208         int loc = name.indexOf('/');
209         if (loc == -1) {
210             // first level
211
catalog.add(new SiteCategory(this, name, category));
212         }
213         else {
214             IPath path = new Path(name);
215             name = path.lastSegment().toString();
216             path = path.removeLastSegments(1);
217             SiteCategory parentCategory = findCategory(path, catalog.toArray());
218             if (parentCategory!=null) {
219                 parentCategory.add(new SiteCategory(this, name, category));
220             }
221         }
222     }
223     private void addFeatureToCatalog(ISiteFeatureReference feature) {
224         ICategory [] categories = feature.getCategories();
225         boolean orphan = true;
226
227         for (int i=0; i<categories.length; i++) {
228             ICategory category = categories[i];
229             String JavaDoc name = category.getName();
230             IPath path = new Path(name);
231             SiteCategory parentCategory = findCategory(path, catalog.toArray());
232             if (parentCategory!=null) {
233                 parentCategory.add(new FeatureReferenceAdapter(feature));
234                 orphan = false;
235             }
236         }
237         if (orphan)
238             otherCategory.add(new FeatureReferenceAdapter(feature));
239     }
240     
241     private SiteCategory findCategory(IPath path, Object JavaDoc [] children) {
242         for (int i=0; i<children.length; i++) {
243             Object JavaDoc child = children[i];
244             if (child instanceof SiteCategory) {
245                 SiteCategory sc = (SiteCategory)child;
246                 if (sc.getName().equals(path.segment(0))) {
247                    if (path.segmentCount()==1) return sc;
248                     else {
249                         path = path.removeFirstSegments(1);
250                         return findCategory(path, sc.getChildren());
251                     }
252                 }
253             }
254         }
255         return null;
256     }
257     
258     /**
259      * @see ISiteAdapter#getLabel()
260      */

261     public String JavaDoc getLabel() {
262         return getName();
263     }
264     
265     public void setReadOnly(boolean readOnly) {
266         this.readOnly = readOnly;
267     }
268     
269     public boolean isReadOnly() {
270         return readOnly;
271     }
272     
273     public void setLocal(boolean local) {
274         this.local = local;
275     }
276     
277     public boolean isLocal() {
278         return local;
279     }
280
281     /**
282      * @param description The description to set.
283      */

284     public void setDescription(String JavaDoc description) {
285         this.description = description;
286     }
287
288     /**
289      * @return Returns the description.
290      */

291     public String JavaDoc getDescription() {
292         if (description == null && isSiteConnected()) {
293             IURLEntry descURL = site.getDescription();
294             if (descURL != null)
295                 description = descURL.getAnnotation();
296         }
297         return description;
298     }
299 }
300
Popular Tags