1
/*******************************************************************************
2
* Copyright (c) 2006 Software Balm Consulting Inc.
4
* TODO: LICENSE DETAILS
5
*******************************************************************************/
6
package org.vcs.bazaar.eclipse.core.repository;
9
import java.io.FileInputStream;
10
import java.io.FileOutputStream;
11
import java.io.ObjectInputStream;
12
import java.io.ObjectOutputStream;
13
import java.util.Iterator;
15
import java.util.SortedSet;
16
import java.util.TreeSet;
18
import org.vcs.bazaar.eclipse.EclipseBazaarCore;
19
import org.vcs.bazaar.eclipse.core.model.IBzrBranch;
22
* A manager for all Bazaar repository locations.
24
* TODO: Need a way to delete these branches. Repo Explorer perspective a la
25
* subclipse? TODO: Should probably use a text based format for the locations.
26
* This is just a crude object dump right now.
28
public class BranchLocationManager {
29
private static final SortedSet<IBzrBranch> branches = new TreeSet<IBzrBranch>();// final ensures that its reference will remain the same.
30
private static final String BRANCH_LOCATION_FILE = "repo_locations.txt";
33
//This will only get instantiated once don't need to synchronize the constructor.
34
private static final BranchLocationManager instance = new BranchLocationManager();
36
private BranchLocationManager(){}
38
* @return The manager instance
40
public static BranchLocationManager getInstance() {
45
* Return a <code>File</code> object representing the location file. The
46
* file may or may not exist and must be checked before use.
48
private File getLocationFile() {
49
return EclipseBazaarCore.getDefault().getStateLocation().append(BRANCH_LOCATION_FILE).toFile();
53
* Load all saved repository locations from the plug-in's default area.
55
public void start() throws Exception {
57
File locationFile = getLocationFile();
59
// If the file doesn't exist, then there's nothing to do.
60
if (!locationFile.exists())
63
FileInputStream istream = new FileInputStream(locationFile);
64
ObjectInputStream p = new ObjectInputStream(istream);
68
// String uri = (String) p.readObject();
69
IBzrBranch branch = (IBzrBranch) p.readObject();
70
addRepoLocation(branch);
72
} catch (Exception e) {
73
// Expected exception when there are no more records. Is this the only reason for an ioexception ??
81
* Flush all repository locations out to a file in the plug-in's default
84
public void stop() throws Exception {
85
// Determine if there's anything that we need to save out to file.
86
if (getAllBranches().size() == 0)
89
File locationFile = getLocationFile();
91
// If the file doesn't exist, then create it.
92
if (!locationFile.exists()) {
93
locationFile.createNewFile();
96
FileOutputStream ostream = new FileOutputStream(locationFile);
97
ObjectOutputStream p = new ObjectOutputStream(ostream);
99
// Add any previously existing URI's to the combo box for ease of use.
100
Iterator<IBzrBranch> iter = getAllBranches().iterator();
101
while (iter.hasNext()) {
102
IBzrBranch branch = (iter.next());
103
p.writeObject(branch);
112
* Return an ordered list of all repository locations that are presently
115
public Set<IBzrBranch> getAllBranches() {
116
return new TreeSet<IBzrBranch>(branches);
120
* Add a repository location to the database.
122
public boolean addRepoLocation(IBzrBranch branch) {
123
if (branch == null || branch.getBranchLocation() == null) return false;
124
return branches.add(branch);
128
* Add a repository location to the database.
130
public boolean removeRepoLocation(IBzrBranch branch) {
131
if (branch == null) return false;
132
return branches.remove(branch);