Browse Source

Edited tree demo.

Fixed properties not being applied after they have been set.
Fixed old boolean renderer staying visible after it has been changed.

Signed-off-by: weisj <weisj@arcor.de>
pull/27/head
weisj 5 years ago
parent
commit
45c2f5cfc5
  1. 44
      src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeUI.java
  2. 2
      src/main/java/com/github/weisj/darklaf/ui/tree/TreeRendererComponent.java
  3. 88
      src/test/java/demo/tree/TreeDemo.java

44
src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeUI.java

@ -42,13 +42,15 @@ import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Enumeration;
/**
* @author Konstantin Bulenkov
* @author Jannis Weis
*/
public class DarkTreeUI extends BasicTreeUI {
public class DarkTreeUI extends BasicTreeUI implements PropertyChangeListener {
public static final String TREE_TABLE_TREE_KEY = "JTree.treeTableTree";
public static final String STRIPED_CLIENT_PROPERTY = "JTree.alternateRowColor";
@ -135,13 +137,9 @@ public class DarkTreeUI extends BasicTreeUI {
@Override
protected void completeUIInstall() {
super.completeUIInstall();
myOldRepaintAllRowValue = UIManager.getBoolean("Tree.repaintWholeRow");
UIManager.put("Tree.repaintWholeRow", true);
tree.putClientProperty("JTree.alternateRowColor", UIManager.getBoolean("Tree.alternateRowColor"));
tree.setShowsRootHandles(true);
tree.addMouseListener(mySelectionListener);
}
@Override
@ -164,6 +162,15 @@ public class DarkTreeUI extends BasicTreeUI {
tree.putClientProperty("JTree.renderBooleanAsCheckBox",
UIManager.getBoolean("Tree.renderBooleanAsCheckBox"));
tree.putClientProperty("JTree.booleanRenderType", UIManager.getString("Tree.booleanRenderType"));
tree.setShowsRootHandles(true);
tree.putClientProperty("JTree.lineStyle", "Line");
}
@Override
protected void installListeners() {
super.installListeners();
tree.addPropertyChangeListener(this);
tree.addMouseListener(mySelectionListener);
}
@Override
@ -320,11 +327,16 @@ public class DarkTreeUI extends BasicTreeUI {
}
@Override
public void uninstallUI(final JComponent c) {
super.uninstallUI(c);
protected void uninstallDefaults() {
super.uninstallDefaults();
UIManager.put("Tree.repaintWholeRow", myOldRepaintAllRowValue);
c.removeMouseListener(mySelectionListener);
}
@Override
protected void uninstallListeners() {
super.uninstallListeners();
tree.removeMouseListener(mySelectionListener);
tree.removePropertyChangeListener(this);
}
@Override
@ -674,6 +686,20 @@ public class DarkTreeUI extends BasicTreeUI {
&& (SystemInfo.isMac ? e.isMetaDown() : e.isControlDown()) && !e.isPopupTrigger();
}
@Override
public void propertyChange(final PropertyChangeEvent evt) {
String key = evt.getPropertyName();
if (STRIPED_CLIENT_PROPERTY.equals(key)) {
tree.repaint();
} else if ("JTree.renderBooleanAsCheckBox".equals(key)) {
tree.repaint();
} else if ("JTree.booleanRenderType".equals(key)) {
tree.repaint();
} else if ("JTree.lineStyle".equals(key)) {
tree.repaint();
}
}
private abstract static class TreeUIAction extends AbstractAction implements UIResource {
}
}

2
src/main/java/com/github/weisj/darklaf/ui/tree/TreeRendererComponent.java

@ -46,7 +46,7 @@ public class TreeRendererComponent extends Container {
}
public void setRenderComponent(final Component renderComponent) {
remove(renderComponent);
removeAll();
this.renderComponent = renderComponent;
add(renderComponent);
}

88
src/test/java/demo/tree/TreeDemo.java

@ -23,49 +23,83 @@
*/
package demo.tree;
import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.components.OverlayScrollPane;
import com.github.weisj.darklaf.components.SelectableTreeNode;
import demo.ComponentDemo;
import demo.DemoPanel;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
public class TreeDemo extends JFrame {
public class TreeDemo implements ComponentDemo {
public TreeDemo() {
//Todo Rework Demo
super("JTree Demo");
public static void main(final String[] args) {
ComponentDemo.showDemo(new TreeDemo());
}
DefaultMutableTreeNode root = new DefaultMutableTreeNode("States");
DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("Andhra Pradesh");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Vijayawada");
DefaultMutableTreeNode child1 = new SelectableTreeNode("This node can be selected", true);
DefaultMutableTreeNode parent2 = new DefaultMutableTreeNode("Telangana");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Hyderabad");
@Override
public JComponent createComponent() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("Node A");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Leaf A");
DefaultMutableTreeNode child1 = new SelectableTreeNode("Leaf B (boolean)", true);
DefaultMutableTreeNode parent2 = new DefaultMutableTreeNode("Node B");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Leaf C");
// Adding child nodes to parent
parent1.add(child);
parent1.add(child1);
parent2.add(child2);
// Adding parent nodes to root
root.add(parent1);
root.add(parent2);
// Adding root to JTree
JTree tree = new JTree(root);
tree.setEditable(true);
// tree.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
getContentPane().add(new JScrollPane(tree));
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
DemoPanel panel = new DemoPanel(new OverlayScrollPane(tree), new BorderLayout());
JPanel controlPanel = panel.getControls();
controlPanel.setLayout(new GridLayout(5, 2));
controlPanel.add(new JCheckBox("editable") {{
setSelected(tree.isEditable());
addActionListener(e -> tree.setEditable(isSelected()));
}});
controlPanel.add(new JCheckBox("LeftToRight") {{
setSelected(tree.getComponentOrientation().isLeftToRight());
addActionListener(e -> tree.setComponentOrientation(isSelected() ? ComponentOrientation.LEFT_TO_RIGHT
: ComponentOrientation.RIGHT_TO_LEFT));
}});
controlPanel.add(new JCheckBox("show root handles") {{
setSelected(tree.getShowsRootHandles());
addActionListener(e -> tree.setShowsRootHandles(isSelected()));
}});
controlPanel.add(new JCheckBox("JTree.alternateRowColor") {{
setSelected(Boolean.TRUE.equals(tree.getClientProperty("JTree.alternateRowColor")));
addActionListener(e -> tree.putClientProperty("JTree.alternateRowColor", isSelected()));
}});
controlPanel.add(new JCheckBox("JTree.renderBooleanAsCheckBox") {{
setSelected(Boolean.TRUE.equals(tree.getClientProperty("JTree.renderBooleanAsCheckBox")));
addActionListener(e -> tree.putClientProperty("JTree.renderBooleanAsCheckBox", isSelected()));
}});
controlPanel.add(new JLabel());
controlPanel.add(new JLabel("JTree.booleanRenderType:", JLabel.RIGHT));
controlPanel.add(new JComboBox<String>() {{
addItem("checkBox");
addItem("radioButton");
setSelectedItem(tree.getClientProperty("JTree.booleanRenderType"));
addItemListener(e -> tree.putClientProperty("JTree.booleanRenderType", e.getItem()));
}});
controlPanel.add(new JLabel("JTree.lineStyle:", JLabel.RIGHT));
controlPanel.add(new JComboBox<String>() {{
addItem("Dashed");
addItem("None");
addItem("Line");
setSelectedItem("Line");
addItemListener(e -> tree.putClientProperty("JTree.lineStyle", e.getItem()));
}});
tree.setLargeModel(true);
return panel;
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
LafManager.install();
new TreeDemo();
});
@Override
public String getTitle() {
return "Tree Demo";
}
}

Loading…
Cancel
Save