KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > EditPane


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

22
23 package org.gjt.sp.jedit;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import java.awt.event.*;
28 import java.awt.*;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.gjt.sp.jedit.gui.*;
33 import org.gjt.sp.jedit.io.VFSManager;
34 import org.gjt.sp.jedit.msg.*;
35 import org.gjt.sp.jedit.options.GlobalOptions;
36 import org.gjt.sp.jedit.syntax.SyntaxStyle;
37 import org.gjt.sp.jedit.textarea.*;
38 import org.gjt.sp.jedit.buffer.JEditBuffer;
39 //}}}
40

41 /**
42  * A panel containing a text area.<p>
43  *
44  * In a BeanShell script, you can obtain the current edit pane from the
45  * <code>editPane</code> variable.<p>
46  *
47  *
48  * Each View can have multiple editPanes, one is active at a time.
49  * Each EditPane has a single JEditTextArea, and is operating on single buffer.
50  * The EditPane also can switch buffers.
51  *
52  * This is the "controller" between a JEditTextArea (view) and a buffer (model).
53  *
54  * This class does not have a public constructor.
55  * Edit panes can be created and destroyed using methods in the
56  * {@link View} class.<p>
57  *
58  *
59  * @see View#splitHorizontally()
60  * @see View#splitVertically()
61  * @see View#unsplitCurrent()
62  * @see View#unsplit()
63  * @see View#getEditPane()
64  * @see View#getEditPanes()
65  *
66  * @author Slava Pestov
67  * @version $Id: EditPane.java 8340 2007-01-10 21:12:06Z kpouer $
68  */

69 public class EditPane extends JPanel implements EBComponent
70 {
71     //{{{ getView() method
72
/**
73      * Returns the view containing this edit pane.
74      * @since jEdit 2.5pre2
75      */

76     public View getView()
77     {
78         return view;
79     } //}}}
80

81     //{{{ getBuffer() method
82
/**
83      * Returns the current buffer.
84      * @since jEdit 2.5pre2
85      */

86     public Buffer getBuffer()
87     {
88         return buffer;
89     } //}}}
90

91     //{{{ setBuffer() method
92
/**
93      * Sets the current buffer.
94      * @param buffer The buffer to edit.
95      * @since jEdit 2.5pre2
96      */

97     public void setBuffer(Buffer buffer)
98     {
99         setBuffer(buffer, true);
100     } //}}}
101

102     //{{{ setBuffer() method
103
/**
104      * Sets the current buffer.
105      * @param buffer The buffer to edit.
106      * @param requestFocus true if the textarea should request focus, false otherwise
107      * @since jEdit 4.3pre6
108      */

109     public void setBuffer(final Buffer buffer, boolean requestFocus)
110     {
111
112         if(buffer == null)
113             throw new NullPointerException JavaDoc();
114
115         if(this.buffer == buffer)
116             return;
117
118         //if(buffer.insideCompoundEdit())
119
// buffer.endCompoundEdit();
120
EditBus.send(new BufferChanging(this, buffer));
121         recentBuffer = this.buffer;
122         if(recentBuffer != null)
123             saveCaretInfo();
124         this.buffer = buffer;
125
126         textArea.setBuffer(buffer);
127             
128         if(!init)
129         {
130             view.updateTitle();
131
132             if(bufferSwitcher != null)
133             {
134                 if(bufferSwitcher.getSelectedItem() != buffer)
135                     bufferSwitcher.setSelectedItem(buffer);
136                 bufferSwitcher.setToolTipText(buffer.getPath());
137             }
138
139             EditBus.send(new EditPaneUpdate(this,EditPaneUpdate
140                 .BUFFER_CHANGED));
141         }
142
143         if (requestFocus)
144         {
145             SwingUtilities.invokeLater(new Runnable JavaDoc()
146             {
147                 public void run()
148                 {
149                     // only do this if we are the current edit pane
150
if(view.getEditPane() == EditPane.this
151                         && (bufferSwitcher == null
152                         || !bufferSwitcher.isPopupVisible()))
153                     {
154                         textArea.requestFocus();
155                     }
156                 }
157             });
158         }
159
160         // Only do this after all I/O requests are complete
161
Runnable JavaDoc runnable = new Runnable JavaDoc()
162         {
163             public void run()
164             {
165                 // avoid a race condition
166
// see bug #834338
167
if(buffer == getBuffer())
168                     loadCaretInfo();
169             }
170         };
171
172         if(buffer.isPerformingIO())
173             VFSManager.runInAWTThread(runnable);
174         else
175             runnable.run();
176     } //}}}
177

178     //{{{ prevBuffer() method
179
/**
180      * Selects the previous buffer.
181      * @since jEdit 2.7pre2
182      */

183     public void prevBuffer()
184     {
185         Buffer buffer = this.buffer.getPrev();
186         if(buffer == null)
187             setBuffer(jEdit.getLastBuffer());
188         else
189             setBuffer(buffer);
190     } //}}}
191

192     //{{{ nextBuffer() method
193
/**
194      * Selects the next buffer.
195      * @since jEdit 2.7pre2
196      */

197     public void nextBuffer()
198     {
199         Buffer buffer = this.buffer.getNext();
200         if(buffer == null)
201             setBuffer(jEdit.getFirstBuffer());
202         else
203             setBuffer(buffer);
204     } //}}}
205

206     //{{{ recentBuffer() method
207
/**
208      * Selects the most recently edited buffer.
209      * @since jEdit 2.7pre2
210      */

211     public void recentBuffer()
212     {
213         if(recentBuffer != null)
214             setBuffer(recentBuffer);
215         else
216             getToolkit().beep();
217     } //}}}
218

219     //{{{ focusOnTextArea() method
220
/**
221      * Sets the focus onto the text area.
222      * @since jEdit 2.5pre2
223      */

224     public void focusOnTextArea()
225     {
226         SwingUtilities.invokeLater(new Runnable JavaDoc()
227         {
228             public void run()
229             {
230                 textArea.requestFocus();
231             }
232         });
233     } //}}}
234

235     //{{{ getTextArea() method
236
/**
237      * Returns the view's text area.
238      * @since jEdit 2.5pre2
239      */

240     public JEditTextArea getTextArea()
241     {
242         return textArea;
243     } //}}}
244

245     //{{{ getBufferSwitcher() method
246
/**
247      * Returns the buffer switcher combo box instance.
248      * @since jEdit 4.1pre8
249      */

250     public BufferSwitcher getBufferSwitcher()
251     {
252         return bufferSwitcher;
253     } //}}}
254

255     //{{{ showBufferSwitcher() method
256
/**
257      * Shows the buffer switcher combo box.
258      * @since jEdit 4.1pre8
259      */

260     public void showBufferSwitcher()
261     {
262         if(bufferSwitcher == null)
263             getToolkit().beep();
264         else
265         {
266             bufferSwitcher.requestFocus();
267             bufferSwitcher.showPopup();
268         }
269     } //}}}
270

271     //{{{ saveCaretInfo() method
272
/**
273      * Saves the caret information to the current buffer.
274      * @since jEdit 2.5pre2
275      */

276     public void saveCaretInfo()
277     {
278         if(!buffer.isLoaded())
279             return;
280
281         buffer.setIntegerProperty(Buffer.CARET,
282             textArea.getCaretPosition());
283         
284         // save a map of buffer.getPath() -> CaretInfo, this is necessary for
285
// when the same buffer is open in more than one EditPane and the user
286
// is switching between buffers. We want to keep the caret in the
287
// right position in each EditPane, which won't be the case if we
288
// just use the buffer caret property.
289
Map JavaDoc<String JavaDoc, CaretInfo> carets = (Map JavaDoc<String JavaDoc, CaretInfo>)getClientProperty(CARETS);
290         if (carets == null)
291         {
292             carets = new HashMap JavaDoc<String JavaDoc, CaretInfo>();
293             putClientProperty(CARETS, carets);
294         }
295         CaretInfo caretInfo = carets.get(buffer.getPath());
296         if (caretInfo == null)
297         {
298             caretInfo = new CaretInfo();
299             carets.put(buffer.getPath(), caretInfo);
300         }
301         caretInfo.caret = textArea.getCaretPosition();
302         
303
304         Selection[] selection = textArea.getSelection();
305         for(int i = 0; i < selection.length; i++)
306             selection[i] = (Selection)selection[i].clone();
307         buffer.setProperty(Buffer.SELECTION,selection);
308         caretInfo.selection = selection;
309
310         caretInfo.rectangularSelection = textArea.isRectangularSelectionEnabled();
311         caretInfo.multipleSelection = textArea.isMultipleSelectionEnabled();
312
313         buffer.setIntegerProperty(Buffer.SCROLL_VERT,
314             textArea.getFirstPhysicalLine());
315         caretInfo.scrollVert = textArea.getFirstPhysicalLine();
316         buffer.setIntegerProperty(Buffer.SCROLL_HORIZ,
317             textArea.getHorizontalOffset());
318         caretInfo.scrollHoriz = textArea.getHorizontalOffset();
319         if (!buffer.isUntitled())
320         {
321             BufferHistory.setEntry(buffer.getPath(), textArea.getCaretPosition(),
322                 (Selection[])buffer.getProperty(Buffer.SELECTION),
323                 buffer.getStringProperty(JEditBuffer.ENCODING));
324         }
325     } //}}}
326

327     //{{{ loadCaretInfo() method
328
/**
329      * Loads the caret and selection information from this EditPane, fall
330      * back to the information from the current buffer if none is already
331      * in this EditPane.
332      * @since jEdit 2.5pre2
333      */

334     public void loadCaretInfo()
335     {
336         // get our internal map of buffer -> CaretInfo since there might
337
// be current info already
338
Map JavaDoc<String JavaDoc, CaretInfo> carets = (Map JavaDoc<String JavaDoc, CaretInfo>) getClientProperty(CARETS);
339         if (carets == null)
340         {
341             carets = new HashMap JavaDoc<String JavaDoc, CaretInfo>();
342             putClientProperty(CARETS, carets);
343         }
344         CaretInfo caretInfo = carets.get(buffer.getPath());
345         if (caretInfo == null)
346         {
347             caretInfo = new CaretInfo();
348         }
349         
350         // set the position of the caret itself.
351
// Caret position could be stored in the internal map already,
352
// if so, use that one first. Otherwise, fall back to any
353
// previously saved caret position that was stored in the
354
// buffer properties.
355
int caret = caretInfo.caret;
356         if (caret == -1 || buffer.getBooleanProperty(Buffer.CARET_POSITIONED))
357         {
358             Integer JavaDoc i = (Integer JavaDoc) buffer.getProperty(Buffer.CARET);
359             caret = i == null ? -1 : i;
360         }
361         buffer.unsetProperty(Buffer.CARET_POSITIONED);
362
363
364         if(caret != -1)
365             textArea.setCaretPosition(Math.min(caret,
366                 buffer.getLength()));
367
368         // set any selections
369
Selection[] selection = caretInfo.selection;
370         if ( selection == null ) {
371             selection = (Selection[])buffer.getProperty(Buffer.SELECTION);
372         }
373         if(selection != null)
374         {
375             for(int i = 0; i < selection.length; i++)
376             {
377                 Selection s = selection[i];
378                 int max = buffer.getLength();
379                 if(s.getStart() > max || s.getEnd() > max)
380                     selection[i] = null;
381             }
382         }
383         textArea.setSelection(selection);
384         textArea.setRectangularSelectionEnabled(caretInfo.rectangularSelection);
385         textArea.setMultipleSelectionEnabled(caretInfo.multipleSelection);
386         // set firstLine value
387
int firstLine = caretInfo.scrollVert;
388         if ( firstLine == -1 )
389         {
390             Integer JavaDoc i = (Integer JavaDoc) buffer.getProperty(Buffer.SCROLL_VERT);
391             firstLine = i == null ? -1 : i;
392         }
393
394         if(firstLine != -1)
395             textArea.setFirstPhysicalLine(firstLine);
396
397         // set horizontal offset
398
int horizontalOffset = caretInfo.scrollHoriz;
399         if (horizontalOffset == -1)
400         {
401             Integer JavaDoc i = (Integer JavaDoc) buffer.getProperty(Buffer.SCROLL_HORIZ);
402             horizontalOffset = i == null ? -1 : i;
403         }
404
405         if(horizontalOffset != -1)
406             textArea.setHorizontalOffset(horizontalOffset);
407
408         /* Silly bug workaround #8694. If you look at the above code,
409          * note that we restore the saved caret position first, then
410          * scroll to the saved location. However, the caret changing
411          * can itself result in scrolling to a different location than
412          * what was saved; and since moveCaretPosition() calls
413          * updateBracketHighlight(), the bracket highlight's out of
414          * bounds calculation will rely on a different set of physical
415          * first/last lines than what we will end up with eventually.
416          * Instead of confusing the user with status messages that
417          * appear at random when switching buffers, we simply hide the
418          * message altogether. */

419         view.getStatus().setMessage(null);
420     } //}}}
421

422     //{{{ bufferRenamed() method
423
/**
424      * This method should be called by the Buffer when the path is changing.
425      * @param oldPath the old path of the buffer
426      * @param newPath the new path of the buffer
427      */

428     void bufferRenamed(String JavaDoc oldPath, String JavaDoc newPath)
429     {
430         Map JavaDoc<String JavaDoc, CaretInfo> carets = (Map JavaDoc<String JavaDoc, CaretInfo>) getClientProperty(CARETS);
431         if (carets == null)
432             return;
433         CaretInfo caretInfo = carets.remove(oldPath);
434         if (caretInfo != null)
435             carets.put(newPath, caretInfo);
436
437     } //}}}
438

439     //{{{
440
/**
441      * Need to track this info for each buffer that this EditPane might edit
442      * since a buffer may be open in more than one EditPane at a time. That
443      * means we need to track this info at the EditPane level rather than
444      * the buffer level.
445      */

446     private static class CaretInfo
447     {
448         public int caret = -1;
449         public Selection[] selection;
450         public int scrollVert = -1;
451         public int scrollHoriz = -1;
452         public boolean rectangularSelection;
453         public boolean multipleSelection;
454     } //}}}
455

456     //{{{ goToNextMarker() method
457
/**
458      * Moves the caret to the next marker.
459      * @since jEdit 4.3pre3
460      */

461     public void goToNextMarker(boolean select)
462     {
463         java.util.List JavaDoc<Marker> markers = buffer.getMarkers();
464         if(markers.isEmpty())
465         {
466             getToolkit().beep();
467             return;
468         }
469
470         Marker marker = null;
471         
472         int caret = textArea.getCaretPosition();
473
474         for(int i = 0; i < markers.size(); i++)
475         {
476             Marker _marker = markers.get(i);
477             if(_marker.getPosition() > caret)
478             {
479                 marker = _marker;
480                 break;
481             }
482         }
483         // the markers list is not empty at this point
484
if(marker == null)
485             marker = markers.get(0);
486
487         if(select)
488             textArea.extendSelection(caret,marker.getPosition());
489         else if(!textArea.isMultipleSelectionEnabled())
490             textArea.selectNone();
491         textArea.moveCaretPosition(marker.getPosition());
492     } //}}}
493

494     //{{{ goToPrevMarker() method
495
/**
496      * Moves the caret to the previous marker.
497      * @since jEdit 2.7pre2
498      */

499     public void goToPrevMarker(boolean select)
500     {
501         java.util.List JavaDoc<Marker> markers = buffer.getMarkers();
502         if(markers.isEmpty())
503         {
504             getToolkit().beep();
505             return;
506         }
507         
508         int caret = textArea.getCaretPosition();
509
510         Marker marker = null;
511         for(int i = markers.size() - 1; i >= 0; i--)
512         {
513             Marker _marker = markers.get(i);
514             if(_marker.getPosition() < caret)
515             {
516                 marker = _marker;
517                 break;
518             }
519         }
520
521         if(marker == null)
522             marker = markers.get(markers.size() - 1);
523
524         if(select)
525             textArea.extendSelection(caret,marker.getPosition());
526         else if(!textArea.isMultipleSelectionEnabled())
527             textArea.selectNone();
528         textArea.moveCaretPosition(marker.getPosition());
529     } //}}}
530

531     //{{{ goToMarker() method
532
/**
533      * Moves the caret to the marker with the specified shortcut.
534      * @param shortcut The shortcut
535      * @param select True if the selection should be extended,
536      * false otherwise
537      * @since jEdit 3.2pre2
538      */

539     public void goToMarker(char shortcut, boolean select)
540     {
541         Marker marker = buffer.getMarker(shortcut);
542         if(marker == null)
543         {
544             getToolkit().beep();
545             return;
546         }
547
548         int pos = marker.getPosition();
549
550         if(select)
551             textArea.extendSelection(textArea.getCaretPosition(),pos);
552         else if(!textArea.isMultipleSelectionEnabled())
553             textArea.selectNone();
554         textArea.moveCaretPosition(pos);
555     } //}}}
556

557     //{{{ addMarker() method
558
/**
559      * Adds a marker at the caret position.
560      * @since jEdit 3.2pre1
561      */

562     public void addMarker()
563     {
564         int caretLine = textArea.getCaretLine();
565         
566         // always add markers on selected lines
567
Selection[] selection = textArea.getSelection();
568         for(int i = 0; i < selection.length; i++)
569         {
570             Selection s = selection[i];
571             if(s.getStartLine() != s.getEndLine())
572             {
573                 if(s.getStartLine() != caretLine)
574                     buffer.addMarker('\0',s.getStart());
575             }
576
577             if(s.getEndLine() != caretLine)
578                 buffer.addMarker('\0',s.getEnd());
579         }
580
581         // toggle marker on caret line
582
buffer.addOrRemoveMarker('\0',textArea.getCaretPosition());
583     } //}}}
584

585     //{{{ swapMarkerAndCaret() method
586
/**
587      * Moves the caret to the marker with the specified shortcut,
588      * then sets the marker position to the former caret position.
589      * @param shortcut The shortcut
590      * @since jEdit 3.2pre2
591      */

592     public void swapMarkerAndCaret(char shortcut)
593     {
594         Marker marker = buffer.getMarker(shortcut);
595         if(marker == null)
596         {
597             getToolkit().beep();
598             return;
599         }
600
601         int caret = textArea.getCaretPosition();
602
603         textArea.setCaretPosition(marker.getPosition());
604         buffer.addMarker(shortcut,caret);
605     } //}}}
606

607     //{{{ handleMessage() method
608
public void handleMessage(EBMessage msg)
609     {
610         if(msg instanceof PropertiesChanged)
611         {
612             propertiesChanged();
613             loadBufferSwitcher();
614         }
615         else if(msg instanceof BufferUpdate)
616             handleBufferUpdate((BufferUpdate)msg);
617     } //}}}
618

619     //{{{ getMinimumSize() method
620
/**
621      * Returns 0,0 for split pane compatibility.
622      */

623     public final Dimension getMinimumSize()
624     {
625         return new Dimension(0,0);
626     } //}}}
627

628     //{{{ toString() method
629
public String JavaDoc toString()
630     {
631         return getClass().getName() + '['
632                + (view.getEditPane() == this
633             ? "active" : "inactive")
634             + ']';
635     } //}}}
636

637     //{{{ Package-private members
638

639     //{{{ EditPane constructor
640
EditPane(View view, Buffer buffer)
641     {
642         super(new BorderLayout());
643
644         init = true;
645
646         this.view = view;
647
648         EditBus.addToBus(this);
649
650         textArea = new JEditTextArea(view);
651         textArea.getPainter().setAntiAlias(new AntiAlias(jEdit.getProperty("view.antiAlias")));
652         textArea.setMouseHandler(new MouseHandler(textArea));
653         textArea.setTransferHandler(new TextAreaTransferHandler());
654         markerHighlight = new MarkerHighlight();
655         textArea.getGutter().addExtension(markerHighlight);
656         textArea.addStatusListener(new StatusHandler());
657
658         add(BorderLayout.CENTER,textArea);
659
660         propertiesChanged();
661
662         if(buffer == null)
663             setBuffer(jEdit.getFirstBuffer());
664         else
665             setBuffer(buffer);
666
667         loadBufferSwitcher();
668
669         init = false;
670     } //}}}
671

672     //{{{ close() method
673
void close()
674     {
675         saveCaretInfo();
676         EditBus.send(new EditPaneUpdate(this,EditPaneUpdate.DESTROYED));
677         EditBus.removeFromBus(this);
678         textArea.dispose();
679     } //}}}
680

681     //}}}
682

683     //{{{ Private members
684

685     //{{{ Instance variables
686
private boolean init;
687     /** The View where the edit pane is. */
688     private final View view;
689     /** The current buffer. */
690     private Buffer buffer;
691     private Buffer recentBuffer;
692     private BufferSwitcher bufferSwitcher;
693     /** The textArea inside the edit pane. */
694     private final JEditTextArea textArea;
695     private final MarkerHighlight markerHighlight;
696
697     private static final String JavaDoc CARETS = "Buffer->caret";
698     
699     //}}}
700

701     //{{{ propertiesChanged() method
702
private void propertiesChanged()
703     {
704         TextAreaPainter painter = textArea.getPainter();
705
706         painter.setFont(jEdit.getFontProperty("view.font"));
707         painter.setStructureHighlightEnabled(jEdit.getBooleanProperty(
708             "view.structureHighlight"));
709         painter.setStructureHighlightColor(
710             jEdit.getColorProperty("view.structureHighlightColor"));
711         painter.setEOLMarkersPainted(jEdit.getBooleanProperty(
712             "view.eolMarkers"));
713         painter.setEOLMarkerColor(
714             jEdit.getColorProperty("view.eolMarkerColor"));
715         painter.setWrapGuidePainted(jEdit.getBooleanProperty(
716             "view.wrapGuide"));
717         painter.setWrapGuideColor(
718             jEdit.getColorProperty("view.wrapGuideColor"));
719         painter.setCaretColor(
720             jEdit.getColorProperty("view.caretColor"));
721         painter.setSelectionColor(
722             jEdit.getColorProperty("view.selectionColor"));
723         painter.setMultipleSelectionColor(
724             jEdit.getColorProperty("view.multipleSelectionColor"));
725         painter.setBackground(
726             jEdit.getColorProperty("view.bgColor"));
727         painter.setForeground(
728             jEdit.getColorProperty("view.fgColor"));
729         painter.setBlockCaretEnabled(jEdit.getBooleanProperty(
730             "view.blockCaret"));
731         painter.setLineHighlightEnabled(jEdit.getBooleanProperty(
732             "view.lineHighlight"));
733         painter.setLineHighlightColor(
734             jEdit.getColorProperty("view.lineHighlightColor"));
735         painter.setAntiAlias(new AntiAlias(jEdit.getProperty("view.antiAlias")));
736         painter.setFractionalFontMetricsEnabled(jEdit.getBooleanProperty(
737             "view.fracFontMetrics"));
738
739         String JavaDoc defaultFont = jEdit.getProperty("view.font");
740         int defaultFontSize = jEdit.getIntegerProperty("view.fontsize",12);
741         painter.setStyles(GUIUtilities.loadStyles(defaultFont,defaultFontSize));
742
743         SyntaxStyle[] foldLineStyle = new SyntaxStyle[4];
744         for(int i = 0; i <= 3; i++)
745         {
746             foldLineStyle[i] = GUIUtilities.parseStyle(
747                 jEdit.getProperty("view.style.foldLine." + i),
748                 defaultFont,defaultFontSize);
749         }
750         painter.setFoldLineStyle(foldLineStyle);
751         Gutter gutter = textArea.getGutter();
752         gutter.setExpanded(jEdit.getBooleanProperty(
753             "view.gutter.lineNumbers"));
754         int interval = jEdit.getIntegerProperty(
755             "view.gutter.highlightInterval",5);
756         gutter.setHighlightInterval(interval);
757         gutter.setCurrentLineHighlightEnabled(jEdit.getBooleanProperty(
758             "view.gutter.highlightCurrentLine"));
759         gutter.setStructureHighlightEnabled(jEdit.getBooleanProperty(
760             "view.gutter.structureHighlight"));
761         gutter.setStructureHighlightColor(
762             jEdit.getColorProperty("view.gutter.structureHighlightColor"));
763         gutter.setBackground(
764             jEdit.getColorProperty("view.gutter.bgColor"));
765         gutter.setForeground(
766             jEdit.getColorProperty("view.gutter.fgColor"));
767         gutter.setHighlightedForeground(
768             jEdit.getColorProperty("view.gutter.highlightColor"));
769         gutter.setFoldColor(
770             jEdit.getColorProperty("view.gutter.foldColor"));
771         markerHighlight.setMarkerHighlightColor(
772             jEdit.getColorProperty("view.gutter.markerColor"));
773         markerHighlight.setMarkerHighlightEnabled(jEdit.getBooleanProperty(
774             "view.gutter.markerHighlight"));
775         gutter.setCurrentLineForeground(
776             jEdit.getColorProperty("view.gutter.currentLineColor"));
777         String JavaDoc alignment = jEdit.getProperty(
778             "view.gutter.numberAlignment");
779         if ("right".equals(alignment))
780         {
781             gutter.setLineNumberAlignment(Gutter.RIGHT);
782         }
783         else if ("center".equals(alignment))
784         {
785             gutter.setLineNumberAlignment(Gutter.CENTER);
786         }
787         else // left == default case
788
{
789             gutter.setLineNumberAlignment(Gutter.LEFT);
790         }
791
792         gutter.setFont(jEdit.getFontProperty("view.gutter.font"));
793
794         int width = jEdit.getIntegerProperty(
795             "view.gutter.borderWidth",3);
796         gutter.setBorder(width,
797             jEdit.getColorProperty("view.gutter.focusBorderColor"),
798             jEdit.getColorProperty("view.gutter.noFocusBorderColor"),
799             textArea.getPainter().getBackground());
800
801         textArea.setCaretBlinkEnabled(jEdit.getBooleanProperty(
802             "view.caretBlink"));
803
804         textArea.setElectricScroll(jEdit.getIntegerProperty(
805             "view.electricBorders",0));
806
807         // Set up the right-click popup menu
808
JPopupMenu popup = GUIUtilities.loadPopupMenu("view.context");
809         JMenuItem customize = new JMenuItem(jEdit.getProperty(
810             "view.context.customize"));
811         customize.addActionListener(new ActionListener()
812         {
813             public void actionPerformed(ActionEvent evt)
814             {
815                 new GlobalOptions(view,"context");
816             }
817         });
818         popup.addSeparator();
819         popup.add(customize);
820         textArea.setRightClickPopup(popup);
821
822         // use old property name for backwards compatibility
823
textArea.setQuickCopyEnabled(jEdit.getBooleanProperty(
824             "view.middleMousePaste"));
825
826         textArea.setDragEnabled(jEdit.getBooleanProperty(
827             "view.dragAndDrop"));
828
829         textArea.setJoinNonWordChars(jEdit.getBooleanProperty(
830             "view.joinNonWordChars"));
831
832         textArea.propertiesChanged();
833     } //}}}
834

835     //{{{ loadBufferSwitcher() method
836
private void loadBufferSwitcher()
837     {
838         if(jEdit.getBooleanProperty("view.showBufferSwitcher"))
839         {
840             if(bufferSwitcher == null)
841             {
842                 bufferSwitcher = new BufferSwitcher(this);
843                 add(BorderLayout.NORTH,bufferSwitcher);
844                 bufferSwitcher.updateBufferList();
845                 revalidate();
846             }
847         }
848         else if(bufferSwitcher != null)
849         {
850             remove(bufferSwitcher);
851             revalidate();
852             bufferSwitcher = null;
853         }
854     } //}}}
855

856     //{{{ handleBufferUpdate() method
857
private void handleBufferUpdate(BufferUpdate msg)
858     {
859         Buffer _buffer = msg.getBuffer();
860         if(msg.getWhat() == BufferUpdate.CREATED)
861         {
862             if(bufferSwitcher != null)
863                 bufferSwitcher.updateBufferList();
864
865             /* When closing the last buffer, the BufferUpdate.CLOSED
866              * handler doesn't call setBuffer(), because null buffers
867              * are not supported. Instead, it waits for the subsequent
868              * 'Untitled' file creation. */

869             if(buffer.isClosed())
870             {
871                 setBuffer(jEdit.getFirstBuffer(), msg.getView() == view);
872                 // since recentBuffer will be set to the one that
873
// was closed
874
recentBuffer = null;
875             }
876         }
877         else if(msg.getWhat() == BufferUpdate.CLOSED)
878         {
879             if(bufferSwitcher != null)
880                 bufferSwitcher.updateBufferList();
881
882             if(_buffer == buffer)
883             {
884                 // The closed buffer is the current buffer
885
Buffer newBuffer = recentBuffer != null ?
886                     recentBuffer : _buffer.getPrev();
887
888                 if(newBuffer != null && !newBuffer.isClosed())
889                 {
890                     setBuffer(newBuffer);
891                     recentBuffer = newBuffer.getPrev();
892                 }
893                 else if(jEdit.getBufferCount() != 0)
894                 {
895                     setBuffer(jEdit.getFirstBuffer());
896                     recentBuffer = null;
897                 }
898             }
899             else if(_buffer == recentBuffer)
900                 recentBuffer = null;
901
902             Buffer closedBuffer = msg.getBuffer();
903             if (closedBuffer.isUntitled())
904             {
905                 // the buffer was a new file so I do not need to keep it's informations
906
Map JavaDoc<String JavaDoc, CaretInfo> carets = (Map JavaDoc<String JavaDoc, CaretInfo>) getClientProperty(CARETS);
907                 if (carets != null)
908                     carets.remove(closedBuffer.getPath());
909             }
910         }
911         else if(msg.getWhat() == BufferUpdate.LOAD_STARTED)
912         {
913             if(_buffer == buffer)
914             {
915                 textArea.setCaretPosition(0);
916                 textArea.getPainter().repaint();
917             }
918         }
919         else if(msg.getWhat() == BufferUpdate.LOADED)
920         {
921             if(_buffer == buffer)
922             {
923                 textArea.repaint();
924                 if(bufferSwitcher != null)
925                     bufferSwitcher.updateBufferList();
926
927                 if(view.getEditPane() == this)
928                 {
929                     StatusBar status = view.getStatus();
930                     status.updateCaretStatus();
931                     status.updateBufferStatus();
932                     status.updateMiscStatus();
933                 }
934
935                 loadCaretInfo();
936             }
937
938         }
939         else if(msg.getWhat() == BufferUpdate.DIRTY_CHANGED)
940         {
941             if(_buffer == buffer)
942             {
943                 if(bufferSwitcher != null)
944                 {
945                     if(buffer.isDirty())
946                         bufferSwitcher.repaint();
947                     else
948                         bufferSwitcher.updateBufferList();
949                 }
950             }
951         }
952         else if(msg.getWhat() == BufferUpdate.MARKERS_CHANGED)
953         {
954             if(_buffer == buffer)
955                 textArea.getGutter().repaint();
956         }
957         else if(msg.getWhat() == BufferUpdate.PROPERTIES_CHANGED)
958         {
959             if(_buffer == buffer && buffer.isLoaded())
960             {
961                 textArea.propertiesChanged();
962                 if(view.getEditPane() == this)
963                     view.getStatus().updateBufferStatus();
964             }
965         }
966         else if(msg.getWhat() == BufferUpdate.SAVED)
967         {
968             if(_buffer == buffer)
969                 textArea.propertiesChanged();
970         }
971     } //}}}
972

973     //}}}
974

975     //{{{ StatusHandler class
976
class StatusHandler implements StatusListener
977     {
978         public void statusChanged(org.gjt.sp.jedit.textarea.TextArea textArea, int flag, boolean value)
979         {
980             StatusBar status = view.getStatus();
981             if(status == null)
982                 return;
983             
984             switch(flag)
985             {
986             case OVERWRITE_CHANGED:
987                 status.setMessageAndClear(
988                     jEdit.getProperty("view.status.overwrite-changed",
989                     new Integer JavaDoc[] { value ? 1 : 0 }));
990                 break;
991             case MULTI_SELECT_CHANGED:
992                 status.setMessageAndClear(
993                     jEdit.getProperty("view.status.multi-changed",
994                     new Integer JavaDoc[] { value ? 1 : 0 }));
995                 break;
996             case RECT_SELECT_CHANGED:
997                 status.setMessageAndClear(
998                     jEdit.getProperty("view.status.rect-select-changed",
999                     new Integer JavaDoc[] { value ? 1 : 0 }));
1000                break;
1001            }
1002            
1003            status.updateMiscStatus();
1004        }
1005    
1006        public void bracketSelected(org.gjt.sp.jedit.textarea.TextArea textArea, int line, String JavaDoc text)
1007        {
1008            StatusBar status = view.getStatus();
1009            if(status == null)
1010                return;
1011            
1012            status.setMessageAndClear(jEdit.getProperty(
1013                "view.status.bracket",new Object JavaDoc[] {
1014                line, text }));
1015        }
1016    
1017        public void narrowActive(org.gjt.sp.jedit.textarea.TextArea textArea)
1018        {
1019            StatusBar status = view.getStatus();
1020            if(status == null)
1021                return;
1022            
1023            status.setMessageAndClear(
1024                jEdit.getProperty("view.status.narrow"));
1025        }
1026    } //}}}
1027

1028    //{{{ MarkerHighlight class
1029
class MarkerHighlight extends TextAreaExtension
1030    {
1031        private boolean markerHighlight;
1032        private Color markerHighlightColor;
1033
1034        //{{{ getMarkerHighlightColor() method
1035
public Color getMarkerHighlightColor()
1036        {
1037            return markerHighlightColor;
1038        } //}}}
1039

1040        //{{{ setMarkerHighlightColor() method
1041
public void setMarkerHighlightColor(Color markerHighlightColor)
1042        {
1043            this.markerHighlightColor = markerHighlightColor;
1044        } //}}}
1045

1046        //{{{ isMarkerHighlightEnabled() method
1047
public boolean isMarkerHighlightEnabled()
1048        {
1049            return markerHighlight;
1050        } //}}}
1051

1052        //{{{ isMarkerHighlightEnabled()
1053
public void setMarkerHighlightEnabled(boolean markerHighlight)
1054        {
1055            this.markerHighlight = markerHighlight;
1056        } //}}}
1057

1058        //{{{ paintValidLine() method
1059
public void paintValidLine(Graphics2D gfx, int screenLine,
1060            int physicalLine, int start, int end, int y)
1061        {
1062            if(isMarkerHighlightEnabled())
1063            {
1064                Buffer buffer = (Buffer)textArea.getBuffer();
1065                if(buffer.getMarkerInRange(start,end) != null)
1066                {
1067                    gfx.setColor(getMarkerHighlightColor());
1068                    FontMetrics fm = textArea.getPainter().getFontMetrics();
1069                    gfx.fillRect(0,y,textArea.getGutter()
1070                        .getWidth(),fm.getHeight());
1071                }
1072            }
1073        } //}}}
1074

1075        //{{{ getToolTipText() method
1076
public String JavaDoc getToolTipText(int x, int y)
1077        {
1078            if(isMarkerHighlightEnabled())
1079            {
1080                int lineHeight = textArea.getPainter().getFontMetrics().getHeight();
1081                if(lineHeight == 0)
1082                    return null;
1083
1084                int line = y / lineHeight;
1085                int start = textArea.getScreenLineStartOffset(line);
1086                int end = textArea.getScreenLineEndOffset(line);
1087                if(start == -1 || end == -1)
1088                    return null;
1089
1090                Buffer buffer = (Buffer)textArea.getBuffer();
1091                Marker marker = buffer.getMarkerInRange(start,end);
1092                if(marker != null)
1093                {
1094                    char shortcut = marker.getShortcut();
1095                    if(shortcut == '\0')
1096                        return jEdit.getProperty("view.gutter.marker.no-name");
1097                    else
1098                    {
1099                        String JavaDoc[] args = { String.valueOf(shortcut) };
1100                        return jEdit.getProperty("view.gutter.marker",args);
1101                    }
1102                }
1103            }
1104
1105            return null;
1106        } //}}}
1107
} //}}}
1108
}
1109
Popular Tags