Project

General

Profile

Download (4.52 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.handler.update;
2

    
3
import java.net.URI;
4
import java.net.URISyntaxException;
5
import java.util.ArrayList;
6
import java.util.List;
7

    
8
import org.eclipse.equinox.internal.p2.ui.ProvUI;
9
import org.eclipse.equinox.internal.p2.ui.model.ElementUtils;
10
import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
11
import org.eclipse.equinox.p2.repository.IRepository;
12
import org.eclipse.equinox.p2.repository.IRepositoryManager;
13
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
14
import org.eclipse.equinox.p2.ui.ProvisioningUI;
15

    
16
import eu.etaxonomy.taxeditor.model.MessagingUtils;
17
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
18
import eu.etaxonomy.taxeditor.util.ApplicationUtil;
19

    
20
/**
21
 * This class is a utility class for updating the editor from a p2 update site,
22
 * greatly inspired by the links given below.
23
 *
24
 *
25
 * To Do :
26
 * - Allow configurable update sites
27
 *
28
 * Notes :
29
 *
30
 *
31
 * @see http://wiki.eclipse.org/Equinox/p2/Adding_Self-Update_to_an_RCP_Application
32
 * @see http://bugs.eclipse.org/281226
33
 * @see http://www.vogella.com/tutorials/EclipseP2Update/article.html
34
 */
35
public class P2Util {
36

    
37
    private static String EDIT_NIGHTLY_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/nightly/";
38
    private static String EDIT_NIGHTLY_UPDATE_SITE_NAME = "Taxonomic Editor Nightly";
39

    
40
    private static String EDIT_STABLE_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/stable/";
41
    private static String EDIT_STABLE_UPDATE_SITE_NAME = "Taxonomic Editor Stable";
42

    
43
    /**
44
     * Retrieve and load the saved list of repositories from the preference store,
45
     * making sure that at least the default repository is always loaded.
46
     */
47
    @SuppressWarnings("restriction")
48
    public static void setP2UpdateRepositories() {
49
        List<MetadataRepositoryElement> repoElements = new ArrayList<>();
50
        List<MetadataRepositoryElement> savedRepoElements = new ArrayList<>();
51

    
52
        MetadataRepositoryElement mre = new MetadataRepositoryElement(null, getP2UpdateRepository(), true);
53
        mre.setNickname(getP2UpdateRepositoryName());
54
        savedRepoElements.add(mre);
55

    
56
        repoElements.addAll(savedRepoElements);
57

    
58
        ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),repoElements
59
                .toArray(new MetadataRepositoryElement[]{} ));
60
    }
61

    
62
    public static String getP2UpdateRepositoryName(){
63
        return ApplicationUtil.isStable()?EDIT_STABLE_UPDATE_SITE_NAME:EDIT_NIGHTLY_UPDATE_SITE_NAME;
64
    }
65

    
66
    public static URI getP2UpdateRepository(){
67
        try {
68
            return ApplicationUtil.isStable()?new URI(EDIT_STABLE_UPDATE_SITE):new URI(EDIT_NIGHTLY_UPDATE_SITE);
69
        } catch (URISyntaxException e) {
70
            MessagingUtils.error(P2Util.class, e);
71
        }
72
        return null;
73
    }
74

    
75
    /**
76
     * {@link org.eclipse.equinox.p2.ui.RepositoryManipulationPage} which handles the repsitory site list
77
     * in preferences does not create a preference store and hence the changes are not saved. This means
78
     * that we need to save it ourselves.
79
     *
80
     * This method saves the list of current repositories in the preference store as a string with
81
     * specific delimiters.
82
     */
83

    
84
    public static void saveP2RepositoryPreferences() {
85

    
86
        IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
87

    
88
        URI[] currentlyEnabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
89
        URI[] currentlyDisabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_DISABLED);
90

    
91
        List<MetadataRepositoryElement> repoElements = new ArrayList<>();
92

    
93
        for(URI repo : currentlyEnabled) {
94
            boolean enabled = true;
95
            String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
96
            MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
97
            element.setNickname(nickname);
98
            element.setEnabled(enabled);
99
            repoElements.add(element);
100
        }
101

    
102
        for(URI repo : currentlyDisabled) {
103
            boolean enabled = false;
104
            String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
105
            MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
106
            element.setNickname(nickname);
107
            element.setEnabled(enabled);
108
            repoElements.add(element);
109
        }
110
        PreferencesUtil.setP2Repositories(repoElements);
111
    }
112
}
(1-1/2)