Git Repositories View: Allow to "remove" multiple Repositories
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / repository / ConfigureUriPage.java
blobbfd83c1d3bd8b9bb86c95dfa97048edee49c6176
1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
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
8 * Contributors:
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.repository;
13 import java.net.URISyntaxException;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.eclipse.egit.ui.UIText;
18 import org.eclipse.jface.layout.GridDataFactory;
19 import org.eclipse.jface.viewers.BaseLabelProvider;
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
21 import org.eclipse.jface.viewers.IStructuredContentProvider;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.ITableLabelProvider;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.TableViewer;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.jface.wizard.WizardDialog;
29 import org.eclipse.jface.wizard.WizardPage;
30 import org.eclipse.jgit.transport.RemoteConfig;
31 import org.eclipse.jgit.transport.URIish;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.swt.widgets.ToolBar;
42 import org.eclipse.swt.widgets.ToolItem;
44 /**
47 public class ConfigureUriPage extends WizardPage {
49 private final boolean myFetchMode;
51 Text uriText;
53 TableViewer tv;
55 private URIish myUri;
57 private final List<URIish> myUris = new ArrayList<URIish>();
59 private final RemoteConfig myConfig;
61 /**
62 * @param fetchMode
64 public ConfigureUriPage(boolean fetchMode) {
65 this(fetchMode, null);
68 /**
69 * @param fetchMode
70 * @param remoteConfig
73 public ConfigureUriPage(boolean fetchMode, RemoteConfig remoteConfig) {
75 super(ConfigureUriPage.class.getName());
77 myFetchMode = fetchMode;
78 myConfig = remoteConfig;
79 // myRepository = repository;
81 if (fetchMode)
82 setTitle(UIText.ConfigureUriPage_ConfigureFetch_pagetitle);
83 else
84 setTitle(UIText.ConfigureUriPage_ConfigurePush_pagetitle);
87 public void createControl(Composite parent) {
89 Composite main = new Composite(parent, SWT.NONE);
90 GridDataFactory.fillDefaults().grab(true, true).applyTo(main);
92 if (myFetchMode) {
93 main.setLayout(new GridLayout(3, false));
94 // we only use the first URI
95 Label uriLabel = new Label(main, SWT.NONE);
96 uriLabel.setText(UIText.ConfigureUriPage_FetchUri_label);
97 uriText = new Text(main, SWT.BORDER);
98 // manual entry is dangerous, as the validate may wait forever
99 uriText.setEnabled(false);
101 Button change = new Button(main, SWT.PUSH);
102 change.setText(UIText.ConfigureUriPage_Change_button);
103 change.addSelectionListener(new SelectionAdapter() {
105 @Override
106 public void widgetSelected(SelectionEvent e) {
107 SelectUriWiazrd slwz = new SelectUriWiazrd(false, uriText
108 .getText());
109 WizardDialog dlg = new WizardDialog(getShell(), slwz);
110 if (dlg.open() == Window.OK) {
111 URIish uri = slwz.getUri();
112 uriText.setText(uri.toPrivateString());
113 checkPage();
119 if (myConfig != null && !myConfig.getURIs().isEmpty()) {
121 uriText.setText(myConfig.getURIs().get(0).toPrivateString());
122 checkPage();
123 } else {
124 setPageComplete(false);
127 GridDataFactory.fillDefaults().grab(true, false).applyTo(uriText);
129 } else {
130 main.setLayout(new GridLayout(1, false));
131 tv = new TableViewer(main);
133 GridDataFactory.fillDefaults().grab(true, true).applyTo(
134 tv.getTable());
136 tv.setLabelProvider(new LabelProvider());
137 tv.setContentProvider(new ContentProvider());
139 ToolBar tb = new ToolBar(main, SWT.HORIZONTAL);
140 ToolItem add = new ToolItem(tb, SWT.PUSH);
141 add.setText(UIText.ConfigureUriPage_Add_button);
143 add.addSelectionListener(new SelectionAdapter() {
145 @Override
146 public void widgetSelected(SelectionEvent e) {
147 SelectUriWiazrd slwz = new SelectUriWiazrd(false);
148 WizardDialog dlg = new WizardDialog(getShell(), slwz);
149 if (dlg.open() == Window.OK) {
150 URIish uri = slwz.getUri();
151 if (!myUris.contains(uri))
152 myUris.add(uri);
153 tv.setInput(myUris);
154 checkPage();
160 final ToolItem remove = new ToolItem(tb, SWT.PUSH);
161 remove.setText(UIText.ConfigureUriPage_Remove_button);
162 remove.setEnabled(false);
164 remove.addSelectionListener(new SelectionAdapter() {
166 @Override
167 public void widgetSelected(SelectionEvent e) {
168 for (Object o : ((IStructuredSelection) tv.getSelection())
169 .toArray())
170 myUris.remove(o);
171 tv.setInput(myUris);
172 checkPage();
177 tv.addSelectionChangedListener(new ISelectionChangedListener() {
179 public void selectionChanged(SelectionChangedEvent event) {
180 remove.setEnabled(!tv.getSelection().isEmpty());
185 if (myConfig != null && !myConfig.getPushURIs().isEmpty()) {
187 for (URIish uri : myConfig.getPushURIs())
188 myUris.add(uri);
189 tv.setInput(myUris);
190 checkPage();
191 } else {
192 setPageComplete(false);
196 setControl(main);
200 private void checkPage() {
201 try {
202 setErrorMessage(null);
204 if (myFetchMode) {
205 if (uriText.getText().equals("")) { //$NON-NLS-1$
206 setErrorMessage(UIText.ConfigureUriPage_MissingUri_message);
207 return;
209 try {
210 myUri = new URIish(uriText.getText());
211 } catch (URISyntaxException e) {
212 setErrorMessage(UIText.ConfigureUriPage_ParsingProblem_message);
213 return;
216 } else {
217 if (myUris.isEmpty()) {
218 setErrorMessage(UIText.ConfigureUriPage_MissingUris_message);
219 return;
223 } finally {
224 setPageComplete(getErrorMessage() == null);
228 private static final class ContentProvider implements
229 IStructuredContentProvider {
231 public Object[] getElements(Object inputElement) {
232 return ((List) inputElement).toArray();
235 public void dispose() {
236 // nothing
239 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
240 // nothing
245 private static final class LabelProvider extends BaseLabelProvider
246 implements ITableLabelProvider {
248 public Image getColumnImage(Object element, int columnIndex) {
249 return null;
252 public String getColumnText(Object element, int columnIndex) {
253 return ((URIish) element).toPrivateString();
259 * @return the URI
261 public URIish getUri() {
262 if (myFetchMode) {
263 return myUri;
265 throw new IllegalStateException();
269 * @return the URI
271 public List<URIish> getUris() {
272 if (myFetchMode) {
273 throw new IllegalStateException();
275 return myUris;