Browse Source

Merge pull request #26 from hendriks73/master

Support for icon-only buttons/fixed listener (un)installation in TextFieldUI
master
Konstantin Bulenkov 8 years ago committed by GitHub
parent
commit
d702f8fa67
  1. BIN
      build/darcula.jar
  2. 2
      src/com/bulenkov/darcula/darcula.properties
  3. 12
      src/com/bulenkov/darcula/ui/DarculaButtonUI.java
  4. 35
      src/com/bulenkov/darcula/ui/DarculaPasswordFieldUI.java
  5. 114
      src/com/bulenkov/darcula/ui/DarculaTextFieldUI.java
  6. 30
      src/com/bulenkov/darcula/ui/DarculaToggleButtonUI.java

BIN
build/darcula.jar

Binary file not shown.

2
src/com/bulenkov/darcula/darcula.properties

@ -144,6 +144,8 @@ Button.darcula.selection.color2=233143
Button.darcula.selectedButtonForeground=bbbbbb
Button.darcula.disabledText.shadow=00000000
ToggleButtonUI=com.bulenkov.darcula.ui.DarculaToggleButtonUI
MenuItem.acceleratorForeground=eeeeee
PopupMenu.translucentBackground=3c3f41

12
src/com/bulenkov/darcula/ui/DarculaButtonUI.java

@ -42,14 +42,16 @@ public class DarculaButtonUI extends BasicButtonUI {
@Override
public void paint(Graphics g, JComponent c) {
final AbstractButton button = (AbstractButton) c;
final ButtonModel model = button.getModel();
final Border border = c.getBorder();
final GraphicsConfig config = GraphicsUtil.setupAAPainting(g);
final boolean square = isSquare(c);
if (c.isEnabled() && border != null) {
if (c.isEnabled() && border != null && button.isContentAreaFilled()) {
final Insets ins = border.getBorderInsets(c);
final int yOff = (ins.top + ins.bottom) / 4;
if (!square) {
if (((JButton)c).isDefaultButton()) {
if (c instanceof JButton && ((JButton)c).isDefaultButton() || model.isSelected()) {
((Graphics2D)g).setPaint(new GradientPaint(0, 0, getSelectedButtonColor1(), 0, c.getHeight(), getSelectedButtonColor2()));
}
else {
@ -63,8 +65,8 @@ public class DarculaButtonUI extends BasicButtonUI {
}
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
AbstractButton button = (AbstractButton)c;
ButtonModel model = button.getModel();
final AbstractButton button = (AbstractButton)c;
final ButtonModel model = button.getModel();
Color fg = button.getForeground();
if (fg instanceof UIResource && button instanceof JButton && ((JButton)button).isDefaultButton()) {
final Color selectedFg = UIManager.getColor("Button.darcula.selectedButtonForeground");
@ -99,7 +101,7 @@ public class DarculaButtonUI extends BasicButtonUI {
@Override
public void update(Graphics g, JComponent c) {
super.update(g, c);
if (((JButton)c).isDefaultButton() && !SystemInfo.isMac) {
if (c instanceof JButton && ((JButton)c).isDefaultButton() && !SystemInfo.isMac) {
if (!c.getFont().isBold()) {
c.setFont(c.getFont().deriveFont(Font.BOLD));
}

35
src/com/bulenkov/darcula/ui/DarculaPasswordFieldUI.java

@ -25,27 +25,40 @@ import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
/**
* @author Konstantin Bulenkov
*/
public class DarculaPasswordFieldUI extends BasicPasswordFieldUI {
private FocusListener myFocusListener = new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
getComponent().repaint();
}
@Override
public void focusLost(FocusEvent e) {
getComponent().repaint();
}
};
@SuppressWarnings({"MethodOverridesStaticMethodOfSuperclass", "UnusedDeclaration"})
public static ComponentUI createUI(final JComponent c) {
c.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
c.repaint();
}
return new DarculaPasswordFieldUI();
}
@Override
public void focusLost(FocusEvent e) {
c.repaint();
}
});
@Override
protected void installListeners() {
super.installListeners();
getComponent().addFocusListener(myFocusListener);
}
return new DarculaPasswordFieldUI();
@Override
protected void uninstallListeners() {
getComponent().removeFocusListener(myFocusListener);
super.uninstallListeners();
}
@Override

114
src/com/bulenkov/darcula/ui/DarculaTextFieldUI.java

@ -32,62 +32,78 @@ import java.awt.event.*;
* @author Konstantin Bulenkov
*/
public class DarculaTextFieldUI extends BasicTextFieldUI {
private final FocusListener myFocusListener = new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
getComponent().repaint();
}
@Override
public void focusLost(FocusEvent e) {
getComponent().repaint();
}
};
private final MouseMotionListener myMouseMotionListener = new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
if (getComponent() != null && isSearchField(getComponent())) {
if (getActionUnder(e) != null) {
getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
}
}
}
};
private final MouseListener myMouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (isSearchField(getComponent())) {
final SearchAction action = getActionUnder(e);
if (action != null) {
switch (action) {
case POPUP:
showSearchPopup();
break;
case CLEAR:
getComponent().setText("");
break;
}
e.consume();
}
}
}
};
private enum SearchAction {POPUP, CLEAR}
private final JTextField myTextField;
protected JLabel myClearIcon;
protected JLabel myRecentIcon;
public DarculaTextFieldUI(JTextField textField) {
myTextField = textField;
}
@SuppressWarnings("MethodOverridesStaticMethodOfSuperclass")
public static ComponentUI createUI(final JComponent c) {
final DarculaTextFieldUI ui = new DarculaTextFieldUI((JTextField)c);
c.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
c.repaint();
}
return new DarculaTextFieldUI();
}
@Override
public void focusLost(FocusEvent e) {
c.repaint();
}
});
c.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
if (ui.getComponent() != null && isSearchField(c)) {
if (ui.getActionUnder(e) != null) {
c.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
c.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
}
}
}
});
c.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (isSearchField(c)) {
final SearchAction action = ui.getActionUnder(e);
if (action != null) {
switch (action) {
case POPUP:
ui.showSearchPopup();
break;
case CLEAR:
((JTextField)c).setText("");
break;
}
e.consume();
}
}
}
});
return ui;
@Override
protected void installListeners() {
super.installListeners();
final JTextComponent c = getComponent();
c.addFocusListener(myFocusListener);
c.addMouseMotionListener(myMouseMotionListener);
c.addMouseListener(myMouseListener);
}
@Override
protected void uninstallListeners() {
final JTextComponent c = getComponent();
c.removeMouseListener(myMouseListener);
c.removeMouseMotionListener(myMouseMotionListener);
c.removeFocusListener(myFocusListener);
super.uninstallListeners();
}
protected void showSearchPopup() {
@ -110,7 +126,7 @@ public class DarculaTextFieldUI extends BasicTextFieldUI {
}
protected Rectangle getDrawingRect() {
final JTextComponent c = myTextField;
final JTextComponent c = getComponent();
final Insets i = c.getInsets();
final int x = i.right - 4 - 16;
final int y = i.top - 3;

30
src/com/bulenkov/darcula/ui/DarculaToggleButtonUI.java

@ -0,0 +1,30 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bulenkov.darcula.ui;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
/**
* @author Konstantin Bulenkov
*/
public class DarculaToggleButtonUI extends DarculaButtonUI {
@SuppressWarnings("MethodOverridesStaticMethodOfSuperclass")
public static ComponentUI createUI(JComponent c) {
return new DarculaToggleButtonUI();
}
}
Loading…
Cancel
Save