Added editor caret listening support. editor_multi_caret editor_multi_caret_stable
authorMiloslav Metelka <mmetelka@netbeans.org>
Thu, 26 Nov 2015 14:21:48 +0100
brancheditor_multi_caret
changeset 3068175cf4b63c4b98
parent 306816 038cb102539b
child 306818 25d6f2910115
Added editor caret listening support.
editor.lib2/src/org/netbeans/api/editor/EditorCaret.java
editor.lib2/src/org/netbeans/api/editor/EditorCaretEvent.java
editor.lib2/src/org/netbeans/api/editor/EditorCaretListener.java
     1.1 --- a/editor.lib2/src/org/netbeans/api/editor/EditorCaret.java	Wed Nov 25 13:29:26 2015 +0100
     1.2 +++ b/editor.lib2/src/org/netbeans/api/editor/EditorCaret.java	Thu Nov 26 14:21:48 2015 +0100
     1.3 @@ -52,6 +52,7 @@
     1.4  import javax.swing.text.Position;
     1.5  import org.netbeans.api.annotations.common.CheckForNull;
     1.6  import org.netbeans.api.annotations.common.NonNull;
     1.7 +import org.netbeans.lib.editor.util.ListenerList;
     1.8  import org.openide.util.Exceptions;
     1.9  
    1.10  /**
    1.11 @@ -70,9 +71,12 @@
    1.12      protected JTextComponent component;
    1.13      
    1.14      protected LinkedList<CaretInfo> carets;
    1.15 +    
    1.16 +    private final ListenerList<EditorCaretListener> listenerList;
    1.17  
    1.18      public EditorCaret() {
    1.19          carets = new LinkedList<>();
    1.20 +        listenerList = new ListenerList<EditorCaretListener>();
    1.21      }
    1.22  
    1.23      @Override
    1.24 @@ -196,6 +200,20 @@
    1.25          return null; // TBD
    1.26      }
    1.27      
    1.28 +    public void addEditorCaretListener(EditorCaretListener listener) {
    1.29 +        listenerList.add(listener);
    1.30 +    }
    1.31 +    
    1.32 +    public void removeEditorCaretListener(EditorCaretListener listener) {
    1.33 +        listenerList.remove(listener);
    1.34 +    }
    1.35 +    
    1.36 +    protected void fireEditorCaretChange(EditorCaretEvent evt) {
    1.37 +        for (EditorCaretListener listener : listenerList.getListeners()) {
    1.38 +            listener.caretChanged(evt);
    1.39 +        }
    1.40 +    }
    1.41 +    
    1.42      protected void setDotCaret(int offset, CaretInfo caret, boolean expandFold) throws IllegalStateException {
    1.43          JTextComponent c = component;
    1.44          if (c != null) {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/editor.lib2/src/org/netbeans/api/editor/EditorCaretEvent.java	Thu Nov 26 14:21:48 2015 +0100
     2.3 @@ -0,0 +1,103 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2015 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * If you wish your version of this file to be governed by only the CDDL
    2.31 + * or only the GPL Version 2, indicate your decision by adding
    2.32 + * "[Contributor] elects to include this software in this distribution
    2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.34 + * single choice of license, a recipient has the option to distribute
    2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.36 + * to extend the choice of license to its licensees as provided above.
    2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.38 + * Version 2 license, then the option applies only if the new code is
    2.39 + * made subject to such option by the copyright holder.
    2.40 + *
    2.41 + * Contributor(s):
    2.42 + *
    2.43 + * Portions Copyrighted 2015 Sun Microsystems, Inc.
    2.44 + */
    2.45 +package org.netbeans.api.editor;
    2.46 +
    2.47 +import java.util.List;
    2.48 +import org.netbeans.api.annotations.common.NonNull;
    2.49 +
    2.50 +/**
    2.51 + * Notification event about changes in editor caret.
    2.52 + *
    2.53 + * @author Miloslav Metelka
    2.54 + */
    2.55 +public final class EditorCaretEvent extends java.util.EventObject {
    2.56 +    
    2.57 +    private final int affectedStartOffset;
    2.58 +    
    2.59 +    private final int affectedEndOffset;
    2.60 +    
    2.61 +    EditorCaretEvent(EditorCaret source, int affectedStartOffset, int affectedEndOffset) {
    2.62 +        super(source);
    2.63 +        this.affectedStartOffset = affectedStartOffset;
    2.64 +        this.affectedEndOffset = affectedEndOffset;
    2.65 +    }
    2.66 +    
    2.67 +    /**
    2.68 +     * Get caret instance to which this event relates.
    2.69 +     *
    2.70 +     * @return caret instance.
    2.71 +     */
    2.72 +    public @NonNull EditorCaret getCaret() {
    2.73 +        return (EditorCaret) getSource();
    2.74 +    }
    2.75 +
    2.76 +    /**
    2.77 +     * Get start of the region that was affected by caret change.
    2.78 +     * <br/>
    2.79 +     * This offset region will be repainted automatically by the editor infrastructure.
    2.80 +     *
    2.81 +     * @return &gt;= 0 offset.
    2.82 +     */
    2.83 +    public int getAffectedStartOffset() {
    2.84 +        return affectedStartOffset;
    2.85 +    }
    2.86 +    
    2.87 +    /**
    2.88 +     * Get end of the region that was affected by caret change.
    2.89 +     * <br/>
    2.90 +     * This offset region will be repainted automatically by the editor infrastructure.
    2.91 +     *
    2.92 +     * @return &gt;= 0 offset.
    2.93 +     */
    2.94 +    public int getAffectedEndOffset() {
    2.95 +        return affectedEndOffset;
    2.96 +    }
    2.97 +    
    2.98 +    public List<CaretInfo> getAddedCarets() {
    2.99 +        return null;
   2.100 +    }
   2.101 +
   2.102 +    public List<CaretInfo> getRemovedCarets() {
   2.103 +        return null;
   2.104 +    }
   2.105 +
   2.106 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/editor.lib2/src/org/netbeans/api/editor/EditorCaretListener.java	Thu Nov 26 14:21:48 2015 +0100
     3.3 @@ -0,0 +1,60 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2015 Oracle and/or its affiliates. All rights reserved.
     3.8 + *
     3.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    3.10 + * Other names may be trademarks of their respective owners.
    3.11 + *
    3.12 + * The contents of this file are subject to the terms of either the GNU
    3.13 + * General Public License Version 2 only ("GPL") or the Common
    3.14 + * Development and Distribution License("CDDL") (collectively, the
    3.15 + * "License"). You may not use this file except in compliance with the
    3.16 + * License. You can obtain a copy of the License at
    3.17 + * http://www.netbeans.org/cddl-gplv2.html
    3.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.19 + * specific language governing permissions and limitations under the
    3.20 + * License.  When distributing the software, include this License Header
    3.21 + * Notice in each file and include the License file at
    3.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    3.23 + * particular file as subject to the "Classpath" exception as provided
    3.24 + * by Oracle in the GPL Version 2 section of the License file that
    3.25 + * accompanied this code. If applicable, add the following below the
    3.26 + * License Header, with the fields enclosed by brackets [] replaced by
    3.27 + * your own identifying information:
    3.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.29 + *
    3.30 + * If you wish your version of this file to be governed by only the CDDL
    3.31 + * or only the GPL Version 2, indicate your decision by adding
    3.32 + * "[Contributor] elects to include this software in this distribution
    3.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.34 + * single choice of license, a recipient has the option to distribute
    3.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.36 + * to extend the choice of license to its licensees as provided above.
    3.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.38 + * Version 2 license, then the option applies only if the new code is
    3.39 + * made subject to such option by the copyright holder.
    3.40 + *
    3.41 + * Contributor(s):
    3.42 + *
    3.43 + * Portions Copyrighted 2015 Sun Microsystems, Inc.
    3.44 + */
    3.45 +package org.netbeans.api.editor;
    3.46 +
    3.47 +import org.netbeans.api.annotations.common.NonNull;
    3.48 +
    3.49 +/**
    3.50 + * Listener for changes in editor caret.
    3.51 + *
    3.52 + * @author Miloslav Metelka
    3.53 + */
    3.54 +public interface EditorCaretListener extends java.util.EventListener {
    3.55 +
    3.56 +    /**
    3.57 +     * Notification about change in terms of caret addition/removal or movement.
    3.58 +     *
    3.59 +     * @param evt caret event describing the change.
    3.60 +     */
    3.61 +    void caretChanged(@NonNull EditorCaretEvent evt);
    3.62 +
    3.63 +}