|
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("");
|
}
|
}
|