001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor;
015:
016: import java.util.ArrayList;
017:
018: /**
019: * List of all bookmarks attached to document. This is helper class for storage
020: * of all bookmarks attached to the document.
021: *
022: * @author David Konecny
023: * @since 07/2001
024: */
025:
026: // TODO: this is first primitive version of storage of bookmark
027: // it must be rewritten and also persistence must be added
028: public class Bookmarks {
029:
030: /** List of bookmarks */
031: private ArrayList bookmarks;
032:
033: /** Creates new Bookmarks */
034: public Bookmarks() {
035: bookmarks = new ArrayList(15);
036: }
037:
038: /** Find bookmark for the given line. */
039: private Bookmark getInternalBookmark(int line) {
040: for (int i = 0; i < bookmarks.size()
041: && ((Bookmark) bookmarks.get(i)).getLine() <= line; i++) {
042: if (((Bookmark) bookmarks.get(i)).getLine() == line)
043: return (Bookmark) bookmarks.get(i);
044: }
045: return null;
046: }
047:
048: /** Find bookmark for the given line. */
049: public Bookmark getBookmark(int line) {
050: return getInternalBookmark(line);
051: }
052:
053: /** Find next bookmark */
054: public Bookmark getNextLineBookmark(int line) {
055: for (int i = 0; i < bookmarks.size(); i++) {
056: if (((Bookmark) bookmarks.get(i)).getLine() >= line)
057: return (Bookmark) bookmarks.get(i);
058: }
059: return null;
060: }
061:
062: /** Add new bookmark into array */
063: public void putBookmark(Bookmark bookmark) {
064: int line = bookmark.getLine();
065: boolean inserted = false;
066: for (int i = 0; i < bookmarks.size(); i++) {
067: if (((Bookmark) bookmarks.get(i)).getLine() > line) {
068: bookmarks.add(i, bookmark);
069: inserted = true;
070: break;
071: }
072: }
073: if (!inserted)
074: bookmarks.add(bookmark);
075: }
076:
077: /** Remove bookmark from the line */
078: public Bookmark removeBookmark(int line) {
079: Bookmark bookmark;
080: bookmark = getInternalBookmark(line);
081: if (bookmark == null)
082: return null;
083: bookmarks.remove(bookmark);
084: return bookmark;
085: }
086:
087: /** Remove given bookmark */
088: public void removeBookmark(Bookmark bookmark) {
089: bookmarks.remove(bookmark);
090: }
091:
092: /** Remove all bookmarks. Usually called when document is going to be closed. */
093: public void removeAll() {
094: for (int i = 0; i < bookmarks.size(); i++) {
095: ((Bookmark) bookmarks.get(i)).remove();
096: }
097: bookmarks = new ArrayList();
098: }
099:
100: /** Interface defining bookmark. */
101: public interface Bookmark {
102:
103: /** Line number of the bookmark */
104: public int getLine();
105:
106: /**
107: * Method which is called when document is going to be closed. Bookmark
108: * can handle this change of state if necessary.
109: */
110: public void remove();
111: }
112:
113: }
|