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;
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;
18
import java.util.TreeSet;
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;
32
* @author Piotr Piastucki
34
public class BranchLocationManager {
36
private static final String KEY_LOCATION = "location"; //$NON-NLS-1$
38
private static final String KEY_LOCATIONS = "locations"; //$NON-NLS-1$
40
private static Set<IBzrBranch> branches;
42
* Return an ordered list of all repository locations that are presently
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));
55
private static Set<BranchLocation> getLocations() {
56
String all = getPreferenceStore().getString(BazaarPreferenceConstants.BRANCH_LOCATION_HISTORY);
57
if (all.length() == 0) {
58
return Collections.emptySet();
60
int max = getLocationHistorySize();
62
return Collections.emptySet();
66
memento = XMLMemento.createReadRoot(new StringReader(all));
67
} catch (WorkbenchException e) {
68
return Collections.emptySet();
70
Set<BranchLocation> locations = new LinkedHashSet<BranchLocation>();
71
for (IMemento child : memento.getChildren(KEY_LOCATION)) {
73
locations.add(new BranchLocation(child.getTextData()));
74
if (locations.size() == max) {
77
} catch (BazaarClientException e) {
84
* Save a new commit message.
88
public static void saveBranchLocation(String location) {
89
if (location == null || location.trim().isEmpty()) {
93
int size = getLocationHistorySize();
94
ArrayList<BranchLocation> locations = new ArrayList<BranchLocation>();
96
BranchLocation bl = new BranchLocation(location);
98
if (locations.size() < size) {
99
Set<BranchLocation> history = getLocations();
101
for (BranchLocation previous : history) {
102
locations.add(previous);
103
if (locations.size() == size)
108
saveBranchLocations(locations, BazaarPreferenceConstants.BRANCH_LOCATION_HISTORY);
110
} catch (BazaarClientException e) {
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());
119
StringWriter writer = new StringWriter();
121
memento.save(writer);
122
getPreferenceStore().setValue(preferenceKey, writer.toString());
123
} catch (IOException e) {
127
private static IPreferenceStore getPreferenceStore() {
128
return EclipseBazaarUI.getDefault().getPreferenceStore();
131
private static int getLocationHistorySize() {