~verterok/bzr-eclipse/fix-license

« back to all changes in this revision

Viewing changes to org.vcs.bazaar.eclipse.ui/src/org/vcs/bazaar/eclipse/ui/branch/BranchLocationManager.java

  • Committer: Guillermo Gonzalez
  • Date: 2013-10-23 12:11:03 UTC
  • mfrom: (357.1.14 trunk)
  • Revision ID: guillo.gonzo@gmail.com-20131023121103-2mpxnmkop5em0rzg
merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2013 Piotr Piastucki
 
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
package org.vcs.bazaar.eclipse.ui.branch;
 
9
 
 
10
import java.io.IOException;
 
11
import java.io.StringReader;
 
12
import java.io.StringWriter;
 
13
import java.util.ArrayList;
 
14
import java.util.Collection;
 
15
import java.util.Collections;
 
16
import java.util.LinkedHashSet;
 
17
import java.util.Set;
 
18
import java.util.TreeSet;
 
19
 
 
20
import org.eclipse.jface.preference.IPreferenceStore;
 
21
import org.eclipse.ui.IMemento;
 
22
import org.eclipse.ui.WorkbenchException;
 
23
import org.eclipse.ui.XMLMemento;
 
24
import org.vcs.bazaar.client.core.BazaarClientException;
 
25
import org.vcs.bazaar.client.core.BranchLocation;
 
26
import org.vcs.bazaar.eclipse.core.model.IBzrBranch;
 
27
import org.vcs.bazaar.eclipse.core.repository.BranchFactory;
 
28
import org.vcs.bazaar.eclipse.ui.EclipseBazaarUI;
 
29
import org.vcs.bazaar.eclipse.ui.preferences.BazaarPreferenceConstants;
 
30
 
 
31
/**
 
32
 * @author Piotr Piastucki
 
33
 */
 
34
public class BranchLocationManager {
 
35
 
 
36
        private static final String KEY_LOCATION = "location"; //$NON-NLS-1$
 
37
 
 
38
        private static final String KEY_LOCATIONS = "locations"; //$NON-NLS-1$
 
39
 
 
40
        private static Set<IBzrBranch> branches;
 
41
        /*
 
42
         * Return an ordered list of all repository locations that are presently
 
43
         * known.
 
44
         */
 
45
        public static Set<IBzrBranch> getAllBranches() {
 
46
                if (branches == null) {
 
47
                        branches = new TreeSet<IBzrBranch>();
 
48
                        for (BranchLocation location : getLocations()) {
 
49
                                branches.add(BranchFactory.getBranch(location));
 
50
                        }
 
51
                }
 
52
                return branches;
 
53
        }
 
54
 
 
55
        private static Set<BranchLocation> getLocations() {
 
56
                String all = getPreferenceStore().getString(BazaarPreferenceConstants.BRANCH_LOCATION_HISTORY);
 
57
                if (all.length() == 0) {
 
58
                        return Collections.emptySet();
 
59
                }
 
60
                int max = getLocationHistorySize();
 
61
                if (max < 1) {
 
62
                        return Collections.emptySet();
 
63
                }
 
64
                XMLMemento memento;
 
65
                try {
 
66
                        memento = XMLMemento.createReadRoot(new StringReader(all));
 
67
                } catch (WorkbenchException e) {
 
68
                        return Collections.emptySet();
 
69
                }
 
70
                Set<BranchLocation> locations = new LinkedHashSet<BranchLocation>();
 
71
                for (IMemento child : memento.getChildren(KEY_LOCATION)) {
 
72
                        try {
 
73
                                locations.add(new BranchLocation(child.getTextData()));
 
74
                                if (locations.size() == max) {
 
75
                                        break;
 
76
                                }
 
77
                        } catch (BazaarClientException e) {
 
78
                        }
 
79
                }
 
80
                return locations;
 
81
        }
 
82
 
 
83
        /**
 
84
         * Save a new commit message.
 
85
         *
 
86
         * @param message
 
87
         */
 
88
        public static void saveBranchLocation(String location) {
 
89
                if (location == null || location.trim().isEmpty()) {
 
90
                        return;
 
91
                }
 
92
                try {
 
93
                        int size = getLocationHistorySize();
 
94
                        ArrayList<BranchLocation> locations = new ArrayList<BranchLocation>();
 
95
                        if (size > 0) {
 
96
                                BranchLocation bl = new BranchLocation(location);
 
97
                                locations.add(bl);
 
98
                                if (locations.size() < size) {
 
99
                                        Set<BranchLocation> history = getLocations();
 
100
                                        history.remove(bl);
 
101
                                        for (BranchLocation previous : history) {
 
102
                                                locations.add(previous);
 
103
                                                if (locations.size() == size)
 
104
                                                        break;
 
105
                                        }
 
106
                                }
 
107
                        }
 
108
                        saveBranchLocations(locations, BazaarPreferenceConstants.BRANCH_LOCATION_HISTORY);
 
109
                        branches = null;
 
110
                } catch (BazaarClientException e) {
 
111
                }
 
112
        }
 
113
        
 
114
        private static void saveBranchLocations(Collection<BranchLocation> locations, String preferenceKey) {
 
115
                XMLMemento memento = XMLMemento.createWriteRoot(KEY_LOCATIONS);
 
116
                for (BranchLocation l : locations) {
 
117
                        memento.createChild(KEY_LOCATION).putTextData(l.toString());
 
118
                }
 
119
                StringWriter writer = new StringWriter();
 
120
                try {
 
121
                        memento.save(writer);
 
122
                        getPreferenceStore().setValue(preferenceKey, writer.toString());
 
123
                } catch (IOException e) {
 
124
                }
 
125
        }
 
126
 
 
127
        private static IPreferenceStore getPreferenceStore() {
 
128
                return EclipseBazaarUI.getDefault().getPreferenceStore();
 
129
        }
 
130
 
 
131
        private static int getLocationHistorySize() {
 
132
                return 10;
 
133
        }
 
134
 
 
135
}