Move the EGitCredentialsProvider to EGit core
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / credentials / EGitCredentialsUI.java
blobd5ffa33e3f210b730ae6e8d42a6faa0ee6846f16
1 /*******************************************************************************
2 * Copyright (C) 2021 Thomas Wolf <thomas.wolf@paranor.ch> and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.credentials;
13 import org.eclipse.egit.core.credentials.CredentialsUI;
14 import org.eclipse.egit.core.credentials.UserPasswordCredentials;
15 import org.eclipse.egit.ui.internal.UIText;
16 import org.eclipse.egit.ui.internal.dialogs.CustomPromptDialog;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.window.Window;
20 import org.eclipse.jgit.transport.CredentialItem;
21 import org.eclipse.jgit.transport.URIish;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.PlatformUI;
24 import org.osgi.service.component.annotations.Component;
26 /**
27 * A basic UI for getting credentials, published as an OSGi service with default
28 * service ranking.
30 @Component(immediate = false)
31 public class EGitCredentialsUI implements CredentialsUI {
33 @Override
34 public boolean fillCredentials(URIish uri, CredentialItem... items) {
35 if (items.length == 0) {
36 return true;
38 boolean[] result = { false };
40 PlatformUI.getWorkbench().getDisplay().syncExec(() -> {
41 Shell shell = PlatformUI.getWorkbench()
42 .getModalDialogShellProvider().getShell();
44 if (items.length == 1) {
45 result[0] = getSingleSpecial(shell, uri, items[0]);
46 } else {
47 result[0] = getMultiSpecial(shell, uri, items);
49 });
51 return result[0];
54 /**
55 * Opens a dialog for a single non-user, non-password type item.
57 * @param shell
58 * the shell to use
59 * @param uri
60 * the uri of the get request
61 * @param item
62 * the item to handle
63 * @return {@code true} if the request was successful and values were
64 * supplied; {@code false} if the user canceled the request and did
65 * not supply all requested values.
67 private boolean getSingleSpecial(Shell shell, URIish uri,
68 CredentialItem item) {
69 if (item instanceof CredentialItem.InformationalMessage) {
70 MessageDialog.openInformation(shell,
71 UIText.EGitCredentialsProvider_information,
72 item.getPromptText());
73 return true;
74 } else if (item instanceof CredentialItem.YesNoType) {
75 CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
76 String[] labels = new String[] { IDialogConstants.YES_LABEL,
77 IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL };
78 int[] resultIDs = new int[] { IDialogConstants.YES_ID,
79 IDialogConstants.NO_ID, IDialogConstants.CANCEL_ID };
81 MessageDialog dialog = new MessageDialog(shell,
82 UIText.EGitCredentialsProvider_question, null,
83 item.getPromptText(), MessageDialog.QUESTION_WITH_CANCEL,
84 labels, 0);
85 int r = dialog.open();
86 if (r < 0) {
87 return false;
90 switch (resultIDs[r]) {
91 case IDialogConstants.YES_ID:
92 v.setValue(true);
93 return true;
94 case IDialogConstants.NO_ID:
95 v.setValue(false);
96 return true;
97 default:
98 // abort
99 return false;
101 } else {
102 // generically handles all other types of items
103 return getMultiSpecial(shell, uri, item);
108 * Opens a generic dialog presenting all CredentialItems to the user.
110 * @param shell
111 * the shell to use
112 * @param uri
113 * the uri of the get request
114 * @param items
115 * the items to handle
116 * @return {@code true} if the request was successful and values were
117 * supplied; {@code false} if the user canceled the request and did
118 * not supply all requested values.
120 private boolean getMultiSpecial(Shell shell, URIish uri,
121 CredentialItem... items) {
122 CustomPromptDialog dialog = new CustomPromptDialog(shell, uri,
123 UIText.EGitCredentialsProvider_information, items);
124 if (dialog.open() == Window.OK) {
125 return true;
127 return false;
130 @Override
131 public UserPasswordCredentials getCredentials(URIish uri) {
132 UserPasswordCredentials[] credentials = { null };
133 PlatformUI.getWorkbench().getDisplay().syncExec(() -> {
134 Shell shell = PlatformUI.getWorkbench()
135 .getModalDialogShellProvider().getShell();
136 credentials[0] = LoginService.login(shell, uri);
138 return credentials[0];