1 package com.explosion.expfmodules.texteditor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.awt.Component;
23 import java.awt.event.MouseEvent;
24 import java.io.IOException;
25 import java.io.Reader;
26
27 import javax.swing.JTextArea;
28 import javax.swing.event.CaretEvent;
29 import javax.swing.event.CaretListener;
30 import javax.swing.event.DocumentEvent;
31 import javax.swing.event.DocumentListener;
32 import javax.swing.text.JTextComponent;
33
34 import com.explosion.expf.Application;
35 import com.explosion.expf.ExpActionListener;
36 import com.explosion.expf.ExpFrame;
37 import com.explosion.expf.menusandtools.menu.MenuHelper;
38 import com.explosion.expf.menusandtools.menu.popup.ExpPopupIntercepter;
39 import com.explosion.expf.menusandtools.menu.popup.ExpPopupableMenu;
40 import com.explosion.expf.menusandtools.menu.segmented.ExpMenuSegment;
41 import com.explosion.expf.supportmodules.EditSupportModule;
42 import com.explosion.expf.supportmodules.FileSupportModule;
43 import com.explosion.utilities.WorkingCaret;
44 import com.explosion.utilities.exception.ExceptionManagerFactory;
45
46 /***
47 * Date created:@16-Oct-2003
48 * @author Stephen Cowx
49 */
50
51 public class ExpfTextArea extends JTextArea implements Editable, ExpPopupIntercepter
52 {
53 private static org.apache.log4j.Logger log = org.apache.log4j.LogManager.getLogger(ExpfTextArea.class);
54
55
56 private boolean dirty = false;
57 private MenuAwareUndoRedoBuffer undoRedoBuffer = null;
58
59
60 private TextComponentLocalListener localListener = null;
61
62 private Component highLevelParent;
63
64 private ExpPopupableMenu menu;
65
66 /***
67 * Initialises a new ExpfTextArea
68 */
69 public ExpfTextArea(Component highLevelParent)
70 {
71 super();
72 this.setName(TextEditorConstants.COMPNAME_EXPF_TEXTAREA);
73 this.highLevelParent = highLevelParent;
74 init();
75 }
76
77 /***
78 * Returns the JTextComponent associated with this object
79 * @return
80 */
81 public JTextComponent getTextComponent()
82 {
83 return this;
84 }
85
86
87 /***
88 * This method initialises the gui
89 */
90 private void init()
91 {
92
93
94 undoRedoBuffer = new MenuAwareUndoRedoBuffer(highLevelParent,this);
95
96
97 localListener = new TextComponentLocalListener(this, highLevelParent);
98
99
100 setDoubleBuffered(true);
101 setCaret(new WorkingCaret());
102 setCaretPosition(0);
103 setDoubleBuffered(true);
104
105 getDocument().addDocumentListener(new DocumentListener()
106 {
107 public void insertUpdate(DocumentEvent e) {}
108 public void removeUpdate(DocumentEvent e) {}
109 public void changedUpdate(DocumentEvent e) {}
110 }
111 );
112
113 getDocument().addUndoableEditListener(undoRedoBuffer);
114
115 this.addCaretListener(new CaretListener()
116 {
117 public void caretUpdate(CaretEvent e)
118 {
119 log.debug("Component for ExpfTextArea is " + highLevelParent);
120 if (e.getDot() != e.getMark())
121 {
122
123 Application.ensureLocalCookie(TextEditorConstants.TO_UPPER,1,highLevelParent);
124 Application.ensureLocalCookie(TextEditorConstants.TO_LOWER,1,highLevelParent);
125 }
126 else
127 {
128 Application.ensureLocalCookie(TextEditorConstants.TO_UPPER,0,highLevelParent);
129 Application.ensureLocalCookie(TextEditorConstants.TO_LOWER,0,highLevelParent);
130 }
131 }
132 });
133 }
134
135 public ExpActionListener getlocalListener()
136 {
137 return localListener;
138 }
139
140 public void applyPreferences()
141 {
142 undoRedoBuffer.setMaxSize(((Integer)TextEditorModuleManager.instance().getPreference(TextEditorConstants.UNDOLIST_SIZE).getValue()).intValue());
143 }
144
145 /***
146 * Overrides the read method to reinsert the document listener when the read is finished
147 */
148 public void read(Reader in, Object desc) throws IOException {
149 super.read(in, desc);
150 getDocument().addUndoableEditListener(undoRedoBuffer);
151 }
152
153 /***
154 * Clear this tool
155 */
156 public void clear() throws Exception
157 {
158 setText("");
159 }
160
161 /***
162 * Undo the last command executed by the user
163 */
164 public void undo() throws Exception
165 {
166 undoRedoBuffer.undo();
167 }
168
169 /***
170 * Undo the last n commands executed by the user
171 */
172 public void undo(int numberOfSteps) throws Exception
173 {
174 int i = 0;
175 while (i < numberOfSteps)
176 {
177 undoRedoBuffer.undo();
178 i++;
179 }
180 }
181
182 public void redo() throws Exception
183 {
184 undoRedoBuffer.redo();
185 }
186
187 /***
188 * Pops up a popup menu
189 * @param event
190 */
191 public void popupEvent(MouseEvent event) {
192 try
193 {
194 if (menu == null)
195 {
196 menu = new ExpPopupableMenu();
197 MenuHelper menuHelper = new MenuHelper(((ExpFrame)Application.getApplicationFrame()).getListener());
198
199 ExpMenuSegment fileSegment = menu.createNewSegment(ExpMenuSegment.ALWAYS_LAST_SEGMENT);
200 ExpMenuSegment redo = menu.createNewSegment(ExpMenuSegment.ALWAYS_FIRST_SEGMENT);
201 ExpMenuSegment cutcopy = menu.createNewSegment(ExpMenuSegment.ANY_POSITION);
202 ExpMenuSegment selectAndClear = menu.createNewSegment(ExpMenuSegment.ANY_POSITION);
203 ExpMenuSegment functionSegment = menu.createNewSegment(ExpMenuSegment.ANY_POSITION);
204
205 functionSegment.addElement(menuHelper.createMenuItem("To upper case", 'u', TextEditorConstants.TO_UPPER, null, 1));
206 functionSegment.addElement(menuHelper.createMenuItem("To lower case", 'l', TextEditorConstants.TO_LOWER, null, 1));
207
208 FileSupportModule.populateSaveAndCloseSegment(menuHelper, fileSegment);
209 EditSupportModule.populateUndoRedoMenuSegment(menuHelper, redo);
210 EditSupportModule.populateCutCopyPasteMenuSegment(menuHelper, cutcopy);
211 EditSupportModule.populateSelectAndClearSegment(menuHelper, selectAndClear);
212
213 menuHelper.checkEnabled();
214 }
215
216 menu.show(event);
217 }
218 catch (Exception e)
219 {
220 ExceptionManagerFactory.getExceptionManager().manageException(e,"Exception caught while handling popupEvent");
221 }
222 }
223
224 }