KickJava   Java API By Example, From Geeks To Geeks.

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


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.model;
12
13 import java.net.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.update.configuration.*;
17 import org.eclipse.update.core.*;
18 import org.eclipse.update.internal.operations.*;
19 import org.eclipse.update.internal.search.*;
20 import org.eclipse.update.internal.ui.UpdateUI;
21 import org.eclipse.update.internal.ui.UpdateUIMessages;
22 import org.eclipse.update.search.*;
23
24 public class DiscoveryFolder extends BookmarkFolder {
25
26     private static final long serialVersionUID = 1L;
27     UpdatePolicy updatePolicy = new UpdatePolicy();
28     
29     public DiscoveryFolder() {
30         super(UpdateUIMessages.DiscoveryFolder_name);
31         setModel(UpdateUI.getDefault().getUpdateModel());
32     }
33     public void initialize() {
34         children.clear();
35         // check if discovery sites defined by features should be exposed to the user
36
if (!UpdateUI.getDefault().getPluginPreferences().getBoolean(UpdateUI.P_DISCOVERY_SITES_ENABLED))
37             return;
38         
39         try {
40             URL updateMapURL = UpdateUtils.getUpdateMapURL();
41             if (updateMapURL!=null) {
42                 updatePolicy = new UpdatePolicy();
43                 // TODO may need to use a proper monitor to be able to cancel connections
44
IStatus status = UpdateUtils.loadUpdatePolicy(updatePolicy, updateMapURL, new NullProgressMonitor());
45                 if (status != null) {
46                     // log and continue
47
UpdateUtils.log(status);
48                 }
49             }
50         } catch (CoreException e) {
51             // log and continue
52
UpdateUtils.log(e.getStatus());
53         }
54         
55         try {
56             ILocalSite site = SiteManager.getLocalSite();
57             IInstallConfiguration config = site.getCurrentConfiguration();
58             IConfiguredSite[] csites = config.getConfiguredSites();
59             for (int i = 0; i < csites.length; i++) {
60                 IConfiguredSite csite = csites[i];
61                 IFeatureReference[] refs = csite.getConfiguredFeatures();
62                 for (int j = 0; j < refs.length; j++) {
63                     IFeatureReference ref = refs[j];
64                     IFeature feature = ref.getFeature(null);
65                     IURLEntry[] entries = feature.getDiscoverySiteEntries();
66                     if (entries.length > 0) {
67                         // Only add discovery sites of root features
68
if (isIncluded(ref, refs))
69                             continue;
70                         addBookmarks(feature);
71                     }
72                 }
73             }
74         } catch (CoreException e) {
75             UpdateUI.logException(e);
76         }
77     }
78     private boolean isIncluded(
79         IFeatureReference ref,
80         IFeatureReference[] refs) {
81         try {
82             VersionedIdentifier vid = ref.getVersionedIdentifier();
83             for (int i = 0; i < refs.length; i++) {
84                 IFeatureReference candidate = refs[i];
85                 // Ignore self
86
if (candidate.equals(ref))
87                     continue;
88                 IFeature cfeature = candidate.getFeature(null);
89                 IFeatureReference[] irefs =
90                     cfeature.getIncludedFeatureReferences();
91                 for (int j = 0; j < irefs.length; j++) {
92                     IFeatureReference iref = irefs[j];
93                     VersionedIdentifier ivid = iref.getVersionedIdentifier();
94                     if (ivid.equals(vid)) {
95                         // bingo - included in at least one feature
96
return true;
97                     }
98                 }
99             }
100         } catch (CoreException e) {
101         }
102         return false;
103     }
104     private void addBookmarks(IFeature feature) {
105         // See if this query site adapter is mapped in the map file
106
// to a different URL.
107
if (updatePolicy != null && updatePolicy.isLoaded()) {
108             IUpdateSiteAdapter mappedSite = updatePolicy.getMappedDiscoverySite(feature.getVersionedIdentifier().getIdentifier());
109             if (mappedSite != null) {
110                 SiteBookmark bookmark =
111                     new SiteBookmark(mappedSite.getLabel(), mappedSite.getURL(), false);
112                 bookmark.setReadOnly(true);
113                 if (!contains(bookmark))
114                     internalAdd(bookmark);
115                 return;
116             } else if (!updatePolicy.isFallbackAllowed()) {
117                 // no match - use original sites if fallback allowed, or nothing.
118
return;
119             }
120         }
121         IURLEntry[] entries = feature.getDiscoverySiteEntries();
122         for (int i = 0; i < entries.length; i++) {
123             IURLEntry entry = entries[i];
124             SiteBookmark bookmark =
125                 new SiteBookmark(
126                     entry.getAnnotation(),
127                     entry.getURL(),
128                     entry.getType() == IURLEntry.WEB_SITE);
129             bookmark.setReadOnly(entry.getType() != IURLEntry.WEB_SITE);
130             if (!contains(bookmark))
131                 internalAdd(bookmark);
132         }
133     }
134     private boolean contains(SiteBookmark bookmark) {
135         for (int i = 0; i < children.size(); i++) {
136             Object JavaDoc o = children.get(i);
137             if (o instanceof SiteBookmark) {
138                 // note: match on URL, not the label
139
if (bookmark.getURL().equals(((SiteBookmark) o).getURL()))
140                     return true;
141             }
142         }
143         return false;
144     }
145     public Object JavaDoc[] getChildren(Object JavaDoc parent) {
146         if (hasChildren() == false)
147             initialize();
148         return super.getChildren(parent);
149     }
150 }
151
Popular Tags