Skip to content

Commit

Permalink
chore: replaced disposable (was: project) & removed TreeHelper.buildM…
Browse files Browse the repository at this point in the history
…odel (#623)

Signed-off-by: Andre Dietisheim <adietish@redhat.com>
  • Loading branch information
adietish committed Nov 17, 2022
1 parent 3e083d2 commit d1b2871
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.intellij.ui.OnePixelSplitter;
import com.intellij.ui.PopupHandler;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.tree.AsyncTreeModel;
import com.intellij.ui.tree.StructureTreeModel;
Expand All @@ -40,12 +41,10 @@
import com.redhat.devtools.intellij.tektoncd.ui.hub.HubItem;
import com.redhat.devtools.intellij.tektoncd.ui.hub.HubItemPanelsBoard;
import com.redhat.devtools.intellij.tektoncd.ui.hub.HubModel;
import com.redhat.devtools.intellij.tektoncd.utils.TreeHelper;
import org.jetbrains.annotations.NotNull;

import javax.swing.JComponent;
import javax.swing.JPanel;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import java.util.function.BiConsumer;

Expand All @@ -56,48 +55,39 @@
public class WindowToolFactory implements ToolWindowFactory {
@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
try {
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();

TektonTreeStructure structure = new TektonTreeStructure(project);
StructureTreeModel<TektonTreeStructure> model = TreeHelper.buildModel(structure, project);
new MutableTektonModelSynchronizer<>(model, structure, structure);
Tree tree = new Tree(new AsyncTreeModel(model, project));
tree.putClientProperty(Constants.STRUCTURE_PROPERTY, structure);
tree.setCellRenderer(new NodeRenderer());
ActionManager actionManager = ActionManager.getInstance();
ActionGroup group = (ActionGroup)actionManager.getAction("com.redhat.devtools.intellij.tektoncd.tree");
PopupHandler.installPopupHandler(tree, group, ActionPlaces.POPUP, actionManager, new TektonTreePopupMenuListener());
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
SimpleToolWindowPanel panel = new SimpleToolWindowPanel(true, true);
Content content = contentFactory.createContent(panel, "", false);

new TektonTreeDoubleClickListener(tree);
TektonTreeStructure structure = new TektonTreeStructure(project);
StructureTreeModel<TektonTreeStructure> model = new StructureTreeModel(structure, content);
new MutableTektonModelSynchronizer<>(model, structure, structure);
Tree tree = new Tree(new AsyncTreeModel(model, content));
tree.putClientProperty(Constants.STRUCTURE_PROPERTY, structure);
tree.setCellRenderer(new NodeRenderer());
ActionManager actionManager = ActionManager.getInstance();
ActionGroup group = (ActionGroup)actionManager.getAction("com.redhat.devtools.intellij.tektoncd.tree");
PopupHandler.installPopupHandler(tree, group, ActionPlaces.POPUP, actionManager, new TektonTreePopupMenuListener());

TknCliFactory.getInstance().getTkn(project).whenComplete((tkn, err) -> {
HubModel hubModel = new HubModel(project, tkn, null);
new TektonTreeDoubleClickListener(tree);

JPanel hubItemsListPanel = new HubItemPanelsBoard(hubModel, getDoSelectAction(project, hubModel))
.withRecommended()
.withInstalled()
.build(Optional.empty());
TknCliFactory.getInstance().getTkn(project).whenComplete((tkn, err) -> {
HubModel hubModel = new HubModel(project, tkn, null);

SimpleToolWindowPanel panel = createPanelWithTree(tree);
addToolbarMenuToPanel(panel);
OnePixelSplitter tabPanel = createTabPanel(panel, hubItemsListPanel);
JPanel hubItemsListPanel = new HubItemPanelsBoard(hubModel, getDoSelectAction(project, hubModel))
.withRecommended()
.withInstalled()
.build(Optional.empty());
panel.setContent(new JBScrollPane(tree));
addToolbarMenuToPanel(panel);

toolWindow.getContentManager().addContent(contentFactory.createContent(tabPanel, "", false));
executeOnProjectClosing(project, () -> {
structure.dispose();
hubModel.dispose();
});
OnePixelSplitter tabPanel = createTabPanel(panel, hubItemsListPanel);
toolWindow.getContentManager().addContent(contentFactory.createContent(tabPanel, "", false));
executeOnProjectClosing(project, () -> {
structure.dispose();
hubModel.dispose();
});
} catch (IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
throw new RuntimeException((e));
}
}

private SimpleToolWindowPanel createPanelWithTree(Tree tree) {
SimpleToolWindowPanel panel = new SimpleToolWindowPanel(true, true);
panel.setContent(new JBScrollPane(tree));
return panel;
});
}

private void addToolbarMenuToPanel(SimpleToolWindowPanel panel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
******************************************************************************/
package com.redhat.devtools.intellij.tektoncd.utils;

import com.intellij.ide.util.treeView.AbstractTreeStructure;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
Expand All @@ -24,67 +22,24 @@
import com.intellij.ui.JBSplitter;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.content.Content;
import com.intellij.ui.tree.StructureTreeModel;
import com.intellij.ui.treeStructure.Tree;
import com.redhat.devtools.intellij.common.actions.StructureTreeAction;
import com.redhat.devtools.intellij.common.utils.ExecHelper;
import com.redhat.devtools.intellij.common.utils.UIHelper;
import com.redhat.devtools.intellij.tektoncd.Constants;
import com.redhat.devtools.intellij.tektoncd.telemetry.TelemetryService;
import com.redhat.devtools.intellij.tektoncd.tkn.Tkn;
import com.redhat.devtools.intellij.tektoncd.tree.ClusterTaskNode;
import com.redhat.devtools.intellij.tektoncd.tree.ClusterTriggerBindingNode;
import com.redhat.devtools.intellij.tektoncd.tree.ConditionNode;
import com.redhat.devtools.intellij.tektoncd.tree.ConfigurationNode;
import com.redhat.devtools.intellij.tektoncd.tree.EventListenerNode;
import com.redhat.devtools.intellij.tektoncd.tree.ParentableNode;
import com.redhat.devtools.intellij.tektoncd.tree.PipelineNode;
import com.redhat.devtools.intellij.tektoncd.tree.PipelineRunNode;
import com.redhat.devtools.intellij.tektoncd.tree.ResourceNode;
import com.redhat.devtools.intellij.tektoncd.tree.TaskNode;
import com.redhat.devtools.intellij.tektoncd.tree.TaskRunNode;
import com.redhat.devtools.intellij.tektoncd.tree.TektonRootNode;
import com.redhat.devtools.intellij.tektoncd.tree.TektonTreeStructure;
import com.redhat.devtools.intellij.tektoncd.tree.TriggerBindingNode;
import com.redhat.devtools.intellij.tektoncd.tree.TriggerTemplateNode;
import com.redhat.devtools.intellij.tektoncd.tree.*;
import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder;
import io.fabric8.kubernetes.api.model.ConfigMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.tree.TreePath;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_CLUSTERTASK;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_CLUSTERTASKS;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_CLUSTERTRIGGERBINDING;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_CLUSTERTRIGGERBINDINGS;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_CONDITION;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_CONDITIONS;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_EVENTLISTENER;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_EVENTLISTENERS;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_PIPELINE;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_PIPELINERUN;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_PIPELINERUNS;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_PIPELINES;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_RESOURCE;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_RESOURCES;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_TASK;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_TASKRUN;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_TASKRUNS;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_TASKS;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_TRIGGERBINDING;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_TRIGGERBINDINGS;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_TRIGGERTEMPLATE;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_TRIGGERTEMPLATES;
import static com.redhat.devtools.intellij.tektoncd.Constants.NOTIFICATION_ID;
import static com.redhat.devtools.intellij.tektoncd.Constants.*;
import static com.redhat.devtools.intellij.tektoncd.telemetry.TelemetryService.NAME_PREFIX_CRUD;
import static com.redhat.devtools.intellij.telemetry.core.util.AnonymizeUtils.anonymizeResource;

Expand Down Expand Up @@ -320,25 +275,4 @@ public static Map<Class, List<ParentableNode>> getResourcesByClass(ParentableNod
.add(element));
return resourcesByClass;
}

/**
* Build the model through reflection as StructureTreeModel does not have a stable API.
*
* @param structure the structure to associate
* @param project the IJ project
* @return the build model
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws NoSuchMethodException
*/
public static StructureTreeModel buildModel(TektonTreeStructure structure, Project project) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {
try {
Constructor<StructureTreeModel> constructor = StructureTreeModel.class.getConstructor(new Class[] {AbstractTreeStructure.class});
return constructor.newInstance(structure);
} catch (NoSuchMethodException e) {
Constructor<StructureTreeModel> constructor = StructureTreeModel.class.getConstructor(new Class[] {AbstractTreeStructure.class, Disposable.class});
return constructor.newInstance(structure, project);
}
}
}

0 comments on commit d1b2871

Please sign in to comment.