OSDN Git Service

Maven3対応。
[jindolf/Jindolf.git] / src / main / java / jp / sourceforge / jindolf / ClipboardAction.java
1 /*
2  * クリップボード操作用Action
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf;
9
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;
17
18 /**
19  * テキストコンポーネント-クリップボード間操作用にカスタム化したAction。
20  */
21 @SuppressWarnings("serial")
22 public class ClipboardAction extends TextAction{
23
24     /** アクション{@value}。 */
25     public static final String ACTION_CUT    = "ACTION_CUT";
26     /** アクション{@value}。 */
27     public static final String ACTION_COPY   = "ACTION_COPY";
28     /** アクション{@value}。 */
29     public static final String ACTION_PASTE  = "ACTION_PASTE";
30     /** アクション{@value}。 */
31     public static final String ACTION_SELALL = "ACTION_SELALL";
32
33
34     /**
35      * コンストラクタ。
36      * @param name ポップアップメニュー名
37      * @param command アクションコマンド名
38      */
39     protected ClipboardAction(String name, String command){
40         super(name);
41         setActionCommand(command);
42         return;
43     }
44
45
46     /**
47      * 文字列をクリップボードにコピーする。
48      * @param data 文字列
49      */
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);
55         return;
56     }
57
58     /**
59      * カット用Actionの生成。
60      * @return カット用Action
61      */
62     public static ClipboardAction cutAction(){
63         return new ClipboardAction("選択範囲をカット", ACTION_CUT);
64     }
65
66     /**
67      * コピー用Actionの生成。
68      * @return コピー用Action
69      */
70     public static ClipboardAction copyAction(){
71         return new ClipboardAction("選択範囲をコピー", ACTION_COPY);
72     }
73
74     /**
75      * ペースト用Actionの生成。
76      * @return ペースト用Action
77      */
78     public static ClipboardAction pasteAction(){
79         return new ClipboardAction("ペースト", ACTION_PASTE);
80     }
81
82     /**
83      * 全選択用Actionの生成。
84      * @return 全選択用Action
85      */
86     public static ClipboardAction selectallAction(){
87         return new ClipboardAction("すべて選択", ACTION_SELALL);
88     }
89
90     /**
91      * アクションコマンド名を設定する。
92      * @param actionCommand アクションコマンド名
93      */
94     private void setActionCommand(String actionCommand){
95         putValue(Action.ACTION_COMMAND_KEY, actionCommand);
96         return;
97     }
98
99     /**
100      * アクションコマンド名を取得する。
101      * @return アクションコマンド名
102      */
103     protected String getActionCommand(){
104         Object value = getValue(Action.ACTION_COMMAND_KEY);
105         if( ! (value instanceof String) ) return null;
106
107         String command = (String) value;
108
109         return command;
110     }
111
112     /**
113      * {@inheritDoc}
114      * アクションの受信によってクリップボード操作を行う。
115      * @param event {@inheritDoc}
116      */
117     @Override
118     public void actionPerformed(ActionEvent event){
119         JTextComponent textComp = getTextComponent(event);
120         if(textComp == null) return;
121
122         String command = getActionCommand();
123
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();
128
129         return;
130     }
131
132     // TODO 文字列以外の物をペーストしたときに無視したい。
133 }