1 4 package com.tc.admin.common; 5 6 import java.awt.event.ActionEvent ; 7 8 import javax.swing.Action ; 9 import javax.swing.ImageIcon ; 10 import javax.swing.JPopupMenu ; 11 import javax.swing.event.CaretEvent ; 12 import javax.swing.event.CaretListener ; 13 import javax.swing.text.BadLocationException ; 14 import javax.swing.text.Document ; 15 import javax.swing.text.JTextComponent ; 16 17 public class TextComponentHelper extends XPopupListener 18 implements CaretListener 19 { 20 protected JTextComponent m_component; 21 protected CutAction m_cutAction; 22 protected CopyAction m_copyAction; 23 protected PasteAction m_pasteAction; 24 protected ClearAction m_clearAction; 25 26 public TextComponentHelper() { 27 super(); 28 } 29 30 public TextComponentHelper(JTextComponent component) { 31 this(); 32 setTarget(component); 33 } 34 35 protected void setTarget(JTextComponent component) { 36 if(m_component != null) { 37 m_component.removeCaretListener(this); 38 } 39 40 super.setTarget(m_component = component); 41 42 if(component != null) { 43 component.addCaretListener(this); 44 } 45 } 46 47 public JPopupMenu createPopup() { 48 JPopupMenu popup = new JPopupMenu ("TextComponent Actions"); 49 50 addCutAction(popup); 51 addCopyAction(popup); 52 addPasteAction(popup); 53 addClearAction(popup); 54 55 return popup; 56 } 57 58 protected void addCutAction(JPopupMenu popup) { 59 popup.add(m_cutAction = new CutAction()); 60 } 61 62 public Action getCutAction() { 63 return m_cutAction; 64 } 65 66 protected void addCopyAction(JPopupMenu popup) { 67 popup.add(m_copyAction = new CopyAction()); 68 } 69 70 public Action getCopyAction() { 71 return m_copyAction; 72 } 73 74 protected void addPasteAction(JPopupMenu popup) { 75 popup.add(m_pasteAction = new PasteAction()); 76 } 77 78 public Action getPasteAction() { 79 return m_pasteAction; 80 } 81 82 protected void addClearAction(JPopupMenu popup) { 83 popup.add(m_clearAction = new ClearAction()); 84 } 85 86 public Action getClearAction() { 87 return m_clearAction; 88 } 89 90 protected class CutAction extends XAbstractAction { 91 protected CutAction() { 92 super("Cut"); 93 String uri = "/com/tc/admin/icons/cut_edit.gif"; 94 setSmallIcon(new ImageIcon (getClass().getResource(uri))); 95 } 96 97 public void actionPerformed(ActionEvent ae) { 98 m_component.cut(); 99 } 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 public boolean hasSelectionRange() { 143 return (m_component.getSelectionStart()-m_component.getSelectionEnd()) != 0; 144 } 145 146 private void testEnableMenuItems() { 147 boolean hasSelectionRange = hasSelectionRange(); 148 boolean editable = m_component.isEditable(); 149 150 if(m_cutAction != null) { 151 m_cutAction.setEnabled(editable && hasSelectionRange); 152 } 153 154 if(m_copyAction != null) { 155 m_copyAction.setEnabled(hasSelectionRange); 156 } 157 158 if(m_pasteAction != null) { 159 m_pasteAction.setEnabled(editable); 160 } 161 162 if(m_clearAction != null) { 163 m_clearAction.setEnabled(m_component.getDocument().getLength() > 0); 164 } 165 } 166 167 public void caretUpdate(CaretEvent e) { 168 testEnableMenuItems(); 169 } 170 } 171 | Popular Tags |