Skip to content

Commit

Permalink
feat: improve extension (de)selection in Quarkus wizard
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Jul 2, 2023
1 parent 561ae2d commit 459399e
Showing 1 changed file with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.redhat.devtools.intellij.quarkus.QuarkusConstants;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +40,7 @@
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
Expand Down Expand Up @@ -204,6 +206,7 @@ public void hyperlinkUpdate(HyperlinkEvent e) {
}
}
});

extensionsTree.addCheckboxTreeListener(new CheckboxTreeListener() {
@Override
public void nodeStateChanged(@NotNull CheckedTreeNode node) {
Expand All @@ -220,6 +223,53 @@ public void nodeStateChanged(@NotNull CheckedTreeNode node) {
selectedExtensions.setModel(new SelectedExtensionsModel(categories));
}
});

//(Un)Check extension on double-click
extensionsTree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
TreePath path = extensionsTree.getPathForLocation(e.getX(), e.getY());
if (path != null && path.getLastPathComponent() instanceof CheckedTreeNode) {
var treeNode = (CheckedTreeNode) path.getLastPathComponent();
extensionsTree.setNodeState(treeNode, !treeNode.isChecked());
}
}
}
});

//Unselect extensions on double-click
selectedExtensions.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int selectedIndex = selectedExtensions.getSelectedIndex();
if (selectedIndex > -1) {
QuarkusExtension extension = selectedExtensions.getModel().getElementAt(selectedIndex);
CheckedTreeNode treeNode = findTreeNodeForExtension(root, extension);
if (treeNode != null) {
extensionsTree.setNodeState(treeNode, false);
}
}
}
}
});

//Unselect extensions when pressing the DELETE or BACKSPACE key
selectedExtensions.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DELETE || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
for (QuarkusExtension extension : selectedExtensions.getSelectedValuesList()) {
CheckedTreeNode treeNode = findTreeNodeForExtension(root, extension);
if (treeNode != null) {
extensionsTree.setNodeState(treeNode, false);
}
}
selectedExtensions.clearSelection();
}
}
});

extensionsTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
Expand Down Expand Up @@ -274,6 +324,17 @@ private CheckedTreeNode getModel(List<QuarkusCategory> categories, SearchTextFie
return root;
}

private @Nullable CheckedTreeNode findTreeNodeForExtension(@NotNull CheckedTreeNode rootNode, @NotNull QuarkusExtension extension) {
Enumeration<TreeNode> enumeration = rootNode.depthFirstEnumeration();
while (enumeration.hasMoreElements()) {
TreeNode node = enumeration.nextElement();
if (node instanceof CheckedTreeNode && ((CheckedTreeNode)node).getUserObject() == extension) {
return (CheckedTreeNode)node;
}
}
return null;
}

/**
* Use reflection to get IntelliJ specific HTML editor kit as it has moved in 2020.1
*
Expand Down

0 comments on commit 459399e

Please sign in to comment.