Remote configuration wizards
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / components / RepositorySelection.java
blob1150ea9443a7a0b37b73b5f32b1ddd79754e4a8c
1 /*******************************************************************************
2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org.eclipse.egit.ui.internal.components;
12 import java.util.Collections;
13 import java.util.List;
15 import org.eclipse.jgit.transport.RemoteConfig;
16 import org.eclipse.jgit.transport.URIish;
18 /**
19 * Data class representing selection of remote repository made by user.
20 * Selection is an URI or remote repository configuration.
21 * <p>
22 * Each immutable instance has at least one of two class fields (URI, remote
23 * config) set to null. null value indicates that it has illegal value or this
24 * form of repository selection is not selected.
25 * <p>
26 * If remote configuration is selected, it always has non-empty URIs list.
28 public class RepositorySelection {
29 private URIish uri;
31 private RemoteConfig config;
33 static final RepositorySelection INVALID_SELECTION = new RepositorySelection(
34 null, null);
36 /**
37 * @param uri
38 * the new specified URI. null if the new URI is invalid or user
39 * chosen to specify repository as remote config instead of URI.
40 * @param config
41 * the new remote config. null if user chosen to specify
42 * repository as URI.
44 public RepositorySelection(final URIish uri, final RemoteConfig config) {
45 if (config != null && uri != null)
46 throw new IllegalArgumentException(
47 "URI and config cannot be set at the same time."); //$NON-NLS-1$
48 this.config = config;
49 this.uri = uri;
52 /**
53 * @return the selected URI (if specified by user as valid custom URI) or
54 * first URI from selected configuration (if specified by user as
55 * May be null if there is no valid selection.
57 public URIish getURI() {
58 if (isConfigSelected())
59 return config.getURIs().get(0);
60 return uri;
63 /**
64 * @return list of all selected URIs - either the one specified as custom
65 * URI or all URIs from selected configuration. May be null in case
66 * of no valid selection.
68 public List<URIish> getAllURIs() {
69 if (isURISelected())
70 return Collections.singletonList(uri);
71 if (isConfigSelected())
72 return config.getURIs();
73 return null;
76 /**
77 * @return list of all selected URIs - either the one specified as custom
78 * URI or all URIs from selected configuration. May be null in case
79 * of no valid selection.
81 public List<URIish> getPushURIs() {
82 if (isURISelected())
83 return Collections.singletonList(uri);
84 if (isConfigSelected())
85 return config.getPushURIs();
86 return null;
89 /**
90 * @return the selected remote configuration. null if user chosen to select
91 * repository as URI.
93 public RemoteConfig getConfig() {
94 return config;
97 /**
98 * @return selected remote configuration name or null if selection is not a
99 * remote configuration.
101 public String getConfigName() {
102 if (isConfigSelected())
103 return config.getName();
104 return null;
108 * @return true if selection contains valid URI or remote config, false if
109 * there is no valid selection.
111 public boolean isValidSelection() {
112 return uri != null || config != null;
116 * @return true if user selected valid URI, false if user selected invalid
117 * URI or remote config.
119 public boolean isURISelected() {
120 return uri != null;
124 * @return true if user selected remote configuration, false if user
125 * selected (invalid or valid) URI.
127 public boolean isConfigSelected() {
128 return config != null;
131 @Override
132 public boolean equals(final Object obj) {
133 if (obj instanceof RepositorySelection) {
134 final RepositorySelection other = (RepositorySelection) obj;
135 if (uri == null ^ other.uri == null)
136 return false;
137 if (uri != null && !uri.equals(other.uri))
138 return false;
140 if (config != other.config)
141 return false;
143 return true;
144 } else
145 return false;