KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > textarea > JEditTextArea


1 /*
2  * JEditTextArea.java - jEdit's text component
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1999, 2005 Slava Pestov
7  * Portions copyright (C) 2000 Ollie Rutherfurd
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */

23
24 package org.gjt.sp.jedit.textarea;
25
26 //{{{ Imports
27
import java.awt.*;
28 import java.awt.event.MouseEvent JavaDoc;
29
30 import org.gjt.sp.jedit.*;
31
32 import javax.swing.*;
33 //}}}
34

35 /**
36  * jEdit's text component.<p>
37  *
38  * Unlike most other text editors, the selection API permits selection and
39  * concurrent manipulation of multiple, non-contiguous regions of text.
40  * Methods in this class that deal with selecting text rely upon classes derived
41  * the {@link Selection} class.
42  *
43  * @author Slava Pestov
44  * @author John Gellene (API documentation)
45  * @version $Id: JEditTextArea.java 8093 2006-11-17 06:46:22Z vanza $
46  */

47 public class JEditTextArea extends TextArea
48 {
49     //{{{ JEditTextArea constructor
50
/**
51      * Creates a new JEditTextArea.
52      */

53     public JEditTextArea(View view)
54     {
55         super(view);
56         enableEvents(AWTEvent.FOCUS_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
57         popupEnabled = true;
58         this.view = view;
59     } //}}}
60

61     //{{{ smartHome() method
62
/**
63      * On subsequent invocations, first moves the caret to the first
64      * non-whitespace character of the line, then the beginning of the
65      * line, then to the first visible line.
66      * @since jEdit 2.7pre2
67      */

68     public void smartHome(boolean select)
69     {
70         Macros.Recorder recorder = view.getMacroRecorder();
71
72         switch(view.getInputHandler().getLastActionCount())
73         {
74         case 1:
75             if(recorder != null)
76                 recorder.record("textArea.goToStartOfWhiteSpace(" + select + ");");
77
78             goToStartOfWhiteSpace(select);
79             break;
80         case 2:
81             if(recorder != null)
82                 recorder.record("textArea.goToStartOfLine(" + select + ");");
83
84             goToStartOfLine(select);
85             break;
86         default: //case 3:
87
if(recorder != null)
88                 recorder.record("textArea.goToFirstVisibleLine(" + select + ");");
89
90             goToFirstVisibleLine(select);
91             break;
92         }
93     } //}}}
94

95     //{{{ smartEnd() method
96
/**
97      * On subsequent invocations, first moves the caret to the last
98      * non-whitespace character of the line, then the end of the
99      * line, then to the last visible line.
100      * @since jEdit 2.7pre2
101      */

102     public void smartEnd(boolean select)
103     {
104         Macros.Recorder recorder = view.getMacroRecorder();
105
106         switch(view.getInputHandler().getLastActionCount())
107         {
108         case 1:
109             if(recorder != null)
110                 recorder.record("textArea.goToEndOfWhiteSpace(" + select + ");");
111
112             goToEndOfWhiteSpace(select);
113             break;
114         case 2:
115             if(recorder != null)
116                 recorder.record("textArea.goToEndOfLine(" + select + ");");
117
118             goToEndOfLine(select);
119             break;
120         default: //case 3:
121
if(recorder != null)
122                 recorder.record("textArea.goToLastVisibleLine(" + select + ");");
123             goToLastVisibleLine(select);
124             break;
125         }
126     } //}}}
127

128     //{{{ showGoToLineDialog() method
129
/**
130      * Displays the 'go to line' dialog box, and moves the caret to the
131      * specified line number.
132      * @since jEdit 2.7pre2
133      */

134     public void showGoToLineDialog()
135     {
136         String JavaDoc line = GUIUtilities.input(view,"goto-line",null);
137         if(line == null)
138             return;
139
140         try
141         {
142             int lineNumber = Integer.parseInt(line) - 1;
143             setCaretPosition(getLineStartOffset(lineNumber));
144         }
145         catch(Exception JavaDoc e)
146         {
147             getToolkit().beep();
148         }
149     } //}}}
150

151     //{{{ userInput() method
152
/**
153      * Handles the insertion of the specified character. It performs the
154      * following operations above and beyond simply inserting the text:
155      * <ul>
156      * <li>Inserting a TAB with a selection will shift to the right
157      * <li>Inserting a space with automatic abbrev expansion enabled will
158      * try to expand the abbrev
159      * <li>Inserting an indent open/close bracket will re-indent the current
160      * line as necessary
161      * </ul>
162      *
163      * @param ch The character
164      * @see #setSelectedText(String)
165      * @see #isOverwriteEnabled()
166      * @since jEdit 2.7pre3
167      */

168     public void userInput(char ch)
169     {
170         if(!isEditable())
171         {
172             getToolkit().beep();
173             return;
174         }
175
176         /* Null before addNotify() */
177         if(hiddenCursor != null)
178             getPainter().setCursor(hiddenCursor);
179
180         if(ch == ' ' && Abbrevs.getExpandOnInput()
181             && Abbrevs.expandAbbrev(view,false))
182             return;
183
184         if(ch == '\t')
185             userInputTab();
186         else
187         {
188             boolean indent = buffer.isElectricKey(ch, caretLine);
189             String JavaDoc str = String.valueOf(ch);
190             if(getSelectionCount() == 0)
191             {
192                 if(!doWordWrap(ch == ' '))
193                     insert(str,indent);
194             }
195             else
196                 replaceSelection(str);
197         }
198     } //}}}
199

200     //{{{ addExplicitFold() method
201
/**
202      * Surrounds the selection with explicit fold markers.
203      * @since jEdit 4.0pre3
204      */

205     public void addExplicitFold()
206     {
207         try
208         {
209             super.addExplicitFold();
210         }
211         catch (TextAreaException e)
212         {
213             GUIUtilities.error(view,"folding-not-explicit",null);
214         }
215     } //}}}
216

217     //{{{ formatParagraph() method
218
/**
219      * Formats the paragraph containing the caret.
220      * @since jEdit 2.7pre2
221      */

222     public void formatParagraph()
223     {
224         try
225         {
226             super.formatParagraph();
227         }
228         catch (TextAreaException e)
229         {
230             GUIUtilities.error(view,"format-maxlinelen",null);
231         }
232     } //}}}
233

234     //{{{ doWordCount() method
235
protected static void doWordCount(View view, String JavaDoc text)
236     {
237         char[] chars = text.toCharArray();
238         int characters = chars.length;
239         int words = 0;
240         int lines = 1;
241
242         boolean word = true;
243         for(int i = 0; i < chars.length; i++)
244         {
245             switch(chars[i])
246             {
247             case '\r': case '\n':
248                 lines++;
249             case ' ': case '\t':
250                 word = true;
251                 break;
252             default:
253                 if(word)
254                 {
255                     words++;
256                     word = false;
257                 }
258                 break;
259             }
260         }
261
262         Object JavaDoc[] args = { characters, words, lines };
263         GUIUtilities.message(view,"wordcount",args);
264     } //}}}
265

266     //{{{ showWordCountDialog() method
267
/**
268      * Displays the 'word count' dialog box.
269      * @since jEdit 2.7pre2
270      */

271     public void showWordCountDialog()
272     {
273         String JavaDoc selection = getSelectedText();
274         if(selection != null)
275         {
276             doWordCount(view,selection);
277             return;
278         }
279
280         doWordCount(view,buffer.getText(0,buffer.getLength()));
281     } //}}}
282

283     //{{{ Getters and setters
284

285     //{{{ getView() method
286
/**
287      * Returns this text area's view.
288      * @since jEdit 4.2pre5
289      */

290     public View getView()
291     {
292         return view;
293     } //}}}
294

295     //}}}
296

297     //{{{ Private members
298

299     //{{{ Instance variables
300
private View view;
301     private JPopupMenu popup;
302     private boolean popupEnabled;
303     //}}}
304
//}}}
305

306     //{{{ isRightClickPopupEnabled() method
307
/**
308      * Returns if the right click popup menu is enabled. The Gestures
309      * plugin uses this API.
310      * @since jEdit 4.2pre13
311      */

312     public boolean isRightClickPopupEnabled()
313     {
314         return popupEnabled;
315     } //}}}
316

317     //{{{ setRightClickPopupEnabled() method
318
/**
319      * Sets if the right click popup menu is enabled. The Gestures
320      * plugin uses this API.
321      * @since jEdit 4.2pre13
322      */

323     public void setRightClickPopupEnabled(boolean popupEnabled)
324     {
325         this.popupEnabled = popupEnabled;
326     } //}}}
327

328     //{{{ getRightClickPopup() method
329
/**
330      * Returns the right click popup menu.
331      */

332     public final JPopupMenu getRightClickPopup()
333     {
334         return popup;
335     } //}}}
336

337     //{{{ setRightClickPopup() method
338
/**
339      * Sets the right click popup menu.
340      * @param popup The popup
341      */

342     public final void setRightClickPopup(JPopupMenu popup)
343     {
344         this.popup = popup;
345     } //}}}
346

347     //{{{ handlePopupTrigger() method
348
/**
349      * Do the same thing as right-clicking on the text area. The Gestures
350      * plugin uses this API.
351      * @since jEdit 4.2pre13
352      */

353     public void handlePopupTrigger(MouseEvent JavaDoc evt)
354     {
355         if(popup.isVisible())
356             popup.setVisible(false);
357         else
358         {
359             int x = evt.getX();
360             int y = evt.getY();
361
362             int dragStart = xyToOffset(x,y,
363                 !(painter.isBlockCaretEnabled()
364                 || isOverwriteEnabled()));
365
366             if(getSelectionCount() == 0 || multi)
367                 moveCaretPosition(dragStart,false);
368             GUIUtilities.showPopupMenu(popup,painter,x,y);
369         }
370     } //}}}
371
}
372
Popular Tags