KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > bookmarks > actions > GotoBookmarkAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.bookmarks.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Caret JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27 import org.netbeans.editor.BaseAction;
28 import org.netbeans.lib.editor.bookmarks.api.Bookmark;
29 import org.netbeans.lib.editor.bookmarks.api.BookmarkList;
30 import org.openide.util.NbBundle;
31
32
33 /**
34  * Action that jumps to next/previous bookmark.
35  *
36  * @author Miloslav Metelka
37  * @version 1.00
38  */

39
40 public final class GotoBookmarkAction extends BaseAction {
41     
42     public static final String JavaDoc GOTO_NEXT_NAME = "bookmark-next"; // NOI18N
43

44     public static final String JavaDoc GOTO_PREVIOUS_NAME = "bookmark-previous"; // NOI18N
45

46     static final long serialVersionUID = -5169554640178645108L;
47     
48     public static GotoBookmarkAction createNext() {
49         return new GotoBookmarkAction(true);
50     }
51     
52     public static GotoBookmarkAction createPrevious() {
53         return new GotoBookmarkAction(false);
54     }
55     
56     private final boolean gotoNext;
57     
58     private final boolean select;
59     
60     public GotoBookmarkAction(boolean gotoNext) {
61         this(gotoNext, false);
62     }
63
64     /**
65      * Construct new goto bookmark action.
66      *
67      * @param gotoNext <code>true</code> if this action should go to a next bookmark.
68      * <code>false</code> if this action should go to a previous bookmark.
69      * @param select whether the selection should extend from the current
70      * caret location to the bookmark.
71      */

72     public GotoBookmarkAction(boolean gotoNext, boolean select) {
73         super(gotoNext ? GOTO_NEXT_NAME : GOTO_PREVIOUS_NAME,
74             ABBREV_RESET | MAGIC_POSITION_RESET | UNDO_MERGE_RESET | WORD_MATCH_RESET
75         );
76         
77         this.gotoNext = gotoNext;
78         this.select = select;
79
80         putValue(BaseAction.ICON_RESOURCE_PROPERTY,
81                 gotoNext
82                 ? "org/netbeans/modules/editor/bookmarks/resources/next_bookmark.png" // NOI18N
83
: "org/netbeans/modules/editor/bookmarks/resources/previous_bookmark.png" // NOI18N
84
);
85     }
86
87     public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
88         if (target != null) {
89             Caret JavaDoc caret = target.getCaret();
90             BookmarkList bookmarkList = BookmarkList.get(target.getDocument());
91             int dotOffset = caret.getDot();
92             Bookmark bookmark = gotoNext
93                 ? bookmarkList.getNextBookmark(dotOffset, true) // next (wrap)
94
: bookmarkList.getPreviousBookmark(dotOffset, true); // previous (wrap)
95

96             if (bookmark != null) {
97                 if (select) {
98                     caret.moveDot(bookmark.getOffset());
99                 } else {
100                     caret.setDot(bookmark.getOffset());
101                 }
102             }
103         }
104     }
105
106     protected Object JavaDoc getDefaultShortDescription() {
107         return NbBundle.getBundle(GotoBookmarkAction.class).getString(
108                 (String JavaDoc)getValue(Action.NAME));
109     }
110
111 }
112
113
114
Popular Tags