~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to editor/libsrc/org/netbeans/editor/ext/ExtCaret.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.editor.ext;
 
43
 
 
44
import java.awt.event.MouseEvent;
 
45
import javax.swing.text.JTextComponent;
 
46
import org.netbeans.editor.BaseCaret;
 
47
import org.netbeans.editor.Utilities;
 
48
import org.netbeans.editor.SettingsUtil;
 
49
import org.netbeans.editor.SettingsChangeEvent;
 
50
 
 
51
/**
 
52
* Extended caret implementation
 
53
*
 
54
* @author Miloslav Metelka
 
55
* @version 1.00
 
56
*/
 
57
 
 
58
public class ExtCaret extends BaseCaret {
 
59
 
 
60
    /** 
 
61
     * Highlight row draw layer name.
 
62
     * 
 
63
     * <p>Using <code>DrawLayer</code>s has been deprecated and this constant
 
64
     * has no longer any meaning.
 
65
     * 
 
66
     * @deprecated Please use Highlighting SPI instead, for details see
 
67
     *   <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
 
68
     */
 
69
    public static final String HIGHLIGHT_ROW_LAYER_NAME = "highlight-row-layer"; // NOI18N
 
70
 
 
71
    /**
 
72
     * Highlight row draw layer visibility.
 
73
     * 
 
74
     * <p>Using <code>DrawLayer</code>s has been deprecated and this constant
 
75
     * has no longer any meaning.
 
76
     * 
 
77
     * @deprecated Please use Highlighting SPI instead, for details see
 
78
     *   <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
 
79
     */
 
80
    public static final int HIGHLIGHT_ROW_LAYER_VISIBILITY = 2050;
 
81
 
 
82
    /** 
 
83
     * Highlight matching brace draw layer name
 
84
     * 
 
85
     * @deprecated Please use Braces Matching SPI instead, for details see
 
86
     *   <a href="@org-netbeans-modules-editor-bracesmatching@/overview-summary.html">Editor Braces Matching</a>.
 
87
     */
 
88
    public static final String HIGHLIGHT_BRACE_LAYER_NAME = "highlight-brace-layer"; // NOI18N
 
89
 
 
90
    /** 
 
91
     * Highlight matching brace draw layer visibility 
 
92
     * 
 
93
     * @deprecated Please use Braces Matching SPI instead, for details see
 
94
     *   <a href="@org-netbeans-modules-editor-bracesmatching@/overview-summary.html">Editor Braces Matching</a>.
 
95
     */
 
96
    public static final int HIGHLIGHT_BRACE_LAYER_VISIBILITY = 11000;
 
97
 
 
98
// XXX: remove
 
99
//    /** Highlight a brace matching character before the caret */
 
100
//    public static final int MATCH_BRACE_BEFORE = -1;
 
101
//    
 
102
//    /** Highlight a brace matching character after (at) the caret */
 
103
//    public static final int MATCH_BRACE_AFTER = 0;
 
104
//    
 
105
//    /** Highlight a brace matching character either before or after caret;
 
106
//        the character before takes precedence. */
 
107
//    public static final int MATCH_BRACE_EITHER = java.lang.Integer.MAX_VALUE;
 
108
//
 
109
//    /** Whether to hightlight the matching brace */
 
110
//    boolean highlightBrace;
 
111
//
 
112
//    /** Coloring used for highlighting the matching brace */
 
113
//    Coloring highlightBraceColoring;
 
114
//
 
115
//    /** Mark holding the starting position of the matching brace. */
 
116
//    MarkFactory.DrawMark highlightBraceStartMark;
 
117
//
 
118
//    /** Mark holding the ending position of the matching brace. */
 
119
//    MarkFactory.DrawMark highlightBraceEndMark;
 
120
//
 
121
//    /** Timer that fires when the matching brace should be displayed */
 
122
//    private Timer braceTimer;
 
123
//    private ActionListener braceTimerListener; // because of unwanted GC
 
124
//
 
125
//    /** Signal that the next matching brace update
 
126
//    * will be immediate without waiting for the brace
 
127
//    * timer to fire the action.
 
128
//    */
 
129
//    private boolean matchBraceUpdateSync;
 
130
//
 
131
//    /** Whether the brace starting and ending marks are currently valid or not.
 
132
//     * If they are not valid the block they delimit is not highlighted.
 
133
//     */
 
134
//    boolean braceMarksValid;
 
135
//
 
136
//    boolean simpleMatchBrace;
 
137
//
 
138
//    private int matchBraceOffset = MATCH_BRACE_EITHER;
 
139
    
 
140
    private boolean popupMenuEnabled;
 
141
 
 
142
    static final long serialVersionUID =-4292670043122577690L;
 
143
 
 
144
// XXX: remove    
 
145
//    protected void modelChanged(BaseDocument oldDoc, BaseDocument newDoc) {
 
146
//        // Fix for #7108
 
147
//        braceMarksValid = false; // brace marks are out of date - new document
 
148
//        if (highlightBraceStartMark != null) {
 
149
//            try {
 
150
//                highlightBraceStartMark.remove();
 
151
//            } catch (InvalidMarkException e) {
 
152
//            }
 
153
//            highlightBraceStartMark = null;
 
154
//        }
 
155
//
 
156
//        if (highlightBraceEndMark != null) {
 
157
//            try {
 
158
//                highlightBraceEndMark.remove();
 
159
//            } catch (InvalidMarkException e) {
 
160
//            }
 
161
//            highlightBraceEndMark = null;
 
162
//        }
 
163
//
 
164
//        super.modelChanged( oldDoc, newDoc );
 
165
//    }
 
166
    
 
167
    /** Called when settings were changed. The method is called
 
168
    * also in constructor, so the code must count with the evt being null.
 
169
    */
 
170
    public void settingsChange(SettingsChangeEvent evt) {
 
171
        super.settingsChange(evt);
 
172
        JTextComponent c = component;
 
173
        if (c != null) {
 
174
            Class kitClass = Utilities.getKitClass(c);
 
175
// XXX: remove
 
176
//            EditorUI editorUI = Utilities.getEditorUI(c);
 
177
//            highlightBraceColoring = editorUI.getColoring(
 
178
//                                           ExtSettingsNames.HIGHLIGHT_MATCH_BRACE_COLORING);
 
179
//
 
180
//            highlightBrace = SettingsUtil.getBoolean(kitClass,
 
181
//                               ExtSettingsNames.HIGHLIGHT_MATCH_BRACE,
 
182
//                               ExtSettingsDefaults.defaultHighlightMatchBrace);
 
183
//            int highlightBraceDelay = SettingsUtil.getInteger(kitClass,
 
184
//                                        ExtSettingsNames.HIGHLIGHT_MATCH_BRACE_DELAY,
 
185
//                                        ExtSettingsDefaults.defaultHighlightMatchBraceDelay);
 
186
//
 
187
//            if (highlightBrace) {
 
188
//                if (highlightBraceDelay > 0) {
 
189
//                    // jdk12 compiler doesn't allow inside run()
 
190
//                    final JTextComponent c2 = component;
 
191
//
 
192
//                    braceTimer = new Timer(highlightBraceDelay, null);
 
193
//                    braceTimerListener = 
 
194
//                         new ActionListener() {
 
195
//                             public void actionPerformed(ActionEvent evt2) {
 
196
//                                 SwingUtilities.invokeLater(
 
197
//                                     new Runnable() {
 
198
//                                         public void run() {
 
199
//                                             if (c2 != null) {
 
200
//                                                 BaseDocument doc = Utilities.getDocument(c2);
 
201
//                                                 if( doc != null ) {
 
202
//                                                     doc.readLock();
 
203
//                                                     try {
 
204
//                                                         updateMatchBrace();
 
205
//                                                     } finally {
 
206
//                                                         doc.readUnlock();
 
207
//                                                     }
 
208
//                                                 }
 
209
//                                             }
 
210
//                                         }
 
211
//                                     }
 
212
//                                 );
 
213
//                             }
 
214
//                         };
 
215
//                         
 
216
//                    braceTimer.addActionListener(new WeakTimerListener(braceTimerListener));
 
217
//                    braceTimer.setRepeats(false);
 
218
//                } else {
 
219
//                    braceTimer = null; // signal no delay
 
220
//                }
 
221
//                c.repaint();
 
222
//            }
 
223
//
 
224
//            simpleMatchBrace = SettingsUtil.getBoolean(kitClass,
 
225
//                                    ExtSettingsNames.CARET_SIMPLE_MATCH_BRACE,
 
226
//                                    ExtSettingsDefaults.defaultCaretSimpleMatchBrace);
 
227
//            
 
228
            popupMenuEnabled = SettingsUtil.getBoolean(kitClass,
 
229
                ExtSettingsNames.POPUP_MENU_ENABLED, true);
 
230
        }
 
231
    }
 
232
// XXX: remove
 
233
//    public void install(JTextComponent c) {
 
234
//        EditorUI editorUI = Utilities.getEditorUI(c);
 
235
//        editorUI.addLayer(new HighlightBraceLayer(), HIGHLIGHT_BRACE_LAYER_VISIBILITY);
 
236
//        super.install(c);
 
237
//    }
 
238
//
 
239
//    public void deinstall(JTextComponent c) {
 
240
//        EditorUI editorUI = Utilities.getEditorUI(c);
 
241
//        editorUI.removeLayer(HIGHLIGHT_BRACE_LAYER_NAME);
 
242
//        super.deinstall(c);
 
243
//    }
 
244
//    
 
245
//    /** Set the match brace offset.
 
246
//     * @param offset One of <code>MATCH_BRACE_BEFORE</code>,
 
247
//     * <code>MATCH_BRACE_AFTER</code> * or <code>MATCH_BRACE_EITHER</code>.
 
248
//     */
 
249
//    public void setMatchBraceOffset(int offset) {
 
250
//        if(offset != MATCH_BRACE_BEFORE && offset != MATCH_BRACE_AFTER
 
251
//           && offset != MATCH_BRACE_EITHER) {
 
252
//            throw new IllegalArgumentException("Offset "+ offset + " not allowed\n");
 
253
//        }
 
254
//        matchBraceOffset = offset;
 
255
//        BaseDocument doc = Utilities.getDocument(component);
 
256
//        if( doc != null ) {
 
257
//            doc.readLock();
 
258
//            try {
 
259
//                updateMatchBrace();
 
260
//            } finally {
 
261
//                doc.readUnlock();
 
262
//            }
 
263
//        }
 
264
//    }
 
265
//    
 
266
//    /** Fetch the match brace offset. */
 
267
//    public int getMatchBraceOffset() {
 
268
//        return matchBraceOffset;
 
269
//    }
 
270
 
 
271
    /** 
 
272
     * Update the matching brace of the caret. The document is read-locked
 
273
     * while this method is called.
 
274
     * 
 
275
     * @deprecated Please use Braces Matching SPI instead, for details see
 
276
     *   <a href="@org-netbeans-modules-editor-bracesmatching@/overview-summary.html">Editor Braces Matching</a>.
 
277
     */
 
278
    protected void updateMatchBrace() {
 
279
// XXX: remove
 
280
//        JTextComponent c = component;
 
281
//        if (c != null && highlightBrace) {
 
282
//            try {
 
283
//                EditorUI editorUI = Utilities.getEditorUI(c);
 
284
//                BaseDocument doc = (BaseDocument)c.getDocument();
 
285
//                int dotPos = getDot();
 
286
//                ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
 
287
//                boolean madeValid = false; // whether brace marks display were validated
 
288
//                int[] matchBlk = null;
 
289
//                if(dotPos > 0 && (matchBraceOffset == MATCH_BRACE_BEFORE
 
290
//                                  || matchBraceOffset == MATCH_BRACE_EITHER)) {
 
291
//                    matchBlk = sup.findMatchingBlock(dotPos - 1, simpleMatchBrace);
 
292
//                }
 
293
//                if(matchBlk == null && (matchBraceOffset == MATCH_BRACE_AFTER
 
294
//                                        || matchBraceOffset == MATCH_BRACE_EITHER)) {
 
295
//                    matchBlk = sup.findMatchingBlock(dotPos, simpleMatchBrace);
 
296
//                }
 
297
//                if (matchBlk != null) {
 
298
//                    if (highlightBraceStartMark != null) {
 
299
//                        int markStartPos = highlightBraceStartMark.getOffset();
 
300
//                        int markEndPos = highlightBraceEndMark.getOffset();
 
301
//                        if (markStartPos != matchBlk[0] || markEndPos != matchBlk[1]) {
 
302
//                            editorUI.repaintBlock(markStartPos, markEndPos);
 
303
//                            Utilities.moveMark(doc, highlightBraceStartMark, matchBlk[0]);
 
304
//                            Utilities.moveMark(doc, highlightBraceEndMark, matchBlk[1]);
 
305
//                            editorUI.repaintBlock(matchBlk[0], matchBlk[1]);
 
306
//                        } else { // on the same position
 
307
//                            if (!braceMarksValid) { // was not valid, must repaint
 
308
//                                editorUI.repaintBlock(matchBlk[0], matchBlk[1]);
 
309
//                            }
 
310
//                        }
 
311
//                    } else { // highlight mark is null
 
312
//                        highlightBraceStartMark = new MarkFactory.DrawMark(
 
313
//                                                    HIGHLIGHT_BRACE_LAYER_NAME, editorUI);
 
314
//                        highlightBraceEndMark = new MarkFactory.DrawMark(
 
315
//                                                    HIGHLIGHT_BRACE_LAYER_NAME, editorUI);
 
316
//                        highlightBraceStartMark.setActivateLayer(true);
 
317
//                        Utilities.insertMark(doc, highlightBraceStartMark, matchBlk[0]);
 
318
//                        Utilities.insertMark(doc, highlightBraceEndMark, matchBlk[1]);
 
319
//                        editorUI.repaintBlock(matchBlk[0], matchBlk[1]);
 
320
//                    }
 
321
//                    braceMarksValid = true;
 
322
//                    madeValid = true;
 
323
//                }
 
324
//
 
325
//                if (!madeValid) {
 
326
//                    if (braceMarksValid) {
 
327
//                        braceMarksValid = false;
 
328
//                        editorUI.repaintBlock(highlightBraceStartMark.getOffset(),
 
329
//                                highlightBraceEndMark.getOffset());
 
330
//                    }
 
331
//                }
 
332
//            } catch (BadLocationException e) {
 
333
//                Utilities.annotateLoggable(e);
 
334
//                highlightBrace = false;
 
335
//            } catch (InvalidMarkException e) {
 
336
//                Utilities.annotateLoggable(e);
 
337
//                highlightBrace = false;
 
338
//            }
 
339
//        }
 
340
    }
 
341
// XXX: remove
 
342
//    protected void update(boolean scrollViewToCaret) {
 
343
//        if (highlightBrace) {
 
344
//            if (matchBraceUpdateSync || braceTimer == null) {
 
345
//                updateMatchBrace();
 
346
//                matchBraceUpdateSync = false;
 
347
//
 
348
//            } else { // delay the brace update
 
349
//                braceTimer.restart();
 
350
//            }
 
351
//        }
 
352
//
 
353
//        super.update(scrollViewToCaret);
 
354
//    }
 
355
 
 
356
    /** 
 
357
     * Signal that the next matching brace update
 
358
     * will be immediate without waiting for the brace
 
359
     * timer to fire the action. This is usually done
 
360
     * for the key-typed action.
 
361
     * 
 
362
     * @deprecated Please use Braces Matching SPI instead, for details see
 
363
     *   <a href="@org-netbeans-modules-editor-bracesmatching@/overview-summary.html">Editor Braces Matching</a>.
 
364
     */
 
365
    public void requestMatchBraceUpdateSync() {
 
366
// XXX: remove
 
367
//        matchBraceUpdateSync = true;
 
368
    }
 
369
    
 
370
    public void mousePressed(MouseEvent evt) {
 
371
        Completion completion = ExtUtilities.getCompletion(component);
 
372
        if (completion != null && completion.isPaneVisible()) {
 
373
            // Hide completion if visible
 
374
            completion.setPaneVisible(false);
 
375
        }
 
376
        super.mousePressed(evt);
 
377
        showPopup(evt);        
 
378
    }
 
379
    
 
380
    private boolean showPopup (MouseEvent evt) {
 
381
        // Show popup menu for right click
 
382
        if (component != null && evt.isPopupTrigger() && popupMenuEnabled) {
 
383
            ExtUtilities.getExtEditorUI(component).showPopupMenu(evt.getX(), evt.getY());
 
384
            return true;
 
385
        }
 
386
        return false;
 
387
    }
 
388
    
 
389
    public void mouseReleased(MouseEvent evt) {
 
390
        if (!showPopup(evt)) {
 
391
            super.mouseReleased(evt);
 
392
        }
 
393
    }
 
394
// XXX: remove
 
395
//    /* package */ static boolean NO_HIGHLIGHT_BRACE_LAYER = Boolean.getBoolean("nbeditor-no-HighlightBraceLayer");
 
396
//    
 
397
//    /** 
 
398
//     * Draw layer to highlight the matching brace.
 
399
//     * 
 
400
//     * XXX: The HighlightBraceLayer needs to be rewritten using the new Highlighting SPI.
 
401
//     */
 
402
//    class HighlightBraceLayer extends DrawLayer.AbstractLayer {
 
403
//
 
404
//        public HighlightBraceLayer() {
 
405
//            super(HIGHLIGHT_BRACE_LAYER_NAME);
 
406
//        }
 
407
//
 
408
//        public void init(DrawContext ctx) {
 
409
//        }
 
410
//
 
411
//        public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) {
 
412
//            if (!NO_HIGHLIGHT_BRACE_LAYER && braceMarksValid) {
 
413
//                if (mark != null)
 
414
//                    return mark.getActivateLayer();
 
415
//                try {
 
416
//                    if (ctx.getStartOffset() > highlightBraceEndMark.getOffset())
 
417
//                        return highlightBraceEndMark.getActivateLayer();
 
418
//                    if (ctx.getStartOffset() > highlightBraceStartMark.getOffset())
 
419
//                        return highlightBraceStartMark.getActivateLayer();
 
420
//                } catch (InvalidMarkException ex) {                    
 
421
//                }
 
422
//            }
 
423
//
 
424
//            return false;
 
425
//        }
 
426
//
 
427
//        public void updateContext(DrawContext ctx) {
 
428
//            if (!NO_HIGHLIGHT_BRACE_LAYER && highlightBraceColoring != null) {
 
429
//                highlightBraceColoring.apply(ctx);
 
430
//            }
 
431
//        }
 
432
//
 
433
//    }
 
434
 
 
435
}