wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.vci.client.ui.swing.components;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
 
import javax.swing.JTextField;
import javax.swing.text.Document;
 
import com.vci.client.ui.swing.VCISwingUtil;
 
/**
 * 
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: VCI</p>
 * @author xchao
 * @time 2012-5-10
 * @version 1.0
 */
public class VCIJTextField extends JTextField implements MouseListener{
 
    
    /**
     * 
     */
    private static final long serialVersionUID = 2046222954902184262L;
 
    private VCIJPanel pal = null;
    private boolean isSupportRightClick = true;
    public VCIJTextField(Document doc, String text, int columns) {
        super(doc, text, columns);
        customConstructor();
    }
    
    public VCIJTextField(Document doc, String text, int columns, boolean isSupportRightClick) {
        super(doc, text, columns);
        this.setSupportRightClick(isSupportRightClick);
        customConstructor();
    }
    
    public VCIJTextField(Document doc, String text, int columns,VCIJPanel pal) {
        super(doc, text, columns);
        customConstructor(pal);
    }
 
    public VCIJTextField(int columns) {
        this(null, null, columns);
    }
    
    public VCIJTextField(String text, boolean isSupportRightClick) {
        this(null, text, 0, isSupportRightClick);
    }
 
 
    public VCIJTextField(String text, int columns) {
        this(null, text, columns);
    }
 
 
    public VCIJTextField(String text) {
        this(null, text, 0);
    }
    
    public VCIJTextField(VCIJPanel pal,boolean required){
        this(null, "", 0,pal);
        setRequired(required);
    }
    
    public VCIJTextField(boolean required){
        this("");
        setRequired(required);
    }
 
 
    private void customConstructor(){
        setFont(VCISwingUtil.FONT_SONTTI_PLAN_13);
        setBorder(VCISwingUtil.JTEXT_FIELD_BORDER_DEFAULT);
        setPreferredSize(new Dimension(VCISwingUtil.JTEXT_FIELD_DEFAULT_WIDTH, VCISwingUtil.JTEXT_FIELD_DEFAULT_HEIGHT));
        this.addMouseListener(this);
        if (this.isSupportRightClick) {
            VCIJTextContextMenu.register(this,null);
        }
    }
    
    private void customConstructor(VCIJPanel pal){
        setFont(VCISwingUtil.FONT_SONTTI_PLAN_13);
        setBorder(VCISwingUtil.JTEXT_FIELD_BORDER_DEFAULT);
        setPreferredSize(new Dimension(VCISwingUtil.JTEXT_FIELD_DEFAULT_WIDTH, VCISwingUtil.JTEXT_FIELD_DEFAULT_HEIGHT));
        this.addMouseListener(this);
        if (this.isSupportRightClick) {
            VCIJTextContextMenu.register(this,pal);
        }
    }
    
    
    public void mouseClicked(MouseEvent e) {
        if(!canResponse()) return;
        hasFocus = true;
        setBackground(VCISwingUtil.JTEXT_FIELD_MOUSE_CLICK_BACKGROUND_COLOR);
        setBorder(VCISwingUtil.JTEXT_FIELD_BORDER_MOUSE_CLICKED);
    }
 
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
 
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
 
    public void mouseEntered(MouseEvent e) {
        if(!canResponse()) return;
        setBorder(VCISwingUtil.JTEXT_FIELD_BORDER_MOUSE_ENTERED);
    }
 
    @Override 
    public void processFocusEvent(FocusEvent event){
        super.processFocusEvent(event);
        if(!canResponse()) return;
        if(hasFocus) {
            hasFocus = false;
            setBorder(VCISwingUtil.JTEXT_FIELD_BORDER_MOUSE_EXITED);
            setBackground(VCISwingUtil.JTEXT_FIELD_MOUSE_EXITED_BACKGROUND_COLOR);
        }
    }
    
    public void mouseExited(MouseEvent e) {
        if(!canResponse()) return;
        if(hasFocus){
            setBorder(VCISwingUtil.JTEXT_FIELD_BORDER_MOUSE_ENTERED);
        }else{
            setBorder(VCISwingUtil.JTEXT_FIELD_BORDER_MOUSE_EXITED);
        }
    }
    
    @Override
    public void paint(Graphics g){
        super.paint(g);
        if(isRequired()) {
            drawRequiredFlag(g);
        }
    }
 
    private void drawRequiredFlag(Graphics g){
        int x = getWidth() - 9;
        int y = 1;
        g.setColor(Color.RED);
        g.fillArc(x, y, 7, 7, 0, 360);
//        int x = getWidth() - 10;
//        int y = 10;
//        g.setColor(Color.RED);
//        g.drawString("*", x, y);
    }
    
    private boolean canResponse(){
        return (isEditable() && isEnabled() && isVisible());
    }
    
    private boolean required = false;
    
    public boolean isRequired() {
        return required;
    }
 
    public void setRequired(boolean required) {
        this.required = required;
    }
 
    private boolean hasFocus = false;
    
 
    private Object obj =  null;
    public Object getObj() {
        return obj;
    }
    public void setObj(Object obj) {
        this.obj = obj;
    }
 
    public boolean isSupportRightClick() {
        return isSupportRightClick;
    }
 
    public void setSupportRightClick(boolean isSupportRightClick) {
        this.isSupportRightClick = isSupportRightClick;
    }
}