use of javax.swing.tree.MutableTreeNode in project pcgen by PCGen.
the class NotesTreeNode method remove.
/**
* removes the child node at index
*
* @param index
* index of child to remove
*/
@Override
public void remove(int index) {
if (!hasBeenPopulated) {
populate();
}
MutableTreeNode child = (MutableTreeNode) getChildAt(index);
children.removeElementAt(index);
child.setParent(null);
}
use of javax.swing.tree.MutableTreeNode in project pcgen by PCGen.
the class NotesTreeNode method refresh.
/** Refereshs the tree to take into account any added/removed directories */
public void refresh() {
// Sometimes duplicating nodes.
if (hasBeenPopulated) {
Enumeration<MutableTreeNode> childNodes = children();
List<File> childDirs = Arrays.asList(dir.listFiles());
List<File> removeDirs = new ArrayList<>();
while (childNodes.hasMoreElements()) {
NotesTreeNode node = (NotesTreeNode) childNodes.nextElement();
File nodeDir = node.getDir();
if (nodeDir.exists()) {
for (int i = 0; i < childDirs.size(); i++) {
File childDir = childDirs.get(i);
if (nodeDir.getName().equals(childDir.getName())) {
removeDirs.add(childDir);
continue;
}
}
} else {
remove(node);
}
}
for (File childDir : childDirs) {
if (!removeDirs.contains(childDir)) {
if (include(childDir)) {
add(new NotesTreeNode(childDir.getName(), childDir, tree));
}
}
}
Enumeration<MutableTreeNode> newNodes = children();
while (newNodes.hasMoreElements()) {
NotesTreeNode node = (NotesTreeNode) newNodes.nextElement();
node.refresh();
}
}
}
use of javax.swing.tree.MutableTreeNode in project pcgen by PCGen.
the class NotesTreeNode method insert.
/**
* Inserts a new MutableTreeNode into this node as a child.
*
* @param child
* Child to insert
* @param index
* Location to insert it.
*/
@Override
public void insert(MutableTreeNode child, int index) {
if (!allowsChildren) {
throw new IllegalStateException("node does not allow children");
} else if (child == null) {
throw new IllegalArgumentException("new child is null");
} else if (isNodeAncestor(child)) {
throw new IllegalArgumentException("new child is an ancestor");
}
if (!hasBeenPopulated) {
populate();
}
MutableTreeNode oldParent = (MutableTreeNode) child.getParent();
if (oldParent != null) {
oldParent.remove(child);
}
child.setParent(this);
if (children == null) {
children = new Vector<>();
}
children.insertElementAt(child, index);
}
use of javax.swing.tree.MutableTreeNode in project intellij-community by JetBrains.
the class DynamicToolWindowWrapper method createTable.
private JScrollPane createTable(final MutableTreeNode myTreeRoot) {
ColumnInfo[] columnInfos = { new ClassColumnInfo(myColumnNames[CLASS_OR_ELEMENT_NAME_COLUMN]), new PropertyTypeColumnInfo(myColumnNames[TYPE_COLUMN]) };
myTreeTableModel = new ListTreeTableModelOnColumns(myTreeRoot, columnInfos);
myTreeTable = new MyTreeTable(myTreeTableModel);
new TreeTableSpeedSearch(myTreeTable, new Convertor<TreePath, String>() {
@Override
public String convert(TreePath o) {
final Object node = o.getLastPathComponent();
if (node instanceof DefaultMutableTreeNode) {
final Object object = ((DefaultMutableTreeNode) node).getUserObject();
if (object instanceof DNamedElement) {
return ((DNamedElement) object).getName();
}
}
return "";
}
});
DefaultActionGroup group = new DefaultActionGroup();
group.add(ActionManager.getInstance().getAction(RemoveDynamicAction.GROOVY_DYNAMIC_REMOVE));
PopupHandler.installUnknownPopupHandler(myTreeTable, group, ActionManager.getInstance());
final MyColoredTreeCellRenderer treeCellRenderer = new MyColoredTreeCellRenderer();
myTreeTable.setDefaultRenderer(String.class, new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof String) {
try {
final PsiType type = JavaPsiFacade.getElementFactory(myProject).createTypeFromText((String) value, null);
String shortName = type.getPresentableText();
return new JLabel(shortName);
} catch (IncorrectOperationException e) {
LOG.debug("Type cannot be created", e);
}
return new JLabel(QuickfixUtil.shortenType((String) value));
}
return new JLabel();
}
});
myTreeTable.setTreeCellRenderer(treeCellRenderer);
myTreeTable.setRootVisible(false);
myTreeTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
final MyPropertyTypeCellEditor typeCellEditor = new MyPropertyTypeCellEditor();
typeCellEditor.addCellEditorListener(new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
final TreeTableTree tree = getTree();
String newTypeValue = ((MyPropertyTypeCellEditor) e.getSource()).getCellEditorValue();
if (newTypeValue == null || tree == null) {
myTreeTable.editingStopped(e);
return;
}
try {
final PsiType type = JavaPsiFacade.getElementFactory(myProject).createTypeFromText(newTypeValue, null);
String canonical = type.getCanonicalText();
if (canonical != null)
newTypeValue = canonical;
} catch (IncorrectOperationException ex) {
//do nothing in case bad string is entered
}
final TreePath editingTypePath = tree.getSelectionPath();
if (editingTypePath == null)
return;
final TreePath editingClassPath = editingTypePath.getParentPath();
Object oldTypeValue = myTreeTable.getValueAt(tree.getRowForPath(editingTypePath), TYPE_COLUMN);
if (!(oldTypeValue instanceof String)) {
myTreeTable.editingStopped(e);
return;
}
final Object editingPropertyObject = myTreeTable.getValueAt(tree.getRowForPath(editingTypePath), CLASS_OR_ELEMENT_NAME_COLUMN);
final Object editingClassObject = myTreeTable.getValueAt(tree.getRowForPath(editingClassPath), CLASS_OR_ELEMENT_NAME_COLUMN);
if (!(editingPropertyObject instanceof DItemElement) || !(editingClassObject instanceof DClassElement)) {
myTreeTable.editingStopped(e);
return;
}
final DItemElement dynamicElement = (DItemElement) editingPropertyObject;
final String name = dynamicElement.getName();
final String className = ((DClassElement) editingClassObject).getName();
if (dynamicElement instanceof DPropertyElement) {
DynamicManager.getInstance(myProject).replaceDynamicPropertyType(className, name, (String) oldTypeValue, newTypeValue);
} else if (dynamicElement instanceof DMethodElement) {
final List<ParamInfo> myPairList = ((DMethodElement) dynamicElement).getPairs();
DynamicManager.getInstance(myProject).replaceDynamicMethodType(className, name, myPairList, (String) oldTypeValue, newTypeValue);
}
}
@Override
public void editingCanceled(ChangeEvent e) {
myTreeTable.editingCanceled(e);
}
});
RefactoringListenerManager.getInstance(myProject).addListenerProvider(new RefactoringElementListenerProvider() {
@Override
@Nullable
public RefactoringElementListener getListener(final PsiElement element) {
if (element instanceof PsiClass) {
final String qualifiedName = ((PsiClass) element).getQualifiedName();
return new RefactoringElementListener() {
@Override
public void elementMoved(@NotNull PsiElement newElement) {
renameElement(qualifiedName, newElement);
}
@Override
public void elementRenamed(@NotNull PsiElement newElement) {
renameElement(qualifiedName, newElement);
}
private void renameElement(String oldClassName, PsiElement newElement) {
if (newElement instanceof PsiClass) {
final String newClassName = ((PsiClass) newElement).getQualifiedName();
final DRootElement rootElement = DynamicManager.getInstance(myProject).getRootElement();
final DClassElement oldClassElement = rootElement.getClassElement(oldClassName);
final TreeNode oldClassNode = TreeUtil.findNodeWithObject((DefaultMutableTreeNode) myTreeRoot, oldClassElement);
DynamicManager.getInstance(myProject).replaceClassName(oldClassElement, newClassName);
myTreeTableModel.nodeChanged(oldClassNode);
}
}
};
}
return null;
}
});
myTreeTable.setDefaultEditor(String.class, typeCellEditor);
myTreeTable.registerKeyboardAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
final int selectionRow = myTreeTable.getTree().getLeadSelectionRow();
myTreeTable.editCellAt(selectionRow, CLASS_OR_ELEMENT_NAME_COLUMN, event);
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), JComponent.WHEN_FOCUSED);
myTreeTable.registerKeyboardAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
final int selectionRow = myTreeTable.getTree().getLeadSelectionRow();
myTreeTable.editCellAt(selectionRow, TYPE_COLUMN, event);
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_F2, InputEvent.CTRL_MASK), JComponent.WHEN_FOCUSED);
myTreeTable.getTree().setShowsRootHandles(true);
myTreeTable.getTableHeader().setReorderingAllowed(false);
myTreeTable.setPreferredScrollableViewportSize(new Dimension(300, myTreeTable.getRowHeight() * 10));
myTreeTable.getColumn(myColumnNames[CLASS_OR_ELEMENT_NAME_COLUMN]).setPreferredWidth(200);
myTreeTable.getColumn(myColumnNames[TYPE_COLUMN]).setPreferredWidth(160);
JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myTreeTable);
scrollPane.setPreferredSize(JBUI.size(600, 400));
return scrollPane;
}
use of javax.swing.tree.MutableTreeNode in project intellij-community by JetBrains.
the class FileTreeModelBuilder method removeNode.
@Nullable
public DefaultMutableTreeNode removeNode(final PsiElement element, PsiDirectory parent) {
LOG.assertTrue(parent != null, element instanceof PsiFile && ((PsiFile) element).getVirtualFile() != null ? ((PsiFile) element).getVirtualFile().getPath() : element);
final VirtualFile parentVirtualFile = parent.getVirtualFile();
Module module = myFileIndex.getModuleForFile(parentVirtualFile);
if (element instanceof PsiDirectory && myFlattenPackages) {
final PackageDependenciesNode moduleNode = getModuleNode(module);
final PsiDirectory psiDirectory = (PsiDirectory) element;
final VirtualFile virtualFile = psiDirectory.getVirtualFile();
final PackageDependenciesNode dirNode = getModuleDirNode(virtualFile, myFileIndex.getModuleForFile(virtualFile), null);
dirNode.removeFromParent();
return moduleNode;
}
DirectoryNode dirNode = myModuleDirNodes.get(parentVirtualFile);
if (dirNode == null)
return null;
DirectoryNode wrapper = dirNode.getWrapper();
while (wrapper != null) {
dirNode = wrapper;
myModuleDirNodes.put(wrapper.getDirectory(), null);
wrapper = dirNode.getWrapper();
}
final PackageDependenciesNode[] classOrDirNodes = findNodeForPsiElement(dirNode, element);
if (classOrDirNodes != null) {
for (PackageDependenciesNode classNode : classOrDirNodes) {
classNode.removeFromParent();
}
}
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) dirNode.getParent();
DefaultMutableTreeNode node = dirNode;
if (element == parent) {
myModuleDirNodes.put(parentVirtualFile, null);
dirNode.removeFromParent();
node = parentNode;
}
while (parent != null && node != null && node.getChildCount() == 0) {
PsiDirectory directory = parent.getParentDirectory();
parentNode = (DefaultMutableTreeNode) node.getParent();
node.removeFromParent();
if (node instanceof DirectoryNode) {
while (node != null) {
//clear all compacted links
myModuleDirNodes.put(((DirectoryNode) node).getDirectory(), null);
node = ((DirectoryNode) node).getCompactedDirNode();
}
} else if (node instanceof ModuleNode) {
myModuleNodes.put(((ModuleNode) node).getModule(), null);
} else if (node instanceof ModuleGroupNode) {
myModuleGroupNodes.put(((ModuleGroupNode) node).getModuleGroupName(), null);
}
node = parentNode;
parent = directory;
}
if (myCompactEmptyMiddlePackages && node instanceof DirectoryNode && node.getChildCount() == 1) {
//compact
final TreeNode treeNode = node.getChildAt(0);
if (treeNode instanceof DirectoryNode) {
node.removeAllChildren();
for (int i = treeNode.getChildCount() - 1; i >= 0; i--) {
node.add((MutableTreeNode) treeNode.getChildAt(i));
}
((DirectoryNode) node).setCompactedDirNode((DirectoryNode) treeNode);
}
}
return parentNode != null ? parentNode : myRoot;
}
Aggregations