Skip to content

Commit

Permalink
Moved string painting to utility method.
Browse files Browse the repository at this point in the history
Refactored more clientproperty checking.
  • Loading branch information
weisJ committed Apr 27, 2020
1 parent f23a523 commit cc32dda
Show file tree
Hide file tree
Showing 23 changed files with 166 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
import java.awt.geom.Path2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.*;
import javax.swing.text.View;

import com.github.weisj.darklaf.ui.html.DarkHTML;

public class PaintUtil {

public static final Color TRANSPARENT_COLOR = new Color(0x0, true);
Expand Down Expand Up @@ -230,6 +235,29 @@ public static void drawRect(final Graphics g, final int x, final int y,
g.fillRect(x, y + height - thickness, width, thickness);
}

public static void drawString(final Graphics g, final JComponent c,
final String text, final Rectangle textRect,
final FontMetrics fm,
final PaintMethod paintMethod) {
GraphicsContext context = GraphicsUtil.setupAntialiasing(g);
g.setClip(textRect);
if (text != null && !text.equals("")) {
View v = (View) c.getClientProperty(DarkHTML.propertyKey);
if (v != null) {
v.paint(g, textRect);
} else {
textRect.y += fm.getAscent();
paintMethod.paintText(g, c, textRect, text);
}
}
context.restore();
}

public interface PaintMethod {

void paintText(final Graphics g, final JComponent c, final Rectangle rect, final String text);
}

public enum Outline {
error {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ static boolean isThin(final Component c) {
static boolean isBorderlessVariant(final Component c) {
if (isBorderlessRectangular(c)) return true;
if (c instanceof JButton) {
JButton b = (JButton) c;
return PropertyUtil.isPropertyEqual(b, KEY_VARIANT, VARIANT_BORDERLESS)
return PropertyUtil.isPropertyEqual(c, KEY_VARIANT, VARIANT_BORDERLESS)
|| doConvertToBorderless((AbstractButton) c);
}
return false;
Expand Down Expand Up @@ -104,9 +103,6 @@ static boolean isDefaultButton(final JComponent c) {
}

static JComponent getNeighbour(final String key, final Component comp) {
if (!(comp instanceof JComponent)) return null;
Object obj = ((JComponent) comp).getClientProperty(key);
if (obj instanceof JComponent) return (JComponent) obj;
return null;
return PropertyUtil.getObject(comp, key, JComponent.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.github.weisj.darklaf.graphics.PaintUtil;
import com.github.weisj.darklaf.util.AlignmentExt;
import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.PropertyUtil;

/**
* @author Konstantin Bulenkov
Expand Down Expand Up @@ -121,11 +122,7 @@ protected int getFocusArc(final Component c) {
}

public static AlignmentExt getCornerFlag(final Component component) {
if (component instanceof JComponent) {
Object align = ((JComponent) component).getClientProperty(DarkButtonUI.KEY_CORNER);
return align instanceof AlignmentExt ? (AlignmentExt) align : null;
}
return null;
return PropertyUtil.getObject(component, DarkButtonUI.KEY_CORNER, AlignmentExt.class, null);
}

protected int getShadowSize(final JComponent c) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import javax.swing.plaf.basic.BasicButtonListener;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View;

import sun.swing.SwingUtilities2;

Expand All @@ -46,7 +44,10 @@
import com.github.weisj.darklaf.graphics.PaintUtil;
import com.github.weisj.darklaf.ui.togglebutton.DarkToggleButtonKeyHandler;
import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonFocusNavigationActions;
import com.github.weisj.darklaf.util.*;
import com.github.weisj.darklaf.util.AlignmentExt;
import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.PropertyKey;
import com.github.weisj.darklaf.util.PropertyUtil;

/**
* @author Konstantin Bulenkov
Expand Down Expand Up @@ -265,29 +266,19 @@ protected void paintText(final Graphics g, final JComponent c,
AbstractButton button = (AbstractButton) c;
ButtonModel model = button.getModel();
g.setColor(getForeground(button));
FontMetrics metrics = SwingUtilities2.getFontMetrics(c, g);
int mnemonicIndex = button.getDisplayedMnemonicIndex();
if (!model.isEnabled()) {
mnemonicIndex = -1;
}
SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + metrics.getAscent() + getTextShiftOffset());
textRect.y + getTextShiftOffset());
config.restore();
}

protected void paintText(final Graphics g, final AbstractButton b, final JComponent c, final String text) {
GraphicsContext context = GraphicsUtil.setupAntialiasing(g);
g.setClip(textRect);
if (text != null && !text.equals("")) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, textRect);
} else {
paintText(g, b, textRect, text);
}
}
context.restore();
PaintUtil.drawString(g, b, text, textRect, SwingUtilities2.getFontMetrics(b, g),
(g1, c1, r1, t1) -> paintText(g1, b, r1, t1));
}

protected void paintIcon(final Graphics g, final AbstractButton b, final JComponent c) {
Expand Down Expand Up @@ -360,11 +351,9 @@ protected Color getBackgroundColor(final JComponent c) {
}

protected Color getBorderlessBackground(final AbstractButton c) {
Object colorHover = c.getClientProperty(KEY_HOVER_COLOR);
Object colorClick = c.getClientProperty(KEY_CLICK_COLOR);
boolean armed = c.getModel().isArmed();
return armed ? colorClick instanceof Color ? (Color) colorClick : borderlessClick
: colorHover instanceof Color ? (Color) colorHover : borderlessHover;
return armed ? PropertyUtil.getColor(c, KEY_CLICK_COLOR, borderlessClick)
: PropertyUtil.getColor(c, KEY_HOVER_COLOR, borderlessHover);
}

protected Color getBorderlessOutline(final AbstractButton c) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public Dimension getPreferredSize() {
}
FontMetrics fm = host.getFontMetrics(getFont());

int ascent = fm.getAscent();
int height = fm.getHeight();
int width = SwingUtilities2.stringWidth(host, fm, getSampleText());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicLabelUI;
import javax.swing.text.View;

import sun.swing.SwingUtilities2;

import com.github.weisj.darklaf.graphics.GraphicsContext;
import com.github.weisj.darklaf.graphics.GraphicsUtil;
import com.github.weisj.darklaf.graphics.PaintUtil;
import com.github.weisj.darklaf.ui.cell.CellUtil;
import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.PropertyKey;
Expand Down Expand Up @@ -101,21 +100,13 @@ public void paint(final Graphics g, final JComponent c) {
icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
}

if (text != null) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, paintTextR);
PaintUtil.drawString(g, c, clippedText, paintTextR, fm, (g2, c2, rect, t) -> {
if (label.isEnabled()) {
paintEnabledText(label, g2, t, rect.x, rect.y);
} else {
int textX = paintTextR.x;
int textY = paintTextR.y + fm.getAscent();

if (label.isEnabled()) {
paintEnabledText(label, g, clippedText, textX, textY);
} else {
paintDisabledText(label, g, clippedText, textX, textY);
}
paintDisabledText(label, g2, t, rect.x, rect.y);
}
}
});
config.restore();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.swing.*;

import com.github.weisj.darklaf.graphics.Animator;
import com.github.weisj.darklaf.util.PropertyUtil;

public class DarkScrollBarListener extends MouseAdapter implements AdjustmentListener, ScrollBarConstants {

Expand Down Expand Up @@ -88,12 +89,12 @@ public void mouseWheelMoved(final MouseWheelEvent e) {
if (scrollbar.getOrientation() == JScrollBar.VERTICAL && !e.isShiftDown()
|| scrollbar.getOrientation() == JScrollBar.HORIZONTAL && e.isShiftDown()) {
scrollbar.setValueIsAdjusting(true);
Object sp = scrollbar.getClientProperty(KEY_SCROLL_PANE_PARENT);
JScrollPane sp = PropertyUtil.getObject(scrollbar, KEY_SCROLL_PANE_PARENT, JScrollPane.class);
if (scrollbar.getParent() instanceof JScrollPane) {
ScrollBarUtil.doScroll(scrollbar, ((JScrollPane) scrollbar.getParent()).getViewport(), e,
scrollbar.getParent().getComponentOrientation().isLeftToRight());
} else if (sp instanceof JScrollPane) {
ScrollBarUtil.doScroll(scrollbar, ((JScrollPane) sp).getViewport(), e,
} else if (sp != null) {
ScrollBarUtil.doScroll(scrollbar, sp.getViewport(), e,
scrollbar.getParent().getComponentOrientation().isLeftToRight());
} else {
ScrollBarUtil.doScroll(scrollbar, null, e, scrollbar.getComponentOrientation().isLeftToRight());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@

import javax.swing.*;

import com.github.weisj.darklaf.util.PropertyUtil;

public interface ScrollBarConstants {
String KEY_SCROLL_PANE_PARENT = "JScrollBar.scrollPaneParent";
String KEY_FAST_WHEEL_SCROLLING = "JScrollBar.fastWheelScrolling";
String KEY_SMALL = "JComponent.small";

static boolean isSmall(final JScrollBar scrollBar) {
return scrollBar.getClientProperty(KEY_SMALL) == Boolean.TRUE;
return PropertyUtil.getBooleanProperty(scrollBar, KEY_SMALL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,7 @@ private void paintPlainSliderThumb(final Graphics2D g) {
}

protected boolean isPlainThumb() {
Boolean paintThumbArrowShape = (Boolean) slider.getClientProperty(KEY_THUMB_ARROW_SHAPE);
return (!slider.getPaintTicks() && paintThumbArrowShape == null) ||
paintThumbArrowShape == Boolean.FALSE;
return !slider.getPaintTicks() || !PropertyUtil.getBooleanProperty(slider, KEY_THUMB_ARROW_SHAPE, true);
}

private void paintSliderThumb(final Graphics2D g) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ protected Color getDragBorderColor() {
}

protected Action getNewTabAction() {
Object action = tabPane.getClientProperty(KEY_NEW_TAB_ACTION);
return action instanceof Action ? (Action) action : null;
return PropertyUtil.getObject(tabPane, KEY_NEW_TAB_ACTION, Action.class);
}

@Override
Expand Down Expand Up @@ -724,14 +723,9 @@ protected void installDefaults() {
moreTabsIcon = UIManager.getIcon("TabbedPane.moreTabs.icon");
newTabIcon = UIManager.getIcon("TabbedPane.newTab.icon");

Object ins = tabPane.getClientProperty(KEY_TAB_AREA_INSETS);
if (ins instanceof Insets) {
tabAreaInsets = (Insets) ins;
}
ins = tabPane.getClientProperty(KEY_CONTENT_BORDER_INSETS);
if (ins instanceof Insets) {
contentBorderInsets = (Insets) ins;
}
tabAreaInsets = PropertyUtil.getObject(tabPane, KEY_TAB_AREA_INSETS, Insets.class, tabAreaInsets);
contentBorderInsets = PropertyUtil.getObject(tabPane, KEY_CONTENT_BORDER_INSETS, Insets.class,
contentBorderInsets);
installComponent(KEY_LEADING_COMP, c -> leadingComp = c);
installComponent(KEY_TRAILING_COMP, c -> trailingComp = c);
installComponent(KEY_NORTH_COMP, c -> northComp = c);
Expand All @@ -742,9 +736,9 @@ protected void installDefaults() {
}

protected void installComponent(final String key, final Consumer<Component> setter) {
Object comp = tabPane.getClientProperty(key);
if (comp instanceof Component) {
Component wrapped = wrapClientComponent((Component) comp);
Component comp = PropertyUtil.getObject(tabPane, key, Component.class);
if (comp != null) {
Component wrapped = wrapClientComponent(comp);
setter.accept(wrapped);
tabPane.add(wrapped);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.PropertyKey;
import com.github.weisj.darklaf.util.PropertyUtil;

public class TabbedPaneHandler implements ChangeListener, ContainerListener, FocusListener,
MouseListener, MouseMotionListener, PropertyChangeListener {
Expand Down Expand Up @@ -261,9 +262,8 @@ public void componentRemoved(final ContainerEvent e) {
// currently no IndexPropertyChangeEvent. Once
// IndexPropertyChangeEvents have been added this code should be
// modified to use it.
Integer indexObj = (Integer) tp.getClientProperty("__index_to_remove__");
if (indexObj != null) {
int index = indexObj;
int index = PropertyUtil.getInteger(tp, "__index_to_remove__", -1);
if (index >= 0) {
if (ui.htmlViews != null && ui.htmlViews.size() > index) {
ui.htmlViews.removeElementAt(index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View;

import sun.swing.SwingUtilities2;

import com.github.weisj.darklaf.components.tabframe.JTabFrame;
import com.github.weisj.darklaf.components.tabframe.TabFrameTab;
import com.github.weisj.darklaf.components.tabframe.TabFrameTabLabel;
import com.github.weisj.darklaf.graphics.PaintUtil;
import com.github.weisj.darklaf.icons.RotatableIcon;
import com.github.weisj.darklaf.listener.HoverListener;
import com.github.weisj.darklaf.ui.label.DarkLabelUI;
Expand Down Expand Up @@ -98,21 +97,13 @@ public void paint(final Graphics g, final JComponent c) {
icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
}

if (text != null) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, paintTextR);
PaintUtil.drawString(g, c, clippedText, paintTextR, fm, (g2, c2, rect, t) -> {
if (label.isEnabled()) {
paintEnabledText(label, g2, t, rect.x, rect.y);
} else {
int textX = paintTextR.x;
int textY = paintTextR.y + fm.getAscent();

if (label.isEnabled()) {
paintEnabledText(label, g, clippedText, textX, textY);
} else {
paintDisabledText(label, g, clippedText, textX, textY);
}
paintDisabledText(label, g2, t, rect.x, rect.y);
}
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.github.weisj.darklaf.ui.cell.CellUtil;
import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.PropertyKey;
import com.github.weisj.darklaf.util.PropertyUtil;

/**
* @author Jannis Weis
Expand Down Expand Up @@ -485,17 +486,13 @@ public void mousePressed(final MouseEvent e) {
}

protected JFileChooser getFileChooser() {
Object obj = table.getClientProperty(DarkTableUI.KEY_FILE_CHOOSER_PARENT);
if (obj instanceof Supplier<?>) {
Object supplied = ((Supplier<?>) obj).get();
return supplied instanceof JFileChooser ? (JFileChooser) supplied : null;
}
return null;
Object obj = PropertyUtil.getObject(table, DarkTableUI.KEY_FILE_CHOOSER_PARENT, Supplier.class, Object::new)
.get();
return obj instanceof JFileChooser ? (JFileChooser) obj : null;
}

protected Integer getFileNameColumnIndex() {
Object obj = table.getClientProperty(DarkTableUI.KEY_FILENAME_COLUMN_INDEX);
return obj instanceof Integer ? (Integer) obj : 0;
return PropertyUtil.getInteger(table, DarkTableUI.KEY_FILENAME_COLUMN_INDEX);
}

protected void startEditing(final int row, final int column) {
Expand Down
Loading

0 comments on commit cc32dda

Please sign in to comment.