Skip to content

Commit

Permalink
added jtree support
Browse files Browse the repository at this point in the history
  • Loading branch information
atarw committed Jun 9, 2018
1 parent 76aa2be commit 761d071
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 107 deletions.
255 changes: 156 additions & 99 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file modified lib/material-ui-swing.jar
Binary file not shown.
Binary file added src/imgs/down_arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/imgs/right_arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/mdlaf/MaterialColors.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public static Color bleach (Color color, float amount) {
int blue = (int) ((color.getBlue () * (1 - amount) / 255 + amount) * 255);
return new Color (red, green, blue);
}

private MaterialColors () {}
}
24 changes: 24 additions & 0 deletions src/mdlaf/MaterialImages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mdlaf;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class MaterialImages {

public static final BufferedImage RIGHT_ARROW = loadImg ("/imgs/right_arrow.png");
public static final BufferedImage DOWN_ARROW = loadImg ("/imgs/down_arrow.png");

private static BufferedImage loadImg (String imgPath) {
try (InputStream inputStream = MaterialImages.class.getResourceAsStream (imgPath)) {
return ImageIO.read (inputStream);
}
catch (IOException e) {
e.printStackTrace ();
throw new RuntimeException ("Image " + imgPath + " wasn't loaded");
}
}

private MaterialImages () {}
}
9 changes: 6 additions & 3 deletions src/mdlaf/MaterialLookAndFeel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mdlaf.table.MaterialTableHeaderUI;
import mdlaf.table.MaterialTableUI;
import mdlaf.textfield.MaterialTextFieldUI;
import mdlaf.tree.MaterialTreeUI;

import javax.swing.BorderFactory;
import javax.swing.UIDefaults;
Expand All @@ -18,21 +19,22 @@ public class MaterialLookAndFeel extends BasicLookAndFeel {
private static final String passwordFieldUI = MaterialPasswordFieldUI.class.getCanonicalName ();
private static final String tableUI = MaterialTableUI.class.getCanonicalName ();
private static final String tableHeaderUI = MaterialTableHeaderUI.class.getCanonicalName ();
private static final String treeUI = MaterialTreeUI.class.getCanonicalName ();

@Override

public String getName () {
return "mdlaf.MaterialLookAndFeel";
return "Material Look And Feel";
}

@Override
public String getID () {
return "mdlaf.MaterialLookAndFeel";
return "MaterialLookAndFeel";
}

@Override
public String getDescription () {
return "A modern, Material Design UI for Java Swing";
return "A modern Material Design UI for Java Swing";
}

@Override
Expand All @@ -53,6 +55,7 @@ protected void initClassDefaults (UIDefaults table) {
table.put ("PasswordFieldUI", passwordFieldUI);
table.put ("TableUI", tableUI);
table.put ("TableHeaderUI", tableHeaderUI);
table.put ("TreeUI", treeUI);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/mdlaf/button/MaterialButtonUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ private void paintBackground (final Graphics g, final JComponent c) {
g.setColor (c.getBackground ());
g.fillRoundRect (0, 0, c.getWidth (), c.getHeight (), 7, 7);
}

@Override
public String getPropertyPrefix () {
return "Button";
}
}
4 changes: 2 additions & 2 deletions src/mdlaf/password/MaterialPasswordFieldUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ public void paintBackground (final Graphics g) {

@Override
public void focusGained (FocusEvent e) {
getComponent ().setBackground (focusedBackground);
e.getComponent ().setBackground (focusedBackground);
}

@Override
public void focusLost (FocusEvent e) {
getComponent ().setBackground (unfocusedBackground);
e.getComponent ().setBackground (unfocusedBackground);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/mdlaf/table/MaterialTableCellEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MaterialTableCellEditor extends DefaultCellEditor {
@Override
public Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
JTextField textField = (JTextField) super.getTableCellEditorComponent (table, value, isSelected, rowIndex, vColIndex);
textField.setText ((String) value);
textField.setText (value.toString ());

return textField;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mdlaf/textfield/MaterialTextFieldUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ public void paintBackground (final Graphics g) {

@Override
public void focusGained (FocusEvent e) {
getComponent ().setBackground (focusedBackground);
e.getComponent ().setBackground (focusedBackground);
}

@Override
public void focusLost (FocusEvent e) {
getComponent ().setBackground (unfocusedBackground);
e.getComponent ().setBackground (unfocusedBackground);
}

@Override
Expand Down
48 changes: 48 additions & 0 deletions src/mdlaf/tree/MaterialTreeCellEditor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mdlaf.tree;

import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class MaterialTreeCellEditor extends DefaultTreeCellEditor {

private JTextField textField;

private void init () {
textField = new JTextField ();

textField.addKeyListener (new KeyAdapter () {
@Override
public void keyTyped (KeyEvent e) {
if (e.getKeyChar () == KeyEvent.VK_ENTER)
stopCellEditing ();
}
});
}

@Override
public Component getTreeCellEditorComponent (JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
textField.setText (value.toString ());
return textField;
}

@Override
public Object getCellEditorValue () {
return textField.getText ();
}

public MaterialTreeCellEditor (JTree tree, DefaultTreeCellRenderer renderer) {
super (tree, renderer);
init ();
}

public MaterialTreeCellEditor (JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor) {
super (tree, renderer, editor);
init ();
}
}
39 changes: 39 additions & 0 deletions src/mdlaf/tree/MaterialTreeCellRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package mdlaf.tree;

import mdlaf.MaterialColors;
import mdlaf.MaterialFonts;
import mdlaf.MaterialImages;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.Color;
import java.awt.Component;

public class MaterialTreeCellRenderer extends DefaultTreeCellRenderer {

@Override
public Component getTreeCellRendererComponent (JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
JComponent component = (JComponent) super.getTreeCellRendererComponent (tree, value, isSelected, expanded, leaf, row, hasFocus);
component.setBorder (BorderFactory.createEmptyBorder (5, 2, 5, 2));

return component;
}

public MaterialTreeCellRenderer () {
setTextSelectionColor (Color.BLACK);
setTextNonSelectionColor (Color.BLACK);

setBackgroundSelectionColor (MaterialColors.LIGHT_GRAY);
setBackgroundNonSelectionColor (Color.WHITE);

setBorderSelectionColor (null);

setClosedIcon (new ImageIcon (MaterialImages.RIGHT_ARROW));
setOpenIcon (new ImageIcon (MaterialImages.DOWN_ARROW));

setFont (MaterialFonts.REGULAR);
}
}
37 changes: 37 additions & 0 deletions src/mdlaf/tree/MaterialTreeUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mdlaf.tree;

import mdlaf.MaterialFonts;

import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;

public class MaterialTreeUI extends BasicTreeUI {

public static ComponentUI createUI (final JComponent c) {
return new MaterialTreeUI ();
}

@Override
public void installUI (JComponent c) {
super.installUI (c);
JTree tree = (JTree) c;

DefaultTreeCellRenderer renderer = new MaterialTreeCellRenderer ();
TreeCellEditor editor = new MaterialTreeCellEditor (tree, renderer);

tree.setCellRenderer (renderer);
tree.setCellEditor (editor);

tree.setFont (MaterialFonts.REGULAR);
tree.setRowHeight (0);
tree.setInvokesStopCellEditing (true);

MaterialTreeUI ui = (MaterialTreeUI) tree.getUI ();
ui.setLeftChildIndent (10);
ui.setRightChildIndent (10);
}
}

0 comments on commit 761d071

Please sign in to comment.