-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for links in tree (#69)
* feat: Add support for links in tree Fixes #67 Signed-off-by: Jeff MAURY <jmaury@redhat.com>
- Loading branch information
Showing
6 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
ideaVersion = IC-2018.3 | ||
projectVersion=1.0.1-SNAPSHOT | ||
projectVersion=1.1.0-SNAPSHOT | ||
nexusUser=invalid | ||
nexusPassword=invalid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/redhat/devtools/intellij/common/tree/LinkElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.intellij.common.tree; | ||
|
||
/** | ||
* Interface to be implemented by tree elements | ||
* to support links. | ||
*/ | ||
public interface LinkElement { | ||
void execute(); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/redhat/devtools/intellij/common/tree/TreeHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.intellij.common.tree; | ||
|
||
import com.intellij.ide.util.treeView.NodeDescriptor; | ||
import com.intellij.ide.util.treeView.PresentableNodeDescriptor; | ||
import com.intellij.util.ui.tree.TreeUtil; | ||
|
||
import javax.swing.JTree; | ||
import javax.swing.tree.TreePath; | ||
import java.awt.Rectangle; | ||
import java.awt.event.MouseAdapter; | ||
import java.awt.event.MouseEvent; | ||
|
||
public class TreeHelper { | ||
public static TreePath getPathForLocation(JTree tree, int x, int y) { | ||
TreePath path = tree.getClosestPathForLocation(x, y); | ||
Rectangle bounds = tree.getPathBounds(path); | ||
return bounds != null && bounds.y <= y && y < bounds.y + bounds.height ? path : null; | ||
} | ||
|
||
public static void addLinkSupport(final JTree tree) { | ||
tree.addMouseListener(new MouseAdapter() { | ||
@Override | ||
public void mouseClicked(MouseEvent e) { | ||
TreePath path = getPathForLocation(tree, e.getX(), e.getY()); | ||
Object object = TreeUtil.getLastUserObject(path); | ||
if (!(object instanceof LinkElement) && object instanceof NodeDescriptor) { | ||
object = ((NodeDescriptor<?>) object).getElement(); | ||
} | ||
if (object instanceof LinkElement) { | ||
((LinkElement)object).execute(); | ||
} | ||
} | ||
}); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/test/java/com/redhat/devtools/intellij/common/tree/LabelAndIconDescriptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.intellij.common.tree; | ||
|
||
import com.intellij.ide.projectView.PresentationData; | ||
import com.intellij.ide.util.treeView.PresentableNodeDescriptor; | ||
import com.intellij.ui.SimpleTextAttributes; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class LabelAndIconDescriptorTest { | ||
|
||
@Test | ||
public void checkLinkWithoutHref() { | ||
assertTrue(LabelAndIconDescriptor.HREF_PATTERN.matcher("Click <a>here</a>.").find()); | ||
} | ||
|
||
@Test | ||
public void checkLinkWithHref() { | ||
assertTrue(LabelAndIconDescriptor.HREF_PATTERN.matcher("Click <a href=\"xxxx\">here</a>.").find()); | ||
} | ||
|
||
@Test | ||
public void checkTextWithoutHyperLink() { | ||
LabelAndIconDescriptor descriptor = new LabelAndIconDescriptor(null, null, "mytext", null, null); | ||
PresentationData data = new PresentationData(); | ||
descriptor.update(data); | ||
assertEquals("mytext", data.getPresentableText()); | ||
|
||
} | ||
|
||
private void testTextWithOneHyperlink(String text) { | ||
LabelAndIconDescriptor descriptor = new LabelAndIconDescriptor(null, null, text, null, null); | ||
PresentationData data = new PresentationData(); | ||
descriptor.update(data); | ||
assertNull(data.getPresentableText()); | ||
List<PresentableNodeDescriptor.ColoredFragment> fragments = data.getColoredText(); | ||
assertNotNull(fragments); | ||
assertEquals(3, fragments.size()); | ||
assertEquals("Click ", fragments.get(0).getText()); | ||
assertEquals(SimpleTextAttributes.REGULAR_ATTRIBUTES, fragments.get(0).getAttributes()); | ||
assertEquals("here", fragments.get(1).getText()); | ||
assertEquals(LabelAndIconDescriptor.LINK_ATTRIBUTES, fragments.get(1).getAttributes()); | ||
assertEquals(".", fragments.get(2).getText()); | ||
assertEquals(SimpleTextAttributes.REGULAR_ATTRIBUTES, fragments.get(2).getAttributes()); | ||
} | ||
|
||
@Test | ||
public void checkTextWithOneHyperLinkAndNoHref() { | ||
testTextWithOneHyperlink("Click <a>here</a>."); | ||
} | ||
|
||
@Test | ||
public void checkTextWithOneHyperLinkAndHref() { | ||
testTextWithOneHyperlink("Click <a href=\"xxx\">here</a>."); | ||
} | ||
|
||
private void testTextWithTwoHyperlinks(String text) { | ||
LabelAndIconDescriptor descriptor = new LabelAndIconDescriptor(null, null, text, null, null); | ||
PresentationData data = new PresentationData(); | ||
descriptor.update(data); | ||
assertNull(data.getPresentableText()); | ||
List<PresentableNodeDescriptor.ColoredFragment> fragments = data.getColoredText(); | ||
assertNotNull(fragments); | ||
assertEquals(5, fragments.size()); | ||
assertEquals("Click ", fragments.get(0).getText()); | ||
assertEquals(SimpleTextAttributes.REGULAR_ATTRIBUTES, fragments.get(0).getAttributes()); | ||
assertEquals("here", fragments.get(1).getText()); | ||
assertEquals(LabelAndIconDescriptor.LINK_ATTRIBUTES, fragments.get(1).getAttributes()); | ||
assertEquals(" or ", fragments.get(2).getText()); | ||
assertEquals(SimpleTextAttributes.REGULAR_ATTRIBUTES, fragments.get(2).getAttributes()); | ||
assertEquals("there", fragments.get(3).getText()); | ||
assertEquals(LabelAndIconDescriptor.LINK_ATTRIBUTES, fragments.get(3).getAttributes()); | ||
assertEquals(".", fragments.get(4).getText()); | ||
assertEquals(SimpleTextAttributes.REGULAR_ATTRIBUTES, fragments.get(4).getAttributes()); | ||
} | ||
|
||
@Test | ||
public void checkTextWithTwoHyperLinkAndNoHref() { | ||
testTextWithTwoHyperlinks("Click <a>here</a> or <a>there</a>."); | ||
} | ||
|
||
@Test | ||
public void checkTextWithTwoHyperLinkAndHref() { | ||
testTextWithTwoHyperlinks("Click <a href=\"xxx\">here</a> or <a>there</a>."); | ||
} | ||
} |