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.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Enumeration; import java.util.Enumeration;
/** /**
* @author Konstantin Bulenkov * @author Konstantin Bulenkov
* @author Jannis Weis * @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 TREE_TABLE_TREE_KEY = "JTree.treeTableTree";
public static final String STRIPED_CLIENT_PROPERTY = "JTree.alternateRowColor"; public static final String STRIPED_CLIENT_PROPERTY = "JTree.alternateRowColor";
@ -135,13 +137,9 @@ public class DarkTreeUI extends BasicTreeUI {
@Override @Override
protected void completeUIInstall() { protected void completeUIInstall() {
super.completeUIInstall(); super.completeUIInstall();
myOldRepaintAllRowValue = UIManager.getBoolean("Tree.repaintWholeRow"); myOldRepaintAllRowValue = UIManager.getBoolean("Tree.repaintWholeRow");
UIManager.put("Tree.repaintWholeRow", true); UIManager.put("Tree.repaintWholeRow", true);
tree.putClientProperty("JTree.alternateRowColor", UIManager.getBoolean("Tree.alternateRowColor")); tree.putClientProperty("JTree.alternateRowColor", UIManager.getBoolean("Tree.alternateRowColor"));
tree.setShowsRootHandles(true);
tree.addMouseListener(mySelectionListener);
} }
@Override @Override
@ -164,6 +162,15 @@ public class DarkTreeUI extends BasicTreeUI {
tree.putClientProperty("JTree.renderBooleanAsCheckBox", tree.putClientProperty("JTree.renderBooleanAsCheckBox",
UIManager.getBoolean("Tree.renderBooleanAsCheckBox")); UIManager.getBoolean("Tree.renderBooleanAsCheckBox"));
tree.putClientProperty("JTree.booleanRenderType", UIManager.getString("Tree.booleanRenderType")); 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 @Override
@ -320,11 +327,16 @@ public class DarkTreeUI extends BasicTreeUI {
} }
@Override @Override
public void uninstallUI(final JComponent c) { protected void uninstallDefaults() {
super.uninstallUI(c); super.uninstallDefaults();
UIManager.put("Tree.repaintWholeRow", myOldRepaintAllRowValue); UIManager.put("Tree.repaintWholeRow", myOldRepaintAllRowValue);
c.removeMouseListener(mySelectionListener); }
@Override
protected void uninstallListeners() {
super.uninstallListeners();
tree.removeMouseListener(mySelectionListener);
tree.removePropertyChangeListener(this);
} }
@Override @Override
@ -674,6 +686,20 @@ public class DarkTreeUI extends BasicTreeUI {
&& (SystemInfo.isMac ? e.isMetaDown() : e.isControlDown()) && !e.isPopupTrigger(); && (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 { 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) { public void setRenderComponent(final Component renderComponent) {
remove(renderComponent); removeAll();
this.renderComponent = renderComponent; this.renderComponent = renderComponent;
add(renderComponent); add(renderComponent);
} }

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

@ -23,49 +23,83 @@
*/ */
package demo.tree; package demo.tree;
import com.github.weisj.darklaf.LafManager; import com.github.weisj.darklaf.components.OverlayScrollPane;
import com.github.weisj.darklaf.components.SelectableTreeNode; import com.github.weisj.darklaf.components.SelectableTreeNode;
import demo.ComponentDemo;
import demo.DemoPanel;
import javax.swing.*; import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
public class TreeDemo extends JFrame { public class TreeDemo implements ComponentDemo {
public TreeDemo() { public static void main(final String[] args) {
//Todo Rework Demo ComponentDemo.showDemo(new TreeDemo());
super("JTree Demo"); }
DefaultMutableTreeNode root = new DefaultMutableTreeNode("States"); @Override
DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("Andhra Pradesh"); public JComponent createComponent() {
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Vijayawada"); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child1 = new SelectableTreeNode("This node can be selected", true); DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("Node A");
DefaultMutableTreeNode parent2 = new DefaultMutableTreeNode("Telangana"); DefaultMutableTreeNode child = new DefaultMutableTreeNode("Leaf A");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Hyderabad"); 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(child);
parent1.add(child1); parent1.add(child1);
parent2.add(child2); parent2.add(child2);
// Adding parent nodes to root
root.add(parent1); root.add(parent1);
root.add(parent2); root.add(parent2);
// Adding root to JTree
JTree tree = new JTree(root); JTree tree = new JTree(root);
tree.setEditable(true); DemoPanel panel = new DemoPanel(new OverlayScrollPane(tree), new BorderLayout());
// tree.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); JPanel controlPanel = panel.getControls();
controlPanel.setLayout(new GridLayout(5, 2));
getContentPane().add(new JScrollPane(tree)); controlPanel.add(new JCheckBox("editable") {{
setSize(300, 300); setSelected(tree.isEditable());
setLocationRelativeTo(null); addActionListener(e -> tree.setEditable(isSelected()));
setVisible(true); }});
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) { @Override
SwingUtilities.invokeLater(() -> { public String getTitle() {
LafManager.install(); return "Tree Demo";
new TreeDemo();
});
} }
} }

Loading…
Cancel
Save