1 19 20 package org.netbeans.lib.editor.bookmarks.actions; 21 22 import java.awt.event.ActionEvent ; 23 import javax.swing.Action ; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.Caret ; 26 import javax.swing.text.JTextComponent ; 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 39 40 public final class GotoBookmarkAction extends BaseAction { 41 42 public static final String GOTO_NEXT_NAME = "bookmark-next"; 44 public static final String GOTO_PREVIOUS_NAME = "bookmark-previous"; 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 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" : "org/netbeans/modules/editor/bookmarks/resources/previous_bookmark.png" ); 85 } 86 87 public void actionPerformed(ActionEvent evt, JTextComponent target) { 88 if (target != null) { 89 Caret caret = target.getCaret(); 90 BookmarkList bookmarkList = BookmarkList.get(target.getDocument()); 91 int dotOffset = caret.getDot(); 92 Bookmark bookmark = gotoNext 93 ? bookmarkList.getNextBookmark(dotOffset, true) : bookmarkList.getPreviousBookmark(dotOffset, true); 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 getDefaultShortDescription() { 107 return NbBundle.getBundle(GotoBookmarkAction.class).getString( 108 (String )getValue(Action.NAME)); 109 } 110 111 } 112 113 114 | Popular Tags |