Skip to content

Commit

Permalink
Merge pull request OpenLiberty#637 from vaisakhkannan/OpenLiberty#198-…
Browse files Browse the repository at this point in the history
…Fix-Terminal-Focus

Fixes OpenLiberty#198 -- Add code to shift focus correctly for each terminal
  • Loading branch information
vaisakhkannan authored May 13, 2024
2 parents 946ced0 + 8f7b32a commit 8155133
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation.
* Copyright (c) 2020, 2024 IBM Corporation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -32,7 +32,7 @@ protected void executeLibertyAction(LibertyModule libertyModule) {
Project project = libertyModule.getProject();
VirtualFile buildFile = libertyModule.getBuildFile();
String runTestsCommand = " ";
ShellTerminalWidget widget = getTerminalWidget(false, project, buildFile, getActionCommandName());
ShellTerminalWidget widget = getTerminalWidgetWithFocus(false, project, buildFile, getActionCommandName());
if (widget == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation.
* Copyright (c) 2020, 2024 IBM Corporation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -76,7 +76,7 @@ protected void executeLibertyAction(LibertyModule libertyModule) {
}
}

ShellTerminalWidget widget = getTerminalWidget(true, project, buildFile, getActionCommandName());
ShellTerminalWidget widget = getTerminalWidgetWithFocus(true, project, buildFile, getActionCommandName());
if (widget == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation.
* Copyright (c) 2020, 2024 IBM Corporation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -30,7 +30,7 @@ protected String getActionCommandName() {
protected void executeLibertyAction(LibertyModule libertyModule) {
Project project = libertyModule.getProject();
VirtualFile buildFile = libertyModule.getBuildFile();
ShellTerminalWidget widget = getTerminalWidget(true, project, buildFile, getActionCommandName());
ShellTerminalWidget widget = getTerminalWidgetWithFocus(true, project, buildFile, getActionCommandName());
if (widget == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation.
* Copyright (c) 2020, 2024 IBM Corporation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -31,7 +31,7 @@ protected String getActionCommandName() {
protected void executeLibertyAction(LibertyModule libertyModule) {
Project project = libertyModule.getProject();
VirtualFile buildFile = libertyModule.getBuildFile();
ShellTerminalWidget widget = getTerminalWidget(false, project, buildFile, getActionCommandName());
ShellTerminalWidget widget = getTerminalWidgetWithFocus(false, project, buildFile, getActionCommandName());
String stopCmd = "q";
if (widget == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation.
* Copyright (c) 2020, 2024 IBM Corporation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -27,6 +27,7 @@
import io.openliberty.tools.intellij.util.LocalizedResourceUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.terminal.ShellTerminalWidget;
import org.jetbrains.plugins.terminal.TerminalView;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -170,9 +171,16 @@ protected void notifyError(String errMsg, Project project) {
* @param createWidget create Terminal widget if it does not already exist
* @return ShellTerminalWidget
*/
protected ShellTerminalWidget getTerminalWidget(boolean createWidget, Project project, VirtualFile buildFile, String actionCmd) {
protected ShellTerminalWidget getTerminalWidgetWithFocus(boolean createWidget, Project project, VirtualFile buildFile, String actionCmd) {
LibertyModule libertyModule = LibertyModules.getInstance().getLibertyModule(buildFile);
ShellTerminalWidget widget = LibertyProjectUtil.getTerminalWidget(project, libertyModule, createWidget);
TerminalView terminalView = TerminalView.getInstance(project);
// look for existing terminal tab
ShellTerminalWidget existingWidget = LibertyProjectUtil.getTerminalWidget(libertyModule, terminalView);
// look for creating new terminal tab
ShellTerminalWidget widget = LibertyProjectUtil.getTerminalWidget(project, libertyModule, createWidget, terminalView, existingWidget);
// Set Focus to existing terminal widget
LibertyProjectUtil.setFocusToWidget(project, existingWidget);

// Shows error for actions where terminal widget does not exist or action requires a terminal to already exist and expects "Start" to be running
if (widget == null || (!createWidget && !widget.hasRunningCommands())) {
String msg;
Expand All @@ -185,8 +193,6 @@ protected ShellTerminalWidget getTerminalWidget(boolean createWidget, Project pr
LOGGER.warn(msg);
return null;
}
widget.getComponent().setVisible(true);
widget.getComponent().requestFocus();
return widget;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2022 IBM Corporation.
* Copyright (c) 2020, 2024 IBM Corporation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -13,28 +13,32 @@
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.terminal.JBTerminalWidget;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.sun.istack.Nullable;
import io.openliberty.tools.intellij.LibertyModule;
import io.openliberty.tools.intellij.LibertyModules;
import io.openliberty.tools.intellij.LibertyProjectSettings;
import org.jetbrains.plugins.terminal.ShellTerminalWidget;
import org.jetbrains.plugins.terminal.TerminalToolWindowManager;
import org.jetbrains.plugins.terminal.TerminalView;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class LibertyProjectUtil {
private static Logger LOGGER = Logger.getInstance(LibertyProjectUtil.class);;
private static Logger LOGGER = Logger.getInstance(LibertyProjectUtil.class);

enum BuildFileFilter {
ADDABLE {
Expand Down Expand Up @@ -145,10 +149,7 @@ public static ArrayList<BuildFile> getRemovableMavenBuildFiles(Project project)
* @param createWidget true if a new widget should be created
* @return ShellTerminalWidget or null if it does not exist
*/
public static ShellTerminalWidget getTerminalWidget(Project project, LibertyModule libertyModule, boolean createWidget) {
TerminalView terminalView = TerminalView.getInstance(project);
// look for existing terminal tab
ShellTerminalWidget widget = getTerminalWidget(libertyModule, terminalView);
public static ShellTerminalWidget getTerminalWidget(Project project, LibertyModule libertyModule, boolean createWidget, TerminalView terminalView, ShellTerminalWidget widget) {
if (widget == null && createWidget) {
// create a new terminal tab
ShellTerminalWidget newTerminal = terminalView.createLocalShellWidget(project.getBasePath(), libertyModule.getName(), true);
Expand All @@ -158,6 +159,29 @@ public static ShellTerminalWidget getTerminalWidget(Project project, LibertyModu
return widget;
}

public static void setFocusToWidget(Project project, ShellTerminalWidget widget) {
TerminalToolWindowManager manager = TerminalToolWindowManager.getInstance(project);
ToolWindow toolWindow = manager.getToolWindow();

if (toolWindow != null && widget != null) {
ContentManager contentManager = toolWindow.getContentManager();
Content[] contents = contentManager.getContents();

int index = 0;
for (int i = 0; i < contents.length; i++) {
if (contents[i].getPreferredFocusableComponent().equals(widget)) {
index = i;
break;
}
}
if (contents.length > 0) {
Content terminalContent = contents[index];
contentManager.setSelectedContent(terminalContent);
terminalContent.getComponent().requestFocus();
}
}
}

// returns valid build files for the current project
private static ArrayList<BuildFile> getBuildFiles(Project project, String buildFileType, BuildFileFilter filter) throws ParserConfigurationException, SAXException, IOException {
ArrayList<BuildFile> buildFiles = new ArrayList<BuildFile>();
Expand Down Expand Up @@ -208,7 +232,7 @@ private static boolean isLibertyProject(PsiFile buildFile) {
* @param terminalView
* @return ShellTerminalWidget or null if it does not exist
*/
private static ShellTerminalWidget getTerminalWidget(LibertyModule libertyModule, TerminalView terminalView) {
public static ShellTerminalWidget getTerminalWidget(LibertyModule libertyModule, TerminalView terminalView) {
ShellTerminalWidget widget = libertyModule.getShellWidget();
// check if widget exists in terminal view
if (widget != null) {
Expand Down

0 comments on commit 8155133

Please sign in to comment.