Skip to content

Commit 0caf80e

Browse files
committed
Added relationship context menu
- Tests implemented - Refactoring needed
1 parent 528f55b commit 0caf80e

File tree

8 files changed

+166
-70
lines changed

8 files changed

+166
-70
lines changed

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/ui/datasource/metadata/DataSourceMetadataUi.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
public class DataSourceMetadataUi {
1010

11+
public static final String RELATIONSHIP_TYPES_TITLE = "relationship types";
12+
public static final String PROPERTY_KEYS_TITLE = "property keys";
13+
public static final String LABELS_TITLE = "labels";
14+
public static final String STORED_PROCEDURES_TITLE = "stored procedures";
15+
public static final String USER_FUNCTIONS_TITLE = "user functions";
16+
1117
private final DataSourcesComponentMetadata dataSourcesComponent;
1218

1319
public DataSourceMetadataUi(DataSourcesComponentMetadata dataSourcesComponent) {
@@ -33,31 +39,31 @@ boolean updateNeo4jBoltCypherMetadataUi(PatchedDefaultMutableTreeNode dataSource
3339

3440
// Labels
3541
PatchedDefaultMutableTreeNode labelsTreeNode = new PatchedDefaultMutableTreeNode(
36-
new ValueWithIcon(GraphIcons.Nodes.LABEL, "labels"));
42+
new ValueWithIcon(GraphIcons.Nodes.LABEL, LABELS_TITLE));
3743
dataSourceMetadata
3844
.getMetadata(Neo4jBoltCypherDataSourceMetadata.LABELS)
3945
.forEach((row) -> labelsTreeNode.add(new PatchedDefaultMutableTreeNode(row.get("label"))));
4046
dataSourceRootTreeNode.add(labelsTreeNode);
4147

4248
// RelTypes
4349
PatchedDefaultMutableTreeNode relationshipTypesTreeNode = new PatchedDefaultMutableTreeNode(
44-
new ValueWithIcon(GraphIcons.Nodes.RELATIONSHIP_TYPE, "relationship types"));
50+
new ValueWithIcon(GraphIcons.Nodes.RELATIONSHIP_TYPE, RELATIONSHIP_TYPES_TITLE));
4551
dataSourceMetadata
4652
.getMetadata(Neo4jBoltCypherDataSourceMetadata.RELATIONSHIP_TYPES)
4753
.forEach((row) -> relationshipTypesTreeNode.add(new PatchedDefaultMutableTreeNode(row.get("relationshipType"))));
4854
dataSourceRootTreeNode.add(relationshipTypesTreeNode);
4955

5056
// Property Keys
5157
PatchedDefaultMutableTreeNode propertyKeysTreeNode = new PatchedDefaultMutableTreeNode(
52-
new ValueWithIcon(GraphIcons.Nodes.PROPERTY_KEY, "property keys"));
58+
new ValueWithIcon(GraphIcons.Nodes.PROPERTY_KEY, PROPERTY_KEYS_TITLE));
5359
dataSourceMetadata
5460
.getMetadata(Neo4jBoltCypherDataSourceMetadata.PROPERTY_KEYS)
5561
.forEach((row) -> propertyKeysTreeNode.add(new PatchedDefaultMutableTreeNode(row.get("propertyKey"))));
5662
dataSourceRootTreeNode.add(propertyKeysTreeNode);
5763

5864
// Stored procedures
5965
PatchedDefaultMutableTreeNode storedProceduresTreeNode = new PatchedDefaultMutableTreeNode(
60-
new ValueWithIcon(GraphIcons.Nodes.STORED_PROCEDURE, "stored procedures"));
66+
new ValueWithIcon(GraphIcons.Nodes.STORED_PROCEDURE, STORED_PROCEDURES_TITLE));
6167
dataSourceMetadata
6268
.getMetadata(Neo4jBoltCypherDataSourceMetadata.STORED_PROCEDURES)
6369
.forEach((row) -> {
@@ -71,7 +77,7 @@ boolean updateNeo4jBoltCypherMetadataUi(PatchedDefaultMutableTreeNode dataSource
7177
// User Functions
7278
if (dataSourceMetadata.isMetadataExists(Neo4jBoltCypherDataSourceMetadata.USER_FUNCTIONS)) {
7379
PatchedDefaultMutableTreeNode userFunctionTreeNode = new PatchedDefaultMutableTreeNode(
74-
new ValueWithIcon(GraphIcons.Nodes.USER_FUNCTION, "user functions"));
80+
new ValueWithIcon(GraphIcons.Nodes.USER_FUNCTION, USER_FUNCTIONS_TITLE));
7581

7682
dataSourceMetadata
7783
.getMetadata(Neo4jBoltCypherDataSourceMetadata.USER_FUNCTIONS)

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/ui/datasource/tree/ContextMenuService.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.tree;
22

33
import com.intellij.ui.treeStructure.PatchedDefaultMutableTreeNode;
4+
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.Neo4jBoltCypherDataSourceMetadata;
45
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.state.impl.DataSourceV1;
56
import com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.tree.dto.ContextMenu;
67
import com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.tree.dto.ValueWithIcon;
@@ -10,23 +11,28 @@
1011
import java.util.Optional;
1112

1213
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.Neo4jBoltCypherDataSourceMetadata.LABELS;
14+
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.Neo4jBoltCypherDataSourceMetadata.RELATIONSHIP_TYPES;
1315

1416
public class ContextMenuService {
1517

1618
private static final int DATASOURCE_INDEX = 1;
1719
private static final int METADATA_INDEX = 2;
18-
private static final int LABELS_DEPTH = 4;
20+
private static final int LABELS_OR_REL_DEPTH = 4;
1921

20-
public Optional<ContextMenu> isContextMenuNeeded(TreePath path) {
21-
TreeNode lastPathComponent = (TreeNode) path.getLastPathComponent();
22+
public Optional<ContextMenu> getContextMenu(TreePath path) {
23+
if (path != null) {
24+
TreeNode lastPathComponent = (TreeNode) path.getLastPathComponent();
2225

23-
if(path.getPathCount() == LABELS_DEPTH) {
24-
String metadataType = getMetadataType(path);
25-
String uuid = getDataSourceUuid(path);
26-
String data = getMetadataValue(lastPathComponent);
26+
if (path.getPathCount() == LABELS_OR_REL_DEPTH) {
27+
String metadataType = getMetadataType(path);
28+
String uuid = getDataSourceUuid(path);
29+
String data = getMetadataValue(lastPathComponent);
2730

28-
if (metadataType.equals(LABELS)) {
29-
return Optional.of(new ContextMenu(LABELS, uuid, data));
31+
if (metadataType.equals("labels")) {
32+
return Optional.of(new ContextMenu(LABELS, uuid, data));
33+
} else if (metadataType.equals("relationship types")) {
34+
return Optional.of(new ContextMenu(RELATIONSHIP_TYPES, uuid, data));
35+
}
3036
}
3137
}
3238
return Optional.empty();

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/ui/datasource/tree/DataSourcesTreeMouseAdapter.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import com.intellij.openapi.ui.popup.ListPopup;
77
import com.intellij.ui.treeStructure.Tree;
88
import com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.tree.dto.ContextMenu;
9+
import com.sun.org.apache.regexp.internal.RE;
910

1011
import javax.swing.tree.TreePath;
1112
import java.awt.event.MouseAdapter;
1213
import java.awt.event.MouseEvent;
1314

1415
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.Neo4jBoltCypherDataSourceMetadata.LABELS;
16+
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.Neo4jBoltCypherDataSourceMetadata.RELATIONSHIP_TYPES;
1517

1618
public class DataSourcesTreeMouseAdapter extends MouseAdapter {
1719

@@ -23,22 +25,22 @@ public void mouseClicked(MouseEvent e) {
2325
Tree tree = (Tree) e.getComponent();
2426
TreePath pathForLocation = tree.getPathForLocation(e.getX(), e.getY());
2527

26-
contextMenuService.isContextMenuNeeded(pathForLocation)
28+
contextMenuService.getContextMenu(pathForLocation)
2729
.ifPresent(dto -> popup(dto, tree));
2830
}
2931
}
3032

3133
private void popup(ContextMenu dto, Tree tree) {
32-
if (dto.getMetadataType().equals(LABELS)) {
34+
if (dto.getMetadataType().equals(LABELS) || dto.getMetadataType().equals(RELATIONSHIP_TYPES)) {
3335
DataContext dataContext = DataManager.getInstance().getDataContext(tree);
34-
contextMenuForLabel(dataContext, dto.getData(), dto.getDatasourceUuid());
36+
contextMenu(dto.getMetadataType(), dto.getData(), dto.getDatasourceUuid(), dataContext);
3537
}
3638
}
3739

38-
private void contextMenuForLabel(DataContext dataContext, String label, String dataSourceUuid) {
40+
private void contextMenu(String type, String data, String uuid, DataContext dataContext) {
3941
ListPopup popup = JBPopupFactory.getInstance().createActionGroupPopup(
40-
label,
41-
new MetadataActionGroup(label, dataSourceUuid),
42+
data,
43+
new MetadataActionGroup(type, data, uuid),
4244
dataContext,
4345
JBPopupFactory.ActionSelectionAid.SPEEDSEARCH,
4446
true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.tree;
2+
3+
import com.intellij.openapi.actionSystem.AnAction;
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import com.intellij.openapi.project.Project;
6+
import com.intellij.util.messages.MessageBus;
7+
import com.neueda.jetbrains.plugin.graphdb.jetbrains.actions.execute.ExecuteQueryEvent;
8+
import com.neueda.jetbrains.plugin.graphdb.jetbrains.actions.execute.ExecuteQueryPayload;
9+
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.DataSourcesComponent;
10+
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.state.DataSourceApi;
11+
12+
import javax.swing.*;
13+
import java.util.Optional;
14+
15+
public abstract class MetadataAction extends AnAction {
16+
private String data;
17+
private String dataSourceUuid;
18+
19+
public MetadataAction(String data, String dataSourceUuid, String title, String description, Icon icon) {
20+
super(title, description, icon);
21+
this.data = data;
22+
this.dataSourceUuid = dataSourceUuid;
23+
}
24+
25+
protected abstract String getQuery(String data);
26+
27+
@Override
28+
public void actionPerformed(AnActionEvent e) {
29+
Project project = getEventProject(e);
30+
MessageBus messageBus = project.getMessageBus();
31+
32+
ExecuteQueryEvent executeQueryEvent = messageBus.syncPublisher(ExecuteQueryEvent.EXECUTE_QUERY_TOPIC);
33+
34+
ExecuteQueryPayload payload = new ExecuteQueryPayload(getQuery(data));
35+
36+
DataSourcesComponent dataSourcesComponent = project.getComponent(DataSourcesComponent.class);
37+
Optional<DataSourceApi> dataSource = dataSourcesComponent.getDataSourceContainer().findDataSource(dataSourceUuid);
38+
39+
executeQueryEvent.executeQuery(dataSource.get(), payload);
40+
}
41+
}

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/ui/datasource/tree/MetadataActionGroup.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,32 @@
77
import org.jetbrains.annotations.NotNull;
88
import org.jetbrains.annotations.Nullable;
99

10+
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.Neo4jBoltCypherDataSourceMetadata.LABELS;
11+
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.Neo4jBoltCypherDataSourceMetadata.RELATIONSHIP_TYPES;
12+
1013
public class MetadataActionGroup extends ActionGroup {
1114

15+
private final String type;
1216
private final String label;
1317
private final String dataSourceUuid;
1418

15-
public MetadataActionGroup(String label, String dataSourceUuid){
19+
public MetadataActionGroup(String type, String label, String dataSourceUuid){
20+
this.type = type;
1621
this.label = label;
1722
this.dataSourceUuid = dataSourceUuid;
1823
}
1924

2025
@NotNull
2126
@Override
2227
public AnAction[] getChildren(@Nullable AnActionEvent e) {
23-
return new AnAction[]{new MetadataLabelAction(label, dataSourceUuid, "Query this label", "", GraphIcons.Database.NEO4J)};
28+
switch (type) {
29+
case RELATIONSHIP_TYPES:
30+
return new AnAction[]{new MetadataLabelAction(label, dataSourceUuid, "Query this relationship", "", GraphIcons.Database.NEO4J)};
31+
case LABELS:
32+
return new AnAction[]{new MetadataLabelAction(label, dataSourceUuid, "Query this label", "", GraphIcons.Database.NEO4J)};
33+
default:
34+
return new AnAction[]{};
35+
}
2436
}
2537

2638
}
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,18 @@
11
package com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.tree;
22

3-
import com.intellij.openapi.actionSystem.AnAction;
4-
import com.intellij.openapi.actionSystem.AnActionEvent;
5-
import com.intellij.openapi.project.Project;
6-
import com.intellij.util.messages.MessageBus;
7-
import com.neueda.jetbrains.plugin.graphdb.jetbrains.actions.execute.ExecuteQueryEvent;
8-
import com.neueda.jetbrains.plugin.graphdb.jetbrains.actions.execute.ExecuteQueryPayload;
9-
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.DataSourcesComponent;
10-
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.state.DataSourceApi;
11-
123
import javax.swing.*;
13-
import java.util.Optional;
144

15-
public class MetadataLabelAction extends AnAction {
5+
public class MetadataLabelAction extends MetadataAction {
166

177
private static final String LABEL_QUERY_START = "MATCH (n:";
188
private static final String LABEL_QUERY_END = ") RETURN n LIMIT 25";
199

20-
private String label;
21-
private String dataSourceUuid;
22-
23-
public MetadataLabelAction(String label, String dataSourceUuid, String title, String description, Icon icon) {
24-
super(title, description, icon);
25-
this.label = label;
26-
this.dataSourceUuid = dataSourceUuid;
10+
public MetadataLabelAction(String data, String dataSourceUuid, String title, String description, Icon icon) {
11+
super(data, dataSourceUuid, title, description, icon);
2712
}
2813

2914
@Override
30-
public void actionPerformed(AnActionEvent e) {
31-
Project project = getEventProject(e);
32-
MessageBus messageBus = project.getMessageBus();
33-
34-
ExecuteQueryEvent executeQueryEvent = messageBus.syncPublisher(ExecuteQueryEvent.EXECUTE_QUERY_TOPIC);
35-
36-
ExecuteQueryPayload payload = new ExecuteQueryPayload(LABEL_QUERY_START + label + LABEL_QUERY_END);
37-
38-
DataSourcesComponent dataSourcesComponent = project.getComponent(DataSourcesComponent.class);
39-
Optional<DataSourceApi> dataSource = dataSourcesComponent.getDataSourceContainer().findDataSource(dataSourceUuid);
40-
41-
executeQueryEvent.executeQuery(dataSource.get(), payload);
15+
protected String getQuery(String data) {
16+
return LABEL_QUERY_START + data + LABEL_QUERY_END;
4217
}
4318
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.tree;
2+
3+
import javax.swing.*;
4+
5+
public class MetadataRelationshipAction extends MetadataAction {
6+
private static final String REL_QUERY_START = "MATCH p=()-[r:";
7+
private static final String REL_QUERY_END = "]->() RETURN p LIMIT 25";
8+
9+
public MetadataRelationshipAction(String data, String dataSourceUuid, String title, String description, Icon icon) {
10+
super(data, dataSourceUuid, title, description, icon);
11+
}
12+
13+
@Override
14+
protected String getQuery(String data) {
15+
return REL_QUERY_START + data + REL_QUERY_END;
16+
}
17+
}

0 commit comments

Comments
 (0)