From 233de0a5ff63761e19aa41357dc89d52759b8562 Mon Sep 17 00:00:00 2001 From: Hans Van Akelyen Date: Tue, 15 Oct 2024 15:51:16 +0200 Subject: [PATCH] Git improvements, #2634 - Show branch name in file explorer - Give a message when authentication fails - Give a message when Pull was successful - Give a message when revert was successful --- .../java/org/apache/hop/git/GitGuiPlugin.java | 18 +- .../java/org/apache/hop/git/model/UIGit.java | 8 + .../java/org/apache/hop/git/model/VCS.java | 22 +-- .../git/messages/messages_en_US.properties | 4 + .../dialog/EnterUsernamePasswordDialog.java | 163 ++++++++++++++++++ .../explorer/ExplorerPerspective.java | 4 + 6 files changed, 201 insertions(+), 18 deletions(-) create mode 100644 ui/src/main/java/org/apache/hop/ui/core/dialog/EnterUsernamePasswordDialog.java diff --git a/plugins/misc/git/src/main/java/org/apache/hop/git/GitGuiPlugin.java b/plugins/misc/git/src/main/java/org/apache/hop/git/GitGuiPlugin.java index 8ec07dbfbd1..601c0a0b7f9 100644 --- a/plugins/misc/git/src/main/java/org/apache/hop/git/GitGuiPlugin.java +++ b/plugins/misc/git/src/main/java/org/apache/hop/git/GitGuiPlugin.java @@ -265,6 +265,13 @@ public void gitPull() { try { git.pull(); ExplorerPerspective.getInstance().refresh(); + MessageBox pullSuccessful = + new MessageBox(HopGui.getInstance().getShell(), SWT.ICON_INFORMATION); + pullSuccessful.setText( + BaseMessages.getString(PKG, "GitGuiPlugin.Dialog.PullSuccessful.Header")); + pullSuccessful.setMessage( + BaseMessages.getString(PKG, "GitGuiPlugin.Dialog.PullSuccessful.Message")); + pullSuccessful.open(); } catch (Exception e) { new ErrorDialog( HopGui.getInstance().getShell(), @@ -361,6 +368,11 @@ public void gitRevert() { git.revertPath(file); } } + MessageBox box = + new MessageBox(HopGui.getInstance().getShell(), SWT.OK | SWT.ICON_INFORMATION); + box.setText(BaseMessages.getString(PKG, "GitGuiPlugin.Dialog.FilesReverted.Header")); + box.setMessage(BaseMessages.getString(PKG, "GitGuiPlugin.Dialog.FilesReverted.Message")); + box.open(); } } catch (Exception e) { new ErrorDialog( @@ -408,7 +420,9 @@ public void rootChanged(String rootFolder, String rootName) { if (gitConfig.exists()) { git = new UIGit(); git.openRepo(rootFolder); - LogChannel.UI.logBasic("Found git project for: " + rootFolder); + ExplorerPerspective.getInstance().setRootName(rootName + " (" + git.getBranch() + ")"); + } else { + git = null; } } catch (Exception e) { // This is not a git project... @@ -525,7 +539,7 @@ public void filePainted(Tree tree, TreeItem treeItem, String path, String name) treeItem.setForeground(colorStaged); break; case ADD: - if (file.getIsStaged()) { + if (Boolean.TRUE.equals(file.getIsStaged())) { treeItem.setForeground(colorStaged); } else { treeItem.setForeground(colorUnstaged); diff --git a/plugins/misc/git/src/main/java/org/apache/hop/git/model/UIGit.java b/plugins/misc/git/src/main/java/org/apache/hop/git/model/UIGit.java index 4c3e0792b22..1bd51073f31 100644 --- a/plugins/misc/git/src/main/java/org/apache/hop/git/model/UIGit.java +++ b/plugins/misc/git/src/main/java/org/apache/hop/git/model/UIGit.java @@ -42,6 +42,7 @@ import org.apache.hop.git.model.revision.ObjectRevision; import org.apache.hop.i18n.BaseMessages; import org.apache.hop.ui.core.dialog.EnterSelectionDialog; +import org.apache.hop.ui.core.dialog.ErrorDialog; import org.apache.hop.ui.hopgui.HopGui; import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.DiffCommand; @@ -523,6 +524,13 @@ public boolean pull() throws HopException { CONST_AUTHENTICATION_IS_REQUIRED_BUT_NO_CREDENTIALS_PROVIDER_HAS_BEEN_REGISTERED) || e.getMessage() .contains(CONST_NOT_AUTHORIZED)) { // when the cached credential does not work + if (e.getMessage().contains(CONST_NOT_AUTHORIZED)) { + new ErrorDialog( + HopGui.getInstance().getShell(), + "Git Error", + "Error Authenticating to Git service", + e); + } if (promptUsernamePassword()) { return pull(); } diff --git a/plugins/misc/git/src/main/java/org/apache/hop/git/model/VCS.java b/plugins/misc/git/src/main/java/org/apache/hop/git/model/VCS.java index 2f7fffc650b..4145a179a4d 100644 --- a/plugins/misc/git/src/main/java/org/apache/hop/git/model/VCS.java +++ b/plugins/misc/git/src/main/java/org/apache/hop/git/model/VCS.java @@ -17,7 +17,7 @@ package org.apache.hop.git.model; -import org.apache.hop.ui.core.dialog.EnterStringDialog; +import org.apache.hop.ui.core.dialog.EnterUsernamePasswordDialog; import org.apache.hop.ui.core.dialog.MessageBox; import org.apache.hop.ui.hopgui.HopGui; import org.eclipse.swt.SWT; @@ -47,24 +47,14 @@ void showMessageBox(String title, String message) { * @return true on success */ protected boolean promptUsernamePassword() { - EnterStringDialog userDialog = - new EnterStringDialog( - HopGui.getInstance().getShell(), "", "Username?", "Enter the git username to use"); - String username = userDialog.open(); - if (username == null) { + EnterUsernamePasswordDialog userDialog = + new EnterUsernamePasswordDialog(HopGui.getInstance().getShell()); + String[] usernamePassword = userDialog.open(); + if (usernamePassword == null) { return false; } - EnterStringDialog passDialog = - new EnterStringDialog( - HopGui.getInstance().getShell(), "", "Password?", "Enter the git password to use"); - passDialog.setEchoChar('*'); - String password = passDialog.open(); - if (password == null) { - return false; - } - - setCredential(username, password); + setCredential(usernamePassword[0], usernamePassword[1]); return true; } diff --git a/plugins/misc/git/src/main/resources/org/apache/hop/git/messages/messages_en_US.properties b/plugins/misc/git/src/main/resources/org/apache/hop/git/messages/messages_en_US.properties index b3ed8d6ae89..7573a2a5589 100644 --- a/plugins/misc/git/src/main/resources/org/apache/hop/git/messages/messages_en_US.properties +++ b/plugins/misc/git/src/main/resources/org/apache/hop/git/messages/messages_en_US.properties @@ -36,6 +36,10 @@ GitGuiPlugin.Dialog.RevertFiles.Header=Select files to revert GitGuiPlugin.Dialog.RevertFiles.Message=Please select the files to revert GitGuiPlugin.Dialog.SelectFilesToCommit.Header=Commit Message GitGuiPlugin.Dialog.SelectFilesToCommit.Message=Please enter a commit message: +GitGuiPlugin.Dialog.PullSuccessful.Header=Successful Pull +GitGuiPlugin.Dialog.PullSuccessful.Message=Successful Pull +GitGuiPlugin.Dialog.FilesReverted.Header=Files Reverted +GitGuiPlugin.Dialog.FilesReverted.Message=Files Reverted GitGuiPlugin.Info.Label=Git Info: {0} GitGuiPlugin.Menu.Add.Text=Git Add GitGuiPlugin.Menu.Commit.Text=Git Commit diff --git a/ui/src/main/java/org/apache/hop/ui/core/dialog/EnterUsernamePasswordDialog.java b/ui/src/main/java/org/apache/hop/ui/core/dialog/EnterUsernamePasswordDialog.java new file mode 100644 index 00000000000..7b2221347a0 --- /dev/null +++ b/ui/src/main/java/org/apache/hop/ui/core/dialog/EnterUsernamePasswordDialog.java @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hop.ui.core.dialog; + +import org.apache.hop.i18n.BaseMessages; +import org.apache.hop.ui.core.PropsUi; +import org.apache.hop.ui.core.gui.GuiResource; +import org.apache.hop.ui.core.gui.WindowProperty; +import org.apache.hop.ui.pipeline.transform.BaseTransformDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.FormAttachment; +import org.eclipse.swt.layout.FormData; +import org.eclipse.swt.layout.FormLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Dialog; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +/** This dialog allows you to enter a (single line) String. */ +public class EnterUsernamePasswordDialog extends Dialog { + private static final Class PKG = EnterUsernamePasswordDialog.class; + + private Text wUsername; + private Text wPassword; + + private Button wOk; + + private Shell shell; + + private PropsUi props; + + private boolean mandatory; + + String[] returnValues; + + /** + * Constructs with the ability to use environmental variable substitution. + * + * @param parent Parent gui object + */ + public EnterUsernamePasswordDialog(Shell parent) { + super(parent, SWT.NONE); + this.props = PropsUi.getInstance(); + } + + public String[] open() { + Shell parent = getParent(); + Control lastControl; + + shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL | SWT.SHEET); + PropsUi.setLook(shell); + + FormLayout formLayout = new FormLayout(); + formLayout.marginWidth = PropsUi.getFormMargin(); + formLayout.marginHeight = PropsUi.getFormMargin(); + + shell.setLayout(formLayout); + shell.setImage(GuiResource.getInstance().getImageHopUi()); + shell.setText("Username/Password"); + + int middle = props.getMiddlePct(); + int margin = PropsUi.getMargin(); + + // The Username line... + Label wlUsername = new Label(shell, SWT.RIGHT); + wlUsername.setText("Username:"); + PropsUi.setLook(wlUsername); + FormData fdlUsername = new FormData(); + fdlUsername.left = new FormAttachment(0, 0); + fdlUsername.top = new FormAttachment(0, 0); + fdlUsername.right = new FormAttachment(middle, -margin); + wlUsername.setLayoutData(fdlUsername); + wUsername = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); + PropsUi.setLook(wUsername); + + FormData fdUsername = new FormData(); + fdUsername.left = new FormAttachment(wlUsername, 0); + fdUsername.top = new FormAttachment(0, 0); + fdUsername.right = new FormAttachment(100, -margin); + wUsername.setLayoutData(fdUsername); + + // The Password line... + Label wlPassword = new Label(shell, SWT.RIGHT); + wlPassword.setText("Password:"); + PropsUi.setLook(wlPassword); + FormData fdlPassword = new FormData(); + fdlPassword.left = new FormAttachment(0, 0); + fdlPassword.top = new FormAttachment(wlUsername, margin * 2); + fdlPassword.right = new FormAttachment(middle, -margin); + wlPassword.setLayoutData(fdlPassword); + wPassword = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER | SWT.PASSWORD); + PropsUi.setLook(wPassword); + lastControl = wPassword; + + FormData fdPassword = new FormData(); + fdPassword.left = new FormAttachment(wlPassword, 0); + fdPassword.top = new FormAttachment(wlUsername, margin * 2); + fdPassword.right = new FormAttachment(100, -margin); + wPassword.setLayoutData(fdPassword); + + // Some buttons + wOk = new Button(shell, SWT.PUSH); + wOk.setText(BaseMessages.getString(PKG, "System.Button.OK")); + Button wCancel = new Button(shell, SWT.PUSH); + wCancel.setText(BaseMessages.getString(PKG, "System.Button.Cancel")); + + BaseTransformDialog.positionBottomButtons( + shell, new Button[] {wOk, wCancel}, margin, lastControl); + + // Add listeners + wOk.addListener(SWT.Selection, e -> ok()); + wCancel.addListener(SWT.Selection, e -> cancel()); + + BaseDialog.defaultShellHandling(shell, c -> ok(), c -> cancel()); + + return returnValues; + } + + public void dispose() { + props.setScreen(new WindowProperty(shell)); + shell.dispose(); + } + + private void cancel() { + dispose(); + } + + private void ok() { + returnValues = new String[] {wUsername.getText(), wPassword.getText()}; + dispose(); + } + + /** + * @return the mandatory + */ + public boolean isMandatory() { + return mandatory; + } + + /** + * @param mandatory the manditory to set + */ + public void setMandatory(boolean mandatory) { + this.mandatory = mandatory; + } +} diff --git a/ui/src/main/java/org/apache/hop/ui/hopgui/perspective/explorer/ExplorerPerspective.java b/ui/src/main/java/org/apache/hop/ui/hopgui/perspective/explorer/ExplorerPerspective.java index dda7b1e77d9..161e260acf3 100644 --- a/ui/src/main/java/org/apache/hop/ui/hopgui/perspective/explorer/ExplorerPerspective.java +++ b/ui/src/main/java/org/apache/hop/ui/hopgui/perspective/explorer/ExplorerPerspective.java @@ -1659,4 +1659,8 @@ public List getSelectionListeners() { public Tree getTree() { return tree; } + + public void setRootName(String rootName) { + this.rootName = rootName; + } }