001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin.common;
005:
006: import java.awt.event.ActionEvent;
007:
008: import javax.swing.Action;
009: import javax.swing.ImageIcon;
010: import javax.swing.JPopupMenu;
011: import javax.swing.event.CaretEvent;
012: import javax.swing.event.CaretListener;
013: import javax.swing.text.BadLocationException;
014: import javax.swing.text.Document;
015: import javax.swing.text.JTextComponent;
016:
017: // XXX: DEPRECATED
018: public class TextComponentHelper extends XPopupListener implements
019: CaretListener {
020: protected JTextComponent m_component;
021: protected CutAction m_cutAction;
022: protected CopyAction m_copyAction;
023: protected PasteAction m_pasteAction;
024: protected ClearAction m_clearAction;
025:
026: public TextComponentHelper() {
027: super ();
028: }
029:
030: public TextComponentHelper(JTextComponent component) {
031: this ();
032: setTarget(component);
033: }
034:
035: protected void setTarget(JTextComponent component) {
036: if (m_component != null) {
037: m_component.removeCaretListener(this );
038: }
039:
040: super .setTarget(m_component = component);
041:
042: if (component != null) {
043: component.addCaretListener(this );
044: }
045: }
046:
047: public JPopupMenu createPopup() {
048: JPopupMenu popup = new JPopupMenu("TextComponent Actions");
049:
050: addCutAction(popup);
051: addCopyAction(popup);
052: addPasteAction(popup);
053: addClearAction(popup);
054:
055: return popup;
056: }
057:
058: protected void addCutAction(JPopupMenu popup) {
059: popup.add(m_cutAction = new CutAction());
060: }
061:
062: public Action getCutAction() {
063: return m_cutAction;
064: }
065:
066: protected void addCopyAction(JPopupMenu popup) {
067: popup.add(m_copyAction = new CopyAction());
068: }
069:
070: public Action getCopyAction() {
071: return m_copyAction;
072: }
073:
074: protected void addPasteAction(JPopupMenu popup) {
075: popup.add(m_pasteAction = new PasteAction());
076: }
077:
078: public Action getPasteAction() {
079: return m_pasteAction;
080: }
081:
082: protected void addClearAction(JPopupMenu popup) {
083: popup.add(m_clearAction = new ClearAction());
084: }
085:
086: public Action getClearAction() {
087: return m_clearAction;
088: }
089:
090: protected class CutAction extends XAbstractAction {
091: protected CutAction() {
092: super ("Cut");
093: String uri = "/com/tc/admin/icons/cut_edit.gif";
094: setSmallIcon(new ImageIcon(getClass().getResource(uri)));
095: }
096:
097: public void actionPerformed(ActionEvent ae) {
098: m_component.cut();
099: }
100: }
101:
102: protected class CopyAction extends XAbstractAction {
103: protected CopyAction() {
104: super ("Copy");
105: String uri = "/com/tc/admin/icons/copy_edit.gif";
106: setSmallIcon(new ImageIcon(getClass().getResource(uri)));
107: }
108:
109: public void actionPerformed(ActionEvent ae) {
110: m_component.copy();
111: }
112: }
113:
114: protected class PasteAction extends XAbstractAction {
115: protected PasteAction() {
116: super ("Paste");
117: String uri = "/com/tc/admin/icons/paste_edit.gif";
118: setSmallIcon(new ImageIcon(getClass().getResource(uri)));
119: }
120:
121: public void actionPerformed(ActionEvent ae) {
122: m_component.paste();
123: }
124: }
125:
126: protected class ClearAction extends XAbstractAction {
127: protected ClearAction() {
128: super ("Clear");
129: String uri = "/com/tc/admin/icons/clear_co.gif";
130: setSmallIcon(new ImageIcon(getClass().getResource(uri)));
131: }
132:
133: public void actionPerformed(ActionEvent ae) {
134: Document doc = m_component.getDocument();
135:
136: try {
137: doc.remove(0, doc.getLength());
138: } catch (BadLocationException ble) {/**/
139: }
140: }
141: }
142:
143: public boolean hasSelectionRange() {
144: return (m_component.getSelectionStart() - m_component
145: .getSelectionEnd()) != 0;
146: }
147:
148: private void testEnableMenuItems() {
149: boolean hasSelectionRange = hasSelectionRange();
150: boolean editable = m_component.isEditable();
151:
152: if (m_cutAction != null) {
153: m_cutAction.setEnabled(editable && hasSelectionRange);
154: }
155:
156: if (m_copyAction != null) {
157: m_copyAction.setEnabled(hasSelectionRange);
158: }
159:
160: if (m_pasteAction != null) {
161: m_pasteAction.setEnabled(editable);
162: }
163:
164: if (m_clearAction != null) {
165: m_clearAction.setEnabled(m_component.getDocument()
166: .getLength() > 0);
167: }
168: }
169:
170: public void caretUpdate(CaretEvent e) {
171: testEnableMenuItems();
172: }
173: }
|