Browse Source

Made sure to properly install/uninstall listeners to avoid NPEs when switching look and feels.

master
Hendrik Schreiber 8 years ago
parent
commit
2d30faa40e
  1. BIN
      build/darcula.jar
  2. 35
      src/com/bulenkov/darcula/ui/DarculaPasswordFieldUI.java
  3. 114
      src/com/bulenkov/darcula/ui/DarculaTextFieldUI.java

BIN
build/darcula.jar

Binary file not shown.

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;

Loading…
Cancel
Save