2 * テキストコンポーネント用ポップアップメニュー
\r
4 * License : The MIT License
\r
5 * Copyright(c) 2008 olyutorskii
\r
8 package jp.sourceforge.jindolf;
\r
10 import java.awt.Component;
\r
11 import java.beans.PropertyChangeEvent;
\r
12 import java.beans.PropertyChangeListener;
\r
13 import javax.swing.Action;
\r
14 import javax.swing.JPopupMenu;
\r
15 import javax.swing.text.Document;
\r
16 import javax.swing.text.JTextComponent;
\r
19 * テキストコンポーネント用ポップアップメニュー。
\r
20 * 各種クリップボード操作(カット、コピー、ペースト、etc.)を備える。
\r
22 @SuppressWarnings("serial")
\r
23 public class TextPopup extends JPopupMenu implements PropertyChangeListener{
\r
25 /** プロパティ変更イベントキー。 */
\r
26 private static final String PROPERTY_UI = "UI";
\r
28 private final Action cutAction = ClipboardAction.cutAction();
\r
29 private final Action copyAction = ClipboardAction.copyAction();
\r
30 private final Action pasteAction = ClipboardAction.pasteAction();
\r
31 private final Action selallAction = ClipboardAction.selectallAction();
\r
47 private void buildMenu(){
\r
48 add(this.cutAction);
\r
49 add(this.copyAction);
\r
50 add(this.pasteAction);
\r
54 add(this.selallAction);
\r
60 * テキストコンポーネントに選択中文字列があるか判定する。
\r
61 * @param textComp テキストコンポーネント
\r
62 * @return 選択中文字列があればtrue
\r
64 protected boolean hasSelectedContent(JTextComponent textComp){
\r
65 int selStart = textComp.getSelectionStart();
\r
66 int selEnd = textComp.getSelectionEnd();
\r
69 if(selStart == selEnd) result = false;
\r
76 * テキストコンポーネントに文字列があるか判定する。
\r
77 * @param textComp テキストコンポーネント
\r
78 * @return 文字列があればtrue
\r
80 protected boolean hasContent(JTextComponent textComp){
\r
81 Document document = textComp.getDocument();
\r
82 int docLength = document.getLength();
\r
83 if(docLength <= 0) return false;
\r
89 * ついでにL&F変更監視機構を仕込む。
\r
90 * @param invoker {@inheritDoc}
\r
93 public void setInvoker(Component invoker){
\r
94 Component old = getInvoker();
\r
96 old.removePropertyChangeListener(this);
\r
99 super.setInvoker(invoker);
\r
101 if(invoker != null){
\r
102 invoker.addPropertyChangeListener(PROPERTY_UI, this);
\r
110 * 文字列選択状況によって一部のポップアップメニューをマスクする。
\r
111 * @param invoker {@inheritDoc}
\r
112 * @param x {@inheritDoc}
\r
113 * @param y {@inheritDoc}
\r
116 public void show(Component invoker, int x, int y){
\r
117 if( ! (invoker instanceof JTextComponent) ) return;
\r
118 JTextComponent textComp = (JTextComponent) invoker;
\r
120 boolean textSelected = hasSelectedContent(textComp);
\r
121 this.cutAction .setEnabled(textSelected);
\r
122 this.copyAction.setEnabled(textSelected);
\r
124 if( ! textComp.isEditable() ){
\r
125 this.cutAction .setEnabled(false);
\r
126 this.pasteAction .setEnabled(false);
\r
129 if(hasContent(textComp)){
\r
130 this.selallAction.setEnabled(true);
\r
132 this.selallAction.setEnabled(false);
\r
135 super.show(invoker, x, y);
\r
142 * ポップアップ呼び出し元を監視してL&Fを変更する。
\r
143 * @param event {@inheritDoc}
\r
145 public void propertyChange(PropertyChangeEvent event){
\r
146 String propertyName = event.getPropertyName();
\r
147 if(PROPERTY_UI.equals(propertyName)) updateUI();
\r
151 // TODO アクセス権チェックによるポップアップメニュー変更
\r