|
|
|
@ -3,6 +3,7 @@ package com.fr.design.gui.itextfield;
|
|
|
|
|
import com.fr.common.inputevent.InputEventBaseOnOS; |
|
|
|
|
import com.fr.design.event.GlobalNameListener; |
|
|
|
|
import com.fr.design.event.GlobalNameObserver; |
|
|
|
|
import com.fr.design.event.HoverAware; |
|
|
|
|
import com.fr.design.event.UIObserver; |
|
|
|
|
import com.fr.design.event.UIObserverListener; |
|
|
|
|
import com.fr.design.utils.gui.GUICoreUtils; |
|
|
|
@ -12,22 +13,25 @@ import com.fr.stable.StringUtils;
|
|
|
|
|
import javax.swing.JFrame; |
|
|
|
|
import javax.swing.JPanel; |
|
|
|
|
import javax.swing.JTextField; |
|
|
|
|
import javax.swing.UIManager; |
|
|
|
|
import javax.swing.event.DocumentEvent; |
|
|
|
|
import javax.swing.event.DocumentListener; |
|
|
|
|
import javax.swing.text.Document; |
|
|
|
|
import java.awt.Color; |
|
|
|
|
import java.awt.Dimension; |
|
|
|
|
import java.awt.Graphics; |
|
|
|
|
import java.awt.Graphics2D; |
|
|
|
|
import java.awt.Insets; |
|
|
|
|
import java.awt.LayoutManager; |
|
|
|
|
import java.awt.RenderingHints; |
|
|
|
|
import java.awt.event.MouseAdapter; |
|
|
|
|
import java.awt.event.MouseEvent; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 文本框 |
|
|
|
|
* |
|
|
|
|
* @author Jerry |
|
|
|
|
* @Created on 2016 |
|
|
|
|
* @since xxx |
|
|
|
|
*/ |
|
|
|
|
public class UITextField extends JTextField implements UIObserver, GlobalNameObserver { |
|
|
|
|
public class UITextField extends JTextField implements UIObserver, GlobalNameObserver, HoverAware { |
|
|
|
|
private boolean isBorderPainted = true; |
|
|
|
|
private boolean isRoundBorder = true; |
|
|
|
|
private int rectDirection = Constants.NULL; |
|
|
|
@ -36,10 +40,13 @@ public class UITextField extends JTextField implements UIObserver, GlobalNameObs
|
|
|
|
|
private GlobalNameListener globalNameListener = null; |
|
|
|
|
private Dimension preferredSize = null; |
|
|
|
|
private String placeholder = StringUtils.EMPTY; |
|
|
|
|
private final int HEIGHT = UIManager.getInt("TextField.height"); |
|
|
|
|
|
|
|
|
|
//有些情况下setText的时候不希望触发attributeChange,添加一个属性标识
|
|
|
|
|
private boolean isSetting = false; |
|
|
|
|
|
|
|
|
|
private boolean hover = false; |
|
|
|
|
|
|
|
|
|
public UITextField() { |
|
|
|
|
super(); |
|
|
|
|
init(); |
|
|
|
@ -70,6 +77,7 @@ public class UITextField extends JTextField implements UIObserver, GlobalNameObs
|
|
|
|
|
*/ |
|
|
|
|
public void init() { |
|
|
|
|
InputEventBaseOnOS.addBasicEditInputMap(this); |
|
|
|
|
setPreferredSize(new Dimension(getPreferredSize().width, HEIGHT)); |
|
|
|
|
initListener(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -91,6 +99,20 @@ public class UITextField extends JTextField implements UIObserver, GlobalNameObs
|
|
|
|
|
attributeChange(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.addMouseListener(new MouseAdapter() { |
|
|
|
|
@Override |
|
|
|
|
public void mouseEntered(MouseEvent e) { |
|
|
|
|
hover = true; |
|
|
|
|
repaint(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void mouseExited(MouseEvent e) { |
|
|
|
|
hover = false; |
|
|
|
|
repaint(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -111,10 +133,6 @@ public class UITextField extends JTextField implements UIObserver, GlobalNameObs
|
|
|
|
|
isSetting = setting; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setPreferredSize(Dimension preferredSize) { |
|
|
|
|
this.preferredSize = preferredSize; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setGlobalName(String name) { |
|
|
|
|
textFieldName = name; |
|
|
|
|
} |
|
|
|
@ -131,59 +149,11 @@ public class UITextField extends JTextField implements UIObserver, GlobalNameObs
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Insets getInsets() { |
|
|
|
|
return new Insets(0, 4, 0, 4); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Dimension getPreferredSize() { |
|
|
|
|
if (preferredSize == null) { |
|
|
|
|
return new Dimension(super.getPreferredSize().width, 20); |
|
|
|
|
} |
|
|
|
|
return preferredSize; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
|
// public UITextFieldUI getUI() {
|
|
|
|
|
// return (UITextFieldUI) ui;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 设置变化的背景颜色 |
|
|
|
|
*/ |
|
|
|
|
public void setBackgroundUIColor(Color color) { |
|
|
|
|
((UITextFieldUI) this.ui).setBackgroundColor4NoGiveNumber(color); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 更新UI |
|
|
|
|
*/ |
|
|
|
|
// public void updateUI() {
|
|
|
|
|
// this.setUI(new UITextFieldUI(this));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void paintComponent(final Graphics pG) { |
|
|
|
|
super.paintComponent(pG); |
|
|
|
|
if (placeholder.length() == 0 || getText().length() > 0) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
final Graphics2D g = (Graphics2D) pG; |
|
|
|
|
g.setRenderingHint( |
|
|
|
|
RenderingHints.KEY_ANTIALIASING, |
|
|
|
|
RenderingHints.VALUE_ANTIALIAS_ON); |
|
|
|
|
g.setColor(getDisabledTextColor()); |
|
|
|
|
g.drawString(placeholder, getInsets().left, pG.getFontMetrics() |
|
|
|
|
.getMaxAscent() + getInsets().top + 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void paintBorder(Graphics g) { |
|
|
|
|
if (!isBorderPainted) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
// getUI().paintBorder((Graphics2D) g, getWidth(), getHeight(), isRoundBorder, rectDirection);
|
|
|
|
|
// ((UITextFieldUI) this.ui).setBackgroundColor4NoGiveNumber(color);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -262,4 +232,8 @@ public class UITextField extends JTextField implements UIObserver, GlobalNameObs
|
|
|
|
|
this.isBorderPainted = isBorderPainted; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean isHovered() { |
|
|
|
|
return hover; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|