4 * License : The MIT License
5 * Copyright(c) 2008 olyutorskii
8 package jp.sourceforge.jindolf;
10 import java.awt.Toolkit;
11 import java.awt.datatransfer.Clipboard;
12 import java.awt.datatransfer.StringSelection;
13 import java.awt.event.ActionEvent;
14 import javax.swing.Action;
15 import javax.swing.text.JTextComponent;
16 import javax.swing.text.TextAction;
19 * テキストコンポーネント-クリップボード間操作用にカスタム化したAction。
21 @SuppressWarnings("serial")
22 public class ClipboardAction extends TextAction{
25 public static final String ACTION_CUT = "ACTION_CUT";
27 public static final String ACTION_COPY = "ACTION_COPY";
29 public static final String ACTION_PASTE = "ACTION_PASTE";
31 public static final String ACTION_SELALL = "ACTION_SELALL";
36 * @param name ポップアップメニュー名
37 * @param command アクションコマンド名
39 protected ClipboardAction(String name, String command){
41 setActionCommand(command);
50 public static void copyToClipboard(CharSequence data){
51 Toolkit toolkit = Toolkit.getDefaultToolkit();
52 Clipboard clipboard = toolkit.getSystemClipboard();
53 StringSelection selection = new StringSelection(data.toString());
54 clipboard.setContents(selection, selection);
62 public static ClipboardAction cutAction(){
63 return new ClipboardAction("選択範囲をカット", ACTION_CUT);
70 public static ClipboardAction copyAction(){
71 return new ClipboardAction("選択範囲をコピー", ACTION_COPY);
78 public static ClipboardAction pasteAction(){
79 return new ClipboardAction("ペースト", ACTION_PASTE);
86 public static ClipboardAction selectallAction(){
87 return new ClipboardAction("すべて選択", ACTION_SELALL);
92 * @param actionCommand アクションコマンド名
94 private void setActionCommand(String actionCommand){
95 putValue(Action.ACTION_COMMAND_KEY, actionCommand);
103 protected String getActionCommand(){
104 Object value = getValue(Action.ACTION_COMMAND_KEY);
105 if( ! (value instanceof String) ) return null;
107 String command = (String) value;
114 * アクションの受信によってクリップボード操作を行う。
115 * @param event {@inheritDoc}
118 public void actionPerformed(ActionEvent event){
119 JTextComponent textComp = getTextComponent(event);
120 if(textComp == null) return;
122 String command = getActionCommand();
124 if (ACTION_CUT .equals(command)) textComp.cut();
125 else if(ACTION_COPY .equals(command)) textComp.copy();
126 else if(ACTION_PASTE .equals(command)) textComp.paste();
127 else if(ACTION_SELALL.equals(command)) textComp.selectAll();
132 // TODO 文字列以外の物をペーストしたときに無視したい。