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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
 
package com.vci.client.ui.swing.components;
 
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
 
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.text.JTextComponent;
 
import com.vci.client.ui.swing.VCISwingUtil;
 
public class VCIJTextContextMenu extends JPopupMenu implements ActionListener {
 
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final String ACTION_COMMAND_COPY = "Copy";
    private final String ACTION_COMMAND_CUT = "Cut";
    private final String ACTION_COMMAND_PASTE = "Paste";
    private final String ACTION_COMMAND_SPECIALCHAR = "SpecialChar";
    private final String ACTION_COMMAND_CLEAR = "ClearClipboard";
    private JTextComponent textCompt = null;
    private MouseEvent textComptMouseEvent = null;
    private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    private JMenuItem miCopy = VCISwingUtil.createVCIJMenuItem(ACTION_COMMAND_COPY, "复制", "复制", "copy.gif", this);
    private JMenuItem miCut = VCISwingUtil.createVCIJMenuItem(ACTION_COMMAND_CUT, "剪切", "剪切", "cut.gif", this);
    private JMenuItem miPaste = VCISwingUtil.createVCIJMenuItem(ACTION_COMMAND_PASTE, "粘贴", "粘贴", "paste.gif", this);
    private JMenuItem miSpecialChar = VCISwingUtil.createVCIJMenuItem(ACTION_COMMAND_SPECIALCHAR, "插入特殊字符", "插入特殊字符", "add.gif", this);
    private JMenuItem miClearClipboard = VCISwingUtil.createVCIJMenuItem(ACTION_COMMAND_CLEAR, "清空剪切板", "清空剪切板", "clear.gif", this);
    private HashMap<String, Runnable> actionMaps = new HashMap<String, Runnable>();
    
    private VCIJPanel mainPal = null;
 
    private static SpecialCharChooseInferface specialCharChooseInferface = null;
    public static SpecialCharChooseInferface getSpecialCharChooseInferface() {
        return VCIJTextContextMenu.specialCharChooseInferface;
    }
    public static void setSpecialCharChooseInferface(SpecialCharChooseInferface specialCharChooseInferface) {
        VCIJTextContextMenu.specialCharChooseInferface = specialCharChooseInferface;
    }
    
    public static void register(JTextComponent textCompt){
        // 为JTextComponent对象注册右键菜单事件
        VCIJTextContextMenu menu = new VCIJTextContextMenu(textCompt);
    }
    
    public static void register(JTextComponent textCompt,VCIJPanel mainPal){
        // 为JTextComponent对象注册右键菜单事件
        VCIJTextContextMenu menu = new VCIJTextContextMenu(textCompt,mainPal);
    }
    
    public VCIJTextContextMenu(JTextComponent textCompt){
        this.textCompt = textCompt;
        initMenuItem();
        initAcitons();
    }
    
    public VCIJTextContextMenu(JTextComponent textCompt,VCIJPanel mainPal){
        this.textCompt = textCompt;
        this.mainPal = mainPal;
        initMenuItem();
        initAcitons();
    }
 
    private void initMenuItem(){
        add(miCopy);
        add(miCut);
        add(miPaste);
        addSeparator();
        add(miSpecialChar);
        addSeparator();
        add(miClearClipboard);
    }
    
    private void initAcitons(){
        actionMaps.put(ACTION_COMMAND_COPY, new Runnable() {
            public void run() {
                copy();
            }
        });
 
        actionMaps.put(ACTION_COMMAND_CUT, new Runnable() {
            public void run() {
                cut();
            }
        });
 
        actionMaps.put(ACTION_COMMAND_PASTE, new Runnable() {
            public void run() {
                paste();
            }
        });
 
        actionMaps.put(ACTION_COMMAND_SPECIALCHAR, new Runnable() {
            public void run() {
                insertSpecialChar();
            }
        });
 
        actionMaps.put(ACTION_COMMAND_CLEAR, new Runnable() {
            public void run() {
                clearClipboard();
            }
        });
        
        textCompt.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e){
                showContextMenu(e);
            }
        });
    }
    
    private void showContextMenu(MouseEvent e){
        textComptMouseEvent = e;
        if(!textCompt.isEnabled() || !textCompt.isEditable()) return;
        if(e.getButton() == MouseEvent.BUTTON3 && textCompt.hasFocus()){
            setMenuEnable();
            show();
        }
    }
    
    private void setMenuEnable(){
        miCopy.setEnabled((textCompt.getSelectedText() != null));
        miCut.setEnabled(miCopy.isEnabled());
        miPaste.setEnabled(hasStringContent());
    }
    
    private boolean hasStringContent(){
        boolean res = false;
        Transferable trans = clipboard.getContents(null);
        if(trans != null){
            res = trans.isDataFlavorSupported(DataFlavor.stringFlavor);
        }
        return res;
    }
    
    @Override
    public void show(){
        super.show(textCompt, textComptMouseEvent.getPoint().x, textComptMouseEvent.getPoint().y);
    }
 
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if(actionMaps.containsKey(actionCommand)){
            new Thread(actionMaps.get(actionCommand)).start();
        }
    }
    
    private void copy(){
        copyOrCut(false);
    }
    
    private void cut(){
        copyOrCut(true);
    }
    
    private void copyOrCut(boolean cut){
        String selectionString = getTextComponentSelectedString(cut);
        setClipboardContent(selectionString);
    }
    
    private void paste(){
        String content = getStringContentFromClipboard();
        if(mainPal == null) {
            textCompt.replaceSelection(content);
            int carePos = textCompt.getCaretPosition() + content.length();
            textCompt.setCaretPosition(carePos);
        } else {
            String[] array = null;
            if (content.indexOf("\t") >= 0) {
                array = content.split("\t");
            } else if (content.indexOf(",") >= 0) {
                array = content.split(",");
            } else {
                array = new String[1];
                array[0] = content;
            }
            List<VCIJTextField> textCompList = getTrueTextFiels();
            for(int k = 0;k < textCompList.size();k++) {
                VCIJTextField textField = textCompList.get(k);
                if(k < array.length ) {
                    textField.setText(array[k]);
                }
            } 
        }
    }
    
    private List<VCIJTextField> getTrueTextFiels() {
        List<VCIJTextField> needCopyTextList = new LinkedList<VCIJTextField>();
        List<VCIJTextField> compList = this.mainPal.getTextComptList();
        int index = compList.indexOf(textCompt);
        for(int i = index;i < compList.size();i++) {
            needCopyTextList.add(compList.get(i));
        }
        
        return needCopyTextList;
    }
    
    private void setClipboardContent(String content){
        clipboard.setContents(new StringSelection(content), new StringSelection(content));
    }
    
    private String getTextComponentSelectedString(boolean cut){
        String res = "";
        if(textCompt.getSelectedText() != null){
            String text = textCompt.getText();
            int start = textCompt.getSelectionStart();
            int end = textCompt.getSelectionEnd();
            res = text.substring(start, end);
            if(cut){
                textCompt.setText(text.substring(0, start) + text.substring(end));
            }
        }
        return res;
    }
    private String getStringContentFromClipboard(){
        String res = "";
        Transferable trans = clipboard.getContents(null);
        if(clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)){
            try {
                Object objContent = trans.getTransferData(DataFlavor.stringFlavor);
                String strContent = "";
                if(objContent instanceof String){
                    strContent = (String)objContent;
                }
                res = strContent;
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }
    
    private void insertSpecialChar(){
        SpecialCharChooseInferface chooseInterface = getSpecialCharChooseInferface();
        if(chooseInterface != null){
            chooseInterface.execute(this.textCompt);
        }
    }
    
    private void clearClipboard(){
        setClipboardContent("");
    }
}