use of com.oxygenxml.git.view.GitTreeNode in project oxygen-git-client-addon by oxygenxml.
the class ChangesPanel method restoreSelectedPathsFromTableToTree.
/**
* Calculates the treePaths from the table selected files and sets them into the tree.
*/
private void restoreSelectedPathsFromTableToTree() {
int[] selectedRows = filesTable.getSelectedRows();
StagingResourcesTableModel fileTableModel = (StagingResourcesTableModel) filesTable.getModel();
TreePath[] selPaths = new TreePath[selectedRows.length];
for (int i = 0; i < selectedRows.length; i++) {
int convertedRow = filesTable.convertRowIndexToModel(selectedRows[i]);
String absolutePath = fileTableModel.getFileLocation(convertedRow);
GitTreeNode nodeBuilder = TreeUtil.getTreeNodeFromString((StagingResourcesTreeModel) tree.getModel(), absolutePath);
GitTreeNode[] selectedPath = new GitTreeNode[absolutePath.split("/").length + 1];
int count = selectedPath.length;
while (nodeBuilder != null) {
count--;
selectedPath[count] = nodeBuilder;
nodeBuilder = (GitTreeNode) nodeBuilder.getParent();
}
selPaths[i] = new TreePath(selectedPath);
}
tree.setSelectionPaths(selPaths);
}
use of com.oxygenxml.git.view.GitTreeNode in project oxygen-git-client-addon by oxygenxml.
the class ChangesPanel method addTreeMouseListener.
/**
* Adds a mouse listener to the tree: When the user right clicks on a node, a
* contextual menu will pop. Also when the user double clicks on a leaf node
* an action will occur depending on it's file status. If the status is MODIFY
* the open in compare editor will be executed, if the status is Add the file
* will be opened in the Oxygen
*/
private void addTreeMouseListener() {
tree.getSelectionModel().addTreeSelectionListener(e -> toggleSelectedButton());
tree.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// For MacOS the popup trigger comes on mouse pressed.
handleContextualMenuEvent(e);
}
@Override
public void mouseReleased(MouseEvent e) {
// Switching between Staged and UnStaged with right click introduced some paint artifacts.
tree.requestFocus();
tree.repaint();
// Maybe the event was a (not pop-up trigger) double-click
showDiff(e);
// Or maybe it was a right click
handleContextualMenuEvent(e);
}
/**
* Shows the contextual menu, if the mouse event is a pop-up trigger.
*
* @param e Mouse event.
*/
private void handleContextualMenuEvent(MouseEvent e) {
if (e.isPopupTrigger() && e.getClickCount() == 1) {
// ============= Right click event ================
// First, check the node under the mouse.
TreePath treePath = tree.getPathForLocation(e.getX(), e.getY());
if (treePath != null) {
boolean treeInSelection = false;
TreePath[] paths = tree.getSelectionPaths();
// A JTree only updates selection for a left button. Also do it for a right click.
if (paths != null) {
for (TreePath path : paths) {
if (treePath.equals(path)) {
treeInSelection = true;
break;
}
}
}
if (!treeInSelection) {
tree.setSelectionPath(treePath);
}
} else {
// A click outside the tree. Go with a selected path.
treePath = tree.getSelectionPath();
}
if (treePath != null) {
String stringPath = TreeUtil.getStringPath(treePath);
StagingResourcesTreeModel model = (StagingResourcesTreeModel) tree.getModel();
GitTreeNode node = TreeUtil.getTreeNodeFromString(model, stringPath);
if (node != null && (!node.isRoot() || node.children().hasMoreElements() || isMergingResolved())) {
showContextualMenuForTree(e.getX(), e.getY(), model);
}
}
}
}
/**
* Shows DIFF for a double click mouse event.
*
* @param e Mouse event.
*/
private void showDiff(MouseEvent e) {
if (!e.isPopupTrigger() && e.getClickCount() == 2) {
// ============= Double click event ==============
TreePath treePath = tree.getPathForLocation(e.getX(), e.getY());
if (treePath != null) {
String stringPath = TreeUtil.getStringPath(treePath);
StagingResourcesTreeModel model = (StagingResourcesTreeModel) tree.getModel();
GitTreeNode node = TreeUtil.getTreeNodeFromString(model, stringPath);
if (model.isLeaf(node) && !model.getRoot().equals(node)) {
FileStatus file = model.getFileByPath(stringPath);
DiffPresenter.showDiff(file, gitController);
}
}
}
}
});
}
use of com.oxygenxml.git.view.GitTreeNode in project oxygen-git-client-addon by oxygenxml.
the class TreeUtil method expandSingleChildPath.
/**
* Adds an expand new functionality to the tree: When the user expands a node, the node
* will expand as long as it has only one child.
*
* @param tree The tree for which to add the listener.
* @param event The event used to get the tree path.
*/
public static void expandSingleChildPath(JTree tree, TreeExpansionEvent event) {
TreePath path = event.getPath();
TreeModel treeModel = tree.getModel();
GitTreeNode node = (GitTreeNode) path.getLastPathComponent();
if (!treeModel.isLeaf(node)) {
int children = node.getChildCount();
if (children == 1) {
GitTreeNode child = (GitTreeNode) node.getChildAt(0);
TreePath childPath = new TreePath(child.getPath());
tree.expandPath(childPath);
}
}
}
use of com.oxygenxml.git.view.GitTreeNode in project oxygen-git-client-addon by oxygenxml.
the class TreeUtil method getLastExpandedPaths.
/**
* Returns all the current expanded paths for a tree.
*
* @param tree The tree used to find the expanded paths.
* @return The current expanded paths.
*/
public static Enumeration<TreePath> getLastExpandedPaths(JTree tree) {
TreeModel treeModel = tree.getModel();
GitTreeNode rootNode = (GitTreeNode) treeModel.getRoot();
Enumeration<TreePath> expandedPaths = null;
if (rootNode != null) {
TreePath rootTreePath = new TreePath(rootNode);
expandedPaths = tree.getExpandedDescendants(rootTreePath);
}
return expandedPaths;
}
use of com.oxygenxml.git.view.GitTreeNode in project oxygen-git-client-addon by oxygenxml.
the class TreeUtil method buildTreeFromStringFullPath.
/**
* Builds a tree from a given forward slash delimited string and puts the full
* path to the node in its user object.
*
* @param model The tree model
* @param str The string to build the tree from
*/
public static void buildTreeFromStringFullPath(final DefaultTreeModel model, final String str) {
GitTreeNode root = (GitTreeNode) model.getRoot();
String[] strings = str.split("/");
// Create a node object to use for traversing down the tree as it
// is being created
GitTreeNode node = root;
StringBuilder currentNodePath = new StringBuilder();
for (int i = 0; i < strings.length; ++i) {
currentNodePath.append(strings[i]);
if (i < strings.length - 1) {
currentNodePath.append("/");
}
// Make sure not to add the refs/ node in the tree.
if (!currentNodePath.toString().equals(Constants.R_REFS) && !currentNodePath.toString().equals(Constants.HEAD)) {
// Look for the index of a node at the current level that
// has a value equal to the current string
int index = childIndex(node, currentNodePath.toString());
// Index less than 0, this is a new node not currently present on the tree
if (index < 0) {
GitTreeNode newChild = new GitTreeNode(currentNodePath.toString());
node.insert(newChild, node.getChildCount());
node = newChild;
} else {
// Existing node, skip to the next string
node = (GitTreeNode) node.getChildAt(index);
}
}
}
}
Aggregations