KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > common > TextComponentHelper


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.common;
5
6 import java.awt.event.ActionEvent JavaDoc;
7
8 import javax.swing.Action JavaDoc;
9 import javax.swing.ImageIcon JavaDoc;
10 import javax.swing.JPopupMenu JavaDoc;
11 import javax.swing.event.CaretEvent JavaDoc;
12 import javax.swing.event.CaretListener JavaDoc;
13 import javax.swing.text.BadLocationException JavaDoc;
14 import javax.swing.text.Document JavaDoc;
15 import javax.swing.text.JTextComponent JavaDoc;
16
17 public class TextComponentHelper extends XPopupListener
18   implements CaretListener JavaDoc
19 {
20   protected JTextComponent JavaDoc 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 JavaDoc component) {
31     this();
32     setTarget(component);
33   }
34
35   protected void setTarget(JTextComponent JavaDoc 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 JavaDoc createPopup() {
48     JPopupMenu JavaDoc popup = new JPopupMenu JavaDoc("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 JavaDoc popup) {
59     popup.add(m_cutAction = new CutAction());
60   }
61   
62   public Action JavaDoc getCutAction() {
63     return m_cutAction;
64   }
65   
66   protected void addCopyAction(JPopupMenu JavaDoc popup) {
67     popup.add(m_copyAction = new CopyAction());
68   }
69
70   public Action JavaDoc getCopyAction() {
71     return m_copyAction;
72   }
73   
74   protected void addPasteAction(JPopupMenu JavaDoc popup) {
75     popup.add(m_pasteAction = new PasteAction());
76   }
77
78   public Action JavaDoc getPasteAction() {
79     return m_pasteAction;
80   }
81   
82   protected void addClearAction(JPopupMenu JavaDoc popup) {
83     popup.add(m_clearAction = new ClearAction());
84   }
85
86   public Action JavaDoc getClearAction() {
87     return m_clearAction;
88   }
89   
90   protected class CutAction extends XAbstractAction {
91     protected CutAction() {
92       super("Cut");
93       String JavaDoc uri = "/com/tc/admin/icons/cut_edit.gif";
94       setSmallIcon(new ImageIcon JavaDoc(getClass().getResource(uri)));
95     }
96
97     public void actionPerformed(ActionEvent JavaDoc ae) {
98       m_component.cut();
99     }
100   }
101   
102   protected class CopyAction extends XAbstractAction {
103     protected CopyAction() {
104       super("Copy");
105       String JavaDoc uri = "/com/tc/admin/icons/copy_edit.gif";
106       setSmallIcon(new ImageIcon JavaDoc(getClass().getResource(uri)));
107     }
108
109     public void actionPerformed(ActionEvent JavaDoc ae) {
110       m_component.copy();
111     }
112   }
113   
114   protected class PasteAction extends XAbstractAction {
115     protected PasteAction() {
116       super("Paste");
117       String JavaDoc uri = "/com/tc/admin/icons/paste_edit.gif";
118       setSmallIcon(new ImageIcon JavaDoc(getClass().getResource(uri)));
119     }
120
121     public void actionPerformed(ActionEvent JavaDoc ae) {
122       m_component.paste();
123     }
124   }
125
126   protected class ClearAction extends XAbstractAction {
127     protected ClearAction() {
128       super("Clear");
129       String JavaDoc uri = "/com/tc/admin/icons/clear_co.gif";
130       setSmallIcon(new ImageIcon JavaDoc(getClass().getResource(uri)));
131     }
132
133     public void actionPerformed(ActionEvent JavaDoc ae) {
134       Document JavaDoc doc = m_component.getDocument();
135       
136       try {
137         doc.remove(0, doc.getLength());
138       } catch(BadLocationException JavaDoc 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 JavaDoc e) {
168     testEnableMenuItems();
169   }
170 }
171
Popular Tags