
com.intellij.openapi.externalSystem.service.task.ui.ExternalSystemTasksTreeModel Maven / Gradle / Ivy
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.externalSystem.service.task.ui;
import com.intellij.execution.executors.DefaultRunExecutor;
import com.intellij.openapi.externalSystem.ExternalSystemUiAware;
import com.intellij.openapi.externalSystem.model.ProjectSystemId;
import com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings;
import com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo;
import com.intellij.openapi.externalSystem.model.execution.ExternalTaskPojo;
import com.intellij.openapi.externalSystem.model.project.ExternalProjectPojo;
import com.intellij.openapi.externalSystem.util.ExternalSystemUiUtil;
import com.intellij.util.containers.ContainerUtilRt;
import gnu.trove.TObjectIntHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.*;
/**
* @author Denis Zhdanov
* @since 5/12/13 10:28 PM
*/
public class ExternalSystemTasksTreeModel extends DefaultTreeModel {
@NotNull private static final Comparator NODE_COMPARATOR = new Comparator() {
@Override
public int compare(TreeNode t1, TreeNode t2) {
Object e1 = ((ExternalSystemNode>)t1).getDescriptor().getElement();
Object e2 = ((ExternalSystemNode>)t2).getDescriptor().getElement();
if (e1 instanceof ExternalProjectPojo) {
if (e2 instanceof ExternalTaskExecutionInfo) {
return 1;
}
else {
return ((ExternalProjectPojo)e1).getName().compareTo(((ExternalProjectPojo)e2).getName());
}
}
else {
if (e2 instanceof ExternalProjectPojo) {
return -1;
}
else {
return getTaskName((ExternalTaskExecutionInfo)e1).compareTo(getTaskName((ExternalTaskExecutionInfo)e2));
}
}
}
};
@NotNull private final ExternalSystemUiAware myUiAware;
@NotNull private final ProjectSystemId myExternalSystemId;
public ExternalSystemTasksTreeModel(@NotNull ProjectSystemId externalSystemId) {
super(new ExternalSystemNode(new ExternalSystemNodeDescriptor("", "", "", null)));
myExternalSystemId = externalSystemId;
myUiAware = ExternalSystemUiUtil.getUiAware(externalSystemId);
}
private static String getTaskName(@NotNull ExternalTaskExecutionInfo taskInfo) {
return taskInfo.getSettings().getTaskNames().get(0);
}
/**
* Ensures that current model has a top-level node which corresponds to the given external project info holder
*
* @param project target external project info holder
*/
@SuppressWarnings("unchecked")
@NotNull
public ExternalSystemNode ensureProjectNodeExists(@NotNull ExternalProjectPojo project) {
ExternalSystemNode> root = getRoot();
// Remove outdated projects.
for (int i = root.getChildCount() - 1; i >= 0; i--) {
ExternalSystemNode> child = root.getChildAt(i);
ExternalSystemNodeDescriptor> descriptor = child.getDescriptor();
Object element = descriptor.getElement();
if (element instanceof ExternalProjectPojo) {
ExternalProjectPojo pojo = (ExternalProjectPojo)element;
if (pojo.getPath().equals(project.getPath())) {
if (!pojo.getName().equals(project.getName())) {
pojo.setName(project.getName());
descriptor.setName(project.getName());
nodeChanged(child);
}
return (ExternalSystemNode)child;
}
}
}
ExternalProjectPojo element = new ExternalProjectPojo(project.getName(), project.getPath());
ExternalSystemNodeDescriptor descriptor = descriptor(element, myUiAware.getProjectIcon());
ExternalSystemNode result = new ExternalSystemNode(descriptor);
insertNodeInto(result, root);
return result;
}
/**
* Asks current model to remove all nodes which have given data as a {@link ExternalSystemNodeDescriptor#getElement() payload}.
*
* @param payload target payload
*/
public void pruneNodes(@NotNull Object payload) {
Deque> toProcess = new ArrayDeque>();
toProcess.addFirst(getRoot());
while (!toProcess.isEmpty()) {
ExternalSystemNode> node = toProcess.removeLast();
if (payload.equals(node.getDescriptor().getElement())) {
removeNodeFromParent(node);
}
else {
for (int i = 0; i < node.getChildCount(); i++) {
toProcess.addFirst(node.getChildAt(i));
}
}
}
}
public void ensureSubProjectsStructure(@NotNull ExternalProjectPojo topLevelProject,
@NotNull Collection subProjects) {
ExternalSystemNode topLevelProjectNode = ensureProjectNodeExists(topLevelProject);
Map toAdd = ContainerUtilRt.newHashMap();
for (ExternalProjectPojo subProject : subProjects) {
toAdd.put(subProject.getPath(), subProject);
}
toAdd.remove(topLevelProject.getPath());
final TObjectIntHashMap
© 2015 - 2025 Weber Informatics LLC | Privacy Policy