Skip to content

Commit

Permalink
Merge pull request #79 from vincenzopalazzo/fixsomeissue
Browse files Browse the repository at this point in the history
Fix some issue
  • Loading branch information
atarw authored Apr 7, 2019
2 parents 8d8debf + e52089a commit 31e6bb3
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/main/java/mdlaf/MaterialLookAndFeel.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ protected void initComponentDefaults (UIDefaults table) {
table.put("ScrollBar.arrowButtonBorder", MaterialBorders.LIGHT_LINE_BORDER);
table.put("ScrollBar[MouseHover].enable", true);
table.put("ScrollBar[MouseHover].color", MaterialColors.GRAY_400);
table.put("ScrollBar[OnClick].color", MaterialColors.GRAY_500);

table.put ("ScrollPane.background", Color.WHITE);
table.put ("ScrollPane.border", BorderFactory.createEmptyBorder ());
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/mdlaf/animation/MaterialUIMovement.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,20 @@ public static MaterialUITimer getMovement (JComponent c, Color fadeTo, int steps
public static MaterialUITimer getMovement (JComponent c, Color fadeTo) {
return getMovement (c, fadeTo, 5, 1000 / 30);
}

/**
* This method create a new effect mouse hover static, not create a wake effect
* This method is util in all component for click, an example: The button for JSpinner, JCombobox, JScroolbar
* @param c is the component
* @param colorEffect is the color for effect, (For using color, look the MaterialColors class)
* @param colorOnClick is the color of the component on click on it
* @return a new MaterialUIStaticMovement this class implement a only MouseListner for moment
* @author https://github.com/vincenzopalazzo
*/
public static MaterialUIStaticMovement getStaticMovement(JComponent c, Color colorEffect, Color colorOnClick){
if(c == null || colorEffect == null){
throw new IllegalArgumentException("Che input arguments is/are null");
}
return new MaterialUIStaticMovement(c.getBackground(), colorEffect, colorOnClick);
}
}
80 changes: 80 additions & 0 deletions src/main/java/mdlaf/animation/MaterialUIStaticMovement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package mdlaf.animation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
* This class recreate a effect mouse hover static,
* This component not implement a wake effect, and is util in all component for click, an example: The button for JSpinner, JCombobox, JScroolbar
* @author https://github.com/vincenzopalazzo
*/
class MaterialUIStaticMovement implements MouseListener {

private Color before;
private Color after;
private Color strongOnClick;

public MaterialUIStaticMovement(Color before, Color after, Color strongOnClick) {
this.before = before;
this.after = after;
this.strongOnClick = strongOnClick;
}

@Override
public void mouseClicked(MouseEvent e) {
/*For effect click, need create a timer ?*/
/* if(e == null){
throw new IllegalArgumentException("MouseEvent null");
}
setColorComponent(e, after);*/
}

@Override
public void mousePressed(MouseEvent e) {
/*For effect click, need create a timer ?*/
if(e == null){
throw new IllegalArgumentException("MouseEvent null");
}
setColorComponent(e, strongOnClick);
}

@Override
public void mouseReleased(MouseEvent e) {
/*For effect click, need create a timer ?*/
if(e == null){
throw new IllegalArgumentException("MouseEvent null");
}
setColorComponent(e, after);
}

@Override
public void mouseEntered(MouseEvent e) {
if(e == null){
throw new IllegalArgumentException("MouseEvent null");
}
setColorComponent(e, after);
}

@Override
public void mouseExited(MouseEvent e) {
if(e == null){
throw new IllegalArgumentException("MouseEvent null");
}
setColorComponent(e, before);
}

/***
* This is service method for recicle code
*/
private void setColorComponent(MouseEvent e, Color colorComponent){
if(e == null || colorComponent == null){
throw new IllegalArgumentException("The argument function is/are null");
}
JComponent component = (JComponent) e.getSource();
if(component.isEnabled()){
component.setBackground(colorComponent);
}
}
}
60 changes: 37 additions & 23 deletions src/main/java/mdlaf/components/scrollbar/MaterialScrollBarUI.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package mdlaf.components.scrollbar;

import mdlaf.animation.MaterialUIMovement;
import mdlaf.utils.MaterialColors;
import mdlaf.utils.MaterialDrawingUtils;
import mdlaf.utils.MaterialManagerListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.Graphics;
import java.awt.*;

/*
* Contributed by https://github.com/downToHell
* Contributed for refactoring by https://github.com/vincenzopalazzo
* */

public class MaterialScrollBarUI extends BasicScrollBarUI {
Expand All @@ -26,14 +27,6 @@ public static ComponentUI createUI (JComponent c) {
@Override
public void installUI (JComponent c) {
super.installUI (c);

JScrollBar scrollBar = (JScrollBar) c;
scrollBar.setFont (UIManager.getFont ("ScrollBar.font"));
trackColor = UIManager.getColor ("ScrollBar.track");
thumbColor = UIManager.getColor ("ScrollBar.thumb");
thumbDarkShadowColor = UIManager.getColor ("ScrollBar.thumbDarkShadow");
thumbHighlightColor = UIManager.getColor ("ScrollBar.thumbHighlight");
thumbLightShadowColor = UIManager.getColor ("ScrollBar.thumbShadow");
}

@Override
Expand All @@ -43,29 +36,50 @@ public void paint (Graphics g, JComponent c) {

@Override
protected JButton createDecreaseButton (int orientation) {
JButton button = new BasicArrowButton (orientation);

MaterialManagerListener.removeAllMouseListener(button);
button.setOpaque (true);
button.setBackground (UIManager.getColor ("ScrollBar.arrowButtonBackground"));
button.setBorder (UIManager.getBorder ("ScrollBar.arrowButtonBorder"));
if(UIManager.getBoolean("ScrollBar[MouseHover].enable")){
button.addMouseListener(MaterialUIMovement.getMovement(button, UIManager.getColor("ScrollBar[MouseHover].color")));
}
return button;
return installButton(orientation);
}

@Override
protected JButton createIncreaseButton (int orientation) {
JButton button = new BasicArrowButton (orientation);
return installButton(orientation);
}

@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
super.paintTrack(g, c, trackBounds);
g.setColor(UIManager.getColor ("ScrollBar.track"));
}

@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
super.paintThumb(g, c, thumbBounds);
g.setColor(UIManager.getColor ("ScrollBar.thumb"));
}

/**
* Method service for not duplicate code
* @author https://github.com/vincenzopalazzo
* @param orientation
* @return JButton with correct orientation
*/
private JButton installButton(int orientation){
JButton button = new BasicArrowButton (orientation);
MaterialManagerListener.removeAllMouseListener(button);
button.setOpaque (true);
button.setBackground (UIManager.getColor ("ScrollBar.arrowButtonBackground"));
button.setBorder (UIManager.getBorder ("ScrollBar.arrowButtonBorder"));
if(UIManager.getBoolean("ScrollBar[MouseHover].enable")){
button.addMouseListener(MaterialUIMovement.getMovement(button, UIManager.getColor("ScrollBar[MouseHover].color")));
button.addMouseListener(MaterialUIMovement.getStaticMovement(button,UIManager.getColor("ScrollBar[MouseHover].color"),
UIManager.getColor("ScrollBar[OnClick].color")));
}
button.setBorder (UIManager.getBorder ("ScrollBar.arrowButtonBorder"));
return button;
}

@Override
protected void configureScrollBarColors() {
super.configureScrollBarColors();
thumbDarkShadowColor = UIManager.getColor ("ScrollBar.thumbDarkShadow");
thumbHighlightColor = UIManager.getColor ("ScrollBar.thumbHighlight");
thumbLightShadowColor = UIManager.getColor ("ScrollBar.thumbShadow");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void actionPerformed(ActionEvent e) {
Action delete = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (getComponent().getSelectedText() == null) {
if (getComponent().getSelectedText() != null) {
int pos = getComponent().getCaretPosition() - 1;

if (pos >= 0) {
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/mdlaf/utils/MaterialDrawingUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package mdlaf.utils;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.*;
import java.util.Map;

public class MaterialDrawingUtils {
Expand All @@ -16,12 +13,11 @@ public class MaterialDrawingUtils {

public static Graphics getAliasedGraphics (Graphics g) {
Map<RenderingHints.Key, Object> hints = (Map<RenderingHints.Key, Object>) Toolkit.getDefaultToolkit ().getDesktopProperty ("awt.font.desktophints");
hints.put (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);


hints.put (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

Graphics2D g2d = (Graphics2D) g;
g2d.addRenderingHints (hints);

//g2d.addRenderingHints (new RenderingHints (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
return g2d;
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/mdlaf/utils/MaterialFonts.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mdlaf.utils;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -24,9 +23,14 @@ public class MaterialFonts {
public static final Font THIN = loadFont ("/fonts/NotoSans-Thin.ttf");
public static final Font THIN_ITALIC = loadFont ("/fonts/NotoSans-ThinItalic.ttf");

/**
* Fix the problem with this post
* https://stackoverflow.com/questions/5829703/java-getting-a-font-with-a-specific-height-in-pixels
* @author https://github.com/vincenzopalazzo
*/
private static Font loadFont (String fontPath) {
if (fontSettings.isEmpty ()) {
fontSettings.put (TextAttribute.SIZE, 14f);
fontSettings.put (TextAttribute.SIZE, new Float( 11 * Toolkit.getDefaultToolkit().getScreenResolution() / 72.0));
fontSettings.put (TextAttribute.KERNING, TextAttribute.KERNING_ON);
}

Expand Down

0 comments on commit 31e6bb3

Please sign in to comment.