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
;
27 * A basic UI for getting credentials, published as an OSGi service with default
30 @Component(immediate
= false)
31 public class EGitCredentialsUI
implements CredentialsUI
{
34 public boolean fillCredentials(URIish uri
, CredentialItem
... items
) {
35 if (items
.length
== 0) {
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]);
47 result
[0] = getMultiSpecial(shell
, uri
, items
);
55 * Opens a dialog for a single non-user, non-password type item.
60 * the uri of the get request
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());
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
,
85 int r
= dialog
.open();
90 switch (resultIDs
[r
]) {
91 case IDialogConstants
.YES_ID
:
94 case IDialogConstants
.NO_ID
:
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.
113 * the uri of the get request
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
) {
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];