Skip to content

Commit

Permalink
added support for jtables, fixed a color selection bug for jtextfields
Browse files Browse the repository at this point in the history
  • Loading branch information
atarw committed May 24, 2018
1 parent e26ea0a commit ef16f56
Show file tree
Hide file tree
Showing 14 changed files with 472 additions and 149 deletions.
319 changes: 217 additions & 102 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file modified lib/material-ui-swing.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion src/mdlaf/DropShadowBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class DropShadowBorder extends AbstractBorder implements Border {
private boolean showLeftShadow;
private boolean showBottomShadow;
private boolean showRightShadow;

public DropShadowBorder () {
this (UIManager.getColor ("Control"), 1, 5);
}
Expand Down Expand Up @@ -98,7 +99,7 @@ public void paintBorder (Component c, Graphics graphics, int x, int y, int width
Map<Position, BufferedImage> images = getImages (null);

//compute the edges of the component -- not including the border
//Insets borderInsets = getBorderInsets(c);
//Insets borderInsets = getBorderInsets (c);
// int leftEdge = x + borderInsets.left - lineWidth;
// int rightEdge = x + width - borderInsets.right;
// int topEdge = y + borderInsets.top - lineWidth;
Expand Down
15 changes: 15 additions & 0 deletions src/mdlaf/MaterialBorders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mdlaf;

import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.Color;

public class MaterialBorders {

public static final Border LIGHT_LINE_BORDER = BorderFactory.createLineBorder (MaterialColors.LIGHT_GRAY, 1);

public static final Border LIGHT_SHADOW_BORDER = new DropShadowBorder (Color.BLACK, 0, 4, 0.3f, 12, true, true, true, true);
public static final Border DEFAULT_SHADOW_BORDER = new DropShadowBorder (Color.BLACK, 5, 5, 0.3f, 12, true, true, true, true);

private MaterialBorders () {}
}
2 changes: 2 additions & 0 deletions src/mdlaf/MaterialFonts.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ private static Font loadFont (String fontPath) {
throw new RuntimeException ("Font " + fontPath + " wasn't loaded");
}
}

private MaterialFonts () {}
}
26 changes: 9 additions & 17 deletions src/mdlaf/MaterialLookAndFeel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import mdlaf.button.MaterialButtonUI;
import mdlaf.password.MaterialPasswordFieldUI;
import mdlaf.table.MaterialTableHeaderUI;
import mdlaf.table.MaterialTableUI;
import mdlaf.textfield.MaterialTextFieldUI;

import javax.swing.BorderFactory;
import javax.swing.UIDefaults;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicLookAndFeel;
import java.awt.Color;

Expand All @@ -15,6 +16,8 @@ public class MaterialLookAndFeel extends BasicLookAndFeel {
private static final String buttonUI = MaterialButtonUI.class.getCanonicalName ();
private static final String textfieldUI = MaterialTextFieldUI.class.getCanonicalName ();
private static final String passwordFieldUI = MaterialPasswordFieldUI.class.getCanonicalName ();
private static final String tableUI = MaterialTableUI.class.getCanonicalName ();
private static final String tableHeaderUI = MaterialTableHeaderUI.class.getCanonicalName ();

@Override

Expand Down Expand Up @@ -48,6 +51,8 @@ protected void initClassDefaults (UIDefaults table) {
table.put ("ButtonUI", buttonUI);
table.put ("TextFieldUI", textfieldUI);
table.put ("PasswordFieldUI", passwordFieldUI);
table.put ("TableUI", tableUI);
table.put ("TableHeaderUI", tableHeaderUI);
}

@Override
Expand All @@ -63,7 +68,6 @@ protected void initComponentDefaults (UIDefaults table) {
System.setProperty ("swing.aatext", "true");
System.setProperty ("sun.java2d.xrender", "true");

table.put ("Button.font", MaterialFonts.MEDIUM);
table.put ("RadioButton.font", MaterialFonts.REGULAR);
table.put ("CheckBox.font", MaterialFonts.REGULAR);
table.put ("ComboBox.font", MaterialFonts.REGULAR);
Expand All @@ -74,14 +78,8 @@ protected void initComponentDefaults (UIDefaults table) {
table.put ("OptionPane.font", MaterialFonts.REGULAR);
table.put ("Panel.font", MaterialFonts.REGULAR);
table.put ("ScrollPane.font", MaterialFonts.REGULAR);
table.put ("Table.font", MaterialFonts.REGULAR);
table.put ("TableHeader.font", MaterialFonts.REGULAR);
table.put ("TextField.font", MaterialFonts.REGULAR);
table.put ("TextArea.font", MaterialFonts.REGULAR);

Border menuBorder = new DropShadowBorder (Color.BLACK, 0, 5, 0.3f, 12, true, true, true, true);
Border defaultBorder = new DropShadowBorder (Color.BLACK, 5, 5, 0.3f, 12, true, true, true, true);

table.put ("Panel.background", Color.WHITE);
table.put ("Panel.border", BorderFactory.createEmptyBorder ());

Expand All @@ -94,6 +92,7 @@ protected void initComponentDefaults (UIDefaults table) {

table.put ("PopupMenu.border", BorderFactory.createLineBorder (MaterialColors.LIGHT_GRAY, 1));
table.put ("PopupMenu.background", Color.WHITE);

table.put ("Menu.border", BorderFactory.createEmptyBorder (5, 5, 5, 5));
table.put ("Menu.selectionBackground", MaterialColors.LIGHT_GRAY);
table.put ("Menu.selectionForeground", Color.BLACK);
Expand All @@ -104,7 +103,7 @@ protected void initComponentDefaults (UIDefaults table) {
table.put ("Menu.menuPopupOffsetY", 10);

table.put ("MenuBar.background", Color.WHITE);
table.put ("MenuBar.border", menuBorder);
table.put ("MenuBar.border", MaterialBorders.LIGHT_SHADOW_BORDER);

table.put ("SplitPane.border", BorderFactory.createEmptyBorder ());
table.put ("SplitPane.background", Color.WHITE);
Expand All @@ -114,20 +113,13 @@ protected void initComponentDefaults (UIDefaults table) {
table.put ("ScrollPane.background", Color.WHITE);
table.put ("ScrollPane.border", BorderFactory.createEmptyBorder ());

table.put ("TextField.background", MaterialColors.LIGHT_BLUE);

table.put ("PasswordField.background", MaterialColors.LIGHT_BLUE);

table.put ("TextArea.background", MaterialColors.LIGHT_GRAY);
table.put ("TextArea.border", BorderFactory.createEmptyBorder ());
table.put ("TextArea.foreground", Color.BLACK);

table.put ("OptionPane.background", Color.WHITE);
table.put ("OptionPane.border", defaultBorder);
table.put ("OptionPane.border", MaterialBorders.DEFAULT_SHADOW_BORDER);

table.put ("Button.background", MaterialColors.LIGHT_BLUE);
table.put ("Button.foreground", Color.WHITE);
table.put ("Button.highlight", MaterialColors.LIGHT_GRAY);
//table.put ("Button.border", BorderFactory.createEmptyBorder (10, 10, 10, 10));
}
}
14 changes: 8 additions & 6 deletions src/mdlaf/button/MaterialButtonUI.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package mdlaf.button;

import mdlaf.MaterialColors;
import mdlaf.MaterialFonts;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.Timer;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

public class MaterialButtonUI extends BasicButtonUI {

private Timer timer;
private AbstractButton button;
private Graphics g;
private int animationRadius = 0;

public static ComponentUI createUI (final JComponent c) {
return new MaterialButtonUI ();
}
Expand All @@ -28,6 +26,10 @@ public void installUI (JComponent c) {
AbstractButton button = (AbstractButton) c;
button.setOpaque (false);
button.setBorder (BorderFactory.createEmptyBorder (7, 17, 7, 17));

button.setBackground (MaterialColors.LIGHT_BLUE);
button.setForeground (Color.WHITE);
button.setFont (MaterialFonts.MEDIUM);
}

@Override
Expand Down
44 changes: 32 additions & 12 deletions src/mdlaf/password/MaterialPasswordFieldUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class MaterialPasswordFieldUI extends BasicPasswordFieldUI implements FocusListener {
public class MaterialPasswordFieldUI extends BasicPasswordFieldUI implements FocusListener, PropertyChangeListener {

private Color focusedBackground;
private Color unfocusedBackground;
private Color focusedSelectedBackground;
private Color unfocusedSelectedBackground;
private Color focusedSelectionBackground;
private Color unfocusedSelectionBackground;

public static ComponentUI createUI (final JComponent c) {
return new MaterialPasswordFieldUI ();
Expand All @@ -39,20 +41,22 @@ public static ComponentUI createUI (final JComponent c) {
public void installUI (JComponent c) {
super.installUI (c);

JPasswordField textField = (JPasswordField) c;
textField.setOpaque (false);
textField.setBorder (BorderFactory.createEmptyBorder (5, 2, 10, 0));
JPasswordField passwordField = (JPasswordField) c;
passwordField.setOpaque (false);
passwordField.setBorder (BorderFactory.createEmptyBorder (5, 2, 10, 0));
passwordField.setBackground (MaterialColors.LIGHT_BLUE);

this.focusedBackground = textField.getBackground ();
this.focusedBackground = passwordField.getBackground ();
this.unfocusedBackground = MaterialColors.LIGHT_GRAY;

this.focusedSelectedBackground = MaterialColors.bleach (focusedBackground, 0.3f);
this.unfocusedSelectedBackground = unfocusedBackground;
this.focusedSelectionBackground = MaterialColors.bleach (focusedBackground, 0.3f);
this.unfocusedSelectionBackground = unfocusedBackground;
}

@Override
protected void installListeners () {
getComponent ().addFocusListener (this);
getComponent ().addPropertyChangeListener (this);
}

@Override
Expand Down Expand Up @@ -118,15 +122,19 @@ public void paintSafely (Graphics g) {

if (getComponent ().hasFocus ()) {
c.setBackground (focusedBackground);
c.setSelectionColor (focusedSelectedBackground);
c.setSelectionColor (focusedSelectionBackground);
}
else {
c.setBackground (unfocusedBackground);
c.setSelectionColor (unfocusedSelectedBackground);
c.setSelectionColor (unfocusedSelectionBackground);
}

int x = getComponent ().getInsets ().left;
int y = getComponent ().getInsets ().top;
int w = getComponent ().getWidth () - getComponent ().getInsets ().left - getComponent ().getInsets ().right;

g.setColor (c.getBackground ());
g.fillRect (0, c.getHeight () - c.getY (), c.getWidth (), 2);
g.fillRect (x, c.getHeight () - y, w, 2);

super.paintSafely (g);
}
Expand All @@ -151,6 +159,18 @@ public String getPropertyPrefix () {
return "PasswordField";
}

@Override
public void propertyChange (PropertyChangeEvent pce) {
if (pce.getPropertyName ().equals ("background")) {
Color newColor = (Color) pce.getNewValue ();

if (!newColor.equals (focusedBackground) && !newColor.equals (unfocusedBackground)) {
this.focusedBackground = (Color) pce.getNewValue ();
this.focusedSelectionBackground = MaterialColors.bleach (this.focusedBackground, 0.3f);
}
}
}

@Override
public View create (Element elem) {
return new MaterialPasswordView (elem);
Expand Down
21 changes: 21 additions & 0 deletions src/mdlaf/table/MaterialTableCellEditor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mdlaf.table;

import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.Component;

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);

return textField;
}

public MaterialTableCellEditor () {
super (new JTextField ());
}
}
24 changes: 24 additions & 0 deletions src/mdlaf/table/MaterialTableCellRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mdlaf.table;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.Component;

public class MaterialTableCellRenderer extends DefaultTableCellRenderer {

@Override
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JComponent component = (JComponent) super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);

// hides yellow selection highlight
component.setBorder (BorderFactory.createEmptyBorder ());

this.setHorizontalAlignment (SwingConstants.CENTER);
this.setVerticalAlignment (SwingConstants.CENTER);

return component;
}
}
28 changes: 28 additions & 0 deletions src/mdlaf/table/MaterialTableHeaderCellRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mdlaf.table;

import mdlaf.MaterialBorders;
import mdlaf.MaterialColors;
import mdlaf.MaterialFonts;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.Component;

public class MaterialTableHeaderCellRenderer extends DefaultTableCellRenderer {

@Override
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JComponent component = (JComponent) super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);
component.setBorder (BorderFactory.createCompoundBorder (MaterialBorders.LIGHT_LINE_BORDER, BorderFactory.createEmptyBorder (10, 10, 10, 10)));
component.setFont (MaterialFonts.BOLD);
component.setBackground (MaterialColors.LIGHT_GRAY);

this.setHorizontalAlignment (SwingConstants.CENTER);
this.setVerticalAlignment (SwingConstants.CENTER);

return component;
}
}
28 changes: 28 additions & 0 deletions src/mdlaf/table/MaterialTableHeaderUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mdlaf.table;

import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTableHeaderUI;
import javax.swing.table.JTableHeader;
import java.awt.Graphics;

public class MaterialTableHeaderUI extends BasicTableHeaderUI {

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

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

JTableHeader header = (JTableHeader) c;

header.setDefaultRenderer (new MaterialTableHeaderCellRenderer ());
}

@Override
public void paint (Graphics g, JComponent c) {
super.paint (g, c);
}
}
Loading

0 comments on commit ef16f56

Please sign in to comment.