Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* #%L
* JAXX :: Runtime
* %%
* Copyright (C) 2008 - 2024 Code Lutin, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nuiton.jaxx.runtime.swing;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.awt.Component;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
public class JAXXTree extends JTree {
private static final long serialVersionUID = 1L;
private static final String SYNTHETIC = "";
public class JAXXTreeModel implements TreeModel {
private final Item root;
private final List listeners = new ArrayList<>();
public JAXXTreeModel(List items) {
if (items.size() == 1) {
root = items.get(0);
} else {
root = new Item(null, null, SYNTHETIC, false);
for (Item item : items) {
root.addChild(item);
}
}
PropertyChangeListener listener = e -> {
if (e.getPropertyName().equals(Item.SELECTED_PROPERTY)) {
Item item = (Item) e.getSource();
if (item.isSelected()) {
addSelectionPath(getTreePath(item));
} else {
removeSelectionPath(getTreePath(item));
}
} else {
Item item = (Item) e.getSource();
boolean root = item.getParent() == null;
TreePath path = !root ? getTreePath(item.getParent()) : null;
fireTreeNodesChanged(new TreeModelEvent(JAXXTreeModel.this, path,
!root ? new int[]{item.getParent().getChildren().indexOf(item)} : null,
new Object[]{item.getValue()}));
}
};
addPropertyChangeListener(root, listener);
}
private void addPropertyChangeListener(Item item, PropertyChangeListener listener) {
item.addPropertyChangeListener(listener);
List children = item.getChildren();
for (Item aChildren : children) {
addPropertyChangeListener(aChildren, listener);
}
}
@Override
public void addTreeModelListener(TreeModelListener listener) {
listeners.add(listener);
}
/* This is an inefficient implementation, but hand-coded tree structures are unlikely to contain
enough nodes for that to really matter. This could be sped up with caching. */
public Item findItem(Object value) {
return findItem(root, value);
}
private Item findItem(Item node, Object value) {
if (node.getValue() == value) {
return node;
} else {
List children = node.getChildren();
for (Item aChildren : children) {
Item result = findItem(aChildren, value);
if (result != null) {
return result;
}
}
return null;
}
}
private TreePath getTreePath(Item node) {
List