From f42b75939ddc807e17be945f53ec4b134055a717 Mon Sep 17 00:00:00 2001 From: Jordan Ang Date: Tue, 17 Sep 2024 16:02:21 -0700 Subject: [PATCH] Refactored to add AmazonQView parent class containing common setup (#11) --- .../amazonq/views/AmazonQChatWebview.java | 88 +++--------- .../eclipse/amazonq/views/AmazonQView.java | 124 +++++++++++++++++ .../amazonq/views/LoginViewActionHandler.java | 5 +- .../amazonq/views/ToolkitLoginWebview.java | 131 ++---------------- .../views/actions/AmazonQCommonActions.java | 61 ++++++++ .../views/actions/ChangeThemeAction.java | 25 ++++ .../FeedbackDialogContributionItem.java | 48 +++++++ .../amazonq/views/actions/SignoutAction.java | 29 ++++ 8 files changed, 325 insertions(+), 186 deletions(-) create mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQView.java create mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/AmazonQCommonActions.java create mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/ChangeThemeAction.java create mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/FeedbackDialogContributionItem.java create mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/SignoutAction.java diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatWebview.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatWebview.java index 17d322a73..39a35163d 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatWebview.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatWebview.java @@ -3,67 +3,55 @@ package software.aws.toolkits.eclipse.amazonq.views; -import org.eclipse.jface.action.Action; -import org.eclipse.jface.action.IMenuManager; -import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.preference.PreferenceDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.swt.SWT; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.BrowserFunction; -import org.eclipse.swt.graphics.Color; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.IActionBars; -import org.eclipse.ui.ISelectionListener; -import org.eclipse.ui.ISharedImages; -import org.eclipse.ui.IViewSite; import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.PlatformUI; import org.eclipse.ui.dialogs.PreferencesUtil; -import org.eclipse.ui.part.ViewPart; import jakarta.inject.Inject; import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspConstants; +import software.aws.toolkits.eclipse.amazonq.util.AuthUtils; import software.aws.toolkits.eclipse.amazonq.util.PluginUtils; +import software.aws.toolkits.eclipse.amazonq.util.ThreadingUtils; +import software.aws.toolkits.eclipse.amazonq.views.actions.AmazonQCommonActions; -public class AmazonQChatWebview extends ViewPart implements ISelectionListener { +public class AmazonQChatWebview extends AmazonQView { public static final String ID = "software.aws.toolkits.eclipse.amazonq.views.AmazonQChatWebview"; @Inject private Shell shell; private Browser browser; + private AmazonQCommonActions amazonQCommonActions; private final ViewCommandParser commandParser; private final ViewActionHandler actionHandler; - private boolean darkMode = Display.isSystemDarkTheme(); - public AmazonQChatWebview() { + browser = getBrowser(); + amazonQCommonActions = getAmazonQCommonActions(); + this.commandParser = new LoginViewCommandParser(); this.actionHandler = new AmazonQChatViewActionHandler(); } @Override public final void createPartControl(final Composite parent) { - browser = new Browser(parent, SWT.NATIVE); - Display display = Display.getCurrent(); - Color black = display.getSystemColor(SWT.COLOR_BLACK); + setupAmazonQView(parent, true); - browser.setBackground(black); - parent.setBackground(black); - browser.setText(getContent()); + AuthUtils.isLoggedIn().thenAcceptAsync(isLoggedIn -> { + handleAuthStatusChange(isLoggedIn); + }, ThreadingUtils::executeAsyncTask); BrowserFunction prefsFunction = new OpenPreferenceFunction(browser, "openEclipsePreferences", this::openPreferences); browser.addDisposeListener(e -> prefsFunction.dispose()); - createActions(); - contributeToActionBars(getViewSite()); - getSite().getPage().addSelectionListener(this); - new BrowserFunction(browser, "ideCommand") { @Override public Object function(final Object[] arguments) { @@ -74,45 +62,6 @@ public Object function(final Object[] arguments) { }; } - private void contributeToActionBars(final IViewSite viewSite) { - IActionBars bars = viewSite.getActionBars(); - fillLocalPullDown(bars.getMenuManager()); - fillLocalToolBar(bars.getToolBarManager()); - } - - private void fillLocalPullDown(final IMenuManager manager) { - manager.add(changeThemeAction); - } - - private void fillLocalToolBar(final IToolBarManager manager) { - manager.add(changeThemeAction); - } - - private void createActions() { - changeThemeAction = new ChangeThemeAction(); - } - - private Action changeThemeAction; - - private class ChangeThemeAction extends Action { - ChangeThemeAction() { - setText("Change Color"); - setToolTipText("Change the color"); - setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ELCL_SYNCED)); - } - - @Override - public void run() { - darkMode = !darkMode; - browser.execute("changeTheme(" + darkMode + ");"); - } - } - - @Override - public final void setFocus() { - browser.setFocus(); - } - private class OpenPreferenceFunction extends BrowserFunction { private Runnable function; @@ -194,9 +143,16 @@ public final void selectionChanged(final IWorkbenchPart part, final ISelection s } @Override - public final void dispose() { - getSite().getPage().removeSelectionListener(this); - super.dispose(); + protected final void handleAuthStatusChange(final boolean isLoggedIn) { + Display.getDefault().asyncExec(() -> { + amazonQCommonActions.updateActionVisibility(isLoggedIn, getViewSite()); + if (!isLoggedIn) { + browser.setText("Signed Out"); + AmazonQView.showView(ToolkitLoginWebview.ID); + } else { + browser.setText(getContent()); + } + }); } } diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQView.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQView.java new file mode 100644 index 000000000..580216712 --- /dev/null +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQView.java @@ -0,0 +1,124 @@ +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. + +package software.aws.toolkits.eclipse.amazonq.views; + + +import java.util.Set; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.browser.Browser; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.ISelectionListener; +import org.eclipse.ui.IViewReference; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.ViewPart; + +import software.aws.toolkits.eclipse.amazonq.util.AuthStatusChangedListener; +import software.aws.toolkits.eclipse.amazonq.util.AuthUtils; +import software.aws.toolkits.eclipse.amazonq.util.PluginLogger; +import software.aws.toolkits.eclipse.amazonq.views.actions.AmazonQCommonActions; + +public abstract class AmazonQView extends ViewPart implements ISelectionListener { + + private static final Set AMAZON_Q_VIEWS = Set.of( + ToolkitLoginWebview.ID, + AmazonQChatWebview.ID + ); + + private Browser browser; + private AmazonQCommonActions amazonQCommonActions; + private AuthStatusChangedListener authStatusChangedListener; + + public static void showView(final String viewId) { + if (!AMAZON_Q_VIEWS + .contains(viewId)) { + PluginLogger.error("Failed to show view. You must add the view " + viewId + " to AMAZON_Q_VIEWS Set"); + return; + } + + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + if (page != null) { + // Show requested view + try { + page.showView(viewId); + PluginLogger.info("Showing view " + viewId); + } catch (Exception e) { + PluginLogger.error("Error occurred while showing view " + viewId, e); + } + + // Hide all other Amazon Q Views + IViewReference[] viewReferences = page.getViewReferences(); + for (IViewReference viewRef : viewReferences) { + if (AMAZON_Q_VIEWS.contains(viewRef.getId()) && !viewRef.getId().equalsIgnoreCase(viewId)) { + try { + page.hideView(viewRef); + } catch (Exception e) { + PluginLogger.error("Error occurred while hiding view " + viewId, e); + } + } + } + } + } + + public final Browser getBrowser() { + return browser; + } + + public final AmazonQCommonActions getAmazonQCommonActions() { + return amazonQCommonActions; + } + + protected abstract void handleAuthStatusChange(boolean isLoggedIn); + + protected final void setupAmazonQView(final Composite parent, final boolean isLoggedIn) { + setupBrowser(parent); + setupActions(isLoggedIn); + setupAuthStatusListeners(); + setupSelectionListener(); + } + + private void setupBrowser(final Composite parent) { + browser = new Browser(parent, SWT.NATIVE); + Display display = Display.getCurrent(); + Color black = display.getSystemColor(SWT.COLOR_BLACK); + + browser.setBackground(black); + parent.setBackground(black); + } + + private void setupActions(final boolean isLoggedIn) { + amazonQCommonActions = new AmazonQCommonActions(isLoggedIn, getViewSite()); + } + + private void setupAuthStatusListeners() { + authStatusChangedListener = this::handleAuthStatusChange; + AuthUtils.addAuthStatusChangeListener(amazonQCommonActions.getSignoutAction()); + AuthUtils.addAuthStatusChangeListener(amazonQCommonActions.getFeedbackDialogContributionAction()); + } + + private void setupSelectionListener() { + getSite().getPage().addSelectionListener(this); + } + + @Override + public final void setFocus() { + browser.setFocus(); + } + + /** + * Disposes of the resources associated with this view. + * + * This method is called when the view is closed. It removes the authentication + * status change listener and the selection listener from the page. + */ + @Override + public void dispose() { + AuthUtils.removeAuthStatusChangeListener(authStatusChangedListener); + getSite().getPage().removeSelectionListener(this); + super.dispose(); + } + +} diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/LoginViewActionHandler.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/LoginViewActionHandler.java index 1616e1ea0..22304ddea 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/LoginViewActionHandler.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/LoginViewActionHandler.java @@ -21,7 +21,10 @@ public final void handleCommand(final ParsedCommand parsedCommand, final Browser ThreadingUtils.executeAsyncTask(() -> { try { AuthUtils.signIn().get(); - Display.getDefault().asyncExec(() -> browser.setText("Login succeeded")); + Display.getDefault().asyncExec(() -> { + browser.setText("Login succeeded"); + AmazonQView.showView(AmazonQChatWebview.ID); + }); } catch (Exception e) { PluginLogger.error("Failed to update token", e); } diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/ToolkitLoginWebview.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/ToolkitLoginWebview.java index 434c3bf8e..fa7564770 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/ToolkitLoginWebview.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/ToolkitLoginWebview.java @@ -6,77 +6,53 @@ import java.io.IOException; import java.net.URL; -import jakarta.inject.Inject; -import org.eclipse.jface.action.Action; -import org.eclipse.jface.action.ContributionItem; -import org.eclipse.jface.action.IMenuManager; -import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.preference.PreferenceDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.swt.SWT; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.BrowserFunction; -import org.eclipse.swt.graphics.Color; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.IActionBars; -import org.eclipse.ui.ISelectionListener; -import org.eclipse.ui.ISharedImages; -import org.eclipse.ui.IViewSite; import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.PlatformUI; import org.eclipse.ui.dialogs.PreferencesUtil; -import org.eclipse.ui.part.ViewPart; -import software.aws.toolkits.eclipse.amazonq.util.AuthStatusChangedListener; +import jakarta.inject.Inject; import software.aws.toolkits.eclipse.amazonq.util.AuthUtils; import software.aws.toolkits.eclipse.amazonq.util.PluginUtils; import software.aws.toolkits.eclipse.amazonq.util.ThreadingUtils; +import software.aws.toolkits.eclipse.amazonq.views.actions.AmazonQCommonActions; -public class ToolkitLoginWebview extends ViewPart implements ISelectionListener { +public final class ToolkitLoginWebview extends AmazonQView { public static final String ID = "software.aws.toolkits.eclipse.amazonq.views.ToolkitLoginWebview"; - private static final String SHARE_FEEDBACK_MENU_ITEM_TEXT = "Share Feedback"; @Inject private Shell shell; private Browser browser; - private AuthStatusChangedListener authStatusChangedListener; + private AmazonQCommonActions amazonQCommonActions; private final ViewCommandParser commandParser; private final ViewActionHandler actionHandler; - private boolean darkMode = Display.isSystemDarkTheme(); - public ToolkitLoginWebview() { + browser = getBrowser(); + amazonQCommonActions = getAmazonQCommonActions(); this.commandParser = new LoginViewCommandParser(); this.actionHandler = new LoginViewActionHandler(); } @Override - public final void createPartControl(final Composite parent) { - browser = new Browser(parent, SWT.NATIVE); - Display display = Display.getCurrent(); - Color black = display.getSystemColor(SWT.COLOR_BLACK); - - browser.setBackground(black); - parent.setBackground(black); + public void createPartControl(final Composite parent) { + setupAmazonQView(parent, true); AuthUtils.isLoggedIn().thenAcceptAsync(isLoggedIn -> { handleAuthStatusChange(isLoggedIn); }, ThreadingUtils::executeAsyncTask); - createActions(true); BrowserFunction prefsFunction = new OpenPreferenceFunction(browser, "openEclipsePreferences", this::openPreferences); browser.addDisposeListener(e -> prefsFunction.dispose()); - contributeToActionBars(getViewSite()); - getSite().getPage().addSelectionListener(this); - AuthUtils.addAuthStatusChangeListener(this::updateFeedbackContributionItemVisibility); - AuthUtils.addAuthStatusChangeListener(this::updateSignoutActionVisibility); - authStatusChangedListener = this::handleAuthStatusChange; new BrowserFunction(browser, ViewConstants.COMMAND_FUNCTION_NAME) { @Override @@ -88,94 +64,18 @@ public Object function(final Object[] arguments) { }; } - private void contributeToActionBars(final IViewSite viewSite) { - IActionBars bars = viewSite.getActionBars(); - fillLocalPullDown(bars.getMenuManager()); - fillLocalToolBar(bars.getToolBarManager()); - } - - private void fillLocalPullDown(final IMenuManager manager) { - manager.add(changeThemeAction); - manager.add(feedbackDialogContributionItem); - manager.add(signoutAction); - } - - private void fillLocalToolBar(final IToolBarManager manager) { - manager.add(changeThemeAction); - } - - private void createActions(final boolean isLoggedIn) { - changeThemeAction = new ChangeThemeAction(); - signoutAction = new SignoutAction(); - updateSignoutActionVisibility(isLoggedIn); - feedbackDialogContributionItem = new DialogContributionItem( - new FeedbackDialog(shell), - SHARE_FEEDBACK_MENU_ITEM_TEXT, - PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_LCL_LINKTO_HELP) - ); - updateFeedbackContributionItemVisibility(isLoggedIn); - } - - private Action changeThemeAction; - private Action signoutAction; - private ContributionItem feedbackDialogContributionItem; - - private class ChangeThemeAction extends Action { - ChangeThemeAction() { - setText("Change Color"); - setToolTipText("Change the color"); - setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ELCL_SYNCED)); - } - - @Override - public void run() { - darkMode = !darkMode; - browser.execute("changeTheme(" + darkMode + ");"); - } - } - - private class SignoutAction extends Action { - SignoutAction() { - setText("Sign out"); - } - - @Override - public void run() { - AuthUtils.invalidateToken(); - Display.getDefault().asyncExec(() -> { - browser.setText(getContent()); - }); - } - } - - private void handleAuthStatusChange(final boolean isLoggedIn) { + protected void handleAuthStatusChange(final boolean isLoggedIn) { Display.getDefault().asyncExec(() -> { - updateSignoutActionVisibility(isLoggedIn); - updateFeedbackContributionItemVisibility(isLoggedIn); + amazonQCommonActions.updateActionVisibility(isLoggedIn, getViewSite()); if (!isLoggedIn) { browser.setText(getContent()); } else { browser.setText("Signed in"); + AmazonQView.showView(AmazonQChatWebview.ID); } }); } - private void updateSignoutActionVisibility(final boolean isLoggedIn) { - signoutAction.setEnabled(isLoggedIn); - } - - private void updateFeedbackContributionItemVisibility(final boolean isLoggedIn) { - feedbackDialogContributionItem.setVisible(isLoggedIn); - Display.getDefault().asyncExec(() -> { - getViewSite().getActionBars().getMenuManager().markDirty(); - getViewSite().getActionBars().getMenuManager().update(true); - }); - } - - @Override - public final void setFocus() { - browser.setFocus(); - } private class OpenPreferenceFunction extends BrowserFunction { private Runnable function; @@ -226,7 +126,7 @@ private String getContent() { } @Override - public final void selectionChanged(final IWorkbenchPart part, final ISelection selection) { + public void selectionChanged(final IWorkbenchPart part, final ISelection selection) { if (selection.isEmpty()) { return; } @@ -237,11 +137,4 @@ public final void selectionChanged(final IWorkbenchPart part, final ISelection s browser.execute("setSelection(\"Something was selected in part " + part.getTitle() + "\");"); } } - - @Override - public final void dispose() { - AuthUtils.removeAuthStatusChangeListener(authStatusChangedListener); - getSite().getPage().removeSelectionListener(this); - super.dispose(); - } } diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/AmazonQCommonActions.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/AmazonQCommonActions.java new file mode 100644 index 000000000..b3dd476ce --- /dev/null +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/AmazonQCommonActions.java @@ -0,0 +1,61 @@ +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. + +package software.aws.toolkits.eclipse.amazonq.views.actions; + +import org.eclipse.jface.action.IMenuManager; +import org.eclipse.jface.action.IToolBarManager; +import org.eclipse.ui.IActionBars; +import org.eclipse.ui.IViewSite; + +public final class AmazonQCommonActions { + + private ChangeThemeAction changeThemeAction; + private SignoutAction signoutAction; + private FeedbackDialogContributionItem feedbackDialogContributionItem; + + public AmazonQCommonActions(final boolean isLoggedIn, final IViewSite viewSite) { + createActions(isLoggedIn, viewSite); + contributeToActionBars(viewSite); + updateActionVisibility(isLoggedIn, viewSite); + } + + public ChangeThemeAction getChangeThemeAction() { + return changeThemeAction; + } + + public SignoutAction getSignoutAction() { + return signoutAction; + } + + public FeedbackDialogContributionItem getFeedbackDialogContributionAction() { + return feedbackDialogContributionItem; + } + + private void createActions(final boolean isLoggedIn, final IViewSite viewSite) { + changeThemeAction = new ChangeThemeAction(); + signoutAction = new SignoutAction(); + feedbackDialogContributionItem = new FeedbackDialogContributionItem(viewSite); + } + + private void contributeToActionBars(final IViewSite viewSite) { + IActionBars bars = viewSite.getActionBars(); + fillLocalPullDown(bars.getMenuManager()); + fillLocalToolBar(bars.getToolBarManager()); + } + + private void fillLocalPullDown(final IMenuManager manager) { + manager.add(changeThemeAction); + manager.add(feedbackDialogContributionItem.getDialogContributionItem()); + manager.add(signoutAction); + } + + private void fillLocalToolBar(final IToolBarManager manager) { + manager.add(changeThemeAction); + } + + public void updateActionVisibility(final boolean isLoggedIn, final IViewSite viewSite) { + signoutAction.updateVisibility(isLoggedIn); + feedbackDialogContributionItem.updateVisibility(isLoggedIn); + } + +} diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/ChangeThemeAction.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/ChangeThemeAction.java new file mode 100644 index 000000000..e02bc85ef --- /dev/null +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/ChangeThemeAction.java @@ -0,0 +1,25 @@ +package software.aws.toolkits.eclipse.amazonq.views.actions; + +import org.eclipse.jface.action.Action; +import org.eclipse.swt.browser.Browser; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; + +public final class ChangeThemeAction extends Action { + private Browser browser; + private boolean darkMode = Display.isSystemDarkTheme(); + + public ChangeThemeAction() { + setText("Change Color"); + setToolTipText("Change the color"); + setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ELCL_SYNCED)); + } + + @Override + public void run() { + darkMode = !darkMode; + browser.execute("changeTheme(" + darkMode + ");"); + } + +} diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/FeedbackDialogContributionItem.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/FeedbackDialogContributionItem.java new file mode 100644 index 000000000..ac9691fc9 --- /dev/null +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/FeedbackDialogContributionItem.java @@ -0,0 +1,48 @@ +package software.aws.toolkits.eclipse.amazonq.views.actions; + +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.IViewSite; +import org.eclipse.ui.PlatformUI; + +import jakarta.inject.Inject; +import software.aws.toolkits.eclipse.amazonq.util.AuthStatusChangedListener; +import software.aws.toolkits.eclipse.amazonq.views.DialogContributionItem; +import software.aws.toolkits.eclipse.amazonq.views.FeedbackDialog; + +public final class FeedbackDialogContributionItem implements AuthStatusChangedListener { + private static final String SHARE_FEEDBACK_MENU_ITEM_TEXT = "Share Feedback"; + + @Inject + private Shell shell; + private IViewSite viewSite; + + private DialogContributionItem feedbackDialogContributionItem; + + public FeedbackDialogContributionItem(final IViewSite viewSite) { + this.viewSite = viewSite; + feedbackDialogContributionItem = new DialogContributionItem( + new FeedbackDialog(shell), + SHARE_FEEDBACK_MENU_ITEM_TEXT, + PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_LCL_LINKTO_HELP) + ); + } + + public void updateVisibility(final boolean isLoggedIn) { + feedbackDialogContributionItem.setVisible(isLoggedIn); + Display.getDefault().asyncExec(() -> { + viewSite.getActionBars().getMenuManager().markDirty(); + viewSite.getActionBars().getMenuManager().update(true); + }); + } + + public DialogContributionItem getDialogContributionItem() { + return feedbackDialogContributionItem; + } + + @Override + public void onAuthStatusChanged(final boolean isLoggedIn) { + updateVisibility(isLoggedIn); + } +} diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/SignoutAction.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/SignoutAction.java new file mode 100644 index 000000000..16aa49dd1 --- /dev/null +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/actions/SignoutAction.java @@ -0,0 +1,29 @@ +package software.aws.toolkits.eclipse.amazonq.views.actions; + +import org.eclipse.jface.action.Action; + +import software.aws.toolkits.eclipse.amazonq.util.AuthStatusChangedListener; +import software.aws.toolkits.eclipse.amazonq.util.AuthUtils; +import software.aws.toolkits.eclipse.amazonq.views.AmazonQView; +import software.aws.toolkits.eclipse.amazonq.views.ToolkitLoginWebview; + +public final class SignoutAction extends Action implements AuthStatusChangedListener { + public SignoutAction() { + setText("Sign out"); + } + + @Override + public void run() { + AuthUtils.invalidateToken(); + AmazonQView.showView(ToolkitLoginWebview.ID); + } + + public void updateVisibility(final boolean isLoggedIn) { + this.setEnabled(isLoggedIn); + } + + @Override + public void onAuthStatusChanged(final boolean isLoggedIn) { + updateVisibility(isLoggedIn); + } +}